码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #293 Div2 E(Arthur and Questions)

时间:2015-03-05 12:58:17      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:贪心   模拟   构造   乱搞   

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+2a3<ak+3ai<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");
}

Codeforces Round #293 Div2 E(Arthur and Questions)

标签:贪心   模拟   构造   乱搞   

原文地址:http://blog.csdn.net/uestc_peterpan/article/details/44080721

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!