Gym 101102C---Bored Judge(区间最大值)
题目链接
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+;
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, , sizeof(a));
scanf("%d%d",&n,&q);
for(int i=; i<=q; i++)
{
scanf("%d%d",&x[i],&p[i]);
a[x[i]] += p[i];
}
int Max = -INF, tmp = -;
for(int i=; i<=n; i++)
{
if(a[i] > Max)
{
Max = a[i];
tmp = i;
}
}
memset(a, , sizeof(a));
for(int i=;i<=n;i++)
s.insert(i);
//cout<<"++: "<<*s.begin()<<endl;
int pos = ;
if(*s.begin()!=tmp) pos=;
for(int i=; i<=q; i++)
{
s.erase(x[i]);
a[x[i]] += p[i];
s.insert(x[i]);
if(*s.begin()!=tmp) pos=i+;
}
printf("%d\n",pos);
}
return ;
}
Gym 101102C---Bored Judge(区间最大值)的更多相关文章
- Gym 101102C Bored Judge(set--结构体集合)
这个故事告诉我们,WA了一定要找自己的原因... ... 当我开始用set去做的时候,发现一直过不去,一开始忘了把初始排名加进去,后来忘了第0秒,第0秒第一的id = 1 这个题目的做法也不只这一种, ...
- POJ3264 Balanced Lineup 线段树区间最大值 最小值
Q个数 问区间最大值-区间最小值 // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <i ...
- hdoj 2795 Billboard【线段树区间最大值】
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdoj1754 I Hate It【线段树区间最大值维护+单点更新】
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU 2795 Billboard 线段树,区间最大值,单点更新
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDOJ(HDU).1754 I Hate It (ST 单点替换 区间最大值)
HDOJ(HDU).1754 I Hate It (ST 单点替换 区间最大值) 点我挑战题目 题意分析 从题目中可以看出是大数据的输入,和大量询问.基本操作有: 1.Q(i,j)代表求区间max(a ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ's Salesman 【离散化+树状数组维护区间最大值】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6447 YJJ's Salesman Time Limit: 4000/2000 MS (Java/O ...
- HDU - 1754 I Hate It (线段树单点修改,求区间最大值)
很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老师有 ...
- Codeforces Round #321 (Div. 2)-B. Kefa and Company,区间最大值!
->链接在此<- B. Kefa and Company time limit per test 2 seconds memory limit per test 256 megabytes ...
- hdoj 2795 Billboard 【线段树 单点更新 + 维护区间最大值】
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
随机推荐
- SlickUpload Upload to disk
The file upload stream provider is a built-in SlickUpload provider that uses the filesystem for uplo ...
- ServletConfig接口默认是哪里实现的?
问题:Servlet接口默认是哪里实现的? 答:GenericServlet 1.结构 2.ServletConfig.GenericServlet.HttpServlet的关系如下: public ...
- 行集函数:OpenRowSet 和 OpenQuery
在SQL Server中,行集函数是不确定性的,这意味着,每次调用,返回值不总是相同的.返回值是不确定的,这意味着,对于相同的输入值,不保证每次返回的值都是相同的.对行集函数的每次调用,行集函数都是单 ...
- .NET程序集强命名删除与再签名技术 源代码剖析
如果你想去除一个程序集的强签名(strong name),目前为止可以有两个途径 1 反编译为IL代码,删除签名部分,再编译为程序集 2 应用Re-Sign程序,直接对一个程序集再签名 生成和读取 ...
- 【原创】Matlab.NET混合编程技巧之找出Matlab内置函数
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 Matlab和C#混合编程文章目录 :[目录]Matlab和C#混合编程文章目录 Matlab与.N ...
- Microsoft Naive Bayes 算法——三国人物身份划分
Microsoft朴素贝叶斯是SSAS中最简单的算法,通常用作理解数据基本分组的起点.这类处理的一般特征就是分类.这个算法之所以称为“朴素”,是因为所有属性的重要性是一样的,没有谁比谁更高.贝叶斯之名 ...
- 10分钟学会理解和解决MySQL乱码问题
在阅读本文之前,强烈建议对字符集编码概念还比较模糊的同学 阅读下博主之前对相关概念的一篇科普:十分钟搞清字符集和字符编码 本博客已经迁移至: http://cenalulu.github.io/ 为了 ...
- (转)Shell函数
Shell函数类似于Shell脚本,里面存放了一系列的指令,不过Shell的函数存在于内存,而不是硬盘文件,所以速度很快,另外,Shell还能对函数进行预处理,所以函数的启动比脚本更快. 1. 函数 ...
- android.os.networkonmainthreadexception怎么解决
在android2.3以后,在主线程中必须使用另一个线程,如handler机制,或者异步任务获取网络数据. 如果你的访问网络操作必须放在主线程中执行,那么在onCreate()中添加 if (GetV ...
- 漫谈可视化Prefuse(六)---改动源码定制边粗细
可视化一路走来,体会很多:博客一路写来,收获颇丰:代码一路码来,思路越来越清晰.终究还是明白了一句古话:纸上得来终觉浅,绝知此事要躬行. 跌跌撞撞整合了个可视化小tool,零零碎碎结交了众多的志同道合 ...