Educational Codeforces Round 115 (Rated for Div. 2)

Keywords: C Algorithm

Introduction

daily

Knowledge points involved

Sequencing, thinking

Link: Educational Codeforces Round 115 (Rated for Div. 2)

subject

A Computer Game

Give a 2 × N matrix, each position is only 0 or 1,1 unreachable, 0 reachable. Now start from (1,1) and move forward to the adjacent reachable positions each time (in addition to edge adjacent, it can also be diagonally adjacent), ask whether it can reach (2, n), and ensure that the starting point and end point are 0

Idea: just directly simulate the process. At the beginning, I actually wrote a violent search. I was dizzy and hot. My teammates said that they could directly judge whether there were two consecutive ones up and down. After thinking about it, they did

code

#include<bits/stdc++.h>
using namespace std;
int t,n;
char maze[3][101];
bool vis[3][101];
int main() {
    scanf("%d",&t);
    while(t--) {
        scanf("%d",&n);
        scanf("%s",maze[1]+1);
        scanf("%s",maze[2]+1);
        int x=1,y=1;
        vis[x][y]=1;
        for(int i=1; i<=2*n; i++) {
            if(x==1) {
                if(maze[x+1][y]=='0'&&!vis[x+1][y]) {
                    x=x+1;
                    vis[x][y]=1;
                    continue;
                }
                if(maze[x][y+1]=='0'&&!vis[x][y+1]) {
                    y=y+1;
                    vis[x][y]=1;
                    continue;
                }
                if(maze[x+1][y+1]=='0'&&!vis[x+1][y+1]) {
                    x=x+1;
                    y=y+1;
                    vis[x][y]=1;
                    continue;
                }
            } else {
                if(maze[x-1][y]=='0'&&!vis[x-1][y]) {
                    x=x-1;
                    vis[x][y]=1;
                    continue;
                }
                if(maze[x][y+1]=='0'&&!vis[x][y+1]) {
                    y=y+1;
                    vis[x][y]=1;
                    continue;
                }
                if(maze[x-1][y+1]=='0'&&!vis[x-1][y+1]) {
                    x=x-1;
                    y=y+1;
                    vis[x][y]=1;
                    continue;
                }
            }
        }
        if(vis[2][n])
            printf("YES\n");
        else
            printf("NO\n");
        memset(vis,0,sizeof(vis));
    }
    return 0;
}

B Groups

Main idea of the title: omitted

Idea: count the size of five sets, and then traverse the situation of each two combinations. If the size of both sets is greater than or equal to n/2, and calculate the sum of their common parts as n, it represents separability

code

#include <bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e5+5;
int a[N][6];
int num[6];
bool check(int x, int y,int n) {
    int res = 0;
    for(int i = 1; i <= n; i++)
        res+=(a[i][x]|a[i][y]);//Public part
    return res==n;//Returns the total value of the public part
}
void solve() {
    int n;
    scanf("%d",&n);
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= 5; j++)
            scanf("%d",&a[i][j]);
    for(int i = 1; i <= 5; i++)
        for(int j = 1; j <= n; j++)//Cumulative number of people per day
            num[i]+=a[j][i];
    for(int i = 1; i <= 5; i++)
        for(int j = i+1; j<=5; j++)
            if(num[i]>=n/2&&num[j]>=n/2&&check(i,j,n)) {
                printf("YES\n");
                return;
            }
    printf("NO\n");
}
int main() {
    int t;
    scanf("%d",&t);
    while(t--) {
        solve();
        memset(num,0,sizeof(num));//empty
    }
    return 0;
}

C Delete Two Elements

Give a sequence of n nonnegative integers and find that the average of the whole sequence remains unchanged after removing the numbers after subscripts i and j (i < j)

Idea: preprocess and sort the number of each number, and save the number with the index. Since the calculated average number may be a decimal, double is needed to find the sequence after sorting, and find the number of another number corresponding to each number

code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e5+10;
ll t,n,a[maxn];
int main() {
    scanf("%lld",&t);
    while(t--) {
        scanf("%lld",&n);
        map<ll,ll>u;//CF does not support unorderd_map, speechless
        double sum=0;
        ll ans=0,maxx=0;
        for(int i=1; i<=n; i++) {
            scanf("%lld",a+i);
            sum+=a[i];
            u[a[i]]++;
            maxx=max(u[a[i]],maxx);//Find the maximum number
        }
        if(maxx==n) {//If it's all the same
            printf("%lld\n",n*(n-1)/2);
            continue;
        }
        sort(a+1,a+1+n);//Prepare for dichotomy
        sum/=n;
        sum*=2;//Calculate twice the average
        for(int i=1; i<=n; i++) {//Direct calculation includes repeated
            ll low=1,high=n;
            double seek=sum-a[i];//Find another required value
            while(low<=high) {
                ll mid=(low+high)>>1;
                if((double)a[mid]>seek&&(double)a[mid]-seek>1e-5)high=mid-1;
                else if((double)a[mid]<seek&&(double)a[mid]-seek<1e-5)low=mid+1;
                else if(((double)a[mid]-seek)<1e-5) {//An equal value was found
                    if(a[mid]==a[i]) {//If two identical numbers add up
                        ans+=u[a[mid]]*(u[a[mid]]-1);//One time calculation
                        u[a[mid]]=0;//Empty number
                    } else
                        ans+=u[a[mid]];//Otherwise, add directly
                    break;
                }
            }
        }
        printf("%lld\n",ans/2);//duplicate removal
    }
    return 0;
}

D Training Session

Main idea of the topic: give n questions. Each question has two attributes. The first is the topic number and the second is the topic difficulty. Set the three questions as a combination and find out the number of combinations that meet at least one of the following conditions

  1. The topics of the three questions are different
  2. The difficulty of the three questions is different

Train of thought: the train of thought comes from the official problem solution. It is difficult to find the combination that meets the conditions. It is better to find all the combinations minus the combination that does not meet the conditions. According to the meaning of the problem, the total number of combinations is C n 3 C^3_n Cn3, the combination shape that does not meet the conditions is similar [ ( x , y ) , ( z , y ) , ( x , p ) ] [(x,y),(z,y),(x,p)] [(x,y),(z,y),(x,p)], then find the number of combinations that do not meet the conditions. For a problem, if its topic value is x, if there are a problems with topic value x, then there are a-1 problems that can form combinations that do not meet the conditions with the current problem. Similarly, if the difficulty is y, if there are b problems with difficulty value y, the number of combinations that can be combined is b-1, Therefore, we can know that for a problem, the total number of combinations that do not meet the conditions is (a-1)(b-1), which can be subtracted from the total value

code

#include <bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
const int maxn=2e5+10;
using namespace std;
int a[maxn],b[maxn],x[maxn],y[maxn];
void solve() {
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; i++) {
        scanf("%d%d",&a[i],&b[i]);
        x[a[i]]++,y[b[i]]++;//Statistical times
    }
    ll ans=n*1LL*(n-1)*(n-2)/6;//Calculate total logarithm
    for(int i=1; i<=n; i++)
        ans-=(x[a[i]]-1)*1LL*(y[b[i]]-1);//Remove the bad logarithm
    printf("%lld\n",ans);
    memset(x,0,sizeof(int)*(n+1));
    memset(y,0,sizeof(int)*(n+1));
}
int main() {
    int t;
    scanf("%d",&t);
    while(t--)
        solve();
    return 0;
}

reference

  1. Educational Codeforces Round 115 Editorial

Posted by uberpolak on Fri, 15 Oct 2021 12:22:04 -0700