JZOJ【USACO 2017 December Silver】Milk Measurement

 

 

Description

Each of Farmer John's cows initially produces G gallons of milk per day (1≤G≤109). Since the milk output of a cow is known to potentially change over time, Farmer John decides to take periodic measurements of milk output and write these down in a log book. Entries in his log look like this:
 35 1234 -2
14 2345 +3
The first entry indicates that on day 35, cow #1234's milk output was 2 gallons lower than it was when last measured. The next entry indicates that on day 14, cow #2345's milk output increased by 3 gallons from when it was last measured. Farmer John has only enough time to make at most one measurement on any given day. Unfortunately, he is a bit disorganized, and doesn't necessarily write down his measurements in chronological order.

To keep his cows motivated, Farmer John proudly displays on the wall of his barn the picture of whichever cow currently has the highest milk output (if several cows tie for the highest milk output, he displays all of their pictures). Please determine the number of days on which Farmer John would have needed to change this display.

Note that Farmer John has a very large herd of cows, so although some of them are noted in his log book as changing their milk production, there are always plenty of other cows around whose milk output level remains at G gallons.
最初,农夫约翰的每头奶牛每天生产G加仑的牛奶(1≤G≤10^9)。由于随着时间的推移,奶牛的产奶量可能会发生变化,农夫约翰决定定期对奶牛的产奶量进行测量,并将其记录在日志中。
他的日志中的记录如下:
35 1234 -2
14 2345 +3
第一个条目表明:在第35天,1234号奶牛的产奶量比上次测量时降低了2加仑。
第二个条目表明:在第14天,2345号奶牛的产奶量比上次测量时增加了3加仑。
农夫约翰只有在任何一天内做最多一次测量的时间(即每天最多做一次测量,但可能不做)。不幸的是,约翰有点杂乱无章,他不一定按照时间顺序记下测量结果。为了保持奶牛的产奶动力,农夫约翰自豪地在谷仓的墙上展示了目前产奶量最高的奶牛的照片(如果有若干头奶牛的产奶量最高,他就会展示所有的图片)。

请求出约翰需要调整所展示的照片的次数。

请注意,农夫约翰有一大群奶牛。所以尽管日志中记录了一些奶牛改变了产奶量,但仍然还有很多奶牛的产奶量保持在G加仑。
 

Input

The first line of input contains the number of measurements N that Farmer John makes (1≤N≤100,000), followed by G. Each of the next N lines contains one measurement, in the format above, specifying a day (an integer in the range 1…10^6), the integer ID of a cow (in the range 1…10^9), and the change in her milk output since it was last measured (a nonzero integer). Each cow's milk output will always be in the range 0…10^9.
第一行是两个整数N和G,分别表示测量的次数和初始产奶量。
接下来N行,每行为一次测量。每行三个数:分别表示日期一(在整数1...10^6范围内),奶牛的编号(在整数1...10^9范围内),该奶牛的产奶量变化值(一个非负数)。无论如何,每头奶牛的产奶量永远保证在0...10^9范围内。

Output

Please output the number of days on which Farmer John needs to adjust his motivational display.
请输出约翰总共调整所展示的照片的次数。
 

Sample Input

4 10
7 3 +3
4 2 -1
9 3 -1
1 1 +2

Sample Output

3

 题解:

先根据天数快排,

然后线段树+离散化(不开Map见祖宗),

如果是第一次ANS++,

否则若该奶牛不再是第一或成为第一则ANS++,

再否则的话,如果第一的个数(线段树处理)变化则ANS++.

CODE如下

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std; 
int n,m,ans,cnt,c[1000005],lmax,lnum,nb[30000005],mx[30000005],x,past;
map<int,int>to;
struct node
{
    int num,sum,d;
}a[1000005];
bool cmp(node a,node b)
{
    return a.d<b.d;
}
void change(int l,int r,int x,int k)
{
    if(l==r)
    {
        mx[k]=c[x];
        nb[k]=1;
    }
    else
    {
        int mid=(l+r)/2;
        if(x<=mid)change(l,mid,x,k*2);
        else change(mid+1,r,x,k*2+1);
        if(mx[k*2]==mx[k*2+1])
        {
            mx[k]=mx[k*2];nb[k]=nb[k*2]+nb[k*2+1];
        }
        if(mx[k*2]>mx[k*2+1])
        {
            mx[k]=mx[k*2];nb[k]=nb[k*2];
        }
        if(mx[k*2]<mx[k*2+1])
        {
            mx[k]=mx[k*2+1];nb[k]=nb[k*2+1];
        }
    }
}
int main()
{
    freopen("measurement.in","r",stdin);
    freopen("measurement.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d%d",&a[i].d,&a[i].num,&a[i].sum);
     } 
     for(int i=1;i<=30000005;i++)mx[i]=-1000000000;
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;i++)
    {
        if(to[a[i].num]==0)to[a[i].num]=++cnt;
    }
    for(int i=1;i<=n;i++)
    {
        x=to[a[i].num];
        past=c[x];
        c[x]+=a[i].sum;
        change(1,cnt,x,1);
        if(i>1)
        {
            if((c[x]==mx[1]&&past!=lmax)||(c[x]!=mx[1]&&past==lmax))
            {
                ans++;
            }
            else
            {
                if(lnum!=nb[1])
                {
                    ans++;
                }
            }
        }
        else ans++;
        lmax=mx[1];
        lnum=nb[1];
    }
    printf("%d",ans);
}

 

posted @ 2020-06-12 20:06  HYDcn666_JZOJ  阅读(371)  评论(0编辑  收藏  举报