本文主要是介绍牛客周赛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;
}
这篇关于牛客周赛22-C-小红的数组构造的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!