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

## 题解：

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF  0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-9
#define maxn 1000100
#define MOD 1000000007
#include<bits/stdc++.h>
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) >> 1;
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()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t,C = 1;
//scanf("%d",&t);
while(scanf("%d%d",&n,&m) != EOF)
{
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(set<pair<int,int> >::iterator it = S.begin(); it != S.end(); it++)
printf("%d ",(*it).second+1);
printf("\n");
}
return 0;
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：Codeforces Round #342 (Div. 2) E. Frog Fights set 模拟★ ★ 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)