题目链接

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
1
5 7
4 5
3 4
2 1
1 10
4 8
3 -5
4 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+;
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(区间最大值)的更多相关文章

  1. Gym 101102C Bored Judge(set--结构体集合)

    这个故事告诉我们,WA了一定要找自己的原因... ... 当我开始用set去做的时候,发现一直过不去,一开始忘了把初始排名加进去,后来忘了第0秒,第0秒第一的id = 1 这个题目的做法也不只这一种, ...

  2. POJ3264 Balanced Lineup 线段树区间最大值 最小值

    Q个数 问区间最大值-区间最小值 // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <i ...

  3. hdoj 2795 Billboard【线段树区间最大值】

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. hdoj1754 I Hate It【线段树区间最大值维护+单点更新】

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  5. HDU 2795 Billboard 线段树,区间最大值,单点更新

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. HDOJ(HDU).1754 I Hate It (ST 单点替换 区间最大值)

    HDOJ(HDU).1754 I Hate It (ST 单点替换 区间最大值) 点我挑战题目 题意分析 从题目中可以看出是大数据的输入,和大量询问.基本操作有: 1.Q(i,j)代表求区间max(a ...

  7. 2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ's Salesman 【离散化+树状数组维护区间最大值】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6447 YJJ's Salesman Time Limit: 4000/2000 MS (Java/O ...

  8. HDU - 1754 I Hate It (线段树单点修改,求区间最大值)

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

  9. 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 ...

  10. hdoj 2795 Billboard 【线段树 单点更新 + 维护区间最大值】

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

随机推荐

  1. 大叔也说Xamarin~Android篇~监听返回键,单击返回某个webView,双击退出

    回到目录 这个操作在原生android里是很容易实现的,在xamarin里也不难,在activity里有方法OnKeyDown,我们只需要重写一下就可以了,然后通过webView重新加载到要返回的页面 ...

  2. Atitit usbQb212 oo 面向对象封装的标准化与规范解决方案java c# php js

    Atitit usbQb212 oo 面向对象封装的标准化与规范解决方案java c# php js 1.1. 封装性是面象对象编程中的三大特性之一  三个基本的特性:封装.继承与多态1 1.2. 魔 ...

  3. salesforce 零基础开发入门学习(三)sObject简单介绍以及简单DML操作(SOQL)

    salesforce中对于数据库操作和JAVA等语言对于数据库操作是有一定区别的.salesforce中的数据库使用的是Force.com 平台的数据库,数据表一行数据可以理解成一个sObject变量 ...

  4. iOS开发——网络实用技术OC篇&网络爬虫-使用青花瓷抓取网络数据

    网络爬虫-使用青花瓷抓取网络数据 由于最近在研究网络爬虫相关技术,刚好看到一篇的的搬了过来! 望谅解..... 写本文的契机主要是前段时间有次用青花瓷抓包有一步忘了,在网上查了半天也没找到写的完整的教 ...

  5. Android WebView 总结 —— Java和JavaScript交互

    交互如何实现 实现Java和js交互十分便捷.通常只需要以下几步. WebView开启JavaScript脚本执行 WebView设置供JavaScript调用的交互接口. 客户端和网页端编写调用对方 ...

  6. KnockoutJS 3.X API 第四章(14) 绑定语法细节

    data-bind绑定语法 Knockout的声明性绑定系统提供了一种简洁而强大的方法来将数据链接到UI. 绑定到简单的数据属性或使用单个绑定通常是容易和明显的. 对于更复杂的绑定,它有助于更好地了解 ...

  7. jQuery贪吃蛇--jQuery学习

    我用JQuery有一段时间了,越来越体会到其强大之处,于是自己尝试写了一个贪吃蛇小游戏,拿来与网友分享一下. 1. 了解JQuery.Timers 除用到了jQuery1.5.1之外,我还用到了jQu ...

  8. input---checked小问题

    想必有很多做前端同学都遇到了这么一个问题. 那就是checkbox.那就是我们通过jquery设置选中的时候,发现checked属性已经设置上去了 但是选中的样式却没有. 我们做一个简单的测试:看下面 ...

  9. SSIS Component的ValidateExternalMetadata属性

    ValidateExternalMetadata Property Indicates whether the component validates its column metadata agai ...

  10. Android入门(十)SQLite创建升级数据库

    原文链接:http://www.orlion.ga/603/ 一.创建数据库 Android为了让我们能够更加方便地管理数据库,专门提供了一个 SQLiteOpenHelper帮助类, 借助这个类就可 ...