2018 ECNU Campus Invitational Contest B Almost AP & 华东师范校赛 数螃蟹

版权声明:欢迎转载,请注明此博客地址。 https://blog.csdn.net/Ever_glow/article/details/82773396

Almost AP

time limit per test

2.0 s

memory limit per test

512 MB

input

standard input

output

standard output

An array, is called an "Almost Arithmetic Progression" (Almost AP) if you can modify at most 3 elements in the array such that it becomes an arithmetic progression.

Recall the definition of the arithmetic progression: For an array a1, a2, ..., an to be an AP, ai + 1 - ai = c for all 1 ≤ i < n, in which c is a constant (c can be either positive, or negative, or 0).

You are asked to turn an Almost AP into an exact AP by modifying at most 3 numbers.

Input

The first line contains an integer n. (3 ≤ n ≤ 105)

The second line contains n space-separated integers: a1, a2, ..., an. ( - 109 ≤ ai ≤ 109)

Output

Output an arithmetic progression consisting of n integers b1, b2, ..., bn. Array b differs from array a by at most three numbers. You should also make sure that |bi| ≤ 1018.

The input guarantees there is at least one solution. If there are multiple of them, you can output any one.

Examples

input

5
1 2 4 4 5

output

1 2 3 4 5

input

4
1 3 5 7

output

1 3 5 7

input

4
2 3 3 3

output

4 3 2 1

kblack 在做数螃蟹的实验。按照「螃蟹爷爷的秘诀」一书上介绍的:由于东方神秘力量的影响,螃蟹的个数是一个完美的等差数列,例如:−2,−1,0,1,2,…  。是的,kblack 的螃蟹比较神奇,有的时候螃蟹的个数可能会是负的。

kblack 每天都去池塘记录螃蟹的个数。但是 kblack 自己给自己放了最多三天的假(也有可能两天,有可能一天,有可能不放),放假的时候螃蟹个数的记录就是 kblack 口胡的,有的时候会假得有点过分。

现给出 kblack n  天的记录,记录中至多有三个数是错误的。请纠正错误并输出正确的实验记录。

思维题,写来写去wa,总是跟正确思路差了那么一点点。数列中出现的差最多次数的一定是等差数列的公差,然后就是枚举每一个数应不应该换的问题了,最后跟原数组比较,若是换了3次之内,就是符合答案的解,输出即可。

代码实现:

/*
Look at the star
Look at the shine for U
*/
#include<bits/stdc++.h>
#define ll long long
#define PII pair<int,int>
#define sl(x) scanf("%lld",&x)
using namespace std;
const int N = 1e6+5;
const int mod = 1e9+7;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1);
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
ll s[N],ans[N],t[N];
map <ll,ll> mmp;
int main()
{
	ll n,i,j,k;
	sl(n);
	for(i = 1;i <= n;i++) sl(s[i]),t[i] = s[i];
	if(n <= 4)
	{
		for(i = 1;i <= n;i++)
		printf("%lld ",s[1]);
		puts("");
		return 0;
	}
	ll dis = 0,maxx = -1e18;
	
	for(i = 1;i < n;i++) mmp[s[i+1]-s[i]]++;
	map <ll,ll> :: iterator it;
 	for(it = mmp.begin();it != mmp.end();it++)
 	{
 		if(maxx < it->second)
 		{
 			maxx = it->second;
 			dis = it->first;
		}
	}
//	cout<<dis<<"***"<<endl;
	ll l = 1;
	for(i = 2;i <= n;i++)
	{
		ll sum = 0;
		if(s[i]-s[i-1] == dis)
		{
		//	cout<<s[i]<<endl;
			for(j = i;j <= n;j++) s[j] = s[j-1]+dis;
			for(j = i-1;j;j--) s[j] = s[j+1]-dis;
			for(j = 1;j <= n;j++) if(s[j] != t[j]) sum++;
			if(sum > 3)
			{
				for(j = 1;j <= n;j++) s[j] = t[j];
			}
			else
			break;
		}
	}
	for(i = 1;i <= n;i++)
	{
		printf("%lld ",s[i]);
	}
	puts("");
}

猜你喜欢

转载自blog.csdn.net/Ever_glow/article/details/82773396