Codeforces Round #293 (Div. 2) E. 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.
Examples
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

题意:给你n 和k ,给你n 个数a1,a2,a3,,,,an(其中包括一些未知数)
使得(a1+a2+,,,+ak),(a2+a3+,,,+ak+1),(a3+a4+,,,ak+2)这n-k+1个数是严格递增的!
最后输出这n 个数,使得这n个数的绝对值之和最小。

解析:有k条链,我们依此枚举每一条链,我们发现如果一段连续的问号两端都有数字的话那么这些问号就可以被确定了下面分四种情况
(1)当前后数字的差值小于问号的个数时,直接输出不满足条件
(2)前面是>=-1的数 ,这样直接从零开始赋值就好
(3)后面是<=-1的数,可以逆序从后面的数-1开始大到小赋值,本文是正序,道理一样
(4)不满足前面的条件(前负后正),就要考虑一下:最高的话仍需满足后面的数减去问号的个数,如果满足这个条件时,我们就可以尽可能向零靠拢(也就是后面的数减去问号的个数仍然很多),所以取这两个值(第二个值请看代码)的最小值就好,但还要控制最低为前一个数加一的下界
第四点还是比较难想的0.0

用到的读入函数也是要注意一下
代码:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rep1(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=1e6+100;
int arr[N];
int main()
{
    ios::sync_with_stdio(false);
    int n,k;
    cin>>n>>k;
    rep(i,0,n)
    {
        char str[30];
        cin>>str;
        if(str[0]=='?')
            arr[i]=inf;
        else arr[i]=atoi(str);
        /*int sum=0;
        string str;
        cin>>str;
        int len=str.size();
        if(str[0]=='?')
            arr[i]=inf ;
        else if(str[0]=='-')
        {
            for(int i=1;i<len;i++)
            {
                int j=1;
                for(int k=0;k<len-1-i;k++)
                    j*=10;
                sum-=(int )(str[i]-'0')*j;
            }
        }
        else
        {
            for(int i=0;i<len;i++)
            {
                int j=1;
                for(int k=0;k<len-1-i;k++)
                    j*=10;
                sum+=(int )(str[i]-'0')*j;
            }
        }
        arr[i]=sum;*/
    }
    rep(i,n,n+k) arr[i]=inf+1;//补齐后面的,方便处理最后一个?
    rep(i,0,k)
    {
        int pre=-inf;
        int sum=0;
        for(int j=i;j<n+k;j+=k)
        {
            //cout<<j<<endl;
            if(arr[j]==inf)sum++;
            else
            {
                if(arr[j]-pre<=sum)//没有足够的数字填?
                {
                    cout<<"Incorrect sequence"<<endl;
                    return 0;
                }
                int num;
                if(pre>=-1) num=pre+1;//直接从小到大填就好
                else if(arr[j]<=1) num=arr[j]-sum;//为了满足绝对值最小,我们要从大往小填
                else num=max(pre+1,min(arr[j]-sum,-sum/2));上文已经介绍,这里不再注释
                //cout<<num<<endl;
                rep1(h,sum,1)
                    arr[j-h*k]=num++;
                sum=0;
                pre=arr[j];
            }
        }

    }
    rep(i,0,n)
        cout<<arr[i]<<' ';
    cout<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值