CodeForces-Singers' Tour

题目

题意简述

有n个人,他们要顺时针的在n座城市开巡演,每到一个新城市,就多唱ai分钟,给你最终每座城市总的歌唱时间,求ai

思路

首先 我们对这个样例分析

6
81 75 75 93 93 87

画一个图先

很明显的,每个人都要开完n场演出,那么他总的歌唱时间其实是n(n+1)/2ai

所以我们其实可以把总歌唱时间算出来,然后除以n*(n+1)/2这个系数

得到a1+a2+...+an,这是肯定可以算的,然后接下来就要明白 这种题肯定需要样例拿来找规律

于是我们写下来这个 这个是我写题草稿 很丑的

于是 我们其实可以发现

	// 6 1 ->1  
	// 6 5 ->6  
	// 5 4 ->5   
	// 4 3 ->4   
	// 3 2 ->3   
	// 2 1-> 2   

这是思路

坑点

这题一定要注意 我们理想的情况就是可以答案这些是肯定互相整除的,那计算机运算不是这样的 他会取整 所以我们对任何不能进行整除的情况直接输出NO即可

代码

#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int range = 2e5 + 10;
int n;
int ans[range];
int a[range];
void solve() {
	cin >> n;
	int sum = 0;
	for (int i = 1; i <= n; i++)cin >> a[i], sum += a[i];
	int cnt = (n + 1) * n / 2;
	int tot = sum / cnt;
	if (tot < n || sum % cnt != 0) {
		cout << "NO" << endl;
		return ;
	}
	int l = n;
	int r = 1;
	int num = n;
	int step = 0;
	while (num) {
		int cha = a[l] - a[r];
		ans[r] = (cha + tot) / n;
		if ((cha + tot) % n != 0) {
			cout << "NO" << endl;
			return ;
		}
		if (ans[r] <= 0) {
			cout << "NO" << endl;
			return ;
		}
		num--;
		r--;
		if (r == 0)r = n;
		l--;
	}
	cout << "YES" << endl;
	for (int i = 1; i <= n; i++) {
		cout << ans[i] << " ";
	}
	cout << endl;
}
signed main() {
	ios::sync_with_stdio();
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while (t--)
		solve();
	return 0;
}

最后

希望看来有收获!

posted @ 2025-04-16 19:46  LteShuai  阅读(12)  评论(0)    收藏  举报