有\(n\)只青蛙在一个长度为\(m\)的环上打架;每只青蛙有一个初始位置\(p_i\),和一个跳跃数值\(a_i\)。从\(1\)号青蛙开始按序号循环行动,每次若第\(i\)只青蛙行动,则它会向前跳 \(a_i\)个格子,撞飞它遇见的所有青蛙,包括终点格子上的,之后它的\(a_i\)减少等同于撞飞的青蛙只数,若\(a_i<0\),它不会移动。求最后剩下的所有青蛙的编号。\(n\leq 10^5,m\leq 10^9\),\(p\)互不不同


这类问题就往相对位置的方向想吧,,,

在第一次有青蛙撞飞其他青蛙之前,他们的相对位置都是不变的。所以考虑优化最朴素的模拟,即维护出第一次撞飞别人是什么时候以及哪只青蛙撞。所以一个优化的算法就是撞飞一只青蛙之后重新计算所有青蛙的位置。又由于每个青蛙只需要注意它正前方的那只,所以我们可以做一个链表来维护青蛙的相对位置。这样复杂度还是很高。但可以发现撞飞别人以后仍然有很多青蛙的相对位置没有变。如果我们每次都通过\(初始距离之差/a之差\)来计算青蛙相撞的时间,当某只青蛙撞完别人并且a减1之后,我们仍然用这个公式算就会有误差。假设这只青蛙在\(t\)时刻撞了别人,可以发现我们让它的位置往前挪\(t\)个单位就可以消除这个误差。所以我们就有一个新的算法,即每次相撞后修改一下撞击者的位置。这样就是\(O(nlogn)\)了。

#include<bits/stdc++.h>
#define rg register
#define il inline
#define cn const
#define gc getchar()
#define fp(i,a,b) for(rg int i=(a),ed=(b);i<=ed;++i)
#define fb(i,a,b) for(rg int i=(a),ed=(b);i>=ed;--i)
#define mp make_pair
using namespace std;
typedef cn int cint;
typedef pair<int,int> pr;
il int rd(){
rg int x(0),f(1); rg char c(gc);
while(c<'0'||'9'<c){if(c=='-')f=-1;c=gc;}
while('0'<=c&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=gc;
return x*f;
}
cint maxn=1e5+10,inf=0x3f3f3f3f;
int n,m,pre[maxn],nxt[maxn];
set<pr> st;
struct frog{int p,a,id;}a[maxn];
il bool cmp(cn frog &a,cn frog &b){return a.p<b.p;}
il bool cmp2(cn frog &a,cn frog &b){return a.id<b.id;}
il int calc(int x,int y){
frog p=a[x],q=a[y];
rg int res=0;
if(x<y){
rg int tmp=p.p,now=tmp+p.a,now2=q.p;
p.p=(p.p+p.a-1)%m+1,res=1;
if(now2<tmp)now2+=m;
if(tmp<=now2&&now2<=now)return 1;
}
rg int dis=(q.p-p.p+m)%m,dec=p.a-q.a;
if(dec<=0)return inf;
return res+(dis+dec-1)/dec;
}
int main(){
n=rd(),m=rd();
fp(i,1,n)a[i].p=rd(),a[i].a=rd(),a[i].id=i;
sort(a+1,a+1+n,cmp);
pre[a[1].id]=a[n].id,nxt[a[1].id]=a[2].id,nxt[a[n].id]=a[1].id,pre[a[n].id]=a[n-1].id;
fp(i,2,n-1)pre[a[i].id]=a[i-1].id,nxt[a[i].id]=a[i+1].id;
sort(a+1,a+1+n,cmp2);
fp(i,1,n)st.insert(mp(calc(i,nxt[i]),i));
int fl=0;
while(st.size()){
set<pr>::iterator it=st.begin();
if(it->first==inf)break;
rg int i=it->second,tem=it->first;
st.erase(it);
st.erase(mp(calc(nxt[i],nxt[nxt[i]]),nxt[i]));
st.erase(mp(calc(pre[i],i),pre[i]));
--a[i].a,a[i].p=(a[i].p+tem-1)%m+1;
pre[nxt[nxt[i]]]=i,nxt[i]=nxt[nxt[i]];
st.insert(mp(calc(pre[i],i),pre[i]));
st.insert(mp(calc(i,nxt[i]),i));
}
printf("%d\n",st.size());
for(auto &x:st)printf("%d ",x.second);
return 0;
}

CF625E Frog Fights的更多相关文章

  1. 【CF625E】Frog Fights(模拟)

    [CF625E]Frog Fights(模拟) 题面 CF 洛谷 翻译: 有\(n\)只青蛙在一个被分为了\(m\)等分的圆上,对于每份顺时针依次标号. 初始时每只青蛙所在的位置是\(p_i\),速度 ...

  2. Codeforces Round #342 (Div. 2) E. Frog Fights set 模拟

    E. Frog Fights 题目连接: http://www.codeforces.com/contest/625/problem/E Description stap Bender recentl ...

  3. Codeforces Round #342 (Div 2) 解题报告

    除夕夜之有生之年CF第一场 下午从奶奶家回到姥姥家,一看还有些时间,先吃点水果陪姥姥姥爷聊了会儿,再一看表,5:20....woc已经开场20分钟了...于是抓紧时间乱搞.. **A. Guest F ...

  4. [LeetCode] Frog Jump 青蛙过河

    A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...

  5. hdu5037 Frog (贪心)

    http://acm.hdu.edu.cn/showproblem.php?pid=5037 网络赛 北京 比较难的题 Frog Time Limit: 3000/1500 MS (Java/Othe ...

  6. CF #305 (Div. 2) C. Mike and Frog(扩展欧几里得&&当然暴力is also no problem)

    C. Mike and Frog time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  7. Frog Jump

    A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...

  8. POJ 1054 The Troublesome Frog

    The Troublesome Frog Time Limit: 5000MS Memory Limit: 100000K Total Submissions: 9581 Accepted: 2883 ...

  9. Leetcode: Frog Jump

    A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...

随机推荐

  1. Hive通过Jdbc获取表的字段信息

    参考代码如下: /** * 按顺序返回字段 * desc table的返回结果形式如下: hive> describe ind01acoM; OK acq_ins_id_cd string cu ...

  2. css 05-CSS样式表的继承性和层叠性

    05-CSS样式表的继承性和层叠性 #本文重点 CSS的继承性 CSS的层叠性 计算权重 权重问题大总结 CSS样式表的冲突的总结 权重问题深入 同一个标签,携带了多个类名 !important标记 ...

  3. node目录

    1 [了解nodejs] 1.1 [node简介] 1.2 [node的特点] 1.3 [如何安装] 1.4 [如何运行] 2 [es6--基本语法] 2.1 [变量的解构赋值] 2.2 [解构赋值] ...

  4. sqlmap进阶篇—POST注入三种方法

    测试是否存在post注入 第一种方法 直接加--form让它自动加载表单 第二种方法 把form表单里面提交的内容复制出来,放到data中跑 第三种方法 先用burp suite抓包,把包的内容存到本 ...

  5. Linux简单复习

    cd 命令:切换目录 ls命令:用于浏览目录下的文件或者文件夹 rm 命令:用于删除文件或者目录,用法 rm –rf test.txt (-r表示递归,-f表示强制) cp 命令:用于拷贝文件,用法, ...

  6. Vue必须必须要注意的几个细节

    1.每次执行完,尽量npm run dev 一次,有时候又缓存问题 2.安装sass 一.使用save会在package.json中自动添加.因为sass-loader依赖于node-sass npm ...

  7. python脚本乱码的解决方法

    使用python2 在windows cmd 执行python脚本发生乱码的解决方法 可以先把中文解码为unicode,然后再转化为gbk显示正常,需要在代码打印中文处添加 print(':这是一段中 ...

  8. Dapper 返回Sql server 自增长ID 标识列SCOPE_IDENTITY

    原理 使用SELECT SCOPE_IDENTITY(),取获取刚刚插入记录自增的主键 示例 entity.Create(); StringBuilder strSql = new StringBui ...

  9. 5.自定义view-评分控件RatingBar

    1.效果 2.实现原理 1.根据分数分别画选中的星星.正常的星星 2.onTouchEvent 中获取点击.滑动的位置,修改分数,在通过invalidate() 去重新绘制 核心代码: @Overri ...

  10. 算法竞赛入门经典第二版第二章习题-(练习Java和C++语法)

    习题2-1水仙花数(daffodil) 输出1000-999中所有的水仙花数.若三位数ABC满足ABC = A3+B3+C3,则称其为水仙花数. Java: package suanfa; publi ...