牛客周赛22-C-小红的数组构造

好啊,很好啊.

关键点:

1.注意上下界的判断,x要 >= 1开头的等差d = 1的序列

                                       <= k - n + 1开头的等差d = 1的序列

这里有个细节  原来应该是 x <= n * (2 * k - n + 1) / 2

但k,x的范围是1e14,这样就爆long long,所以得是x  * 2 / n <= 2 * k - n + 1

2.直接看代码的实现就行,很直观

// Problem: 小红的数组构造
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/70996/C
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
// Date: 2024-03-21 21:29:19
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define endl '\n'
#define int int64_t
#define ld long double
using namespace std;
int a[100005];
void solve() {
	int n, k, x; cin >> n >> k >> x;
	int mn = n * (n + 1) / 2;
	if (k >= n && mn <= x && x * 2 / n <= 2 * k - n + 1) {
		if (x - n * (n - 1) / 2 <= k) {
			for (int i = 1; i <= n - 1; ++i)
				cout << i << " ";
			cout << x - n * (n - 1) / 2 << endl;
		}
		else {
			int rem = x - n * (n - 1) / 2 - k;
			//cout << "rem = " <<rem << endl;
			int t = rem / (n - 1) + 1;
			int q = rem % (n - 1), p = n - 1;
			while (q--) {
				a[p]++;
				p--;
			}
			for (int i = 1; i < n; ++i) {
				cout << t + a[i] << " ";
				t++;
			}
			cout << k << endl;
		}
	}
	else cout << -1 << endl;
}
signed main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int t = 1;
	//cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值