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

Source: Internet
Author: User

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

A:
Topic address: Toy Cars
Give a matrix of n * n.-1 only appears on the diagonal line (because you cannot hit yourself). 0 indicates that there is no car in the collision, and 1 indicates the I car (horizontal coordinate) 2 indicates that the j-car (Y-axis) is damaged, and 3 indicates that both cars are damaged. Ask which cars are intact.
Idea: traverse I. For each line, as long as there are 1 and 3, it means that the car is kneeling, and then it is very spicy to find out the lines without 1 and 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:
Address: Equidistant String
Evaluate a string so that it differs from S, T, and similarity.
The idea is to find out the number of differences between S and T, with an odd number of differences and output impossible. If the output is an even number, different first half outputs T in the second half.

#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:
Address: Woodcutters
There are n trees that give the position and height of each tree, and then cut down the tree. The tree can be left or right. The output can cut several trees at most.
Idea: Use greedy ideas. There is no tree on the left of the first tree and on the right of the last tree, so they fall to both sides. For the middle tree, first fall to the left, and then to the right if the Left is not enough, update the distance to the right.

#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:
Question address: Queue
Question: There are n people and each person has a waiting time. If the total waiting time for the current person exceeds the waiting time, the person will be disappointed and ask in another order, the minimum number of people who are disappointed is asked.
Train of Thought: sort the order, add it, and then compare it with the current one. Why is this question 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;}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.