题解:CF1137D Cooperative Game

本文介绍了如何使用双指针技巧和Floyd判环算法解决CF1137DCooperativeGame中的问题,通过控制快慢棋子的移动,找出最少操作次数使所有棋子相遇,涉及C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值