Gym 101102C---Bored Judge(区间最大值)
2018-06-17 23:43:31来源:未知 阅读 ()
题目链接
http://codeforces.com/gym/101102/problem/C
problem description
Judge Bahosain was bored at ACM AmrahCPC 2016 as the winner of the contest had the first rank from the second hour until the end of the contest.
Bahosain is studying the results of the past contests to improve the problem sets he writes and make sure this won’t happen again.
Bahosain will provide you with the log file of each contest, your task is to find the first moment after which the winner of the contest doesn’t change.
The winner of the contest is the team with the highest points. If there’s more than one team with the same points, then the winner is the team with smallest team ID number.
The first line of input contains a single integer T, the number of test cases.
The first line of each test case contains two space-separated integers N and Q (1 ≤ N, Q ≤ 105), the number of teams and the number of events in the log file. Teams are numbered from 1 to N.
Each of the following Q lines represents an event in the form: X P, which means team number X (1 ≤ X ≤ N) got P( - 100 ≤ P ≤ 100, P ≠ 0) points. Note that P can be negative, in this case it represents an unsuccessful hacking attempt.
Log events are given in the chronological order.
Initially, the score of each team is zero.
For each test case, if the winner of the contest never changes during the contest, print 0. Otherwise, print the number of the first event after which the winner of the contest didn’t change. Log events are numbered from 1 to Q in the given order.
1
5 7
4 5
3 4
2 1
1 10
4 8
3 -5
4 2
5
题意:有n个人参加活动,现在有Q次事件,标号从1~Q,每个事件为x p 表示第x个人加上p分(-100=<p<=100&&p!=0) 求到第几个事件之后冠军不再变化,冠军为得分最多的那个人,如果多个人得分相同,冠军为序号最小的那个人。
思路:先遍历一遍事件,找到冠军tmp,然后再从第一个事件开始遍历,判断当前的冠军是否是tmp,如果不是则ans=i+1 第二次遍历时就是修改a[x[i]]的值,然后判断最大是是否还是tmp,故可以用RMQ或平衡二叉树(set集合也是平衡二叉树,需要自定义排序);
代码如下:
#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> #include <set> const int MAXN = 1e5+10; using namespace std; const int INF = 1e9; int a[MAXN], x[MAXN], p[MAXN]; struct compare { bool operator() (const int s1, const int s2) const { if(a[s1]==a[s2]) return s1<s2; return a[s1]>a[s2]; } }; set<int,compare>s; set<int,compare>:: iterator it; int main() { int T; cin>>T; while(T--) { s.clear(); int n, q; memset(a, 0, sizeof(a)); scanf("%d%d",&n,&q); for(int i=1; i<=q; i++) { scanf("%d%d",&x[i],&p[i]); a[x[i]] += p[i]; } int Max = -INF, tmp = -1; for(int i=1; i<=n; i++) { if(a[i] > Max) { Max = a[i]; tmp = i; } } memset(a, 0, sizeof(a)); for(int i=1;i<=n;i++) s.insert(i); //cout<<"++: "<<*s.begin()<<endl; int pos = 0; if(*s.begin()!=tmp) pos=1; for(int i=1; i<=q; i++) { s.erase(x[i]); a[x[i]] += p[i]; s.insert(x[i]); if(*s.begin()!=tmp) pos=i+1; } printf("%d\n",pos); } return 0; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:C++ STL简述
- 【题解】Building Strings Gym - 102152E 2020-03-31
- CodeForces Gym 100213F Counterfeit Money 2020-02-13
- 『ACM C++』Virtual Judge | 两道基础题 - The Architect Om 2019-02-25
- 2018-10-06 Gym-101864F丨STL爽题丨想法 2018-10-08
- Gym 100703I---Endeavor for perfection(尺取) 2018-06-27
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash