G.人生的起落(2024牛客寒假训练营6)

思路解析:

可以分为三类情况分开讨论

1、当2*k+1>n时,很显然无法构造

2、当2*k+1==n时,这种情况注意一下细节,注意一下y的取值可能各不相同,另外提供两种测试案例供大家测试:11 32 5 和 11 33 5

3、当2*k+1<n时,在三元组(2,1,2)的基础上构造即可,注意5 8 1 需要输出2 1 2 2 1(保证三元组恰好为一个)。

代码如下:

#include<bits/stdc++.h>
using namespace std;
#define int long long
using T = pair<int, int>;
//set<int>S;
//unordered_map<int, int>mp;
const int N = 2e5 + 10;
int a[N];
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int t; cin >> t;
	while (t--) {
		int n, S, k; cin >> n >> S >> k;
		int i;
		if (2 * k + 1 > n) {
			cout << -1 << endl;
		}
		else if (2 * k + 1 == n) {
			if (k == 0) {
				cout << S << endl;
			}
			else {
				int x, flag = 0;
				int y;
				for (x = 2; x <= S; ++x) {
					y = S - (k + 1) * x;
					if (y <= 0)break;
					if (y >= k && y <= (x - 1) * k) {
						flag = 1;
						break;
					}
				}
				if (flag == 0)cout << -1 << endl;
				else {
					int y1 = y / k;
					int y2 = S - (k + 1) * x - y1 * (k - 1);
					for (i = 1; i <= n; ++i) {
						if (i & 1)cout << x << " ";
						else {
							if (y2 >= x) {
								if (i == n - 1)cout << y2 - 2 << " ";
								else if (i == n - 3)cout << y1 + 1<<" ";
								else if(i==n-5)cout << y1 + 1 << " ";
								else cout << y1 << " ";
							}
							else {
								if (i != n - 1)cout << y1 << " ";
								else cout << y2 << ' ';
							}
						}
					}cout << endl;
				}
			}
		}
		else {
			if (k == 0) {
				if (S < n)cout << -1 << endl;
				else
					for (i = 1; i <= n; ++i) {
						if (i != n) {
							cout << 1 << " ";
						}
						else {
							cout << S - (n - 1) << endl;
						}
					}
			}
			else {
				if (S - (3 * k + 2) < n - (2 * k + 1))cout << -1 << endl;
				else {
					for (i = 1; i <= 2 * k + 1; ++i) {
						if (i & 1)cout << 2 << " ";
						else cout << 1 << " ";
					}
					if (S == 3 * (k + 1) + 2 && 2 * k + 3 == n) {
						cout << 2 << " " << 1 << endl;
					}
					else {
						for (; i <= n; ++i) {
							if (i != n)cout << 1 << " ";
							else cout << S - k - n << endl;
						}
					}
				}
			}
		}
	}
	return 0;
}

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值