Codeforces Round 584 题解

……

A, B cut first seconds, C is a malignant tumor details of the problem, wasting a lot of time after finally passed.

D sb could have been a problem, but still like a long time, but fortunately nothing serious.

E1, will be the moment, however, the details of the pit dead, long time before too.

I do feel very E2, E2 always wanted, more than half thought the results would last a little thought out.

At this point G1 more people to do it. However, I will not ah ......

Hopeless. Off points off disaster.


A

Row a sequence, if it's about the number in front of it dyed the same color, or open a new color.

The following code, because first I thought the program to be output, it is more ugly.

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> PII;
    const int maxn=100010;
    #define MP make_pair
    #define PB push_back
    #define lson o<<1,l,mid
    #define rson o<<1|1,mid+1,r
    #define FOR(i,a,b) for(int i=(a);i<=(b);i++)
    #define ROF(i,a,b) for(int i=(a);i>=(b);i--)
    #define MEM(x,v) memset(x,v,sizeof(x))
    inline ll read(){
        char ch=getchar();ll x=0,f=0;
        while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
        while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
        return f?-x:x;
    }
    struct hhh{
        int x,id;
        bool operator<(const hhh &h)const{return x<h.x;}
    }h[maxn];
    int n,cnt;
    int main(){
        n=read();
        FOR(i,1,n) h[i].x=read(),h[i].id=i;
        sort(h+1,h+n+1);
        FOR(i,1,n){
            int pos=0;
            FOR(j,1,i-1) if(h[i].x%h[j].x==0) pos=j;
            if(!pos) cnt++;
        }
        printf("%d\n",cnt);
    }

B

Obviously there will be periodic, direct simulation. Period is \ (125 \) , but as long as T is not on the list.

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> PII;
    const int maxn=100010;
    #define MP make_pair
    #define PB push_back
    #define lson o<<1,l,mid
    #define rson o<<1|1,mid+1,r
    #define FOR(i,a,b) for(int i=(a);i<=(b);i++)
    #define ROF(i,a,b) for(int i=(a);i>=(b);i--)
    #define MEM(x,v) memset(x,v,sizeof(x))
    inline ll read(){
        char ch=getchar();ll x=0,f=0;
        while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
        while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
        return f?-x:x;
    }
    int n,a[maxn],b[maxn],ans;
    char s[maxn];
    int calc(){
        int sum=0;
        FOR(i,1,n) if(s[i]=='1') sum++;
        return sum;
    }
    int main(){
        n=read();
        scanf("%s",s+1);
        FOR(i,1,n) a[i]=read(),b[i]=read();
        FOR(i,1,100000){
            ans=max(ans,calc());
            FOR(j,1,n) if(i>=b[j] && (i-b[j])%a[j]==0) s[j]^=1;
        }
        printf("%d\n",ans);
    }

C

Nothing to say, like how to do how to engage.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn=200020;
#define MP make_pair
#define PB push_back
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define ROF(i,a,b) for(int i=(a);i>=(b);i--)
#define MEM(x,v) memset(x,v,sizeof(x))
inline ll read(){
    char ch=getchar();ll x=0,f=0;
    while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
    while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
    return f?-x:x;
}
struct item{
    int x,id;
    bool operator<(const item &i)const{
        if(x!=i.x) return x<i.x;
        return id<i.id;
    }
}it[maxn];
int t,n,ans[maxn],back[maxn];
vector<int> hhh[10];
bool in[maxn];
char s[maxn];
int main(){
    t=read();
    while(t--){
        n=read();
        scanf("%s",s+1);
        bool flag=true;
        FOR(i,0,9) hhh[i].clear();
        FOR(i,1,n) hhh[s[i]-'0'].push_back(i);
        int pos=-1,lst=-1;
        FOR(i,0,9) if(hhh[i].size()){
            back[i]=hhh[i].back();
            FOR(j,0,(int)hhh[i].size()-1){
                if(pos!=-1) ans[hhh[i][j]]=2;
                else if(lst!=-1 && hhh[i][j]<hhh[lst].back()) ans[hhh[i][j]]=2,back[i]=hhh[i][j];
                else ans[hhh[i][j]]=1;
            }
            if(lst!=-1){
                if(pos==-1){
                    if(hhh[i].front()<back[lst]) pos=i;
                }
                else{
                    if(hhh[i].front()<back[lst]){puts("-");flag=false;break;}
                }
            }
            lst=i;
        }
        if(flag){
            FOR(i,1,n) printf("%d",ans[i]);
            puts("");
        }
    }
}

D

Everyone \ (x \) and \ (y \) even edge.

Consider a ring, at least one of the above can not be satisfied. Proposal to put this man stuffed into the final surface on the line.

If it is a tree, it can all be satisfied. Program structure is not difficult.

Then consider each block Unicom, casual get a spanning tree, let's meet the people side of the tree, the tree side of the non-people are not satisfied (stuffed into the final surface) on the line. Obviously the best.

In fact only need to count the number of edges and points.

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> PII;
    const int maxn=200020;
    #define MP make_pair
    #define PB push_back
    #define lson o<<1,l,mid
    #define rson o<<1|1,mid+1,r
    #define FOR(i,a,b) for(int i=(a);i<=(b);i++)
    #define ROF(i,a,b) for(int i=(a);i>=(b);i--)
    #define MEM(x,v) memset(x,v,sizeof(x))
    inline ll read(){
        char ch=getchar();ll x=0,f=0;
        while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
        while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
        return f?-x:x;
    }
    int n,k,ans,el,head[maxn],to[maxn],nxt[maxn];
    bool vis[maxn];
    map<PII,bool> vise;
    inline void add(int u,int v){
        to[++el]=v;nxt[el]=head[u];head[u]=el;
    }
    void dfs(int u,int f){
        vis[u]=true;
        for(int i=head[u];i;i=nxt[i]){
            int v=to[i];
            if(v==f) continue;
            if(vis[v]){
                ans++;continue;
            }
            dfs(v,u);
        }
    }
    int main(){
        n=read();k=read();
        FOR(i,1,k){
            int x=read(),y=read();
            if(x>y) swap(x,y);
            if(vise[MP(x,y)]) ans+=2;
            else{
                vise[MP(x,y)]=true;
                add(x,y);add(y,x);
            }
        }
        FOR(i,1,n) if(!vis[i]) dfs(i,0);
        printf("%d\n",ans/2);
    }

E

The conversion requires about something: After the cycle shift column, each row selected from a number, find the greatest number of these and.

Simple the DP: \ (F [I] [J] \) representing the forward \ (I \) columns, where \ (J \) These lines have chosen number.

Time complexity \ (O (2M + 2 ^ NN ^ ^ nm. 3) \) . By E1.

For E2 of, found by the maximum value of each column to sort, when \ (m> n \) , the only need to consider the maximum \ (n-\) column.

Proof, consider this \ (n \) optimal solution to the column, which is certainly greater than or equal \ (n \) column maximum sum (and these \ (n \) column by a maximum arranged in a row each row is definitely the optimal solution). Which will replace the one that is proportional to \ (n \) after the maximum value of the column are smaller column, regardless of how the cycle shift this column, the election not as a number of other selected a number on a column on this column.

Time complexity \ (O (^ 2 ^. 3 NN + NN ^. 3) \) .

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> PII;
    const int maxn=100010;
    #define MP make_pair
    #define PB push_back
    #define lson o<<1,l,mid
    #define rson o<<1|1,mid+1,r
    #define FOR(i,a,b) for(int i=(a);i<=(b);i++)
    #define ROF(i,a,b) for(int i=(a);i>=(b);i--)
    #define MEM(x,v) memset(x,v,sizeof(x))
    inline ll read(){
        char ch=getchar();ll x=0,f=0;
        while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
        while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
        return f?-x:x;
    }
    int t,n,m,a[22][22],b[22][2222],mx[2222],p[2222],sum[22][4444],f[22][4444];
    bool cmp(int x,int y){return mx[x]>mx[y];}
    int main(){
        t=read();
        while(t--){
            n=read();m=read();
            FOR(i,0,n-1) FOR(j,0,m-1) b[i][j]=read();
            FOR(i,0,m-1){
                mx[i]=0;
                FOR(j,0,n-1) mx[i]=max(mx[i],b[j][i]);
            }
            FOR(i,0,m-1) p[i]=i;
            sort(p,p+m,cmp);
            FOR(i,0,min(n,m)-1) FOR(j,0,n-1) a[j][i]=b[j][p[i]];
            FOR(i,0,min(n,m)-1) FOR(S,0,(1<<n)-1){
                sum[i][S]=0;
                FOR(j,0,n-1){
                    int s=0;
                    FOR(k,0,n-1) if((S>>k)&1) s+=a[(j+k)%n][i];
                    sum[i][S]=max(sum[i][S],s);
                }
            }
            FOR(S,0,(1<<n)-1) f[0][S]=sum[0][S];
            FOR(i,1,min(n,m)-1) FOR(S,0,(1<<n)-1){
                f[i][S]=f[i-1][S];
                for(int S0=S;S0;S0=(S0-1)&S) f[i][S]=max(f[i][S],f[i-1][S^S0]+sum[i][S0]);
            }
            printf("%d\n",f[min(n,m)-1][(1<<n)-1]);
        }
    }

G1

Sb legend is a problem? Over ......

First divided into several blocks, each color in exactly one block, and the block as much as possible. Apparently only one program can be greedy and do. It will become a color within such blocks, and each block independently of each other. For a block, except for the highest number of color appear, the other must become that color.

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> PII;
    const int maxn=222222;
    #define MP make_pair
    #define PB push_back
    #define lson o<<1,l,mid
    #define rson o<<1|1,mid+1,r
    #define FOR(i,a,b) for(int i=(a);i<=(b);i++)
    #define ROF(i,a,b) for(int i=(a);i>=(b);i--)
    #define MEM(x,v) memset(x,v,sizeof(x))
    inline ll read(){
        char ch=getchar();ll x=0,f=0;
        while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
        while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
        return f?-x:x;
    }
    int n,q,a[maxn],mn[maxn],l,r,tmp[maxn],tl,cnt[maxn],ans;
    int calc(){
        tl=0;
        FOR(i,l,r) if(++cnt[a[i]]==1) tmp[++tl]=a[i];
        FOR(i,1,tl) tmp[i]=cnt[tmp[i]];
        sort(tmp+1,tmp+tl+1);
        int s=0;
        FOR(i,1,tl-1) s+=tmp[i];
        FOR(i,l,r) cnt[a[i]]--;
        return s;
    }
    int main(){
        n=read();q=read();
        FOR(i,1,n){
            a[i]=read();
            if(!mn[a[i]]) mn[a[i]]=i;
        }
        int curmn=1e9;
        r=n;
        ROF(i,n,1){
            curmn=min(curmn,mn[a[i]]);
            if(i==curmn){
                curmn=1e9;
                l=i;
                ans+=calc();
                r=i-1;
            }
        }
        printf("%d\n",ans);
        while(q--) puts("PB AK ZROI");
    }

The rest, the cushions.

Guess you like

Origin www.cnblogs.com/1000Suns/p/11537177.html