2019.10.20 Race Week

A - Minimum Ternary String  CodeForces - 1009B 

Sign in question, can easily adjust the position of the number 1, 2 and 0 of the relative positions unchanged.

#include<cstdio> 
#include<cstring>
using namespace std;
char s[100010];
int main(){
    scanf("%s",s);
    int n=strlen(s),cnt=0; bool flag=0;
    for(int i=0;i<n;++i)
    if(s[i]=='1') cnt++;
    for(int i=0;i<n;++i){
        if(flag){
            if(s[i]=='1') continue;
            printf("%c",s[i]);
            continue;
        }
        
        if(s[i]=='0') printf("0");
        else if(s[i]=='1') continue;
        else if(s[i]=='2'){
            for(int j=1;j<=cnt;++j)
            printf("1");
            printf("2"); 
            flag=1;
        }
    }
    if(!flag){
        for(int i=1;i<=cnt;++i)
        printf("1");
    }
    return 0;
    
}
View Code

B - Equalize CodeForces - 1037C

Or attendance problems. When the distance between the two different numbers is greater than 1, change directly.

#include<cstdio>
#include<cstring>
using namespace std;
const int N=1000010;
int n,ans=0;
char a[N],b[N];
bool df[N];
int main(){
    scanf("%d",&n);
    scanf("%s%s",a,b);
    for(int i=0;i<n;++i)
    if(a[i]!=b[i]) df[i]=1;
    for(int i=0;i<n;++i){
        if(df[i]==0) continue;
        if(b[i]=='0'){
            if(b[i+1]=='1'&&df[i+1]){
                ans++; df[i]=df[i+1]=0;
            }else{
                ans++;
                df[i]=0;
            }
        }else if(b[i]=='1'){
            if(b[i+1]=='0'&&df[i+1]){
                ans++; df[i]=df[i+1]=0;
            }else {
                ans++;
                df[i]=0;
            }
        }
    }
    printf("%d\n",ans);
    return 0;
    
}
View Code

C - Cut 'em all! CodeForces - 982C 

Consider a side can not be removed, if this point becomes an even number, then both sides can be removed.

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1000010;
int n,cnt,hd[N],size[N],dep[N];
struct node{
    int nxt,to;
}e[N*2];
struct data{
    int x,y;
}a[N];
inline void ins(int from,int to){
    e[++cnt].nxt=hd[from];
    e[cnt].to=to;
    hd[from]=cnt;
}
void dfs(int x,int fa){
    size[x]=1;
    for(int i=hd[x];i;i=e[i].nxt){
        if(e[i].to==fa) continue;
        dep[e[i].to]=dep[x]+1;
        dfs(e[i].to,x);
        size[x]+=size[e[i].to];
    }
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<n;++i){
        scanf("%d%d",&a[i].x,&a[i].y);
        ins(a[i].x,a[i].y); ins(a[i].y,a[i].x);
    }
    if(n&1){
        puts("-1"); return 0;
    }
    dfs(1,-1);
    int ans=0;
    for(int i=1;i<n;++i){
        int x=a[i].x,y=a[i].y;
        if(dep[x]<dep[y]) swap(x,y);
        if(!(size[x]&1)) ans++;
    }
    printf("%d\n",ans);
}
View Code

D - Word Count  HDU - 1735 

A greedy title. The beginning of the wrong ideas, lead to code written in very complex. Initialization wrong, can over time or else this problem. . .

1. Consider the worst case, all 0s are destroyed. ans = zeros.

2. The number of passages is fixed, ans = ans-2 * paragraph number.

3. Consider the end segment, the grade is not counted 0, ans = 0 the number ans- end of the segment.

4. We claim ans minimum of 1 and 2 are fixed, that is, the maximum required number of 0 tail section.

The number of paragraphs to be greater than the possible number of paragraphs to the subject, whichever end of the segment more than 0 on the line of a paragraph.

In addition, the last line must be the end of the paragraph, do not forget leak.

A super start to write complex code. .

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int a[10010][105],father[100010],num,num2;
struct node{
    int pre,suf,cnt;
}pgh[10010],p[10010];
struct data{
    int sum,x,y;
    bool operator < (const data &j) const {
     return sum>j.sum;
    }
    
};
priority_queue<data> q;
int find(int x){
    if(x!=father[x]) father[x]=find(father[x]);
    return father[x];
}
int main(){
    int n,l,m;
    while(~scanf("%d%d%d",&n,&l,&m)){
        while(!q.empty()) q.pop();
        num=0;
        for(int i=0;i<=10010;++i){
            pgh[i].pre=pgh[i].suf=pgh[i].cnt=0;
            p[i].pre=p[i].suf=p[i].cnt=0;
        }
        for(int i=1;i<=n;++i)
        for(int j=1;j<=l;++j)
        scanf("%d",&a[i][j]);
        int i=1;
        while(i<=n){
            if(a[i][1]==0&&a[i][2]==0){
                int tmp=0;
                for(int j=l;j>=1;--j)
                if(a[i-1][j]==0) tmp++;
                else break;
                pgh[num].suf=tmp;
                tmp=0;
                for(int j=1;j<=l;++j)
                if(a[i][j]==0) tmp++;
                else break;
                pgh[++num].pre=tmp;
            }
            for(int j=1;j<=l;++j)
            if(a[i][j]==0) pgh[num].cnt++;
            i++;
        }

        for(int i=1;i<=num;++i) father[i]=i;
        for(int i=1;i<num;++i){
            q.push((data){pgh[i].suf,i,i+1});
        }
        for(int i=1;i<=num-m;++i){
            data tmp;
            tmp=q.top(); q.pop();
            int x=tmp.x,y=tmp.y;
            int r1=find(x),r2=find(y);
            father[r2]=r1;
        }
        int root=find(1); num2=1;
        p[num2].pre=pgh[1].pre;
        for(int i=1;i<=num;++i){
            if(find(i)==root) 
                p[num2].cnt+=pgh[i].cnt;
            else{
                root=find(i);
                p[num2].suf=pgh[i-1].suf;
                p[++num2].pre=pgh[i].pre;
                p[num2].cnt=pgh[i].cnt;
            }
        }
    
        p[num2].suf=0;
        for(int j=l;j>=1;--j)
        if(a[n][j]==0) p[num2].suf++;
        else break;
    
        int ans=0;
        for(int i=1;i<=num2;++i)
            ans=ans+p[i].cnt-2-p[i].suf;
        printf("%d\n",ans);
      }
       return 0;
}
View Code

Want is to merge more than a paragraph, in fact, the combined operations do not need to write it, only the tail section number 0 on the line.

And the first paragraph of the number of zeros and no use, I was wrong. . . If the first paragraph of useful words I write about on the right.

simple

https://blog.csdn.net/snowy_smile/article/details/49666233

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,p;
int a[105],b[10010];
int main(){
    while(~scanf("%d%d%d",&n,&m,&p)){
        int ans=0,num=0,ed=0;
        for(int i=1;i<=n;++i){
            for(int j=1;j<=m;++j){
                scanf("%d",&a[j]);
                ans+=a[j]==0;
            }
            if(a[1]==0&&a[2]==0) b[++ed]=num;
            for(int j=m;j>=1;--j)
            if(a[j]==1){
                num=m-j; break;
            }
        }
        ans=ans-2*p-num;
        sort(b+1,b+ed+1);
        for(int i=ed;i>=ed-p+2;--i)
        ans-=b[i];
        printf("%d\n",ans);
    }
    return 0;
    
}
View Code

 

....................................

 

F - Semifinals CodeForces - 378B 

Subject to the effect: There are two semi-finals, each n everyone to participate in every field, now has a value of k, k represents said the former name of the semi-finals of the finals directly, because you want to select n individuals to participate in the finals, so 2 * (n -k) to be selected before the remainder of the 2 * (nk), a string of k is 0 ~ n / 2, asked, those who are likely to qualify for the finals.

 

Outline of Solution: directly in accordance with k = 0 and k scheme = n / 2 to the election, including all candidates can be cut, the two extremes.
----------------
Original link: https: //blog.csdn.net/keshuai19940722/article/details/18864545

For the energy is selected to FIG 1,5,2,6. Now consider 3 can not be selected.

3 can be selected if the first 1.2 certainly can.

1. k = 0, the 3 selected order than at least 6 hours.

2. k = 1, the selected 1 and 5,

  i, 3 5 larger than not influence the results

 II, 3 less than 5, that is a selected ratio of 3 inferior element, and certainly not as a result when 0 k =;

In summary available, k = 0, the solutions of difference is at least not more than 1 k =, it is considered directly when k = 0, and when k = n / 2 on the line.

#include <stdio.h>
#include <string.h>
 
const int N = 100005;
 
int n, a[N], b[N];
int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%d%d", &a[i], &b[i]);
    int p = 0, q = 0, k = n / 2;
    for (int i = 0; i < n; i++) {
        if (a[p] < b[q]) p++;
        else q++;
    }
 
    for (int i = 0; i < n; i++) {
        if (i < p || i < k) printf("1");
        else printf("0");
    }
    printf("\n");
    for (int i = 0; i < n; i++) {
        if (i < q || i < k) printf("1");
        else printf(" 0 " ); 
    } 
    printf ( " \ the n- " );
     return  0 ; 
} 
---------------- 
Disclaimer: This article is CSDN blogger "JeraKrs" of the original article, follow CC 4.0 BY- SA copyright agreement, reproduced, please attach the original source link and this statement. 
Original link: HTTPS: // blog.csdn.net/keshuai19940722/article/details/18864545
View Code

My idea: if selected than 3 want at least 6 hours, if selected for at least 4 want than 5 hours.

#include<cstdio>
#include<cstring>
using namespace std;
const int N=1000010;
int n,a[N],b[N],ansa[N],ansb[N];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
    scanf("%d%d",&a[i],&b[i]);
    int t=n/2;
    for(int i=t+1;i<=n;++i){
        int j=n-i+1;
        if(a[i]<b[j]) ansa[i]=1;
    }
    for(int i=t+1;i<=n;++i){
        int j=n-i+1;
        if(b[i]<a[j]) ansb[i]=1;
    }
    for(int i=1;i<=t;++i)
    ansa[i]=ansb[i]=1;
    for(int i=1;i<=n;++i)
    printf("%d",ansa[i]);
    printf("\n");
    for(int i=1;i<=n;++i)
    printf("%d",ansb[i]);
    return 0;
}
My thoughts

 

 

I - Pie or die CodeForces - 55C 

First contact with the topic of game theory, almost ah, I tried to 4, 5 result the answer is, I'm too hard! It should be more on paper painting a picture.

[Title] has a meaning n * m board, there are k pieces above, the upper hand can move a piece to each neighboring grid, if a piece can be moved to the outside of the board, the upper hand wins; flip per It can be a closed border (edge between the two vertices can not be moved outside the boundary of the closed rear side), if the upper hand piece can not be moved outside to a board, the flip wins;
[idea] checkerboard analysis can be found in for the four corners, each corner has two sides, that is to say the four corners of a total of more than four sides, plus the original one boundary edge, then a total of five sides, all as a piece from the border when the distance is less than 5, there must be a five edges which edges are not closed, the upper hand winning, losing or upper hand.
----------------
Original link: https: //blog.csdn.net/i1020/article/details/79791234

#include<cstdio>
#include<cstring>
using namespace std;
int n,m,k,p[106][3];
int main(){
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=k;++i){
        scanf("%d%d",&p[i][0],&p[i][1]);
    }
    bool flag=0;
    for(int i=1;i<=k;++i){
        int x=p[i][0],y=p[i][1];
        if(x<=5||x>=n-4) flag=1;
        if(y<=5||y>=m-4) flag=1;
        if(flag) break;
    }
    if(flag) puts("YES");
    else puts("NO");
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/huihao/p/11716912.html