Codeforces Round #293 Div2 E(Arthur and Questions)

Problem

Limits

TimeLimit(ms):2000

MemoryLimit(MB):256

k,n∈[1,105],k<=n

ai∈[?109,109]

Look up Original Problem From here

Solution

要满足数列bn单调递增。b1<b2,则a1+a2+…+ak<a2+…+ak+ak+1,即a1<ak+1,依此类推,a2<ak+2,a3<ak+3,…,ai<ak+i,…,这样子我们就可以把an划分成若干条链,每一条链需满足ai<ai+k<ai+2k<…这个条件。

那么问题就转变成:给若干条链,需要将每条链里的未知数赋值,且需满足两个条件,一,每条链的数严格单调递增,二,每条链的数绝对值之和最小。。然后,贪心模拟搞就好了(分三种情况…)

Complexity

TimeComplexity:O(n)

MemoryComplexity:O(n)

My Code

//Hello. I‘m Peter.
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<cctype>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef unsigned int uin;
#define peter cout<<"i am peter"<<endl
#define input freopen("data.txt","r",stdin)
#define randin srand((unsigned int)time(NULL))
#define INT (0x3f3f3f3f)*2
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define gsize(a) (int)a.size()
#define len(a) (int)strlen(a)
#define slen(s) (int)s.length()
#define pb(a) push_back(a)
#define clr(a) memset(a,0,sizeof(a))
#define clr_minus1(a) memset(a,-1,sizeof(a))
#define clr_INT(a) memset(a,INT,sizeof(a))
#define clr_true(a) memset(a,true,sizeof(a))
#define clr_false(a) memset(a,false,sizeof(a))
#define clr_queue(q) while(!q.empty()) q.pop()
#define clr_stack(s) while(!s.empty()) s.pop()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define dep(i, a, b) for (int i = a; i > b; i--)
#define repin(i, a, b) for (int i = a; i <= b; i++)
#define depin(i, a, b) for (int i = a; i >= b; i--)
#define pi 3.1415926535898
#define eps 1e-9
#define MOD 1000000007
#define MAXN
#define N 100100
#define M
int n,k;
char s[100];
int lens;
void printno(){
    printf("Incorrect sequence\n");
    exit(0);
}
ll stringtoint(char *s,int lens){
    ll res=0,ten=1;
    depin(i,lens-1,0){
        if(!isdigit(s[i])) break;
        res+=(s[i]-‘0‘)*ten;
        ten*=10LL;
    }
    if(s[0]==‘-‘) res=-res;
    return res;
}
int num_b,p[N],pos[N];
ll a[N],b[N];
bool vis[N];
void make_it(ll *b,int num){
    int num_pos=0;
    pos[num_pos++]=0;
    b[0]=-INT;
    repin(i,1,num){
        if(b[i]!=INT) pos[num_pos++]=i;
    }
    b[num+1]=INT;
    pos[num_pos++]=num+1;
    rep(i,1,num_pos){
        if(b[pos[i]]-b[pos[i-1]]<pos[i]-pos[i-1]) printno();
        if(pos[i-1]+1==pos[i]) continue;
        if(b[pos[i]]<=0){
            dep(j,pos[i]-1,pos[i-1]){
                b[j]=b[j+1]-1;
            }
        }
        else if(b[pos[i-1]]>=0){
            rep(j,pos[i-1]+1,pos[i]){
                b[j]=b[j-1]+1;
            }
        }
        else{
            int mid=(pos[i-1]+pos[i])>>1;
            b[mid]=0;
            rep(j,mid+1,pos[i]){
                b[j]=b[j-1]+1;
            }
            dep(j,mid-1,pos[i-1]){
                b[j]=b[j+1]-1;
            }
            ll d=0;
            if(b[pos[i-1]+1]<=b[pos[i-1]]){
                d=b[pos[i-1]]-b[pos[i-1]+1]+1;
            }
            else if(b[pos[i]-1]>=b[pos[i]]){
                d=b[pos[i]]-b[pos[i]-1]-1;
            }
            rep(j,pos[i-1]+1,pos[i]){
                b[j]+=d;
            }
        }
    }
}
int main(){
    scanf("%d %d",&n,&k);
    repin(i,1,n){
        scanf("%s",s);
        lens=len(s);
        if(s[0]==‘?‘) a[i]=INT;
        else a[i]=stringtoint(s,lens);
    }
    //构造
    repin(i,1,n){
        if(vis[i]) continue;
        num_b=0;
        for(int j=i;j<=n;j+=k){
            vis[j]=true;
            b[++num_b]=a[j];
            p[num_b]=j;
        }
        make_it(b,num_b);
        repin(j,1,num_b){
            a[p[j]]=b[j];
        }
    }
    //判断..
    repin(i,1,n){
        if(i-k>=1 && a[i-k]>=a[i]){
            printno();
        }
        if(i+k<=n && a[i]>=a[i+k]){
            printno();
        }
    }
    repin(i,1,n){
        if(i!=1) printf(" ");
        printf("%lld",a[i]);
    }
    printf("\n");
}
时间: 2024-08-11 23:11:49

Codeforces Round #293 Div2 E(Arthur and Questions)的相关文章

Codeforces Round #293 Div2 F(Pasha and Pipe)

Problem Limits TimeLimit(ms):4000 MemoryLimit(MB):512 n,m∈[2,2000] Look up Original Problem From here Solution 一共就14种情况.有些情况可以合并一起算,算出来就好了. Complexity TimeComplexity:O(n×m) MemoryComplexity:O(n×m) My Code //Hello. I'm Peter. #include<cstdio> #includ

Codeforces Round #287 Div2 D(The Maths Lecture)

Problem 长度为N的数X(十进制),如果X的某一段后缀Y(十进制)可被k整除,则可被统计.问有多少这样的X?(mod m)(不可含前导0) Limits TimeLimit(ms):1000 MemoryLimit(MB):256 N:∈[1,1000] k∈[1,100] m∈[1,109] Look up Original Problem From here Solution 数位dp.设dp[i][j]表示数长度为 i 且数mod k 为 j时, 有多少个.dp[i][0]不要往前转

Codeforces Round #344 (Div. 2)(按位或运算)

Blake is a CEO of a large company called "Blake Technologies". He loves his company very much and he thinks that his company should be the best. That is why every candidate needs to pass through the interview that consists of the following probl

Codeforces Round #294 Div2 D(A and B and Interesting Substrings)

Problem 给一个长度为N的字符串S,字符集是[a,z],每个字符都有一个权值Vi,求有多少个子串subS满足以下条件: 1. |subS|>=2 2. subS[0]=subS[|subS|?1] 3. ∑|subS|?2i=1Vi=0 Limits TimeLimit(ms):2000 MemoryLimit(MB):256 N∈[1,105] Vi∈[?105,105] Look up Original Problem From here Solution 如果没有条件2,用前缀和思想

Codeforces Round #294 Div2 E(A and B and Lecture Rooms)

Problem 给一棵树,含有N个节点,N?1条边.进行M次查询,每次给定两个节点x,y,问树上有多少个节点到x,y的距离相同. Limits TimeLimit(ms):2000 MemoryLimit(MB):256 N,M∈[1,105] x,y∈[1,N] Look up Original Problem From here Solution 求出a,b两个节点的lca,再找到lca?>a或lca?>b上的某个节点v使得dis(v,a)=dis(v,b).分v=lca,v∈lca?&g

Codeforces Round #268 (Div. 2) (被屠记)

c被fst了................ 然后掉到600+.... 然后...估计得绿名了.. sad A.I Wanna Be the Guy 题意:让你判断1-n个数哪个数没有出现.. sb题...开个数组即可.. #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <algorith

Codeforces Round #246 (Div. 2)(第一次做cf)

早就想做CF,但是在校党,没办法,断网又断电,一天实在忍不住了,就跑着流量做了一次CF 看到A题,看完题就有思路了,然后一把A掉,接着做第二道,看了N久,题意不懂,泪奔啊,然后就没激情再做了, 我的第一次就这样没了...................... 链接:http://codeforces.com/contest/432/problem/A =========================================================================

Codeforces Round #426 (Div. 2) (A B C)

A. The Useless Toy Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays – caramel spinner! Wanting to join the craze, she has immediately bought the strang

codeforces Round 63-div2-D.Beautiful Array(线性动归)

原题地址 D. Beautiful Array You are given an array aa consisting of nn integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, a