12.27 [diary] / [explanations] CF Edu79

12.27

CF Edu79

A.New Year Garland

Meaning of the questions: there r, g, b a red, green and blue balloons, you can now ask whether a row so that no two adjacent same color balloons.

Idea: If the maximum> two decimal +1 hung up.

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define mid ((l+r)>>1)
const int M=1e5+20;
struct Task{
    int a,b,c;
    void init(){
        scanf("%d%d%d",&a,&b,&c);
    }
    void run(){
        init();
        int mx=max(a,max(b,c)),sum=a+b+c;
        if (mx>sum-mx+1)
            printf("No\n");
        else
            printf("Yes\n");
    }
}t;
int main(){
    int T;
    scanf("%d",&T);
    for(int i=1;i<=T;++i)
        t.run();
    return 0;
}

B.Verse For Santa

The meaning of problems: there are n segments, each requires a [i] minutes, a total of s min, have to start in order to read, but a skip away. Q. In order to read out the largest fragment, which need to delete?

Thinking: ten thousand fake algorithm. In fact, very simple, to not skip, find the critical point, then either skip the first study could not finish, or skip out in front of the largest, the largest election that skip the line.

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define mid ((l+r)>>1)
const int M=1e5+20;
struct Task{
    int n,s,a[M];
    int mxp=0;
    void init(){
        mxp=0;
        scanf("%d%d",&n,&s);
        for(int i=1;i<=n;++i)
            scanf("%d",&a[i]);
        a[n+1]=0;
    }
    void run(){
        init();
        int p=1;
        while(p<=n&&a[p]<=s){
            s-=a[p];
            if (a[p]>a[mxp])
                mxp=p;
            ++p;
        }
        if (p>n||n==1)
            printf("0\n");
        else{
            if (a[mxp]>a[p])
                printf("%d\n",mxp);
            else
                printf("%d\n",p);
        }
    }
}t;
int main(){
    int T;
    scanf("%d",&T);
    for(int i=1;i<=T;++i)
        t.run();
    return 0;
}

C.Stack of Presents

Meaning of the questions: There are a bunch of gifts, from top to bottom number followed by a1, a2, ...... an. Sequentially now take b1, b2, ......, bn gifts, each requiring both take seconds 2k + 1, k is the number of targets present gift above. But after each get a gift, the gift can be sorted by any of the above order. Now I ask all the presents in order to come up with the minimum time is.

Ideas: it is conceivable that, if the current gift before had been moving, so take this gift of time is 1, because the last move it, you will be able to arrange to have a way to get it when it happens in most top. So directly vis pointers and arrays, if it had not been accessed looking back, looking for the course to turn in all of which recorded vis click.

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define mid ((l+r)>>1)
#define db(x) cout<<#x<<":"<<x<<endl;
const int M=1e5+20;
struct Task{
    int n,m,a[M],b[M],vis[M];
    void init(){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;++i)
            scanf("%d",&a[i]),vis[i]=0;
        for(int i=1;i<=m;++i)
            scanf("%d",&b[i]);
    }
    void run(){
        init();
        LL ans=0;
        int p=1;
        for(int i=1;i<=m;++i){
            if (vis[b[i]])
                ++ans;
            else{
                while(p<=n&&a[p]!=b[i])
                    vis[a[p]]=1,++p;
                ans+=2*(p-i)+1;
            }
        }
        printf("%lld\n",ans);
    }
}t;
int main(){
    int T;
    scanf("%d",&T);
    for(int i=1;i<=T;++i)
        t.run();
    return 0;
}

D.Santa's Bot

Meaning of the questions: There are n children, every child has the k desired gift, each gift has a different number. Now there is a algorithm first randomly selected a child, after the child's wish list from which randomly selected a gift, then randomly selected a child, just the gift for the child. Ask how many possibilities there are that can send right.

Ideas: First, the statistical probability that all gifts be selected, apparently read the encounter once + \ (\ {1} {the n-FRAC} * \ FRAC {1} {k} \) . Since the last time after a person is randomly selected to be sent, so the probability of being selected for each gift you want * the number of its children / n can be.

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define mid ((l+r)>>1)
const int M=1e6+20,P=998244353;
int Inv[M];
void ln_Inv(int p){  
    Inv[0]=Inv[1]=1;
    for(int i=2;i<=1e6;i++)
        Inv[i]=1LL*(p-p/i)*Inv[p%i]%p; 
}  
struct Task{
    int n,giftnum[M],giftprob[M];
    void run(){
        scanf("%d",&n);
        for(int i=1;i<=n;++i){
            int k;
            scanf("%d",&k);
            int prob=1LL*Inv[n]*Inv[k]%P;
            for(int j=1;j<=k;++j){
                int c;
                scanf("%d",&c);
                ++giftnum[c];
                giftprob[c]=(giftprob[c]+prob)%P;
            }
        }
        int ans=0;
        for(int i=1;i<=1e6;++i)
            ans=(ans+1LL*giftprob[i]*giftnum[i]%P*Inv[n]%P)%P;
        printf("%d\n",ans);
    }
}t;
int main(){
    ln_Inv(P);
    t.run();
    return 0;
}

Guess you like

Origin www.cnblogs.com/diorvh/p/12110224.html