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 ...
随机推荐
- PDO连接mysql和pgsql数据库
PDO连接mysql数据库 <?php $dsn="mysql:host=localhsot;dbname=lamp87"; $user="root"; ...
- salesforce 零基础开发入门学习(十)IDE便捷小知识
在这里介绍两个IDE的便捷开发的小知识. 一) 本地调试 由于salesforce代码只能提交以后才能调试,所以很多时候调试代码很麻烦.新版增加了一个特性:即可以在本地调试相关的代码或者查看相关代码运 ...
- 转:Acegi Security
Acegi Security -- Spring下最优秀的安全系统 http://www.springside.org.cn/docs/reference/Acegi.htm 1. Acegi 介绍 ...
- react7 react 三目运算
<body><!-- React 真实 DOM 将会插入到这里 --><div id="example"></div> <!- ...
- WPF入门教程系列二——Application介绍
一.Application介绍 WPF和WinForm 很相似, WPF与WinForm一样有一个 Application对象来进行一些全局的行为和操作,并且每个 Domain (应用程序域)中仅且只 ...
- C++中“类”相关知识点汇总
一:类中默认的成员函数 一个空的class在C++编译器处理过后就不再为空,编译器会自动地为我们声明一些member function,如果你写 class Empty{}; 就相当于: class ...
- OpenCascade Matrix
OpenCascade Matrix eryar@163.com 摘要Abstract:本文对矩阵作简要介绍,并结合代码说明OpenCascade矩阵计算类的使用方法. 关键字Key Words:Op ...
- jsoup简单的爬取网页数据
/** * Project Name:JavaTest * File Name:BankOfChinaExchangeRate.java * Package Name:com.lee.javatest ...
- Oracle工具之DBNEWID
DBNEWID是Oracle提供的一个用于修改数据库DBID和DBNAME的工具. 在引进该工具之前,如果我们想修改数据库的数据库名,必须重建控制文件.但即便如此,也无法修改该数据库的DBID.众所周 ...
- [Node.js] Node.js中的流
原文地址:http://www.moye.me/2015/03/29/streaming_in_node/ 什么是流? 说到流,就涉及到一个*nix的概念:管道——在*nix中,流在Shell中被实现 ...