Codeforces Round #585 (Div. 2) D、E

D

The meaning of problems

A string of sequences, each character is a number, and there are many unknown location, two people have to fill numbers, the former to victory before n 2 \frac{n}{2} And after n 2 \frac{n}{2} Different, which will have the same victory.

answer

We take into account the existing poor figures, the former minus the latter difference x x .
And the difference between the number of question marks Y Y .
If the two different symbol, indicating that the latter doomed to failure, because I can fill in all the required parts plus 9 9 , the latter in order to increase the difference would not let me fill 0 0 , then the last question mark about the same time I arbitrarily choose a location, increasing the difference in fill 9 9 , to reduce the difference in fill 0 0 , which is the most I cancel the current operation.

If the number of the same, can be considered if the latter is to win, extra question mark 9 *9 must make up the difference, if not less certain, and the former will grab, we can only get half the difference between a question mark. If half the difference between a question mark > > Digital poor, then the former can take it over the entire fill 9 9 leading to the gap increases. But if just the right words, no matter how the former rob how to fill in, I can fill out after him, her and me this operation and make up 9 9 , the remaining brackets around the same time, he filled one, I fill a can on the other side.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 200050;
 
int A[maxn],B[maxn];
int sum[maxn],pre0[maxn],pre1[maxn];
 
int main(){
    int n;cin>>n;
    string str;cin>>str;
    int l=0,r=0,ln=0,rn=0;
    for(int i=1;i<=n/2;i++){
        if(str[i-1]=='?')ln++;
        else l+=str[i-1]-'0';
    }
    for(int i=n/2+1;i<=n;i++){
        if(str[i-1]=='?')rn++;
        else r+=str[i-1]-'0';
    }
    if(l==r){
        if(ln==rn)puts("Bicarp");
        else puts("Monocarp");
    }
    else{
        if(l<r&&ln>rn){
            int tmp=(ln-rn)/2;
            if(tmp*9==r-l)puts("Bicarp");
            else puts("Monocarp");
        }
        else if(l>r&&ln<rn){
            int tmp=(rn-ln)/2;
            if(tmp*9==l-r)puts("Bicarp");
            else puts("Monocarp");
        }
        else puts("Monocarp");
    }
}

E

The meaning of problems

n n digits, m m seed value ( 1 m 20 ) (1 \ leq m \ leq 20)
requires all the same value in a continuous segment, adjacent to accomplish this switching, the minimum number of operations required.

answer

Finally 222111333444 222111333444 this state, we can think of an adjacent exchange bubble sort.
But we're not sure what the final state of this sort are, so we need to know this state, first factorial, but the complex is too large.
The second form is the pressure, because 1 < < 20 = 1 e 6 1<<20=1e6 .
DETAILED shaped pressure state is shown, have been sorted state, taking into account the requirements for the number of reverse, we can convert each sorted are the largest (not affect). This makes it easy to calculate, insert a new number, to count the number of current greater than themselves, and finally to state the number of all sorted.
Transfer obvious.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2000050;
 
int n;
int A[maxn],cnt[maxn];
ll pre[30][30],dp[maxn];
 
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        scanf("%d",&A[i]);
        for(int j=1;j<=20;j++){
            pre[A[i]][j]+=cnt[j];
        }
        cnt[A[i]]++;
    }
    memset(dp,0x3f,sizeof(dp));
    dp[0]=0;
    for(int i=0;i<(1<<20);i++){
        ll tmp;
        for(int j=1;j<=20;j++){
            if(i&(1<<(j-1)))continue;
            tmp=0;
            for(int k=1;k<=20;k++){
                if(i&(1<<(k-1)))tmp+=pre[j][k];
            }
            dp[i+(1<<(j-1))]=min(dp[i+(1<<(j-1))],dp[i]+tmp);
        }
    }
    cout<<dp[(1<<20)-1]<<endl;
}
Published 203 original articles · won praise 17 · views 20000 +

Guess you like

Origin blog.csdn.net/mxYlulu/article/details/104115344