C. Number Game

Problem - C - Codeforces

数据量很小,直接模拟就行了。枚举n个数字。bob想赢,就每次去掉一个1。alice没有1就输了。每次alice去掉的数字直接放成最大值,排序即可。

代码如下

#include <bits/stdc++.h>
#include <cstdlib>
#define int long long
using namespace std;

int n;

bool pan(int x, vector<int> a)
{
	int k = x;
	
	while (k)
	{
		sort(a.begin(), a.end());
		bool flag = false;
		for (int i = n - 1; i >= 0; i --)
		{
			if (a[i] <= k)
			{
				flag = true;
				a[i] = 1e9;
				break;
			}
		}
		if (!flag) return 0;
		a[0] += k;
		k --;
	}
	return true;
}

void solve()
{
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i ++) cin >> a[i];
	int k = n;
	for (int i = k; i >= 0; i --)
	{
		if (pan(i, a))
		{
			cout << i << "\n";
			return ;
		}
	}
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int t;
	cin >> t;
	while (t --)
	{
		solve();
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The Hanoi game is a classic problem in computer science and can be solved using recursion. Here is a Python implementation of the Hanoi game: ```python def move(n, source, target, auxiliary): if n > 0: # Move n-1 plates from source to auxiliary using target as auxiliary move(n-1, source, auxiliary, target) # Move the largest plate from source to target target.append(source.pop()) # Move the remaining n-1 plates from auxiliary to target using source as auxiliary move(n-1, auxiliary, target, source) # Example usage source = [3, 2, 1] target = [] auxiliary = [] move(len(source), source, target, auxiliary) print(target) ``` The `move` function takes four arguments: `n` is the number of plates to move, `source` is the source rod, `target` is the target rod, and `auxiliary` is the auxiliary rod. The function recursively moves `n-1` plates from the source rod to the auxiliary rod using the target rod as an auxiliary, then moves the largest plate from the source rod to the target rod, and finally moves the `n-1` plates from the auxiliary rod to the target rod using the source rod as an auxiliary. The optimized algorithm for the Hanoi game is to use an iterative approach instead of recursion. Here is a Python implementation of the iterative Hanoi game: ```python def move(n, source, target, auxiliary): if n % 2 == 0: auxiliary, target = target, auxiliary else: source, target = target, source for i in range(1, 2**n): if i % 3 == 1: source, target = target, source elif i % 3 == 2: source, auxiliary = auxiliary, source else: auxiliary, target = target, auxiliary if i & (i-1) == 0: plate = source.pop() target.append(plate) # Example usage source = [3, 2, 1] target = [] auxiliary = [] move(len(source), source, target, auxiliary) print(target) ``` The `move` function takes the same arguments as before. The function first determines the order in which to move the plates based on the parity of `n` and the index of the plate. Then, the function uses a loop to move each plate in the correct order. Finally, the function checks if the current plate is the largest plate on the source rod and moves it to the target rod if it is. This approach is more efficient than the recursive approach because it avoids the overhead of function calls and stack frames.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值