CF1137D Cooperative Game 题解
前言:
今天学双指针的时候,讲了这个神奇的 Floyd 判环。
大致思路:
虽然题目上写了有 101010 枚棋子,但我们最多只需要用到 333 颗即可。利用类似 Floyd 判环的方法,随便拿出 222 颗棋子,其中命名慢棋子和快棋子,他们代表的意思显而易见,慢棋子移动慢,快棋子移动快。快棋子每次操作都移动一遍,慢棋子每两次操作移动一遍,那么在最多 2(t+c)2(t + c)2(t+c) 次操作后,两个棋子会相遇在一个点上,接下来每次操作把所有棋子都挪动一次,如果所有棋子都在一个点时,说明找到答案点了,总次数最多 3t+2c3t + 2c3t+2c 次,可以通过。
代码实现:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 10;
const int MOD = 1000000007;
inline int read(){
int r = 0,w = 1;
char c = getchar();
while (c < '0' || c > '9'){
if (c == '-'){
w = -1;
}
c = getchar();
}
while (c >= '0' && c <= '9'){
r = (r << 3) + (r << 1) + (c ^ 48);
c = getchar();
}
return r * w;
}
int n;
signed main()
{
while(1)
{
cout << "next 0\n";
fflush(stdout);
n = read();
for(int i = 1;i <= n; ++ i)
{
int x;
x = read();
}
cout << "next 0 1\n";
fflush(stdout);
n = read();
for(int i = 1;i <= n; ++ i)
{
int x;
x = read();
}
if(n == 2)
{
break;
}
}
while (1)
{
cout << "next 0 1 2 3 4 5 6 7 8 9\n";
fflush(stdout);
n = read();
for(int i = 1;i <= n; ++ i)
{
int x;
x = read();
}
if(n == 1)
{
break;
}
}
cout << "done\n";
fflush(stdout);
return 0;
}