Codeforces Round 607 Div2

round 607

A

The meaning of problems

Which language from the judgment in accordance with the suffix

Thinking

Water problem

Code

#include<bits/stdc++.h>

using namespace std;
const int MAX=1e3+3;

char s[MAX];

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%s",s);
        int len=strlen(s);
        if(s[len-2]=='p'&&s[len-1]=='o')
        {
            printf("FILIPINO\n");
            continue;
        }
        if(s[len-4]=='d'&&s[len-3]=='e'&&s[len-2]=='s'&&s[len-1]=='u')
        {
            printf("JAPANESE\n");
            continue;
        }
        if(s[len-4]=='m'&&s[len-3]=='a'&&s[len-2]=='s'&&s[len-1]=='u')
        {
            printf("JAPANESE\n");
            continue;
        }
        if(s[len-5]=='m'&&s[len-4]=='n'&&s[len-3]=='i'&&s[len-2]=='d'&&s[len-1]=='a')
        {
            printf("KOREAN\n");
            continue;
        }
    }
}

B

The meaning of problems

To two strings a, b , a possible pair of characters of any exchange (exchange only once), he asked whether such a lexicographically less than b

Thinking

Greedy, the smaller the better character before the processing at the suffix min , from front to back and then determines whether enough exchanged. This must be the best, after switching over to compare the output.

Code

#include<bits/stdc++.h>

using namespace std;
const int MAX=5e3+3;

char s[MAX],c[MAX],minn[MAX];
int index[MAX];

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%s%s",s,c);
        int lens=strlen(s);
        minn[lens-1]=s[lens-1];
        index[lens-1]=lens-1;
        for(int i=lens-2; i>0; i--)
        {
            index[i]=index[i+1];
            minn[i]=minn[i+1];
            if(s[i]<minn[i+1])
            {
                index[i]=i;
                minn[i]=s[i];
            }
        }
        for(int i=0; i<lens-1; i++)
            if(s[i]>minn[i+1])
            {
                swap(s[i],s[index[i+1]]);
                break;
            }
        if(strcmp(s,c)<0)
            printf("%s\n",s);
        else
            printf("---\n");
    }
}

C

The meaning of problems

To a string, a 1,2,3 composition. Then simulate a cursor, the cursor moves to the right one for each operation, and then cut into strings around the cursor sl and sr , then sr at sl paste after [ sl last bit] times. Operation Q x after time length of this string, and outputs 1e9 + 7 mod.

Thinking

Looks like is to find the law, but in fact simulated violence on the line. Because every action is only a move to the right, so the characters do not need to record a total of more than x months. Therefore analog complexity O (X) .

Code

#include<bits/stdc++.h>

using namespace std;
const int MAX=2e6+6;
const int mod=1e9+7;

char s[MAX];

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int x;
        scanf("%d%s",&x,s+1);
        int len=strlen(s+1);
        int res=len;
        for(int i=1; i<=x; i++)
        {
            if(s[i]=='1')continue;
            int cnt=s[i]-'1';
            int cur=(res-i+mod)%mod;
            if(len<x)
            {
                len+=cur;
                for(int j=1;j<=cnt;j++)
                    for(int k=i+1; k<=len&&k+j*cur<=x; k++)
                        s[k+j*cur]=s[k];
            }
            res=((res+cur*cnt*1ll)%mod+mod)%mod;
        }
        printf("%d\n",(res+mod)%mod);
    }
}

D

The meaning of problems

To a character matrix, the A, P configuration. Each operation may be selected from 1 × K segment or K × 1 period, and to extend any length down or left and right, so that the period of the extended period has become selected and as required minimum number of times the entire matrix can be whole becomes A .

Thinking

Obviously, if the answer exists, the [0,4] between. Then press the line judge then look like a column, the first sentence is not the whole row / column, then the sentence is not ............ edge computing answers take min can be.

Code

#include<bits/stdc++.h>

using namespace std;
const int MAX=66;

char G[MAX][MAX];

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n,m,res=MAX,cnt=0;
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; i++)
            scanf("%s",G[i]);
        for(int i=0; i<n; i++)
        {
            bool flag1=1,flag2=0;
            for(int j=0; j<m; j++)
            {
                flag1&=(G[i][j]=='A');
                flag2|=(G[i][j]=='A');
                cnt+=(G[i][j]=='A');
            }
            if(flag1)
            {
                if(i==0||i==n-1)
                    res=min(res,1);
                else
                    res=min(res,2);
            }
            else
            {
                if(!flag2)continue;
                if(G[i][0]=='A'||G[i][m-1]=='A')
                {
                    if(i==0||i==n-1)
                        res=min(res,2);
                    else
                        res=min(res,3);
                }
                else
                {
                    if(i==0||i==n-1)
                        res=min(res,3);
                    else
                        res=min(res,4);
                }
            }
        }
        if(cnt==n*m)
        {
            printf("0\n");
            continue;
        }
        for(int j=0; j<m; j++)
        {
            bool flag1=1,flag2=0;
            for(int i=0; i<n; i++)
            {
                flag1&=(G[i][j]=='A');
                flag2|=(G[i][j]=='A');
            }
            if(flag1)
            {
                if(j==0||j==m-1)
                    res=min(res,1);
                else
                    res=min(res,2);
            }
            else
            {
                if(!flag2)continue;
                if(G[0][j]=='A'||G[n-1][j]=='A')
                {
                    if(j==0||j==m-1)
                        res=min(res,2);
                    else
                        res=min(res,3);
                }
                else
                {
                    if(j==0||j==m-1)
                        res=min(res,3);
                    else
                        res=min(res,4);
                }
            }
        }
        if(res==MAX)
            printf("MORTAL\n");
        else
            printf("%d\n",res);
    }
}

E

The meaning of problems

To a weighted tree, 2K nodes, the k on this person distribution node of the tree, each distance for both human and maximum and minimum values of the allocation request.

Thinking

It is conceivable that a maximum of two people each will as far as possible, as close to the minimum two people. So first find the center of gravity of the tree denoted as RT . It can be considered rt tree is divided into left and right sections (not necessarily the two sub-tree), a difference of two nodes 1 . Consider this model.

The maximum is paired with all the right to the left, not paired with their own part, you can find the maximum value that all the nodes to rt distance and.

The minimum value of each part is self-sufficient as possible. You can think of: from rt starting traversing, if the number of child nodes of the tree is even, even better than a certain self-sufficiency edge to rt , so even the right side of this sub-tree must not contribute. If the sub-tree nodes is odd, then it must not be self-sufficient, then the sub-tree must be connected to rt on. Then the problem can be broken down into sub-problems. Each sub-question seeking a node x contribution of all sub-trees. Border is a leaf node, leaf nodes must be connected to the parent node, also in line with the parity law.

Code

#include<bits/stdc++.h>

using namespace std;
const int MAX=2e5+5;
typedef long long ll;

struct Edge
{
    int to,val,nxt;
}edges[MAX<<1];
int head[MAX],siz[MAX],mx_son_siz[MAX],n,tot,rt;
ll minn,maxx;

inline void addedge(int u,int v,int w)
{
    edges[tot].to=v;
    edges[tot].val=w;
    edges[tot].nxt=head[u];
    head[u]=tot++;
}

void getroot(int x,int fa)
{
    siz[x]=1;
    mx_son_siz[x]=0;
    for(int i=head[x];i!=-1;i=edges[i].nxt)
    {
        int v=edges[i].to;
        if(v==fa)continue;
        getroot(v,x);
        siz[x]+=siz[v];
        mx_son_siz[x]=max(mx_son_siz[x],siz[v]);
    }
    mx_son_siz[x]=max(mx_son_siz[x],n-siz[x]);
    if(!rt||mx_son_siz[x]<mx_son_siz[rt])
        rt=x;
}

void dfs(int x,int fa,ll sum,int w)
{
    siz[x]=1;
    for(int i=head[x];i!=-1;i=edges[i].nxt)
    {
        int v=edges[i].to;
        if(v==fa)continue;
        dfs(v,x,sum+edges[i].val,edges[i].val);
        siz[x]+=siz[v];
    }
    if(siz[x]&1)
        minn+=w;
    maxx+=sum;
}

void init()
{
    for(int i=1;i<=n;i++)
        head[i]=-1;
    rt=tot=0;
    minn=maxx=0ll;
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%d",&n);
        n<<=1;
        init();
        for(int i=1;i<n;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
            addedge(v,u,w);
        }
        getroot(1,-1);
        dfs(rt,-1,0,0);
        printf("%I64d %I64d\n",minn,maxx);
    }
}

Guess you like

Origin www.cnblogs.com/cryingrain/p/12348072.html