第一次CSP模拟

#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#define inf 2147483647
#define N 1000010
#define p(a) putchar(a)
#define For(i,a,b) for(int i=a;i<=b;++i)

using namespace std;
int n,ans,l,t;
int a[N];
char c[N];
void in(int &x){
    int y=1;char c=getchar();x=0;
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    x*=y;
}
void o(int x){
    if(x<0){p('-');x=-x;}
    if(x>9)o(x/10);
    p(x%10+'0');
}

signed main(){
    cin>>(c+1);
    n=strlen(c+1);
    For(i,1,100) a[i]=i;
    l=1;
    For(i,1,n){
        t=c[i]-'a'+1;
        ans+=min(min(abs(a[l]-a[t]),a[l+26]-a[t]),min(abs(a[l]-a[t]),a[t+26]-a[l]));
        l=t;
    }
    o(ans);
    return 0;
}
View Code
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#define inf 2147483647
#define N 1000010
#define p(a) putchar(a)
#define For(i,a,b) for(int i=a;i<=b;++i)

using namespace std;
int n,x,ans;
int a[N];
void in(int &x){
    int y=1;char c=getchar();x=0;
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    x*=y;
}
void o(int x){
    if(x<0){p('-');x=-x;}
    if(x>9)o(x/10);
    p(x%10+'0');
}

signed main(){
    in(n);
    For(i,1,n) in(a[i]);
    For(i,1,n-1){
        if(a[i]<0){
            puts("NO");
            return 0;
        }
        if(a[i]&1) a[i+1]--;
    }
    if(a[n]<0||(a[n]&1)) puts("NO");
    else puts("YES");
    return 0;
}
View Code
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#define inf 2147483647
#define N 1000010
#define p(a) putchar(a)
#define For(i,a,b) for(int i=a;i<=b;++i)

using namespace std;
int n,X,Y,ans;
int a[N];
bool vis[1010][1010];
struct node{
    int x;
    int y;
}d[100];

struct pos{
    int x;
    int y;
    int f;
    int id;
};
queue<pos>q;

void in(int &x){
    int y=1;char c=getchar();x=0;
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    x*=y;
}
void o(int x){
    if(x<0){p('-');x=-x;}
    if(x>9)o(x/10);
    p(x%10+'0');
}

void bfs(){
    pos t;
    t.x=30;t.y=30;t.f=0;t.id=1;
    q.push(t);
    while(!q.empty()){
        t=q.front();q.pop();
        if(t.id>n) break;
        X=t.x;Y=t.y;
        For(i,1,a[t.id]){
            X+=d[t.f].x,Y+=d[t.f].y;
            if(!vis[X][Y]){
                ans++;
                vis[X][Y]=1;
            }
        }
        q.push((pos){X,Y,(t.f+1)%8,t.id+1});
        q.push((pos){X,Y,(t.f+7)%8,t.id+1});
    }
}

signed main(){
    in(n);
    For(i,1,n) in(a[i]);
    d[0].x=0;d[0].y=1;
    d[1].x=1;d[1].y=1;
    d[2].x=1;d[2].y=0;
    d[3].x=1;d[3].y=-1;
    d[4].x=0;d[4].y=-1;
    d[5].x=-1;d[5].y=-1;
    d[6].x=-1;d[6].y=0;
    d[7].x=-1;d[7].y=1;
    bfs();
    // For(i,1,50){
    //     For(j,1,50){
    //         if(vis[i][j]) p('*');
    //         else p('.');
    //         p(' ');
    //     }   
    //     p('\n');
    // }
    o(ans);
    return 0;
}
View Code

 

posted @ 2020-03-11 15:40  WeiAR  阅读(154)  评论(0编辑  收藏  举报