CodeForces 14 problem solution

A

Simulation title

Thinking

* First find, and then find the boundary, the final output. Then find the minimum boundary should be assigned a maximum value

Code

char ch[101][101],c;
int p,q,x,y;
signed main(){
    int n=read(),m=read();
    p=x=0x3f;
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++){
        cin>>c;
        ch[i][j]=c;
        if(c=='*'){
            if(i<=p) p=i;
            if(j<=x) x=j;
            if(i>=q) q=i;
            if(j>=y) y=j;
        }
    }
    for(int i=p;i<=q;i++){
        for(int j=x;j<=y;j++)
        printf("%c",ch[i][j]);
        puts("");
    }
    return 0;
}

B

Thinking

Violence can enumerate

Code


int a[5005],ans=inf;
signed main(){
    int n=read(),x0=read();
    for(int i=0;i<n;i++){
        int x=read(),y=read();
        if(x>y) swap(x,y);//ai可能大于bi
        for(int j=x;j<=y;j++){
            a[j]++;//枚举点
            if(a[j]==n)
                if(abs(j-x0)<ans) ans=abs(j-x0);//找最大距离
        }   
    } 
    if(ans==inf) ans=-1;
    printf("%d\n",ans);
    return 0;
}

C

Thinking

For each point and only one point to see if it is the same
if it is to see whether the input is four sides and wherein two (X \) \ parallel axis and the other two \ (Y \) parallel to the axis

Code

struct Node{
    int x,y;
}p[10];
signed main(){
    for(int i=0;i<8;i++) scanf("%d%d",&p[i].x,&p[i].y);
    bool flag=true;
    for(int i=0;i<8;i++){
        int cnt=0;
        for(int j=0;j<8;j++ ) 
        if(p[i].x==p[j].x&&p[i].y==p[j].y) cnt++;
        if(cnt!=2){flag=false;break;}
    }
    if(flag==false) return puts("NO"),0;
    int tx=0,ty=0;
    for(int i=0;i<4;i++){
      if(p[i*2].x==p[i*2+1].x&&p[i*2].y!=p[i*2+1].y) tx++;
      if(p[i*2].x!=p[i*2+1].x&&p[i*2].y==p[i*2+1].y) ty++;
    }
    if(!(tx==2&&ty==2)) flag=false;
    if(flag) printf("YES");
    else printf("NO");
    return 0;
}

D

Seeking the diameter of the tree, the adjacency matrix used here, \ (DFS \) requirements

Thinking

\ (n-\) points, \ (n-1 \) bar in this figure without seeking the longest chain length of two disjoint to the side,
because there is only \ (n-1 \) edges, so that a certain ring not exist,
we can enumerate each edge, this edge with the FIG divided into two parts,
find the diameter of the respective portions of the tree, then the product,
\ (DFS \) when the recording time and the maximum length of the tree chain length

int dep,n,ch[maxn][maxn];//邻接矩阵 
inline int dfs(int x,int y){
    int maxx=0,maxx1=0,maxpath=0;
    for(int i=1;i<=n;i++){
        if(ch[i][x]&&i!=y){
            int z=dfs(i,x);
            if(maxpath<z) maxpath=z;
            if(maxx<dep){
                maxx1=maxx;
                maxx=dep;
            }
            else if(maxx1<dep) maxx1=dep;
        }
    }
    if(maxpath<maxx+maxx1) maxpath=maxx1+maxx;
    dep=maxx+1;
    return maxpath;
}
signed main(){
    n=read();
    for(int i=1;i<n;i++){
        int x=read(),y=read();
        ch[x][y]=ch[y][x]=1;
    }
    int ans=-1;
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    if(ch[i][j]){
        int a=dfs(i,j);
        int b=dfs(j,i);
        ans=max(ans,a*b);
    }
    printf("%d",ans);
    return 0;
}

E

\ (The DP \)
, but can be optimized to a three-dimensional

Meaning of the questions:

To give you a number \ (N \) , from \ (1 \) to \ (N \) formed \ (T \) crests and \ (t-1 \) a trough, total number of cases.

Ideas:

To break the problem for the sake \ (2 \ times t-1 \) turning point, set \ (dp [x] [y ] [t] \) is the last point when the fall \ ((x, y) \ ) when the appears (t \) \ the total number of a turning point. So there are two cases for the current point, the first point to the current point either his height than his large, or smaller than him.

If the current point \ ((x, y) \ ) appears before the \ (t \) turning point, if \ (t \) is even, that is to say a point just before the impact point below the current point to the current state I want to keep any natural appear only even-numbered \ (T \) where a turning point, then
\ [dp [x] [y ] [t] = \ sum (dp [x-1] [h] [t]) (1 \ le h <y) \]

At the same time a point just before the top falls on the current point, can achieve

\ [dp [x] [y
] [t + 1] = \ sum (dp [x] [h] [t]) (y <h \ le 4) \] When \ (T \) is an odd number, the same of,

\[dp[x][y][t]=\sum (dp[x-1][h][t])(y<h\le 4);\]

\[dp[x][y][t+1]=\sum (dp[x-1][h][t])(1\le h<y)\]

Of course, the beginning of a turning point for certain is rising.

Code

int dp[25][5][25];
signed main(){
    for(int i=1;i<=3;i++) dp[1][i][0]=1;
    for(int x=2;x<22;x++)
    for(int y=1;y<=4;y++)
    for(int t=0;t<21;t++)
    for(int h=1;h<=4;h++){
        if(x==2){
            if(y>h){
            if(t%2) dp[2][y][t+1]+=dp[1][h][t];
            else dp[2][y][t]+=dp[1][h][t];
            }
        }
        else{
            if(t%2){
            if(h>y) dp[x][y][t]+=dp[x-1][h][t];
            else if(h<y) dp[x][y][t+1]+=dp[x-1][h][t];
            }
            else{
            if(h<y) dp[x][y][t]+=dp[x-1][h][t];
            else if(h>y) dp[x][y][t+1]+=dp[x-1][h][t];
            }
        }
    }
    int n=read(),t=read();
    if(t*2+1>n||n>6*t+1) return puts("0"),0;
    int ans=0;
    for(int i=1;i<=4;i++)
    ans+=dp[n][i][2*t-1];
    printf("%d",ans); 
    return 0;
}

Guess you like

Origin www.cnblogs.com/cbyyc/p/11487755.html