Milk Measurement

Milk Measurement

时间限制: 1 Sec 内存限制: 128 MB

题目描述

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 GG gallons.

输入

The first line of input contains the number of measurements NNthat 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…106), the integer ID of a cow (in the range 1…109), 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…109.

输出

Please output the number of days on which Farmer John needs to adjust his motivational display.

样例输入

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

样例输出

3

提示

来源

USACO 2017 December Contest, Silver

Code

#include <stdio.h>
#include <iostream>
#include <algorithm>
#define L(u) (u<<1)
#define R(u) (u<<1|1)
using namespace std;
const int mx=100010;
struct Tree {
    int lch,rch,sum;
} tree[mx<<2];
struct data {
    int time,id,x;
} f[mx];
int d[mx],ans,n,G,Max,ID[mx],Tmp[mx],N;
bool cmp(data A,data B) {
    return A.time<B.time;
}
void Pushup(int u) {
    tree[u].sum=max(tree[L(u)].sum,tree[R(u)].sum);
}
void Build(int u,int l,int r) {
    tree[u].lch=l;
    tree[u].rch=r;
    if (l==r) {
        tree[u].sum=d[l];
        return ;
    }
    int mid=(l+r)>>1;
    Build(L(u),l,mid);
    Build(R(u),mid+1,r);
    Pushup(u);
}
void Update(int u,int pos,int val) {
    if (tree[u].lch==tree[u].rch) {
        tree[u].sum=val;
        return;
    }
    int mid=(tree[u].lch+tree[u].rch)>>1;
    if (pos<=mid) Update(L(u),pos,val);
    else Update(R(u),pos,val);
    Pushup(u);
}

bool Judge(int idx,int rev) { //判断是不是当前唯一的最大值
    int OldMax = tree[1].sum;
    Update(1,idx,-10);
    int NewMax = tree[1].sum;
    Update(1,idx,rev);
    return !(OldMax == NewMax);//唯一为真
}

int main() {
    // freopen("in","r",stdin);
    scanf("%d%d",&n,&G);
    for (int i=1; i<=n; ++i) {
        scanf("%d%d%d",&f[i].time,&f[i].id,&f[i].x);
        Tmp[i]=f[i].id;
    }
    sort(f+1,f+1+n,cmp);
    sort(Tmp+1,Tmp+1+n);
    for (int i=1; i<=n; ++i) {
        if (Tmp[i]!=Tmp[i-1]) ID[++N]=Tmp[i];
    }
    for (int i=1; i<=N; ++i) d[i]=G;
    Build(1,1,N);
    for (int i=1; i<=n; ++i) {
        int val=f[i].x;
        int idx=lower_bound(ID+1,ID+1+N,f[i].id)-ID;
        if(d[idx] != tree[1].sum) { //原来不是最大值
            Update(1,idx,d[idx] + val);
            if(d[idx] + val == tree[1].sum) {   //更新之后是最大值
                ans ++;
            } else {        //更新之后不是最大值
                ;
            }
        } else {    //原来是最大值
            if(val > 0) {
                if(!Judge(idx,d[idx])) { //原来不是唯一的最大值
                    ans ++;
                } else {
                    ans += 0;
                }
                Update(1,idx,d[idx] + val);
            } else {    //它减小了
                if(Judge(idx,d[idx])) { //如果原来只有一个最大值
                    Update(1,idx,d[idx] + val);
                    if(d[idx] + val != tree[1].sum) { //如果此时不是最大值
                        ans ++;
                    } else { //如果此时还是最大值
                        if(!Judge(idx, d[idx] + val)) { //如果现在有很多最大值
                            ans ++;
                        } else {
                            ans += 0;
                        }
                    }
                } else {
                    Update(1,idx,d[idx] + val);
                    ans += 1;
                }
            }
        }
        d[idx] += val;
    }
    if (ans==0) ans=1;
    printf("%d\n",ans);
    return 0;
}

/**************************************************************
    Problem: 5424
    User: WC006
    Language: C++
    Result: 正确
    Time:256 ms
    Memory:8740 kb
****************************************************************/

猜你喜欢

转载自blog.csdn.net/ACM2017/article/details/79625138