Wrong Answer(构造法,智商被虐)- CodeForces div2 541

版权声明:未经博主同意,不可转载 https://blog.csdn.net/pythonbanana/article/details/87950213

题目传送门
构造法真奇妙,自己想了很久,有点眉目后,结果还是要处理某些细节,麻烦。说明没有构造好。看了题解后,感觉智商被虐。
像这种构造题,一般代码都比较简单,要处理的细节和分类讨论都比较少,否则就是你的构造方法有点问题。构造法,考验一个人的创造力和洞察力。
官方题解:
Suppose a0=−1 and ai≥1 for each 1≤i<n. Let S=∑i=0n−1ai.

Assume also that n≥2.

It is easy to see that Alice’s algorithm produces (n−1)(S+1) as the answer. Meanwhile, there are two possible correct answers: either nS or (n−1)(S+1), whichever is greater.

Assume further that S≥1. The correct answer for this array is then nS. The difference between these two results is nS−(n−1)(S+1)=S−n+1.

Now, we can easily create array a greedily so that S−n+1=k.

The time complexity is O(n).

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<cstring>
#include<string>
#include<cmath>

using namespace std;

typedef long long LL;

#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define mem(a,b) memset(a,b,sizeof(a))
#define per(i,a,b) for(int i = a;i <= b;++i)
#define rep(i,a,b) for(int i = a;i >= b;--i)
const int maxn = 2e3;
int k = 0;
int a[maxn+10];
int main(){
	while(~scanf("%d",&k)){ 
		a[1] = -1;
		int sum = -1;
		per(i,2,maxn-1){
			a[i] = (k + maxn - 1 + 1)/(maxn-1);
			sum += a[i];
		}
		a[maxn] = k + maxn -1 - sum;
		printf("%d\n",maxn);
		per(i,1,maxn){
			printf("%d%c",a[i],i == maxn ? '\n' : ' ');
		}
	}
	
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/pythonbanana/article/details/87950213