Codeforces Round #303 (Div. 2)(A,B,C,D),

來源:互聯網
上載者:User

Codeforces Round #303 (Div. 2)(A,B,C,D),

A:
題目地址:Toy Cars
題意:給一個n*n的矩陣,-1隻在對角線出現(因為自己不能撞自己),0代表沒有車在碰撞,1代表第i輛車(橫座標)被撞壞了,2代表第j輛車(縱座標)被撞壞了,3代表兩輛車都撞壞了。問哪幾輛車完好無損。
思路:遍曆i。對每一行來說,只要有1和3就代表這輛車跪了,然後只要找出每一行沒1和3的就好辣。

#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const double pi= acos(-1.0);const double esp=1e-6;const int maxn=110;int a[maxn][maxn];int b[maxn];int main(){    int n;    while(~scanf("%d",&n)){        for(int i=0;i<n;i++){            for(int j=0;j<n;j++){                scanf("%d",&a[i][j]);            }        }        int flag;        int num=0;        for(int i=0;i<n;i++){                flag=0;            for(int j=0;j<n;j++){                if(a[i][j]==1||a[i][j]==3){                    flag=1;                    break;                }           }           if(!flag) b[num++]=i;        }        printf("%d\n",num);        if(num==0)            continue;        for(int i=0;i<num-1;i++)            printf("%d ",b[i]+1);            printf("%d\n",b[num-1]+1);    }    return 0;}

B:
題目地址:Equidistant String
題意:求一個字串,使得它與S,T,相似性相差相等。
思路:就是找出S,T之間的相差個數,相差為奇數,輸出impossible。輸出為偶數的話不同的前半部分輸出S後半部分輸出T就好了。

#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const double pi= acos(-1.0);const double esp=1e-6;const int maxn=1e5+10;char s[maxn];char t[maxn];int main(){    scanf("%s %s",s,t);    int cnt=0;    int len=strlen(s);    for(int i=0;i<len;i++){        if(s[i]!=t[i])            cnt++;    }    if(cnt&1)        puts("impossible");    else{           cnt=cnt/2;        for(int i=0;i<len;i++){            if(s[i]!=t[i]){                if(cnt){                    printf("%c",s[i]);                    cnt--;                }                else                    printf("%c",t[i]);            }            else                printf("%c",s[i]);        }    }    puts("");    return 0;}

C:
題目地址: Woodcutters
題意:有n棵樹,給出每棵樹的位置和高度,然後把樹砍掉,樹可以向左倒也可以向右倒。輸出最多能砍幾棵樹。
思路:利用貪心的思想。第一棵樹的左邊和最後一棵樹的右邊沒樹,所以他們向兩邊倒,然後對於中間的樹來說,首先先向左邊倒,然後左邊距離如果不夠的話再向右邊倒,向右倒的時候注意更新一下距離。

#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const double pi= acos(-1.0);const double esp=1e-6;const int maxn=1e5+10;int x[maxn];int h[maxn];int main(){    int n;    scanf("%d",&n);    if(n==1){        puts("1");    }    else{    for(int i=1;i<=n;i++){        scanf("%d %d",&x[i],&h[i]);    }    int cnt=2;    for(int i=2;i<=n-1;i++){        if(x[i]-x[i-1]>h[i])            cnt++;        else if(x[i+1]-x[i]>h[i]&&(x[i]-x[i-1])<=h[i]){            x[i]=x[i]+h[i];            cnt++;        }    }    printf("%d\n",cnt);    }    return 0;}

D:
題目地址: Queue
題意:有n個人,每個人都有一個等待時間,如果對於當前的人來說總等待時間超過自己的等待時間,那麼這個人就會失望,問換一下順序,使失望的人最少,問最多有多少個人不失望。
思路:排一下序然後加然後與當前的比較。這種題為啥會是D?

#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const double pi= acos(-1.0);const double esp=1e-6;const int maxn=1e5+10;int t[maxn];int main(){    int n;    scanf("%d",&n);    for(int i=0;i<n;i++){        scanf("%d",&t[i]);    }    int cnt=0;    LL sum=0;    sort(t,t+n);        for(int i=0;i<n;i++){        if(sum<=t[i]){            sum+=t[i];            cnt++;        }    }    printf("%d\n",cnt);    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.