Codeforces 518E Arthur and Questions【贪心+模拟】很考量代码能力的一个题

针对一组包含问号的序列,通过填充问号位置使序列满足特定递增条件的同时,确保序列中所有数值的绝对值之和最小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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个数字的和,小于下一个相邻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");
            }
        }
    }
}






当前提供的引用内容并未提及关于Codeforces比赛M1的具体时间安排[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"错误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值