Each of Farmer John's cows initially produces GG gallons of milk per day (1 \leq G \leq 10^91≤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.
最初,农夫约翰的每头奶牛每天生产G加仑的牛奶(1≤G≤10^9)(1≤G≤109)。由于随着时间的推移,奶牛的产奶量可能会发生变化,农夫约翰决定定期对奶牛的产奶量进行测量,并将其记录在日志中。
他的日志中的记录如下:
35 1234 -2
14 2345 +3
第一个条目表明:在第35天,1234号奶牛的产奶量比上次测量时降低了2加仑。
第二个条目表明:在第14天,2345号奶牛的产奶量比上次测量时增加了3加仑。
农夫约翰只有在任何一天内做最多一次测量的时间(即每天最多做一次测量,但可能不做)。不幸的是,约翰有点杂乱无章,他不一定按照时间顺序记下测量结果。为了保持奶牛的产奶动力,农夫约翰自豪地在谷仓的墙上展示了目前产奶量最高的奶牛的照片(如果有若干头奶牛的产奶量最高,他就会展示所有的图片)。
请求出约翰需要调整所展示的照片的次数。
请注意,农夫约翰有一大群奶牛。所以尽管日志中记录了一些奶牛改变了产奶量,但仍然还有很多奶牛的产奶量保持在G加仑。
输入格式:
The first line of input contains the number of measurements NN that Farmer John makes (1 \leq N \leq 100,0001≤N≤100,000), followed by GG. Each of the next NNlines contains one measurement, in the format above, specifying a day (an integer in the range 1 \ldots 10^61…106), the integer ID of a cow (in the range 1 \ldots 10^91…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 \ldots 10^90…109.
第一行是两个整数N和G,分别表示测量的次数和初始产奶量。
接下来N行,每行为一次测量。每行三个数:分别表示日期一(在整数1...10^6106范围内),奶牛的编号(在整数1...10^9109范围内),该奶牛的产奶量变化值(一个非负数)。无论如何,每头奶牛的产奶量永远保证在0...10^90...109范围内。
输出格式:
Please output the number of days on which Farmer John needs to adjust his motivational display.
请输出约翰总共调整所展示的照片的次数。
输入样例#1:
4 10 7 3 +3 4 2 -1 9 3 -1 1 1 +2
输出样例#1:
3
其他人啊都是用一些十分高级的算法(像什么平衡树啊,线段树啊等等),而我太菜了,不会啊(就算会也不打QAQ),一看这种*题啊,肯定是模拟啊QWQ。
不过,这题既然这么花里胡哨,坑点和细节还是很多的。
1:在展示的照片中只有一头产奶很高的牛,一天这头牛的奶量下降了,但他还是 唯一! 的第一,照片不用换;
2:还有就是要挂上照片的奶牛的奶量还有每次处理。
3:题目中最后一句话是什么意思呢,就是说一定有奶牛保持在G加仑,也就是说,如果当前计算出来奶牛的最高产奶量不足G加仑,那么贴着照片的不是这头奶牛,而是那些G加仑的奶牛
反正是模拟嘛,看了程序你们就懂了(对吧),注释解直接加里面了:
#include
using namespace std;
struct E
{
int t,m,s;
}a[110000];
bool mycmp(E x,E y)
{
return x.tMap;
inline void find()
{
s=-0x3f3f3f3f3f;
S=0;
for(int i=1;i<=cnt;i++)
{
if(cow[i]!=f&&cow[i]>s) s=cow[i],S=1;
else
{
if(cow[i]==s) S++;
}
}
}//重找第二名
int main()
{
freopen("measurement.in","r",stdin);
freopen("measurement.out","w",stdout);
scanf("%d%d",&n,&g);
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&a[i].t,&a[i].m,&a[i].s);
}
sort(a+1,a+n+1,mycmp);//先要按时间排序,这是肯定的啦
for(int i=1;i<=n;i++)//接下来就是把每次农夫约翰记录的那天的所有情况都判断并处理
{
if(!Map[a[i].m]) Map[a[i].m]=++cnt;
//这里就是把编号离散化一下
if(cow[Map[a[i].m]]==f)//第一种情况,当前处理的奶牛是排位第一的
{
cow[Map[a[i].m]]+=a[i].s;
if(a[i].s>0)//如果当天那头牛的产奶量是上升的
{
if(fis!=1)
//如果有很多排位第一的奶牛
//这些奶牛变成第二名,当前奶牛独占第一,照片墙会替换
{
s=f;
S=fis-1;
f=cow[Map[a[i].m]];
fis=1;
ans++;
}
else
{
f=cow[Map[a[i].m]];
}
}
else//反之如果产奶量下降
{
if(fis!=1)//如果产奶量第一的牛不止一只
{
fis--;
ans++;
//因为这头牛产奶量减少了,所以他的照片要去下来
if(cow[Map[a[i].m]]>s)
//如果当前奶牛的产奶量比第二名的产奶量大
//产奶量第二名就变成当前奶牛
{
s=cow[Map[a[i].m]];
S=1;
}
if(cow[Map[a[i].m]]==s) S++;
//如果当前奶牛的产奶量等于第二名的产奶量
//产奶量第二名奶牛数量增加
}
else//如果第一的只要一头
{
if(cow[Map[a[i].m]]>s) //如果这头牛产奶量减小后还是比第二名大
f=cow[Map[a[i].m]]; //更新他的产奶量,但是照片不换
if(cow[Map[a[i].m]]==s) //如果产奶量和第二名一样,那么更新最大产奶量
//把产奶量第二名的牛挂上照片
{
f=s;
fis=S+1;
ans++;
find();//重新找第二
}
if(cow[Map[a[i].m]]f) swap(s,f),swap(S,fis),ans++;
////还有可能超过第一名,此时照片墙替换
}
}
}
else//如果这头牛不是第一或者第二
{
cow[Map[a[i].m]]+=a[i].s;
if(cow[Map[a[i].m]]==s) S++;
if(cow[Map[a[i].m]]==f) fis++,ans++;//如果变成第一,换照片
else if(cow[Map[a[i].m]]>s)//如果没变第一,但是超过了第二
{
s=cow[Map[a[i].m]];//更新第二名的产奶量
S=1;//更新第二名的个数,就他一个
if(s>f) swap(s,f),swap(S,fis),ans++;
//还有可能超过第一名,此时照片墙替换
}
}
}
}
cout<
好了,那我就认为你们都听懂了。