Codeforces Round #354 (Div. 2)

Nicholas and Permutation

题意给你一个大小为n的数组,保证数组里的数是1-n.可以任意交换一次位置,求1的位置和n的位置的最大差.

/*************************************************************************
     File Name: cf_A.cpp
     ID: obsoles1
     PROG: 
     LANG: C++ 
     Mail: 384099319@qq.com 
     Created Time: 三  5/25 23:08:09 2016
 ************************************************************************/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cctype>
#include<ctime>
#include<cstdlib>
#include<string>
#include<vector>
#include<set>
#include<bitset>
#define Max(x,y) ((x)>(y)?(x):(y))
#define Min(x,y) ((x)<(y)?(x):(y))
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();++it)
#define Abs(x,y) ((x)>(y)?((x)-(y)):((y)-(x)))
#define ll long long
#define Mem0(x) memset(x,0,sizeof(x))
#define Mem1(x) memset(x,-1,sizeof(x))
#define MemX(x) memset(x,0x3f,sizeof(x))
#define pb push_back
using namespace std;
const int N=110;
int pos[N];

int main(){
  int n,x,i;
  scanf("%d",&n);
  for(i=1;i<=n;++i){
    scanf("%d",&x);
    pos[x]=i;
  }
  int ans;
  if(pos[1]==1 || pos[1]==n || pos[n]==1 || pos[n]==n)ans=n-1;
  else ans=Max(Max(Abs(pos[1],n),Abs(pos[1],1)),Max(Abs(pos[n],n),Abs(pos[n],1)));
  //if(ans<0)ans=-ans;
  //ans++;
  //cout<<"ans="<<ans<<endl;
  //if((pos[1]!=1 && pos[1]!=n) || (pos[n]!=1 && pos[n]!=n))ans++;
  printf("%d\n",ans);
}



Pyramid of Glasses

题意宴会上金字塔酒杯,给出n层数,t时间.第一秒能灌满顶层的杯子,之后每过1s,溢出的部分往下流.问t时间后灌满的杯子有几个.

/*************************************************************************
     File Name: cf_B.cpp
     ID: obsoles1
     PROG: 
     LANG: C++ 
     Mail: 384099319@qq.com 
     Created Time: 三  5/25 23:52:58 2016
 ************************************************************************/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cctype>
#include<ctime>
#include<cstdlib>
#include<string>
#include<vector>
#include<set>
#include<bitset>
#define Max(x,y) ((x)>(y)?(x):(y))
#define Min(x,y) ((x)<(y)?(x):(y))
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();++it)
#define Abs(x,y) ((x)>(y)?((x)-(y)):((y)-(x)))
#define ll long long
#define Mem0(x) memset(x,0,sizeof(x))
#define Mem1(x) memset(x,-1,sizeof(x))
#define MemX(x) memset(x,0x3f,sizeof(x))
#define pb push_back
using namespace std;
const int N=15;
int bot[N][N];
bool f[N][N];

int main(){
  int n,t,i,j,ans;
  scanf("%d%d",&n,&t);
  ans=0;
  bot[1][1]=2048*t;
  for(i=1;i<=n;++i){
    for(j=1;j<=i;++j){
      //cout<<"t="<<k<<endl;
      if(bot[i][j]>=2048){
        bot[i+1][j]+=(bot[i][j]-2048)/2;
        bot[i+1][j+1]+=(bot[i][j]-2048)/2;
        ans++;
        //cout<<"bot["<<i+1<<"]["<<j<<"]="<<bot[i+1][j]<<endl;
        //cout<<"bot["<<i+1<<"]["<<j+1<<"]="<<bot[i+1][j+1]<<endl;
      }
    }
  }
  printf("%d\n",ans);
}




Vasya and String

题意给一个只含有a,b的字符串,给你为n长度的字符串,可以翻转m个位置(即a->b,b->a),问最长相同的字母的连续子串长度.

/*************************************************************************

     File Name: cf_C.cpp
     ID: obsoles1
     PROG: 
     LANG: C++ 
     Mail: 384099319@qq.com 
     Created Time: 三  5/25 23:32:36 2016
 ************************************************************************/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cctype>
#include<ctime>
#include<cstdlib>
#include<string>
#include<vector>
#include<set>
#include<bitset>
#define Max(x,y) ((x)>(y)?(x):(y))
#define Min(x,y) ((x)<(y)?(x):(y))
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();++it)
#define Abs(x,y) ((x)>(y)?((x)-(y)):((y)-(x)))
#define ll long long
#define Mem0(x) memset(x,0,sizeof(x))
#define Mem1(x) memset(x,-1,sizeof(x))
#define MemX(x) memset(x,0x3f,sizeof(x))
#define pb push_back
using namespace std;
const int N=1e5+10;
char s[N];
int pre_a[N],pre_b[N];

int main(){
  int n,i,k;
  scanf("%d%d%s",&n,&k,s);
  pre_a[0]=pre_b[0]=0;
  for(i=1;i<=n;++i){
    if(!(s[i-1]-'a')){
      pre_a[i]=pre_a[i-1]+1;
      pre_b[i]=pre_b[i-1];
    }else{
      pre_a[i]=pre_a[i-1];
      pre_b[i]=pre_b[i-1]+1;
    }
  }
  int low,high,mid,ans=0;
  for(i=0;i<=n;++i){
    low=i,high=n;
    while(low<=high){
      mid=(low+high)>>1;
      if(pre_a[mid]-pre_a[i]>k && pre_b[mid]-pre_b[i]>k)high=mid-1;
      else low=mid+1;
    }
    ans=Max(ans,high-i);
  }
  printf("%d\n",ans);
}




Theseus and labyrinth

题意给出一个n*m的地图,每个格子代表一个房间,规定房间上哪几个方向有门,之后要想走到相邻房间,该房间必须也要有通向当前房间的门,也可以将地图上各个房间顺时针旋转90度。

/*************************************************************************

     File Name: cf_D.cpp
     ID: obsoles1
     PROG: 
     LANG: C++ 
     Mail: 384099319@qq.com 
     Created Time: 二  5/31 21:56:49 2016
 ************************************************************************/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cctype>
#include<ctime>
#include<cstdlib>
#include<string>
#include<vector>
#include<set>
#include<bitset>
#define Max(x,y) ((x)>(y)?(x):(y))
#define Min(x,y) ((x)<(y)?(x):(y))
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();++it)
#define Abs(x,y) ((x)>(y)?((x)-(y)):((y)-(x)))
#define ll long long
#define Mem0(x) memset(x,0,sizeof(x))
#define Mem1(x) memset(x,-1,sizeof(x))
#define MemX(x) memset(x,0x3f,sizeof(x))
#define pb push_back
using namespace std;
const int N=1010;
char mp[4][N][N];
struct node{
  int step,x,y,k;
}s,t;
queue<node> q;
bool vis[4][N][N];
int n,m,sx,sy,ex,ey,mx[]={0,0,1,-1},my[]={1,-1,0,0};

char change(char x){
  if(x=='-')return '|';
  if(x=='|')return '-';
  if(x=='^')return '>';
  if(x=='>')return 'v';
  if(x=='v')return '<';
  if(x=='<')return '^';
  if(x=='L')return 'U';
  if(x=='U')return 'R';
  if(x=='R')return 'D';
  if(x=='D')return 'L';
  return x;
}

bool left(char x){
  if(x=='-' || x=='>' || x=='L' || x=='U' || x=='D' || x=='+')return 1;
  return 0;
}

bool right(char x){
  if(x=='-' || x=='<' || x=='R' || x=='U' || x=='D' || x=='+')return 1;
  return 0;
}

bool up(char x){
  if(x=='|' || x=='v' || x=='L' || x=='R' || x=='U' || x=='+')return 1;
  return 0;
}

bool down(char x){
  if(x=='|' || x=='^' || x=='L' || x=='R' || x=='D' || x=='+')return 1;
  return 0;
}

bool deal(node s,node t){
  char sw=mp[s.k][s.x][s.y],tw=mp[t.k][t.x][t.y];
  //cout<<"sw="<<sw<<" tw="<<tw<<endl;
  if(sw=='+'){
    if(s.x-1==t.x)return up(tw);
    if(s.x+1==t.x)return down(tw);
    if(s.y-1==t.y)return left(tw);
    if(s.y+1==t.y)return right(tw);
  }else if(sw=='-'){
    if(s.y-1==t.y)return left(tw);
    if(s.y+1==t.y)return right(tw);
  }else if(sw=='|'){
    if(s.x-1==t.x)return up(tw);
    if(s.x+1==t.x)return down(tw);
  }else if(sw=='^' && s.x-1==t.x)
    return up(tw);
  else if(sw=='v' && s.x+1==t.x)
    return down(tw);
  else if(sw=='>' && s.y+1==t.y)
    return right(tw);
  else if(sw=='<' && s.y-1==t.y)
    return left(tw);
  else if(sw=='L'){
    if(s.x-1==t.x)return up(tw);
    if(s.x+1==t.x)return down(tw);
    if(s.y+1==t.y)return right(tw);
  }else if(sw=='R'){
    if(s.x-1==t.x)return up(tw);
    if(s.x+1==t.x)return down(tw);
    if(s.y-1==t.y)return left(tw);
  }else if(sw=='U'){
    if(s.x+1==t.x)return down(tw);
    if(s.y-1==t.y)return left(tw);
    if(s.y+1==t.y)return right(tw);
  }else if(sw=='D'){
    if(s.x-1==t.x)return up(tw);
    if(s.y-1==t.y)return left(tw);
    if(s.y+1==t.y)return right(tw);
  }
  return 0;
}

void bfs(){
  Mem0(vis);
  while(!q.empty())q.pop();
  s.x=sx,s.y=sy,s.step=0,s.k=0;
  vis[0][sx][sy]=1;
  q.push(s);
  while(!q.empty()){
    s=q.front();
    //cout<<"s("<<s.x<<','<<s.y<<")   step="<<s.step<<"   k="<<s.k<<endl;
    q.pop();
    if(s.x==ex && s.y==ey){
      printf("%d\n",s.step);
      return;
    }
    for(int i=0;i<4;++i){
      t=s;
      t.x+=mx[i],t.y+=my[i];
      t.step++;
      if(t.x<0 || t.x>=n || t.y<0 || t.y>=m || vis[t.k][t.x][t.y] || mp[t.k][t.x][t.y]=='*')continue;
      if(deal(s,t)){
        q.push(t);
        vis[t.k][t.x][t.y]=1;
      }
    }
    t=s;
    t.k=(t.k+1)%4;
    t.step++;
    if(vis[t.k][t.x][t.y])continue;
    vis[t.k][t.x][t.y]=1;
    q.push(t);
  }
  puts("-1");
}

int main(){
  int i,j,k;
  scanf("%d%d",&n,&m);
  for(i=0;i<n;++i){
    scanf("%s",mp[0][i]);
    for(k=1;k<4;++k){
      for(j=0;j<m;++j)
        mp[k][i][j]=change(mp[k-1][i][j]);
    }
  }
  scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
  sx--,sy--,ex--,ey--;
  bfs();
}

 



The Last Fight Between Human and AI



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值