有\(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. DRF类视图让你的代码DRY起来

    刚开始写views.py模块的代码,一般都是用def定义的函数视图,不过DRF更推荐使用class定义的类视图,这能让我们的代码更符合DRY(Don't Repeat Yourself)设计原则: 使 ...

  2. 【漏洞复现】Struts2-045分析(CVE-2017-5638)

    如果需要大佬写好的脚本,可以直接去github上面搜 struts2 - 045 一个还比较出名的漏洞,因为涉及到利用Gopher协议反弹shell,所以写篇文章来简单学习下这个漏洞. Struts2 ...

  3. C# 中 ConcurrentDictionary 一定线程安全吗?

    根据 .NET 官方文档的定义:ConcurrentDictionary<TKey,TValue> Class 表示可由多个线程同时访问的线程安全的键/值对集合.这也是我们在并发任务中比较 ...

  4. Redis集群的分布式部署

    3.2.2:Redis Cluster: Redis  分布式部署方案: 1)  客户端分区:由客户端程序决定 key 写分配和写入的 redis node,但是需要客户端自己处理写入 分配.高可用管 ...

  5. Python 炫技操作:安装包的八种方法,你知道吗?

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理 1. 使用 easy_install easy_install 这应该是最古老的包安装方式了,目前基 ...

  6. python简单爬去前程无忧信息招聘

    import sys reload(sys) sys.setdefaultencoding('utf-8') import requests import csv from BeautifulSoup ...

  7. Centos7 根目录存储空间扩展方法

    Centos7 根目录存储空间扩展方法   一.首先通过 df -hl 命令查看磁盘占用情况,其中根目录已经被占满,此时需要对其进行扩容   二.针对虚拟机环境的centos7系统根存储空间扩容,可利 ...

  8. 686. Repeated String Match判断字符串重复几次可以包含另外一个

    public static int repeatedStringMatch(String A, String B) { //判断字符串a重复几次可以包含另外一个字符串b,就是不断叠加字符串a直到长度大 ...

  9. DNS主从服务器配置实现

    主服务器-centos7-IP:192.168.184.201 从服务器-centos7-IP:192.168.184.202 客户端-ubuntu1804-IP:192.168.184.150 ①客 ...

  10. java线程与内核线程的关系,及怎么定义ThreadPoolExecutor相关参数

    p.p1 { margin: 0; font: 12px Menlo } p.p1 { margin: 0; font: 12px Menlo } p.p2 { margin: 0; font: 12 ...