程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> CF 518E(Arthur and Questions-貪心)

CF 518E(Arthur and Questions-貪心)

編輯:C++入門知識

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[2]

...

因此貪心填最小的

注意考場上沒注意到ai可以填>=1e9的含恨沒過PT


#include
#include
#include
#include
#include
#include
#include
#include
#include
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=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[j])
				{
					cout<<"Incorrect sequence"<=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"<

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved