zoukankan      html  css  js  c++  java
  • [Codeforces] Round #354 (Div. 2)(E题艹不出)

    A题题意:给定一个1到n的排列,可以交换其中某一对数,求1与n的距离最大值

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int main(){
     4     int n,x;
     5     scanf("%d",&n);
     6     int Min,Max;
     7     for(int i=1;i<=n;i++){
     8         scanf("%d",&x);
     9         if(x==1)Min=i;
    10         if(x==n)Max=i;
    11     }
    12     if(Min<Max)printf("%d
    ",max(Max-1,n-Min));
    13     else printf("%d
    ",max(Min-1,n-Max));
    14     return 0;
    15 }
    View Code

    B题题意:一堆摞好的高脚杯,往里倒酒,每个单位时间可以倒满一杯,问一共n层杯子,m个时间可以倒满多少个杯子

    题解:加边大模拟

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define maxn 5055
     4 double val[maxn];
     5 int main(){
     6     int n,m;
     7     scanf("%d%d",&n,&m);
     8     int ans=0,cnt=0;
     9     val[1]=m;
    10     for(int i=1;i<=n;i++){
    11         for(int j=1;j<=i;j++)
    12             if(val[cnt+j]>=1){
    13                 ans++;
    14                 double x=(val[cnt+j]-1)/2;
    15                 val[cnt+j]=1,val[cnt+j+i]+=x,val[cnt+j+i+1]+=x;
    16             }
    17         cnt+=i;
    18     }
    19     printf("%d
    ",ans);
    20     return 0;
    21 }
    View Code

    C题题意:给定一个只包含a,b的字符串,每次可以把一个a变成b或把一个b变成a,求改变k次后的串只包含a或b的子串长度的最大值

    题解:对于a和b,分别维护长度为k+2的队列

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define maxn 1000005
     4 int n,k,ans,qa[maxn],qb[maxn],ha,ta,hb,tb;
     5 char s[maxn];
     6 void inserta(int x){
     7     qa[++ta]=x;
     8     while(ha<ta&&ta-ha+1>k+2)++ha;
     9     ans=max(ans,qa[ta]-qa[ha]-1);
    10 }
    11 void insertb(int x){
    12     qb[++tb]=x;
    13     while(hb<tb&&tb-hb+1>k+2)++hb;
    14     ans=max(ans,qb[tb]-qb[hb]-1);
    15 }
    16 int main(){
    17     scanf("%d%d",&n,&k);
    18     scanf("%s",s+1);
    19     for(int i=1;i<=n;i++){
    20         if(s[i]=='a')inserta(i);
    21         else insertb(i);
    22     }
    23     inserta(n+1),insertb(n+1);
    24     printf("%d
    ",ans);
    25     return 0;
    26 }
    View Code

    D题题意:水管工,,,自行破隔膜

    题解:建图bfs

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define fir first
     4 #define sec second
     5 #define mp make_pair
     6 #define maxn 1005
     7 char s[maxn];
     8 int n,m,poi[maxn][maxn][4][4],vis[maxn][maxn][4];
     9 typedef pair<pair<int,int>,int>pii;
    10 queue<pii>Q;
    11 int dir[4][2]={{0,-1},{-1,0},{0,1},{1,0}};
    12 void bfs(){
    13     int sx,sy,tx,ty;
    14     scanf("%d%d%d%d",&sx,&sy,&tx,&ty);
    15     vis[sx][sy][0]=1;
    16     Q.push(mp(mp(sx,sy),0));
    17     int ans=0;
    18     while(!Q.empty()){
    19         pii tmp=Q.front(); Q.pop();
    20         int x=tmp.fir.fir,y=tmp.fir.sec,d=tmp.sec;
    21         if(x==tx&&y==ty){
    22             ans=vis[tx][ty][d];
    23             break;
    24         }
    25         if(!vis[x][y][(d+1)%4]){
    26             vis[x][y][(d+1)%4]=vis[x][y][d]+1;
    27             Q.push(mp(mp(x,y),(d+1)%4));
    28         }
    29         for(int i=0;i<4;i++){
    30             int xx=x+dir[i][0],yy=y+dir[i][1];
    31             if(xx<1||xx>n||yy<1||yy>m)continue;
    32             if(vis[xx][yy][d])continue;
    33             if(poi[x][y][d][i]&&poi[xx][yy][d][(i+2)%4]){
    34                 vis[xx][yy][d]=vis[x][y][d]+1;
    35                 Q.push(mp(mp(xx,yy),d));
    36             }
    37         }
    38     }
    39     printf("%d
    ",ans-1);
    40 }
    41 int main(){
    42     scanf("%d%d",&n,&m);
    43     for(int i=1;i<=n;i++){
    44         scanf("%s",s+1);
    45         for(int j=1;j<=m;j++){
    46             if(s[j]=='+')poi[i][j][0][0]=poi[i][j][0][1]=poi[i][j][0][2]=poi[i][j][0][3]=1;
    47             if(s[j]=='-')poi[i][j][0][0]=poi[i][j][0][2]=1;
    48             if(s[j]=='|')poi[i][j][0][1]=poi[i][j][0][3]=1;
    49             if(s[j]=='^')poi[i][j][0][1]=1;
    50             if(s[j]=='>')poi[i][j][0][2]=1;
    51             if(s[j]=='<')poi[i][j][0][0]=1;
    52             if(s[j]=='v')poi[i][j][0][3]=1;
    53             if(s[j]=='L')poi[i][j][0][1]=poi[i][j][0][2]=poi[i][j][0][3]=1;
    54             if(s[j]=='R')poi[i][j][0][0]=poi[i][j][0][1]=poi[i][j][0][3]=1;
    55             if(s[j]=='U')poi[i][j][0][0]=poi[i][j][0][2]=poi[i][j][0][3]=1;
    56             if(s[j]=='D')poi[i][j][0][0]=poi[i][j][0][1]=poi[i][j][0][2]=1;
    57         }
    58     }
    59     for(int i=1;i<=n;i++)
    60         for(int j=1;j<=m;j++)
    61             for(int k=1;k<4;k++)
    62                 for(int l=0;l<4;l++)
    63                     poi[i][j][k][(l+k)%4]=poi[i][j][0][l];
    64     bfs();
    65     return 0;
    66 }
    View Code
  • 相关阅读:
    04-JQuery
    03-JavaScript
    02-CSS&JS
    01-HTML
    [LeetCode]Insert Interval
    [shell编程]正则表达式
    [LeetCode]Jump Game II
    [LeetCode]Jump Game
    [LeetCode]Wildcard Matching
    [shell编程]初识sed和gawk
  • 原文地址:https://www.cnblogs.com/Ngshily/p/5543012.html
Copyright © 2011-2022 走看看