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
题目大意:
给你N个数,其中有?也有固定的数字,让你给这些个?设定值使得最终的序列所有数字的绝对值的和最小,并且满足下列要求:
每相邻K个数字的和,小于下一个相邻K个数字的和。
思路:
很明显:
a1+a2+a3+...+ak<a2+a3+a4+...+ak+1-------------------->a1<ak+1
a2+a3+a4+...+ak+1<a3+a4+a5+...+ak+2-------------------->a2<ak+2
那么依次类推就有:
a1<ak+1<a2k+1<a2k+1...................................
a2<ak+2<a2k+2<a3k+2...................................
a3<ak+3<a2k+3<a3k+3...................................
那么我们对于每一段?进行贪心即可。
大概分成三种情况:
①L<0&&R<0:-100 ???? -40.明显结果设定为-100 -43 -42 -41 -40是贪心的。
②L>0&&R>0:4 ???? 100.明显结果设定为 4 5 6 7 8 100是贪心的。
③L<0&&R>0:-100 ??? 100明显结果设定为-100 -1 0 1 100是贪心的。
前两种较为好处理,第三种情况要注意左右区间极值问题。
贪心很好想,只是代码问题比较复杂比较麻烦。
谨慎一点慢慢来即可。
注意要对处理完的数组再判定一下是否合法。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define INF 100000000000000000
#define ll __int64
vector<ll >mp[100500];
ll a[100500];
ll vis[100500];
int use[100500];
ll n,k;
ll judge()
{
for(ll i=1; i<=n; i++)
{
if(i>=k+1)
{
if(vis[i]==1&&vis[i-k]==1)
{
if(a[i-k]>=a[i])return 0;
}
}
}
return 1;
}
ll judge2()
{
for(ll i=1; i<=n; i++)
{
if(i>=k+1)
{
if(a[i-k]>=a[i])return 0;
}
}
return 1;
}
int main()
{
while(~scanf("%I64d%I64d",&n,&k))
{
memset(a,0,sizeof(a));
char tmp[100];
for(ll i=1; i<=n; i++)
{
scanf("%s",tmp);
if(tmp[0]=='?')vis[i]=0;
else
{
vis[i]=1;
ll len=strlen(tmp);
ll fu=0;
for(ll j=0; j<len; j++)
{
if(j==0&&tmp[0]=='-')
{
fu=1;
continue;
}
a[i]=a[i]*10+tmp[j]-'0';
}
if(fu==1)a[i]=-a[i];
}
}
if(judge()==0)printf("Incorrect sequence\n");
else
{
for(int i=0;i<k;i++)
{
for(int j=i+k;j<=n;j+=k)
{
if(vis[j]==0)
{
a[j]=a[j-k]+1;
}
}
}
for(int i=1; i<=n; i++)
{
if(use[i])continue;
if(vis[i]==0)
{
int last=i;
int tot=1;
for(int j=i+k; j<=n&&vis[j]==0; j+=k)
{
last=j;
tot++;
use[j]=1;
}
ll L=i<=k?-INF:a[i-k];
ll R=last+k>n?INF:a[last+k];
if(L>=0)continue;
else if(R<=0)
{
for(int j=last; j>=1&&vis[j]==0; j-=k)
{
R--;
a[j]=R;
}
}
else
{
L++;
R--;
ll num=-(tot/2);
if(num<L)num=L;
if(num+tot-1>R)
{
num=R-tot+1;
}
for(int j=i; j<=n&&vis[j]==0; j+=k)
{
a[j]=num;
num++;
}
}
}
}
if(judge2()==0)printf("Incorrect sequence\n");
else
{
for(int i=1; i<=n; i++)
{
printf("%I64d ",a[i]);
}
printf("\n");
}
}
}
}