Codeforces Round #354 (Div. 2)

原创 2016年06月01日 01:07:08

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



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Codeforces Round #354 (Div. 2) D.Theseus and labyrinth(BFS)

题目链接:http://codeforces.com/contest/676/problem/D D. Theseus and labyrinth time limit per t...

【Codeforces Round 354 (Div 2)E】【数学 多项式除法 讨论】The Last Fight Between Human and AI 多项式除以x-k是否值整除

E. The Last Fight Between Human and AI time limit per test 1 second memory limit per test 256 me...

Codeforces Round #354 (Div. 2)B.Pyramid of Glasses(思维题)

题目链接:http://codeforces.com/contest/676/problem/B B. Pyramid of Glasses time limit per ...

Codeforces Round #354 (Div. 2) B. Pyramid of Glasses (模拟)

B. Pyramid of Glassestime limit per test 1 second memory limit per test 256 megabytes input sta...

Codeforces Round #354 (Div. 2) A Nicholas and Permutation(水题)

http://codeforces.com/problemset/problem/676/A A. Nicholas and Permutation time limit per ...

Codeforces Round #354 (Div. 2) C Vasya and String

Codeforces Round #354 (Div. 2) C Vasya and String time limit per test 1 second memory limit...

Codeforces Round #354 (Div. 2) D (BFS)

D. Theseus and labyrinth time limit per test 3 seconds memory limit per test 256 megabyt...

Codeforces Round #354 (Div. 2) A (水题)

A. Nicholas and Permutation time limit per test 1 second memory limit per test 256 megab...

【Codeforces Round 354 (Div 2)A】【水题 贪心】Nicholas and Permutation 恰好交换一次的pos[n]-pos[1]

A. Nicholas and Permutation time limit per test 1 second memory limit per test 256 megab...

【Codeforces Round 354 (Div 2)C】【前缀和二分or双指针】Vasya and String ab序列最多改变k位置的最长同字符子串长度

C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codeforces Round #354 (Div. 2)
举报原因:
原因补充:

(最多只允许输入30个字)