Codeforces Round #287 (Div. 2) 解題報告 A.B.C.D.E,

來源:互聯網
上載者:User

Codeforces Round #287 (Div. 2) 解題報告 A.B.C.D.E,

這次的CF挺水的,當時B題犯了一個很SB的錯誤,浪費了好多時間,所以D和E也沒來得及看。sad,。。

A - Amr and Music

水題,從小的開始選。

代碼如下:

#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace std;#define LL __int64#define pi acos(-1.0)const int mod=9901;const int INF=0x3f3f3f3f;const double eqs=1e-9;struct node {        int x, n;} fei[400];int cmp(node f1, node f2){        return f1.x<f2.x;}int main(){        int n, m, i, sum=0, j, pos;        scanf("%d%d",&n,&m);        for(i=0; i<n; i++) {                scanf("%d",&fei[i].x);                fei[i].n=i+1;        }        sort(fei,fei+n,cmp);        pos=n-1;        for(i=0; i<n; i++) {                if(sum+fei[i].x>m) {                        pos=i-1;                        break;                } else {                        sum+=fei[i].x;                }        }        printf("%d\n",pos+1);        for(j=0; j<=pos; j++) {                printf("%d ",fei[j].n);        }        return 0;}
B - Amr and Pins

只要距離小於2*r,那麼肯定能找到一個點可以正好將圓心移到目標點。所以只要看距離是2*r的多少倍就可以了。該死的精度。。取10^-9就掛了。。換成別的都過了。。無語。。

代碼如下:

#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace std;#define LL __int64#define pi acos(-1.0)const int mod=9901;const int INF=0x3f3f3f3f;const double eqs=1e-11;int main(){        LL r, x1, y1, x2, y2;        LL a;        double d, c;        while(scanf("%I64d%I64d%I64d%I64d%I64d",&r,&x1,&y1,&x2,&y2)!=EOF){                        r*=2;                d=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));                c=d/r;                a=(LL)c;                if(c==a){                        printf("%I64d\n",a);                }                else                        printf("%I64d\n",a+1);        }        return 0;}

C - Guess Your Way Out!

我的做法是先建樹,記錄到目標點的最短路徑,然後再從根節點出發開始走,如果下一步偏離了路徑,那麼就加上下一步的所有子樹節點的個數。(因為它肯定會遍曆完所有子樹然後返回)

代碼如下:

#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace std;#define LL __int64#define pi acos(-1.0)const int mod=9901;const int INF=0x3f3f3f3f;const double eqs=1e-9;#define lson l, mid, rt<<1#define rson mid+1, r, rt<<1|1LL ans, c, num[100], cnt, p[100], H;void update(LL n, LL l, LL r, LL rt){        num[cnt++]=rt;        if(l==r) return ;        LL mid=l+r>>1;        if(n<=mid) update(n,lson);        else update(n,rson);}void dfs(int f, int h, LL l, LL r, LL rt){        if(l==r) return ;        LL next;        if(f==-1) next=rt<<1;        else next=rt<<1|1;        if(next!=num[h+1]) {                ans+=p[H-h-1];                f=-f;        }        c>>=1;        LL mid=l+r>>1;        if(f==-1) {                dfs(-f,h+1,lson);        } else {                dfs(-f,h+1,rson);        }}void init(){        p[0]=1;        for(int i=1; i<=60; i++) {                p[i]=p[i-1]+((LL)1<<i);                //printf("%I64d\n",p[i]);        }}int main(){        LL n, i;        while(scanf("%I64d%I64d",&H,&n)!=EOF) {                ans=0;                c=(LL)1<<H;                cnt=0;                init();                update(n-1,0,c-1,1);                /*for(i=0;i<cnt;i++){                        printf("%I64d\n",num[i]);                }*/                dfs(-1,0,0,c-1,1);                printf("%I64d\n",ans+H);        }        return 0;}

D - The Maths Lecture

水題啊。。。普通的數位DP而已,用dp[i][j][k]記錄第i位(從低位開始數),模為j時,k記錄是否已經存在模為0的尾碼的狀態時的個數。因為要算尾碼,所以應該從低位向高位開始搜。

代碼如下:

#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace std;#define LL __int64#define pi acos(-1.0)int mod, k, n;const int INF=0x3f3f3f3f;const double eqs=1e-6;LL dp[1002][101][3], mo[1002];LL dfs(int cnt, int mods, int in, int zero){        if(cnt==-1) return in;        if(zero&&dp[cnt][mods][in]!=-1) return dp[cnt][mods][in];        int l=(cnt==0), i, m, z;        LL ans=0;        for(i=l;i<=9;i++){                m=(mods+i*mo[n-cnt])%k;                if(i||zero) z=1;                else z=0;                ans+=dfs(cnt-1,m,in||(m==0&&z),z);        }        ans%=mod;        if(zero)    {                        dp[cnt][mods][in]=ans;        }        return ans;}void init(){        int i;        mo[1]=1%k;        for(i=2;i<=1000;i++){                mo[i]=(mo[i-1]*10)%k;        }}int main(){        memset(dp,-1,sizeof(dp));        scanf("%d%d%d",&n,&k,&mod);        init();        printf("%I64d\n",dfs(n-1,0,0,0));        return 0;}

E - Breaking Good

還是水題。。求一次最短路,將距離標記成一個比較大的數,然後將花費標記成1,這樣就能找到了最短而且花費最短的路徑了。然後再根據狀態標記找到需要改動的路徑即可。

代碼如下:

#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace std;#define LL __int64#define pi acos(-1.0)int mod, k, n;const int INF=0x3f3f3f3f;const double eqs=1e-6;int head[110000], cnt, vis[110000], dd[110000], pre[110000], c[110000];struct node{        int u, v, w, next, f, s;}edge[210000];void add(int u, int v, int w, int f){        edge[cnt].v=v;        edge[cnt].w=w;        edge[cnt].f=f;        edge[cnt].next=head[u];        head[u]=cnt++;}void spfa(){        queue<int>q;        memset(vis,0,sizeof(vis));        q.push(1);        dd[1]=0;        while(!q.empty()){                int u=q.front();                q.pop();                vis[u]=0;                for(int i=head[u];i!=-1;i=edge[i].next){                        int v=edge[i].v;                        if(dd[v]>dd[u]+edge[i].w){                                dd[v]=dd[u]+edge[i].w;                                pre[v]=u;                                if(!vis[v]){                                        vis[v]=1;                                        q.push(v);                                }                        }                }        }}void init(){        memset(head,-1,sizeof(head));        cnt=0;        memset(dd,INF,sizeof(dd));        memset(pre,-1,sizeof(pre));}int main(){        int n, m, i, u, v, w, j, sum=0, cnt1=0;        scanf("%d%d",&n,&m);        init();        while(m--){                scanf("%d%d%d",&u,&v,&w);                add(u,v,10000+1-w, w);                add(v,u,10000+1-w, w);        }        spfa();        for(i=n;i!=-1;i=pre[i]){                c[cnt1++]=i;        }        for(i=1;i<=n;i++){                for(j=head[i];j!=-1;j=edge[j].next){                        edge[j].s=0;                }        }        for(i=cnt1-1;i>=1;i--){                u=c[i];                for(j=head[u];j!=-1;j=edge[j].next){                        int v=edge[j].v;                        if(v==c[i-1]){                                edge[j].s=1;                                edge[j^1].s=1;                                //printf("%d %d %d\n",u,v,edge[j].w);                        }                }        }        for(i=1;i<=n;i++){                for(j=head[i];j!=-1;j=edge[j].next){                        if(edge[j].s+edge[j].f==1){                                edge[j^1].f=1-edge[j^1].f;                                //printf("%d %d %d %d\n",i,edge[j].v,edge[j].s,edge[j].w);                                sum++;                        }                }        }        printf("%d\n",sum);        for(i=1;i<=n;i++){                for(j=head[i];j!=-1;j=edge[j].next){                        if(edge[j].s+edge[j].f==1){                                printf("%d %d %d\n",i,edge[j].v,edge[j].s);                        }                }        }        return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.