CF 518E(Arthur and Questions-贪心)

E. Arthur and Questions
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

After bracket sequences Arthur took up number theory. He has got a new favorite sequence of length n (a1, a2, ..., an), consisting of integers and integer k, not exceeding n.

This sequence had the following property: if you write out the sums of all its segments consisting of k consecutive elements (a1  +  a2 ...  +  ak,  a2  +  a3  +  ...  +  ak + 1,  ...,  an - k + 1  +  an - k + 2  +  ...  +  an), then those numbers will form strictly increasing sequence.

For example, for the following sample: n = 5,  k = 3,  a = (1,  2,  4,  5,  6) the sequence of numbers will look as follows: (1  +  2  +  4,  2  +  4  +  5,  4  +  5  +  6) = (7,  11,  15), that means that sequence a meets the described property.

Obviously the sequence of sums will have n - k + 1 elements.

Somebody (we won't say who) replaced some numbers in Arthur's sequence by question marks (if this number is replaced, it is replaced by exactly one question mark). We need to restore the sequence so that it meets the required property and also minimize the sum |ai|, where |ai| is the absolute value of ai.

Input

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105), showing how many numbers are in Arthur's sequence and the lengths of segments respectively.

The next line contains n space-separated elements ai (1 ≤ i ≤ n).

If ai  =  ?, then the i-th element of Arthur's sequence was replaced by a question mark.

Otherwise, ai ( - 109 ≤ ai ≤ 109) is the i-th element of Arthur's sequence.

Output

If Arthur is wrong at some point and there is no sequence that could fit the given information, print a single string "Incorrect sequence" (without the quotes).

Otherwise, print n integers — Arthur's favorite sequence. If there are multiple such sequences, print the sequence with the minimum sum|ai|, where |ai| is the absolute value of ai. If there are still several such sequences, you are allowed to print any of them. Print the elements of the sequence without leading zeroes.

Sample test(s)
input
3 2
? 1 2
output
0 1 2 
input
5 1
-10 -9 ? -7 -6
output
-10 -9 -8 -7 -6 
input
5 3
4 6 7 2 9
output
Incorrect sequence

显然原题公式可以化为

a[1]<a[k+1]<a[2k+1]...

a[2]<a[k+2]<a[2k+2]...

...

因此贪心填最小的

注意考场上没注意到ai可以填>=1e9的含恨没过PT


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (1000000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int n,k;
char s[MAXN];
ll a[MAXN]={0};
bool b[MAXN]={0};
void work(int t,int l,int r)
{
	for(int i2=l;i2<=r;i2+=k) 
	{
		a[i2]=t;
		t++;
	}
	
}
int main()
{
//	freopen("Questions.in","r",stdin);
//	freopen(".out","w",stdout);
	cin>>n>>k;
	For(i,n)
	{
		scanf("%s",s);
		if (s[0]=='?') b[i]=1;
		else 
		{
			int len=strlen(s);
			bool flag=0;
			if (s[0]=='-') 
			{
				For(j,len-1) a[i]=a[i]*10+s[j]-'0';
				a[i]=-a[i];
			}
			else
			{
				Rep(j,len) a[i]=a[i]*10+s[j]-'0';
			}
		}
	}
	
//	For(i,n) cout<<a[i]<<' '<<b[i]<<endl; 
	
	For(i,k)
	{
		ll p=-2000000000-1; 
		for(int j=i;j<=n;j+=k)
		{
			if (!b[j])
			{
				if (p>=a[j])
				{
					cout<<"Incorrect sequence"<<endl;
					return 0;
				}
				p=a[j];
			}
			else
			{
				int l=j,r=j,tot=1;
				ll p2=2000000000+1;
				while(j+k<=n) 
				{
					j+=k;
					if (!b[j]) {p2=a[j];break;}
					else tot++,r=j;
				}
				if (tot<=p2-p-1)
				{
					if (p2<=0)
					{
						int t=p2-1;
						for(int i2=r;i2>=l;i2-=k) 
						{
							a[i2]=t;
							t--;
						}
					}
					else if (p>=0)
					{
						work(p+1,l,r);
					} 
					else
					{
						int suit1=-(tot-1)/2-(tot-1)%2,suit2=-(tot-1)/2;
						int pos1=p+1,pos2=p2-tot;
						if (pos1<=suit1&&suit1<=pos2)
						{
							int t=suit1;
							for(int i2=l;i2<=r;i2+=k) 
							{
								a[i2]=t;
								t++;
							}
						}
						else if (pos1<=suit2&&suit2<=pos2)
						{
							int t=suit2;
							for(int i2=l;i2<=r;i2+=k) 
							{
								a[i2]=t;
								t++;
							}
						}
						else if (suit2<=pos1) work(pos1,l,r);
						else if (suit1>=pos2) work(pos2,l,r);					
					}
				}
				else
				{
					cout<<"Incorrect sequence"<<endl;
					return 0;
				}			
				p=p2;	
			}
		}
		
	}
	
	For(i,n-1) printf("%d ",a[i]);
	printf("%d\n",a[n]);

	
	
	
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值