[28] Game on Ranges

这篇博客讨论了一种编程竞赛问题,其中两个人A和B交替从1到n的数列中删除元素。A选择一段连续的子序列,而B从中删除一个数字。策略是记录每个数字被选择的次数,然后B按照次数从小到大选择。文章指出在实现过程中需要注意避免一些常见错误,如忘记清零数组。提供的代码实现了该策略,并通过了所有测试用例。

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

 

题意:一个1到n的数列,每次一个人a选出其中一段(连续的)数列,另一个人b从中选择并删除一个数,直到1-n的数列为空结束。告诉a每次选出的数列段(告诉的顺序是任意的),求其对应每次b取的数字(按任意顺序输出)。

算法:无

问题:主体思路没错,编程时有一些小错误,导致微调时间过长。(比如忘记memset,一端忘记写了……以后绝不能再犯这种错误了)

思路: 记录每个数字被a选到过的次数,可以发现b是按照次数由小到大的顺序取数的。

代码:ac

#include<bits/stdc++.h>
using namespace std;
int t;
int n;
int p[1010];
void dp(int l, int r, int step);
int main()
{
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		int tep = n;
		memset(p, 0, sizeof(p));
		while (n--) {
			int a, b;
			scanf("%d%d", &a, &b);
			for (int i = a; i <= b; i++)
				p[i]++;
		}
		dp(1,tep,1);
	}
	return 0;
}
void dp(int l,int r,int step)
{
	if (l == r) {
		printf("%d %d %d\n", l, l, l); return;
	}
	int temp;
	for (int k = l; k <= r; k++) {
		if (p[k] == step) {
			temp = k; break;
		}
	}
	printf("%d %d %d\n", l, r, temp);
	if (l == temp||r==temp) {
		if(l==temp&&r==temp)dp(temp, temp, step + 1); 
		else if (temp == l && temp != r)dp(temp + 1, r, step + 1);
		else if (temp == r && temp != l)dp(l, temp - 1, step + 1);
	}
	else {
		dp(l, temp - 1, step + 1);
		dp(temp + 1, r, step + 1);
	}
	return;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值