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.
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.
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.
3 2 ? 1 2
0 1 2
5 1 -10 -9 ? -7 -6
-10 -9 -8 -7 -6
5 3 4 6 7 2 9
Incorrect sequence
题意:让你构造绝对值和最小的序列,使得按照k变化后的序列是一个递增序列。
思路:a_1<a_k+1<a_2*k+1<a_3*k+1,那么每次如果有?的话,就找到连续的这一段,然后找到它左边限定的最小值和右边限定的最大值,然后尽量让中间的数靠近0,其他的没一个变化1。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
int T,t,n,m,k;
char s[110];
bool flag=true;
ll num[100010];
bool vis[100010];
void Solve(int a,int b,ll l,ll r)
{
int i,j;
ll ret,k2;
ret=(b-a)/k+1;
if(r-l-1<ret)
{
flag=false;
return;
}
k2=max(-ret/2,l+1);
k2=min(k2,r-ret);
for(;a<=b;a+=k)
{
num[a]=k2++;
vis[a]=1;
}
}
void solve()
{
int i,j,a,b,pos;
ll l,r,L,R;
scanf("%d%d",&n,&k);
for(i=1;i<=n;i++)
{
scanf("%s",s);
if(s[0]=='?')
continue;
else
{
sscanf(s,"%I64d",&num[i]);
vis[i]=1;
}
}
L=-2000000000;
R=2000000000;
//printf("ok\n");
for(i=1;i<=k;i++)
{
for(pos=i;pos<=n;pos+=k)
{
if(vis[pos])
{
if(pos>k && num[pos]<=num[pos-k])
{
flag=false;
return;
}
}
else
{
a=pos;b=pos;
if(pos>k)
l=num[pos-k];
else
l=L;
while(true)
{
pos+=k;
if(pos>n)
{
b=pos-k;
r=R;
Solve(a,b,l,r);
break;
}
if(vis[pos])
{
b=pos-k;
r=num[pos];
Solve(a,b,l,r);
break;
}
}
}
//printf("%d %d %d\n",i,pos,flag);
if(!flag)
return;
}
}
}
int main()
{
int i,j;
solve();
if(!flag)
printf("Incorrect sequence\n");
else
{
for(i=1;i<=n;i++)
printf("%d ",num[i]);
printf("\n");
}
}