题目链接

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.

Input

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.

Output

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.

Example
input
15 74 53 42 11 104 83 -54 2

output
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;
}

 

转载于:https://www.cnblogs.com/chen9510/p/5923054.html

Gym 101102C---Bored Judge(区间最大值)相关推荐

  1. 线段树维护区间最大值+第 45 届(ICPC)亚洲区域赛(昆明)L题Simone and Graph Coloring

    题意: 给你n个数的序列,当满足i<ji<ji<j andandand ai>aja_i>a_jai​>aj​时,这两个点之间有一条边,现在对点染色,要求每个点相邻 ...

  2. 【HDU - 1754】I Hate It (线段树模板 单点覆盖更新+区间最大值查询)

    题干: 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.  这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当 ...

  3. 求数组所有区间最大值减去最小值之差的和(贝壳笔试题)

    这个题直接暴力求解的话时间复杂度肯定是不行的,所以,我们要计算每个数值的贡献,对每一个数求他当最小值当了多少次,当最大值当了多少次,最后当最大值的次数乘以这个数值减去当最小值的次数乘以数值就得到这个数 ...

  4. 【RMQ问题】求数组区间最大值,NYOJ-1185-最大最小值

    转自:http://blog.csdn.net/lilongherolilong/article/details/6624390 先挖好坑,明天该去郑轻找虐 RMQ(Range Minimum/Max ...

  5. AcWing 1270. 数列区间最大值(RMQ问题)

    题目链接 https://www.acwing.com/problem/content/description/1272/ 思路 我们这次需要维护一下区间的最大值,也就是经典的 RMQ 问题,由于没有 ...

  6. 文远知行杯广东工业大学第十六届 A题 区间最大值

    给出位置区间,求区间内最大值. 已知:y 是 关于 位置x 的函数 问题:求位置区间内的最大值, 算法1 算出区间内每个位置x对应的函数值y 算法2 找到函数的"单调性" 我们发现 ...

  7. NYOJ-备用2344 盖伦的告白(线段树,区间最大值)

    题目: 2344: 盖伦的告白 时间限制: 1 秒   内存限制: 128 MB 提交: 57   解决: 20 提交  状态 题目描述 盖伦和赵信这对基友又在打赌,谁输了就去向卡特琳娜告白.. 这一 ...

  8. Splay ---- 区间翻转 区间最大值 区间加 P4146 序列终结者

    题目链接 题目大意: 解题思路: 这是一道很入门的Splay的题目 但是第一次写有很多坑点 首先是maxmaxmax值的更新:就是因为这个点的最小值是会出现负数负数的,当你左右儿子有一个没有的话,那么 ...

  9. 【BZOJ3956】Count,单调栈+ST表维护区间最大值

    Time:2016.08.11 Author:xiaoyimi 转载注明出处谢谢 传送门 思路: TA爷眼中的水题 首先有个特别的结论 总共的点对数不会超过2n 因为对于元素i来说,如果只考虑与比它高 ...

最新文章

  1. Android aar 代码查看
  2. pythontcp服务器框架_tcp服务器框架python
  3. git 你get了吗(git命令日常使用)
  4. 比较高明的暗部提亮方法:选取暗部,滤色叠加
  5. Get Form type using javascript in CRM 2011
  6. mysql typeindex_explain mysql的type字段,索引的类型
  7. python怎么安装request_【python】如何安装requests
  8. 百度和腾讯之间就差一个好的投资团队
  9. Windows 3.1 往事:历史上第一个真正占据主导地位的操作系统
  10. 大师兄科研网_拜托啦,师兄!
  11. sql如何在两张表中得到每组数据,并知道数据的个数,举例,判断有多少班级,每个班的人数
  12. 【前端】JS 计算贷款月付
  13. 中小企业如何取舍OA办公系统的功能?
  14. Git-Dumper工具:从站点中导出一个Git库
  15. 几个不太常用,需要记录一下的Excel经验
  16. iPhone 手机存储空间没有了
  17. vue校验表格数据_如何通过数据验证限制Google表格中的数据
  18. 品管七大手法-2排列图(转载)
  19. BT5 在线视频教程
  20. Arkime 2.7(原Moloch)docker镜像构建

热门文章

  1. jQuery弹出层插件大全
  2. 3.15曝光“山寨”杀毒软件“杀毒三宗罪”
  3. Django前后端增删改查
  4. TypeError at / __init__() takes exactly 1 argument (2 given)
  5. 生成式模型和判别式模型(转)
  6. wingIDE设置python虚拟环境并运行
  7. 使用任意数量的关键字实参
  8. java oracle 视图不存在_Weblogic 10.3,JDBC,Oracle,SQL - 表或视图不存在
  9. 优酷html5视频没有弹幕,优酷弹幕怎么设置 优酷PC端怎么屏蔽底下弹幕?
  10. for循环的break和continue