[Updating] Tinkoff Challenge

来源:互联网 发布:腾讯数据分析 编辑:程序博客网 时间:2024/06/05 18:38

Problem A- Oleg and shares

判断Ai是否满足AiC(modK)其中C为常数

//Author: Lixiang#include<stdio.h>#include<algorithm>const int maxn=100001;struct A{    int a[maxn];    int Min,N,K;    void init(){        Min=1000000000;        scanf("%d%d",&N,&K);        for(int i=1;i<=N;i++){            scanf("%d",&a[i]);            Min=std::min(a[i],Min);        }    }    void work(){        long long ans=0;        int now=a[1]%K;        for(int i=2;i<=N;i++)            if(a[i]%K!=now){                puts("-1");                return;            }        for(int i=1;i<=N;i++)            ans+=(a[i]-Min)/K;        printf("%I64d\n",ans);    }}sol;int main(){    sol.init();    sol.work();    return 0;}

Problem B - Igor and his way to work

判断是否能在最多两次转弯后到达目的地
BFS就行了

//Author: Lixiang#include<stdio.h>#include<queue>using namespace std;const int dx[]={0,0,1,-1};const int dy[]={1,-1,0,0};const int maxn=1001;struct Node{    short x,y;    char s,last;    Node make(int a,int b,int c,int la){        x=a;y=b;s=c;last=la;        return *this;    }}tmp;struct B{    queue <Node> Q;    bool a[maxn][maxn],hash[maxn][maxn][5];    int N,M,sx,sy,ex,ey;    char s[maxn];    void init(){        scanf("%d%d",&N,&M);        for(int i=1;i<=N;i++){            scanf("%s",s+1);            for(int j=1;j<=M;j++){                if(s[j]=='*')a[i][j]=0;                else{                    a[i][j]=1;                    if(s[j]=='S')sx=i,sy=j;                    if(s[j]=='T')ex=i,ey=j;                }            }        }    }    bool check(int x,int y){        if(x>=1&&x<=N&&y>=1&&y<=M&&a[x][y]==1)return 1;        else return 0;    }    void work(){        int x,y,turns,nx,ny,la;        Q.push(tmp.make(sx,sy,0,-1));        //hash[sx][sy]=1;        while(!Q.empty()){            x=Q.front().x;y=Q.front().y;turns=Q.front().s;            la=Q.front().last;            if(la!=-1)hash[x][y][la]=0;            Q.pop();            for(int i=0;i<4;i++){                nx=x+dx[i];ny=y+dy[i];                if(!check(nx,ny))continue;                if(hash[nx][ny][i])continue;                if(nx==ex&&ny==ey){                    puts("YES");                    return ;                }                if(i==la){                    Q.push(tmp.make(nx,ny,turns,i));                    hash[nx][ny][i]=1;                }                else if(turns<=2){                    if(turns==2&&nx!=ex&&ny!=ey)continue;                    Q.push(tmp.make(nx,ny,turns+1,i));                    hash[nx][ny][i]=1;                }            }        }        puts("NO");    }}sol;int main(){    sol.init();    sol.work();    return 0;}

Problem C - Mice Problem

求出每个老鼠经过捕鼠器的时间段,找有没有重合的时间就行了

//Author: Lixiang#include<stdio.h>#include<algorithm>using namespace std;const int maxn=100001;const double eps=1e-11;double x1,y1,x2,y2,tmin,tmax;struct Mice{    double rx,ry,vx,vy;    void init(){        scanf("%lf%lf%lf%lf",&rx,&ry,&vx,&vy);    }    void update(double a,double b,double v){        if(v==0){            if(a<0&&b>0)return;            tmax=-1;        }        double p=a/v,q=b/v;        if(p>q)swap(p,q);        tmin=max(tmin,p);        tmax=min(tmax,q);    }    void Update(){        update(x1-rx,x2-rx,vx);        update(y1-ry,y2-ry,vy);    }};struct C{    Mice mice[maxn];    int N;    void init(){        tmin=0;tmax=1e100;        scanf("%d%lf%lf%lf%lf",&N,&x1,&y1,&x2,&y2);        for(int i=1;i<=N;i++){            mice[i].init();            mice[i].Update();        }    }    void work(){        if(tmin+eps>tmax)puts("-1");        else printf("%.20lf\n",tmin);    }}sol;int main(){    sol.init();    sol.work();    return 0;}
0 0