Educational Codeforces Round 97 (Div. 2) A—E题解

A. Marketing Scheme

Title Portal:

Marketing Scheme

Main idea:

Does x exist such that all numbers in the interval [l, r] mod x> = x / 2

Ideas:

Another x=2*l, if x> r, it is true

AC Code

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
    int t;
    scanf("%d",&t);
    while(t--)
    {
    
    
        int l,r;
        scanf("%d%d",&l,&r);
        if(2*l>r) printf("YES\n");
        else printf("NO\n");
    }
    //system("pause");
    return 0;
}

B. Reverse Binary Strings

Topic Portal

Reverse Binary Strings

Main idea:

Give you a string consisting of 0 and 1, ask at least several times to reverse to get the target string 01010101……or 10101010……

Ideas:

There are two target strings at the end, so try both cases. If there are consecutive strings that need to be flipped, then flip them together.

AC Code

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int a[N];
int main()
{
    
    
    int t;
    scanf("%d",&t);
    while(t--)
    {
    
    
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%1d",&a[i]);
        int idx=0;
        int res1=0,res2=0;
        while(idx<n)
        {
    
    
            while(a[idx]==idx%2&&idx<n) idx++;
            int num=0;
            while(a[idx]!=idx%2&&idx<n)
            {
    
    
                num++;
                idx++;
            }
            if(num>0) res1++; 
        }
        idx=0;
        while(idx<n)
        {
    
    
            while(a[idx]!=idx%2&&idx<n) idx++;
            int num=0;
            while(a[idx]==idx%2&&idx<n)
            {
    
    
                num++;
                idx++;
            }
            if(num>0) res2++; 
        }
        printf("%d\n",min(res1,res2));
    }
    //system("pause");
    return 0;
}

C. Chief Monocarp

Title Portal:

Chef Monocarp

Main idea:

There are n dishes, and the best cooking time for the i-th dish is ti. From the first minute, at most one dish can be taken out of the oven every minute. The degree of dissatisfaction of each dish | ti-T | (T refers to the time the dish is taken out of the oven). Find the minimum of the sum of dissatisfaction levels.

Ideas:

dp[ i ][ j] means to take out the first j dishes i minutes ago.
Then the state transition equation has:
dp[ i ][ j] = min (dp [i -1 ][ j ], dp [i -1 ][ j -1] + abs (t [j]-i))

AC Code

#include<bits/stdc++.h>
using namespace std;
const int N=205;
int ti[N];
int dp[N*2][N];
int main()
{
    
    
    int q;
    scanf("%d",&q);
    while(q--)
    {
    
    
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&ti[i]);
        memset(dp,0x3f,sizeof(dp));
        sort(ti+1,ti+1+n);
        for(int i=0;i<=N*2;i++)
            dp[i][0]=0;
        for(int i=1;i<=2*n;i++)
        {
    
    
            for(int j=1;j<=n;j++)
            {
    
    
                dp[i][j]=min(dp[i-1][j],dp[i-1][j-1]+abs(ti[j]-i));
            }
        }
        printf("%d\n",dp[2*n][n]);
    }
    //system("pause");
    return 0;
}

D.Minimal Height Tree

Title Portal
Minimal Height Tree

General idea

Give you the bfs order of a tree (not necessarily a binary tree, I just read the sample and thought it was a binary tree, so wa was sent several times), and then the child nodes of each node are arranged in ascending order , Ask you the minimum depth of this tree. 1 is the root node, and the depth is 0.

Ideas:

The bfs sequence is divided into increasing intervals, and each increasing interval is a child node of a node in the upper layer. Then record the number of layers.

AC Code

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int a[N];
int sum[N];
int main()
{
    
    
    int t;
    scanf("%d",&t);
    while(t--)
    {
    
    
        memset(sum,0,sizeof(sum));
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        sum[0]=1;//树每一层数的数量
        int idx=2;//当前在数组中所在位置
        int k=1;//当前层数
        while(idx<=n)
        {
    
    
            while(idx+1<=n&&a[idx]<a[idx+1])
            {
    
    
                idx++;
                sum[k]++;
            }
            idx++;
            sum[k]++;
            sum[k-1]--;
            if(sum[k-1]==0&&idx<=n) k++;
        }
        printf("%d\n",k);
    }
    //system("pause");
    return 0;
}

E.Make It Increasing

(Additional questions after the game)
Title portal:
Make It Increasing

Main idea:

Gives you an integer sequence a with n elements and an integer set b with k elements. The element with subscript bi in a cannot be changed. Other numbers can become any integer. Ask at least how many transformations can make a sequence into an increasing sequence. If it is impossible, output -1.

Ideas:

First consider the legal situation of the sequence. Obviously, a strictly ascending sequence can be turned into a non-strictly ascending sequence by a[i]=a[i]−i, so we first scan the opposite direction twice to find out whether there is a solution and which positions It must be changed, and the remaining positions can be the longest ascending subsequence.

AC Code

#include<bits/stdc++.h>
using namespace std;
const int N=5e5+10;
int n,k;
int a[N];
int ans[N]; 
int res=0;
int st[N];
void insert(int x)
{
    
    
    if(res==0)
    {
    
    
        st[++res]=x;
        return ;
    }
    int l=1,r=res;
    while(l<=r)
    {
    
    
        int mid=(l+r)/2;
        if(st[mid]<=x) l=mid+1;
        else r=mid-1;
    }
    if(l>res) st[++res]=x;
    else st[l]=x;
}
int main()
{
    
    
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
    {
    
    
        scanf("%d",&a[i]);
        a[i]=a[i]-i;
    }
    for(int i=1;i<=k;i++)
    {
    
    
        int j;
        scanf("%d",&j);
        ans[j]=1;
    }
    int lim=-1e9;
    for(int i=1;i<=n;i++)
    {
    
    
        if(ans[i]==1)
        {
    
    
            if(a[i]<lim)
            {
    
    
                printf("-1\n");
                return 0;
            }
            lim=max(lim,a[i]);
        }
        if(a[i]<lim) ans[i]=-1;
    }
    lim=1e9;
    for(int i=n;i>=1;i--)
    {
    
    
        if(ans[i]==1)
        {
    
    
            if(a[i]>lim)
            {
    
    
                printf("-1\n");
                return 0;
            }
            lim=min(lim,a[i]);
        }
        if(a[i]>lim) ans[i]=-1;
    }
    memset(st,0x3f,sizeof(st));
    st[0]=0;
    for(int i=1;i<=n;i++)
    {
    
    
        if(ans[i]) continue;
        insert(a[i]);
    }
    printf("%d\n",n-res-k);
    //system("pause");
    return 0;
}

Guess you like

Origin blog.csdn.net/Stevenwuxu/article/details/109338792