题目描述
One hundred years from now, in 2117, the International Collegiate Programming Contest (of which the NCPC is a part) has expanded significantly and it is now the Galactic Collegiate Programming Contest (GCPC).
This year there are n teams in the contest. The teams are numbered 1, 2, . . . , n, and your favorite team has number 1.
Like today, the score of a team is a pair of integers (a, b) where a is the number of solved problems and b is the total penalty of that team.
When a team solves a problem there is some associated penalty (not necessarily calculated in the same way as in the NCPC – the precise details are not important in this problem). The total penalty of a team is the sum of the penalties for the solved problems of the team.
Consider two teams t 1 and t 2 whose scores are (a1 , b1 ) and (a2 , b2 ). The score of team t 1 is better than that of t 2 if either a1 > a2 , or if a1 = a2 and b1 < b2 . The rank of a team is k + 1 where k is the number of teams whose score is better.
You would like to follow the performance of your favorite team. Unfortunately, the organizers of GCPC do not provide a scoreboard. Instead, they send a message immediately whenever a team solves a problem.
输入
The first line of input contains two integers n and m, where 1 ≤ n ≤ 105 is the number of teams,and 1 ≤ m ≤ 105 is the number of events.
Then follow m lines that describe the events. Each line contains two integers t and p (1 ≤ t ≤ n and 1 ≤ p ≤ 1000), meaning that team t has solved a problem with penalty p. The events are ordered by the time when they happen.
输出
Output m lines. On the i’th line, output the rank of your favorite team after the first i events have happened.
样例:
3 4 2 7 3 5 1 6 1 9
输出:
2 3 2 1
分析:有n名选手(编号1-n)在打一场比赛,m个事件每个事件给出a和b,a代表a队伍A了一个题,b代表a队伍A这个题用的罚时。每次事件发生后输出队伍编号为1选手的排名。因为只关心选手1,所以没必要保存所有选手的排名,只需要关心排名比1靠前的有多少队就好了,所以开一个维护比1靠前队伍的队列,如果出题的不是1队,且这个队没有在队列里,那么就询问他是不是比1靠前了,如果是的话就入队,不是的话不管他。如果是1队出题了,那就查找队列里是不是有比1队靠后的队伍,把他们出队就好了。操作的话可以先pop当前队列里的所有队伍,如果还在里面就再push进去。
代码:
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef struct
{
int a,b;
} DUI;
DUI s[100005];
int comp(DUI a,DUI b)
{
if(a.a==b.a&&a.b==b.b)
{
return 0;
}
else if(a.a>b.a||(a.a==b.a&&a.b<b.b))
{
return 1;
}
else
{
return -1;
}
}
queue<int>q;
bool vis[100005];
int n,m;
int main()
{
int tot=0;
scanf("%d %d",&n,&m);
int x,y;
for(int i=1; i<=m; i++)
{
scanf("%d %d",&x,&y);
s[x].a++;
s[x].b+=y;
if(x!=1)
{
if(!vis[x])
{
if(comp(s[x],s[1])>0)
{
vis[x]=true;
tot++;
q.push(x);
}
printf("%d\n",tot+1);
}
else
{
printf("%d\n",tot+1);
}
}
else
{
int w;
int t=tot;
for(int j=1; j<=t; j++)
{
w=q.front();
vis[w]=false;
q.pop();
tot--;
if(comp(s[w],s[1])>0)
{
tot++;
vis[w]=true;
q.push(w);
}
}
printf("%d\n",tot+1);
}
}
return 0;
}