Codeforces Round #342 (Div. 2) E. Frog Fights set 模拟
E. Frog Fights
题目连接:
http://www.codeforces.com/contest/625/problem/E
Description
stap Bender recently visited frog farm and was inspired to create his own frog game.
Number of frogs are places on a cyclic gameboard, divided into m cells. Cells are numbered from 1 to m, but the board is cyclic, so cell number 1 goes right after the cell number m in the direction of movement. i-th frog during its turn can jump for ai cells.
Frogs move in turns, game starts with a move by frog 1. On its turn i-th frog moves ai cells forward, knocking out all the frogs on its way. If there is a frog in the last cell of the path of the i-th frog, that frog is also knocked out. After this the value ai is decreased by the number of frogs that were knocked out during this turn. If ai is zero or goes negative, then i-th frog doesn't make moves anymore.
After frog number 1 finishes its turn, frog number 2 starts to move, then frog number 3 and so on. After the frog number n makes its move, frog 1 starts to move again, then frog 2 and so on this process goes forever. If some frog was already knocked out from the board, we consider that it skips all its moves.
Help Ostap to identify, what frogs will stay on the board at the end of a game?
Input
First line of the input contains two integers n and m (1 ≤ n ≤ 100000, 1 ≤ m ≤ 109, n ≤ m) — number of frogs and gameboard size, respectively.
Following n lines contains frogs descriptions — two integers pi and ai (1 ≤ pi, ai ≤ m) — the number of cell occupied by i-th frog initially and initial jump length. All pi are guaranteed to be distinct.
Output
In the first line output number of frogs on the final gameboard. In the second line output their numbers in any order.
Sample Input
3 5
2 1
5 3
4 3
Sample Output
1
3
Hint
题意
有n只青蛙,站在m大的一个环形上面
青蛙的位置是pi,每次移动ai
每只青蛙按照编号顺序移动,会撞掉他所经过的青蛙,每撞掉一只,会使得这只青蛙移动的距离减小1
然后问你一直循环下去,还剩下哪些青蛙
题解:
模拟
但是别按照编号的顺序去模拟,我们按照相撞的时间先后顺序去模拟就好了
首先我们可以得到一个信息,这只青蛙如果会和别的青蛙相撞,那么最先相撞的,肯定是他的下一只青蛙。
然后我们把相撞的时间记录下来(究竟是第几个回合,才会撞到那只青蛙
然后我们用一个Set或者优先队列,不断模拟这个过程就好了
每次抽出相撞时间最短的青蛙来,然后撞掉。
最后剩下的青蛙都不相撞为止。
因为每次更新的复杂度是logn,每次更新必定会减小一只青蛙,所以复杂度是nlogn的
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
const int inf = 1e9+7;
int n,m;
int p[maxn],a[maxn];
int nxt[maxn],pre[maxn];
set<pair<int,int> >S;
pair<int,int> c[maxn];
int time(int x,int y)
{
if(x==y)return inf;
long long p1 = p[x],p2 = p[y];
if(x>y)p2=(p2+a[y])%m;
if(p2<p1)p2+=m;
if(p2-p1<=a[x])return 1;
if(a[y]>=a[x])return inf;
int l = 1,r = inf,ans = inf;
while(l<=r)
{
int mid = (l+r)/2;
if(p1+1ll*a[x]*mid>=p2+1ll*a[y]*(mid-1))
ans=mid,r=mid-1;
else
l=mid+1;
}
return ans;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%d%d",&p[i],&a[i]);
p[i]--;
c[i].first = p[i];
c[i].second = i;
}
sort(c,c+n);
for(int i=0;i<n;i++)
{
nxt[c[i].second]=c[(i+1)%n].second;
pre[c[i].second]=c[(i-1+n)%n].second;
}
for(int i=0;i<n;i++)
S.insert(make_pair(time(i,nxt[i]),i));
while(!S.empty())
{
pair<int,int> now = *S.begin();
if(now.first == inf)break;
S.erase(now);
int x = now.second;
S.erase(make_pair(time(nxt[x],nxt[nxt[x]]),nxt[x]));
S.erase(make_pair(time(pre[x],x),pre[x]));
p[x]+=now.first,a[x]--;
nxt[x]=nxt[nxt[x]];
pre[nxt[x]]=x;
S.insert(make_pair(time(pre[x],x),pre[x]));
S.insert(make_pair(time(x,nxt[x]),x));
}
printf("%d\n",S.size());
for(auto s:S)
printf("%d ",s.second+1);
printf("\n");
}
Codeforces Round #342 (Div. 2) E. Frog Fights set 模拟的更多相关文章
- Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)
Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...
- CodeForces Round #521 (Div.3) A. Frog Jumping
http://codeforces.com/contest/1077/problem/A A frog is currently at the point 00 on a coordinate axi ...
- Codeforces Round #342 (Div. 2) D. Finals in arithmetic 贪心
D. Finals in arithmetic 题目连接: http://www.codeforces.com/contest/625/problem/D Description Vitya is s ...
- Codeforces Round #342 (Div. 2) C. K-special Tables 构造
C. K-special Tables 题目连接: http://www.codeforces.com/contest/625/problem/C Description People do many ...
- Codeforces Round #342 (Div. 2) B. War of the Corporations 贪心
B. War of the Corporations 题目连接: http://www.codeforces.com/contest/625/problem/B Description A long ...
- Codeforces Round #342 (Div. 2) A - Guest From the Past 数学
A. Guest From the Past 题目连接: http://www.codeforces.com/contest/625/problem/A Description Kolya Geras ...
- Codeforces Round #342 (Div. 2) D. Finals in arithmetic(想法题/构造题)
传送门 Description Vitya is studying in the third grade. During the last math lesson all the pupils wro ...
- Codeforces Round #342 (Div. 2) C. K-special Tables(想法题)
传送门 Description People do many crazy things to stand out in a crowd. Some of them dance, some learn ...
- Codeforces Round #342 (Div. 2) B. War of the Corporations(贪心)
传送门 Description A long time ago, in a galaxy far far away two giant IT-corporations Pineapple and Go ...
随机推荐
- 去掉每行的特定字符py脚本
百度下载一个脚本的时候遇到那么一个情况.每行的开头多了一个空格.https://www.0dayhack.com/post-104.html 一个个删就不要说了,很烦.于是就有了下面这个脚本. #! ...
- 【HDU5306】Gorgeous Sequence
这个题目是Segment-Tree-beats的论文的第一题. 首先我们考虑下这个问题的不同之处在于,有一个区间对x取max的操作. 那么如何维护这个操作呢? 就是对于线段树的区间,维护一个最大值标记 ...
- 设计模式之笔记--工厂方法模式(Factory Method)
工厂方法模式(Factory Method) 定义 工厂方法模式(Factory Method),定义一个用于创建对象的接口,让子类决定实例化哪一个类.工厂方法使一个类的实例化延迟到其子类. 类图 描 ...
- java虚拟机字节码执行引擎
定义 java虚拟机字节码执行引擎是jvm最核心的组成部分之一,它做的事情很简单:输入的是字节码文件,处理过程是字节码解析的等效过程,输出的是执行结果.在不同的虚拟机实现里,执行引擎在执行java代码 ...
- http协议及http协议和tcp协议的区别
http是应用层的协议,并且无连接,无状态的协议. http协议的特点: 1.支持c/s模式 2.简单快速:客户端向服务器端传送数据的时候,只需要发送请求方法和路径,请求方法有:post,get,he ...
- 《深入浅出MyBatis技术原理与实战》——4. 映射器,5. 动态SQL
4.1 映射器的主要元素 4.2 select元素 4.2.2 简易数据类型的例子 例如,我们需要统计一个姓氏的用户数量.应该把姓氏作为参数传递,而将结果设置为整型返回给调用者,如: 4.2.3 自动 ...
- 一次cloudstack启动cloudstack-agent报错的处理过程
http://www.bubuko.com/infodetail-2397888.html
- Vue.js—组件快速入门及Vue路由实例应用
上次我们学习了Vue.js的基础,并且通过综合的小实例进一步的熟悉了Vue.js的基础应用.今天我们就继续讲讲Vue.js的组件,更加深入的了解Vue,js的使用.首先我们先了解一下什么是Vue.js ...
- CentOS7.5安装坚果云
1.下载坚果云rpm包,对于CentOS,选fedora那个版本的包 https://www.jianguoyun.com/s/downloads/linux 2.安装 坚果云安装依赖notify-p ...
- LoadRunner中常用函数参考手册
基础篇1:LoadRunner中常用函数参考手册 常用函数列表 web_url web_submmit_form VS web_submmit_data VS web_custom_request w ...