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: 5424User: WC006Language: C++Result: 正确Time:256 msMemory:8740 kb
****************************************************************/

Milk Measurement相关推荐

  1. Mixing Milk(USACO)

    /* ID:tianlin2 PROG:milk LANG:C++ */ #include <iostream> #include <cstdlib> #include < ...

  2. usaco Mixing Milk

    我太菜了先从简单题慢慢刷 /* ID: jinbo wu LANG: C++ PROB: milk */ #include<bits/stdc++.h> using namespace s ...

  3. 2019 GDUT Rating Contest I : Problem H. Mixing Milk

    题面: H. Mixing Milk Input file: standard input Output file: standard output Time limit: 1 second Memory ...

  4. 讀書收穫 The Untold Story of Milk, Ron Schmid

    2019独角兽企业重金招聘Python工程师标准>>> The Untold Story of Milk, Ron Schmid, ISBN 0979209528 主要說明草原飼養健 ...

  5. NI Measurement Studio 打包问题的解决(原创)

    问题及现象: 程序中调用了NI的Led.switch等控件的3D样式,在开发环境下可正常显示,但打包后装入新系统后出现不能正常显示的问题. 解决: 1.在NI网上查到如下信息: Why Do Meas ...

  6. USACO_1_4_Mother's Milk

    又是深度优先搜索,枚举,为了不出现无限循环,为了所出现的每种状态(三个杯子现有milk数)仅考虑一次,使用isAppeared变量来记录某种状态是否已经出现过. Code /**//* ID: sdj ...

  7. Work measurement II

    Work measurement II 文章目录 Work measurement II Outline learned so far How work measurement techniques ...

  8. Work Measurement - 1

    Work Measurement - 1 文章目录 Work Measurement - 1 Overview Introduction to work measurement Time is imp ...

  9. hoj1003 Mixing Milk (哈工大)

    //贪心法,使用排序函数,每次都找最便宜的牛奶,然后判断够没够重量 #include <iostream> #include <algorithm> #define X 501 ...

最新文章

  1. Apollo代码学习(六)—模型预测控制(MPC)_follow轻尘的博客-CSDN博客_mpc代码
  2. srs 配置http flv集群
  3. redis常用优化及持久化到硬盘
  4. oracle字符集与客户端
  5. HDFS块文件和存放目录的关系
  6. PANDAS 数据合并与重塑(concat篇) 原创 2016年09月13日 19:26:30 47784 pandas作者Wes McKinney 在【PYTHON FOR DATA ANALYS
  7. 上传文件白名单_十大常见web漏洞——文件上传漏洞
  8. laravel5.4之artisan使用总结一
  9. 教育部明确!这类人才聘期流出,取消称号和经费!
  10. java从控制台读取数据_Java不同版本从控制台读取数据方法及优缺点分析
  11. 使用springMVC提供的CommonsMultipartResolver文件解析器,实现文件轻松上传
  12. 编辑距离问题(Edit Distance Problem)
  13. 【转】GIS:为什么我和别的软件不一样
  14. 解读升压电路(BOOST)与降压电路(BUCK)
  15. 【iOS 15】iPhone如何录屏?iPhone屏幕录制技巧分享
  16. nginx 基本入门
  17. 迅雷9屏蔽所有游览器和网站相应
  18. Mac 运行windows软件
  19. AWTRIX像素屏时钟搭建
  20. 细数 GameFi 模型发展 ,未来仍可期?

热门文章

  1. 图像对抗学习笔记:复现DPatch
  2. 三星android功能怎么用,三星画中画功能是什么意思?三星手机画中画功能使用教程图解...
  3. 图像的灰度化灰度值的读取Matlab
  4. 初识设计模式 - 解释器模式
  5. 求助:matlab报错:位置 2 处的索引超出数组边界(1)
  6. canvas-实现放大镜效果
  7. MTP 模式 pc端文件和文件管理其中的不同步
  8. ubuntu:VIM使用
  9. Python——from collections import namedtuple
  10. CDN防盗链与有效使用CDN缓存