1. 程式人生 > >Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 1)

Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 1)

一個 rain getch long long sta ++ algo freopen --

A - Toy Train

很顯然,一個站有多少個糖,那麽就要從這個點運多少次。設第i個點有\(a_i\)個糖,那麽就要轉\(a_i-1\)圈,然後再走一段。很顯然最後一段越小越好。

然後枚舉起點後,每個點的答案就是起點到他的距離加上再走的距離。然後取個max就好了。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cctype>
#define qmin(x,y) (x=min(x,y))
#define qmax(x,y) (x=max(x,y))
#define vi vector<int>
#define vit vector<int>::iterator
#define pir pair<int,int>
#define fr first
#define sc second
#define mp(x,y) make_pair(x,y)
using namespace std;
 
inline char gc() {
//  static char buf[100000],*p1,*p2;
//  return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
    return getchar();
}
 
template<class T>
int read(T &ans) {
    ans=0;char ch=gc();T f=1;
    while(!isdigit(ch)) {
        if(ch==EOF) return -1;
        if(ch=='-') f=-1;
        ch=gc();
    }
    while(isdigit(ch))
        ans=ans*10+ch-'0',ch=gc();
    ans*=f;return 1;
}
 
template<class T1,class T2>
int read(T1 &a,T2 &b) {
    return read(a)!=EOF&&read(b)!=EOF?2:EOF;
}
 
template<class T1,class T2,class T3>
int read(T1 &a,T2 &b,T3 &c) {
    return read(a,b)!=EOF&&read(c)!=EOF?3:EOF;
}
 
typedef long long ll;
const int Maxn=1100000;
const ll inf=0x3f3f3f3f3f3f3f3fll;
 
int n,m,a[Maxn],b[Maxn],x,y;
 
signed main() {
//  freopen("test.in","r",stdin);
    read(n,m);
    memset(b,0x3f,sizeof(b));
    for(int i=1;i<=m;i++) {
        read(x,y);
        a[x]++;
        if(y>x) qmin(b[x],y-x);
        else qmin(b[x],y+n-x);
    }
    for(int i=1;i<=n;i++) if(!a[i]) b[i]=0;
    for(int i=1;i<=n;i++) {
        int ans=0;
        for(int j=1;j<=n;j++) {
            int temp;
            if(j>=i) temp=j-i;
            else temp=j+n-i;
            temp+=(a[j]-1)*n+b[j];
            qmax(ans,temp);
        }
        printf("%d ",ans);
    }
    return 0;
}

B - Wrong Answer

厚顏無恥地求一波贊

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cctype>
#define qmin(x,y) (x=min(x,y))
#define qmax(x,y) (x=max(x,y))
#define vi vector<int>
#define vit vector<int>::iterator
#define pir pair<int,int>
#define fr first
#define sc second
#define mp(x,y) make_pair(x,y)
using namespace std;
 
inline char gc() {
//  static char buf[100000],*p1,*p2;
//  return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
    return getchar();
}
 
template<class T>
int read(T &ans) {
    ans=0;char ch=gc();T f=1;
    while(!isdigit(ch)) {
        if(ch==EOF) return -1;
        if(ch=='-') f=-1;
        ch=gc();
    }
    while(isdigit(ch))
        ans=ans*10+ch-'0',ch=gc();
    ans*=f;return 1;
}
 
template<class T1,class T2>
int read(T1 &a,T2 &b) {
    return read(a)!=EOF&&read(b)!=EOF?2:EOF;
}
 
template<class T1,class T2,class T3>
int read(T1 &a,T2 &b,T3 &c) {
    return read(a,b)!=EOF&&read(c)!=EOF?3:EOF;
}
 
typedef long long ll;
const int Maxn=1100000;
const ll inf=0x3f3f3f3f3f3f3f3fll;
 
int k;
 
signed main() {
//  freopen("test.in","r",stdin);
    read(k);
    int temp=1999-k%1999;
    puts("2000");
    for(int i=1;i<=1998;i++) printf("0 ");
    printf("%d ",-temp);
    printf("%d\n",(k+temp)/1999+temp);
    return 0;
}

C - Morse Code

首先,直接n方DP求每一段能代表的字符串的個數很簡單,然後因為相同的子串只能統計一次答案,那麽就用一顆trie樹來存就好了。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cctype>
#define qmin(x,y) (x=min(x,y))
#define qmax(x,y) (x=max(x,y))
#define vi vector<int>
#define vit vector<int>::iterator
#define pir pair<int,int>
#define fr first
#define sc second
#define mp(x,y) make_pair(x,y)
using namespace std;
 
inline char gc() {
//  static char buf[100000],*p1,*p2;
//  return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
    return getchar();
}
 
template<class T>
int read(T &ans) {
    ans=0;char ch=gc();T f=1;
    while(!isdigit(ch)) {
        if(ch==EOF) return -1;
        if(ch=='-') f=-1;
        ch=gc();
    }
    while(isdigit(ch))
        ans=ans*10+ch-'0',ch=gc();
    ans*=f;return 1;
}
 
template<class T1,class T2>
int read(T1 &a,T2 &b) {
    return read(a)!=EOF&&read(b)!=EOF?2:EOF;
}
 
template<class T1,class T2,class T3>
int read(T1 &a,T2 &b,T3 &c) {
    return read(a,b)!=EOF&&read(c)!=EOF?3:EOF;
}
 
typedef long long ll;
const int Maxn=11000000;
const int inf=0x3f3f3f3f;
const int mod=1000000007;

int n,f[5100],a[Maxn],ans,ch[Maxn][2],cnt=1;
 
signed main() {
//  freopen("test.in","r",stdin);
    read(n);
    f[0]=1;
    for(int i=1;i<=n;i++)
        read(a[i]);
    for(int i=1;i<=n;i++) {
        memset(f,0,sizeof(f));
        f[i+1]=1;
        int now=1;
        for(int j=i;j>=1;j--) {
            for(int k=1;k<=3;k++) f[j]=(f[j]+f[j+k])%mod;
            if(j<=i-3) {
                if(!a[j]) {
                    if(a[j+1]) {
                        if(a[j+2]||!a[j+3]) f[j]=(f[j]+f[j+4])%mod;
                    }
                    else {
                        if(!a[j+2]||!a[j+3]) f[j]=(f[j]+f[j+4])%mod;
                    }
                }
                else
                if(a[j+3]) {
                    if(!a[j+1]||!a[j+2]) f[j]=(f[j]+f[j+4])%mod;
                }
                else if(!a[j+1]||!a[j+2]) f[j]=(f[j]+f[j+4])%mod;
            }
            if(!ch[now][a[j]]) {
                ch[now][a[j]]=++cnt;
                ans=(ans+f[j])%mod;
            }
            now=ch[now][a[j]];
        }
        printf("%d\n",ans);
    }
    return 0;
}

Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 1)