Educational Codeforces Round 100 A-D Problem Solution

A. Dungeon

Question portal:

A. Dungeon

Main idea:

Three monsters have a, b, and c HP. Each time a shell is fired, one monster will be damaged a little. After every 6 shots, the 7th is a charged cannon, causing a little damage to three monsters at the same time. Ask if three monsters can be killed by the same charged cannon at the same time.

Ideas:

If the sum of a + b + c is a multiple of 9, the reason is that only multiples of 9 can be dropped by the battery gun at the same time. And min(a,b,c)>=sum/9, because every monster cannot die before the last charge cannon.

AC Code

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
    int t;
    scanf("%d",&t);
    while(t--)
    {
    
    
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        int sum=a+b+c;
        if(sum%9) printf("NO\n");
        else
        {
    
    
            int k=sum/9;
            if(a>=k&&b>=k&&c>=k) printf("YES\n");
            else printf("NO\n");   
        }
    }
    //system("pause");
    return 0;
}

B. Find The Array

Question portal:

B. Find The Array

Main idea:

Give you a sequence a, and ask if you can construct a sequence b that satisfies
1, b i % b i+1 == 0 or b i+1 % b i == 0
2,
Insert picture description here

Ideas:

We find that
X=1 + a [2] + 1 + a [4] + 1 ……

Y = a [1] + 1 + a [3] + 1 + a [5] + ……

Wx = S - X = a [ 1 ] + a [ 3 ] + a [ 5 ] + ……- n/2
Wy = S - Y =a [ 2 ] + a [ 4 ] + a [ 6 ] ……- n/2
Wx + Wy = S - n

Then obviously there must be a type of <= (Sn) / 2 in Wx and Wy. Meet the subject requirements

AC Code

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL a[55];
int main()
{
    
    
    int t;
    scanf("%d",&t);
    while(t--)
    {
    
    
        int n;
        scanf("%d",&n);
        LL sum=0;
        for(int i=1;i<=n;i++)
        {
    
    
            scanf("%d",&a[i]);
            sum=sum+a[i];
        }
        LL ans=0;
        for(int i=1;i<=n;i=i+2)
            ans=ans+a[i]-1;
        if(ans*2<=sum)
        {
    
    
            for(int i=1;i<=n;i++)
            {
    
    
                if(i%2) printf("1 ");
                else printf("%d ",a[i]);
            }
        }
        else
        {
    
    
            for(int i=1;i<=n;i++)
            {
    
    
                if(i%2) printf("%d ",a[i]);
                else printf("1 ");
            }
        }
        printf("\n");
    }
    //system("pause");
    return 0;
}

C. Busy Robot

Question portal:

C. Busy Robot

Main idea:

There are n instructions, each instruction is at the time ti, and the robot moves to the point xi. When the robot is moving, the robot will ignore the instructions until the current instruction is completed. In [t i , t i+1 ] time, the robot moves to the target point xi, then res++. (Note including ignored instructions)

Ideas:

It's actually a disgusting simulation. See the code for details.

AC Code

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e5+10;
LL ti[N],x[N];
int main()
{
    
    
    int t;
    scanf("%d",&t);
    while(t--)
    {
    
    
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%lld%lld",&ti[i],&x[i]);
        ti[n+1]=1e10;//将n+1点的时间设置为无限大
        int idx=1;
        LL now=0,pos=0;
        int flag=0,res=0;
        while(idx<=n)
        {
    
    
            LL y=pos;
            LL b=ti[idx];
            now=ti[idx]+abs(x[idx]-pos);
            if(x[idx]<pos) flag=-1;//机器人的行进方向
            else flag=1;
            pos=x[idx];
            int k=idx+1;//中间要跳过的指令
            while(ti[k]<now&&k<=n)
                k++;
            for(int i=idx;i<k;i++)
            {
    
    
                LL st=min(ti[i],now);
                LL ed=min(ti[i+1],now);
                LL stx=y+flag*(st-b);
                LL edx=y+flag*(ed-b);
                if(x[i]>=min(stx,edx)&&x[i]<=max(stx,edx)) res++;//判断满足条件的指令数量
            }
            idx=k;
        }
        printf("%d\n",res);
    }
    //system("pause");
    return 0;
}

D. Pairs

Question portal:

D. Pairs

General idea

Divide 2n numbers into n pairs. Among them, the x pair performs the small operation, and the remaining number performs the large operation. Give you a sequence a of n elements. Ask you how many kinds of numbers x can be, you can get an array.

Ideas:

Know the least number that needs to be taken from the smaller and the least number that needs to be taken from the greater. n-the number of both + 1 is the answer.

How to get it. Take the smallest number required for calculation as an example. Let sum be the number that is smaller than the current number and is not in the sequence a and has not been matched yet.
Then if sum>0, then we can find a smaller number than the current one, then the current pair is performing the operation of taking the larger one. (If you can take the big operation, try to take the big one, because we are looking for the minimum number of times to take the small.)
If sum==0, then the current number can only find the largest number that is not in the sequence and does not match. Take small operations.

AC Code

#include<bits/stdc++.h>
using namespace std;
const int N=5e5+10;
int a[N];
int main()
{
    
    
    int t;
    scanf("%d",&t);
    while(t--)
    {
    
    
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        int minn=0,maxn=0;//取小的最小次数,和取大的最小次数
        int sum=0;
        for(int i=1;i<=n;i++)
        {
    
    
            sum=sum+a[i]-a[i-1]-1;
            if(!sum) minn++;
            else sum--;
        }
        a[n+1]=2*n+1;
        sum=0;
        for(int i=n;i>=1;i--)
        {
    
    
            sum=sum+a[i+1]-a[i]-1;
            if(!sum) maxn++;
            else sum--;
        }
        printf("%d\n",n-minn-maxn+1);
    }
    //system("pause");
    return 0;
}

Guess you like

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