3168: Drink Milk Every Day

For the health’s sake, Mr. Doraemonok drinks one and only one package of pure milk every day. However, Mr. Doraemonok will never drink sour milk which is out of storage period. When coming to them, he will throw them away. To save money, he wishes to save milk as much as possible. Considering these factors, he starts to check the milk stored in his closet to decide the drinking sequence. Can you offer a hand to him?

输入

 

The first line of the input data will be a positive integer n (n<=100000) to represent the amount of the milk. Every one of the following n lines describes a package of milk: The first string stands for the brand of the milk, the second string stands for the production date, and the format is like ’03-20’. The n+2 line will be a positive integer m (m<=50) which stands for the number of the milk brands. Every line of the following m lines describes a milk brands: The first string is the name of the brand which is no longer than 20 characters and with no blanks. The following positive integer in this line stands for the storage period D (D<=45). All the milk brands in the problem will be listed. The last line will include a string which stands for today’s date. Mr. Doraemonok has not drunk milk today.

The dates in the input data are all in the range of Year 2008. The just out-of-date day should be considered out of date. Sample Input/Output and Hint can be references. We guaranteed that the data is right logically.

 

输出

Output one line which includes only one nonnegative integer to represent the least amount of milk to be wasted.

 

样例输入

3
Haihe_Pure_Milk 03-01
Haihe_Pure_Milk 03-01
Mengniu_Pure_Milk 03-20
2
Haihe_Pure_Milk 30
Mengniu_Pure_Milk 30
03-30

样例输出

1

提示

In the Sample Input, two packages of Haihe_Pure_Milk will be out of date tomorrow which means they have to be drunk today. However, only one package can be drunk, i.e. one package must be wasted. In this way, the only one Mengniu_Pure_Milk can be arranged to be drunk tomorrow. Under this circumstance, at least one package of milk will be wasted.

 题意:n行给出牛奶名称及其过期时间

m行给出牛奶名称及其保质期
每天喝一瓶 求过期的最少瓶数
 
思路:先存n行的内容  然后m行的用map存对应的牛奶名字及其保质期
再遍历n行 求出他们的过期时间 日期都转化为天数好算 
再用二分找最早要过期的 并且当天可以喝的
然后看代码注释把
#include<bits/stdc++.h>
using namespace std;
#define NN 100005
int mo[13]={31,29,31,30,31,30,31,31,30,31,30,31}; 
map<string,int>mp;
int a[NN],b[NN],qu[NN];
string s[NN];
int main()
{
    int i,j,m,n;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>s[i];
        char c;
        cin>>a[i]>>c>>b[i];
    }
    cin>>m;
    while(m--)
    {
        string st;
        int ti;
        cin>>st>>ti;
        mp[st]=ti;
    }
    int x,y;
    char c;
    cin>>x>>c>>y;
    int ydw=0;
    for(i=0;i<x-1;i++)ydw+=mo[i];
    ydw+=y;//ydw存现在的日期 
    for(i=0;i<n;i++)
    {
        x=a[i],y=b[i];
        int yy=0;
        for(j=0;j<x-1;j++)yy+=mo[j];
        yy+=y;
        yy+=mp[s[i]];
        qu[i]=yy; //把每个的日期存进qu数组 
    }
    sort(qu,qu+n);//排序 
    int yy2=0,sum=0;
    for(i=0;i<n;i++) 
    {//二分每次找当天可以喝的 最早要过期的 
        int yy1=upper_bound(qu+yy2,qu+n,ydw)-qu;
        yy2=yy1+1;//下次从这个开始找 
        if(yy1!=n)sum++;
        else break;
        ydw++;//天数++ 
    }
    cout<<n-sum<<endl;
    return 0;
}
View Code

 

posted @ 2019-05-21 18:01  -第4题-  阅读(334)  评论(0编辑  收藏  举报