Codeforces Round #354 (Div. 2) ABCD

Codeforces Round #354 (Div. 2)

Problems

# Name    
A

Nicholas and Permutation

standard input/output

1 s, 256 MB

   x3384
B

Pyramid of Glasses

standard input/output

1 s, 256 MB

   x1462
C

Vasya and String

standard input/output

1 s, 256 MB

   x1393
D

Theseus and labyrinth

standard input/output

3 s, 256 MB

   x375
E

The Last Fight Between Human and AI

standard input/output

1 s, 256 MB

   x58

Complete problemset

Nicholas and Permutation

题意:允许你交换一次任意两个数位置,要使最大的数和最小的数的距离最大,输出最大值

题解:

大水题,就先找到最小的和最大的,把其中一个交换到尽头。

代码:

 1 //#pragma comment(linker, "/STACK:102400000,102400000")
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<iostream>
 5 #include<cstring>
 6 #include<algorithm>
 7 #include<cmath>
 8 #include<map>
 9 #include<set>
10 #include<stack>
11 #include<queue>
12 using namespace std;
13
14 #define MZ(array) memset(array, 0, sizeof(array))
15 #define MF1(array) memset(array, -1, sizeof(array))
16 #define MINF(array) memset(array, 0x3f, sizeof(array))
17 #define REP(i,n) for(i=0;i<(n);i++)
18 #define FOR(i,x,n) for(i=(x);i<=(n);i++)
19 #define ROF(i,x,y) for(i=(x);i>=(y);i--)
20 #define RD(x) scanf("%d",&x)
21 #define RD2(x,y) scanf("%d%d",&x,&y)
22 #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
23 #define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
24 #define WN(x) printf("%d\n",x);
25 #define RE  freopen("D.in","r",stdin)
26 #define WE  freopen("huzhi.txt","w",stdout)
27 #define MP make_pair
28 #define PB push_back
29 #define PF push_front
30 #define PPF pop_front
31 #define PPB pop_back
32 #define lowbit(x) ((x)&(-x))
33 template<class T>inline void OA(const T &a,const int &st,const int &ed) {
34     if(ed>=st)cout<<a[st];
35     int i;
36     FOR(i,st+1,ed)cout<<‘ ‘<<a[i];
37     puts("");
38 }
39 typedef long long LL;
40 typedef unsigned long long ULL;
41 typedef pair<int,int> PII;
42 const double PI=acos(-1.0);
43 const double EPS=1e-10;
44 inline int sgn(double &x) {
45     if(fabs(x) < EPS)return 0;
46     if(x < 0)return -1;
47     else return 1;
48 }
49 const int INF=0x3f3f3f3f;
50 const int NINF=0x80000001;
51 const int MAXN=111111;
52 const int MAXM=33;
53 const int MOD = 1000000007;
54
55
56
57 int main() {
58     int i;
59     int x;
60     int q,w,n;
61     RD(n);
62     REP(i,n){
63         RD(x);
64         if(x==n)q=i;
65         if(x==1)w=i;
66     }
67     if(q>w)swap(q,w);
68     WN(max(n-q-1, w));
69     return 0;
70 }

Pyramid of Glasses

题意:摆了一个高脚杯金字塔,每秒钟往最上面倒一整杯,一杯满了会往两边溢出流到下面两个杯子,求n层高的金字塔,倒水k分钟后,有多少杯是满的。

题解:

直接把k分钟的全倒进最上面的杯子,然后模拟流下去就行。

代码:

 1 //#pragma comment(linker, "/STACK:102400000,102400000")
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<iostream>
 5 #include<cstring>
 6 #include<algorithm>
 7 #include<cmath>
 8 #include<map>
 9 #include<set>
10 #include<stack>
11 #include<queue>
12 using namespace std;
13
14 #define MZ(array) memset(array, 0, sizeof(array))
15 #define MF1(array) memset(array, -1, sizeof(array))
16 #define MINF(array) memset(array, 0x3f, sizeof(array))
17 #define REP(i,n) for(i=0;i<(n);i++)
18 #define FOR(i,x,n) for(i=(x);i<=(n);i++)
19 #define ROF(i,x,y) for(i=(x);i>=(y);i--)
20 #define RD(x) scanf("%d",&x)
21 #define RD2(x,y) scanf("%d%d",&x,&y)
22 #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
23 #define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
24 #define WN(x) printf("%d\n",x);
25 #define RE  freopen("D.in","r",stdin)
26 #define WE  freopen("huzhi.txt","w",stdout)
27 #define MP make_pair
28 #define PB push_back
29 #define PF push_front
30 #define PPF pop_front
31 #define PPB pop_back
32 #define lowbit(x) ((x)&(-x))
33 template<class T>inline void OA(const T &a,const int &st,const int &ed) {
34     if(ed>=st)cout<<a[st];
35     int i;
36     FOR(i,st+1,ed)cout<<‘ ‘<<a[i];
37     puts("");
38 }
39 typedef long long LL;
40 typedef unsigned long long ULL;
41 typedef pair<int,int> PII;
42 const double PI=acos(-1.0);
43 const double EPS=1e-10;
44 inline int sgn(double &x) {
45     if(fabs(x) < EPS)return 0;
46     if(x < 0)return -1;
47     else return 1;
48 }
49 const int INF=0x3f3f3f3f;
50 const int NINF=0x80000001;
51 const int MAXN=111111;
52 const int MAXM=33;
53 const int MOD = 1000000007;
54
55 int n,t;
56
57 int farm(){
58     double a[20][20];
59     int i,j;
60     REP(i,11)REP(j,11)a[i][j]=0.0;
61     a[1][1] = t;
62     int cnt=0;
63     FOR(i,1,n){
64         FOR(j,1,i){
65             if(a[i][j]>1.0){
66                 double t = (a[i][j] - 1.0)/2.0;
67                 a[i+1][j] += t;
68                 a[i+1][j+1] += t;
69                 a[i][j]=1.0;
70             }
71             if(a[i][j]>=1.0)cnt++;
72         }
73     }
74 //    FOR(i,1,n){
75 //        FOR(j,1,i){
76 //            printf("%f ",a[i][j]);
77 //        }
78 //        puts("");
79 //    }
80     return cnt;
81 }
82
83 int main() {
84     int i;
85     RD2(n,t);
86     WN(farm());
87     return 0;
88 }

Vasya and String

题意:给出一个由a和b两个字母组成的字符串,最多修改其中k个,使得其中最长的连续同一字母子串最长,求最长的长度。

题解:

用双端队列,以a为本体扫一遍,再以b为本体扫一遍。以a为本体的时候,扫到b的话,就加到双端队列里,如果双端队列里超过k个,就从左端弹出,保持里面剩k个。记录全a子序列的区间左端,弹出的时候更新 区间左端L = 弹出的元素的位置+1。时刻更新ans = max(ans , R-L+1),R为当前扫到的位置。

只用扫两遍,O(n)。

代码:

 1 //#pragma comment(linker, "/STACK:102400000,102400000")
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<iostream>
 5 #include<cstring>
 6 #include<algorithm>
 7 #include<cmath>
 8 #include<map>
 9 #include<set>
10 #include<stack>
11 #include<queue>
12 using namespace std;
13
14 #define MZ(array) memset(array, 0, sizeof(array))
15 #define MF1(array) memset(array, -1, sizeof(array))
16 #define MINF(array) memset(array, 0x3f, sizeof(array))
17 #define REP(i,n) for(i=0;i<(n);i++)
18 #define FOR(i,x,n) for(i=(x);i<=(n);i++)
19 #define ROF(i,x,y) for(i=(x);i>=(y);i--)
20 #define RD(x) scanf("%d",&x)
21 #define RD2(x,y) scanf("%d%d",&x,&y)
22 #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
23 #define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
24 #define WN(x) printf("%d\n",x);
25 #define RE  freopen("D.in","r",stdin)
26 #define WE  freopen("huzhi.txt","w",stdout)
27 #define MP make_pair
28 #define PB push_back
29 #define PF push_front
30 #define PPF pop_front
31 #define PPB pop_back
32 #define lowbit(x) ((x)&(-x))
33 template<class T>inline void OA(const T &a,const int &st,const int &ed) {
34     if(ed>=st)cout<<a[st];
35     int i;
36     FOR(i,st+1,ed)cout<<‘ ‘<<a[i];
37     puts("");
38 }
39 typedef long long LL;
40 typedef unsigned long long ULL;
41 typedef pair<int,int> PII;
42 const double PI=acos(-1.0);
43 const double EPS=1e-10;
44 inline int sgn(double &x) {
45     if(fabs(x) < EPS)return 0;
46     if(x < 0)return -1;
47     else return 1;
48 }
49 const int INF=0x3f3f3f3f;
50 const int NINF=0x80000001;
51 const int MAXN=111111;
52 const int MAXM=33;
53 const int MOD = 1000000007;
54
55 int n,k;
56 char s[MAXN];
57
58 int gank(char c){
59     deque<int> d;
60     int l=0;
61     int cnt=0;
62     int re=0;
63     int i;
64     REP(i,n){
65         if(s[i]==c){
66             d.push_back(i);
67             if(cnt==k){
68                 l = d.front() + 1;
69                 d.pop_front();
70             }else cnt++;
71         }
72         re = max(re, i-l+1);
73     }
74     return re;
75 }
76
77 int farm(){
78     return max(gank(‘a‘), gank(‘b‘));
79 }
80
81 int main() {
82     int i;
83     RD2(n,k);
84     scanf(" %s",s);
85     WN(farm());
86     return 0;
87 }

Theseus and labyrinth

题意:给出一个n*m的地图,英雄从(xt,yt)出发,要到达敌人所在地(xm,ym)。地图每个格子有设定:

^>v<代表向箭头方向有门,其他方向没门;

URDL代表某个方向没门,其他方向都有门,例如U,代表上面没门,其他3个方向各有一个门。

-|代表左右有门、上下有门。

+代表4个门,*代表没有门。

每分钟,英雄可以进行一项操作:

A.将所有方块顺时针转90度。

B.移动到相邻方块,要求自己方块到它的方向有门,它的方块到自己的方向也有门。

给出地图、英雄位置、敌人位置,求英雄到达敌人的最少分钟数,或者不能到达输出-1。

题解:

走迷宫,典型的广搜题。广搜每个节点有5种扩展:旋转90度、往4个方向走。注意记录某个点是否走过,需要save[x][y][z],x,y为该点坐标,z为旋转的次数,z<=3。

比较难写的是判断某个点能否向某个方向走一步,可以写到一个函数里can(x,y,z,j),(x,y)为当前坐标,z为当前旋转了几次(0~3),j为方向,我设定方向0123代表上右下左。

方块旋转、门的位置可以预处理,记到一个数组里,例如rot[‘<‘][2] = ‘>‘,代表<符号旋转2次得到>符号。

例如door[‘-‘] = "0101",代表-符号的门开在右、左。

这块写好了就无敌了。

我就写错了一个符号,卧槽,就一直wa6,比赛结束以后发现一个-写成了|,改了就过了。这种题要认真点写啊。

(可恶,刚才看官方题解,哇,直接预处理出4种方向的地图,a[x][y][z],就是简单的三维地图bfs了,卧槽,我怎么没想到)

代码:

  1 //#pragma comment(linker, "/STACK:102400000,102400000")
  2 #include<cstdio>
  3 #include<cmath>
  4 #include<iostream>
  5 #include<cstring>
  6 #include<algorithm>
  7 #include<cmath>
  8 #include<map>
  9 #include<set>
 10 #include<stack>
 11 #include<queue>
 12 using namespace std;
 13
 14 #define MZ(array) memset(array, 0, sizeof(array))
 15 #define MF1(array) memset(array, -1, sizeof(array))
 16 #define MINF(array) memset(array, 0x3f, sizeof(array))
 17 #define REP(i,n) for(i=0;i<(n);i++)
 18 #define FOR(i,x,n) for(i=(x);i<=(n);i++)
 19 #define ROF(i,x,y) for(i=(x);i>=(y);i--)
 20 #define RD(x) scanf("%d",&x)
 21 #define RD2(x,y) scanf("%d%d",&x,&y)
 22 #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
 23 #define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
 24 #define WN(x) printf("%d\n",x);
 25 #define RE  freopen("D.in","r",stdin)
 26 #define WE  freopen("huzhi.txt","w",stdout)
 27 #define MP make_pair
 28 #define PB push_back
 29 #define PF push_front
 30 #define PPF pop_front
 31 #define PPB pop_back
 32 #define lowbit(x) ((x)&(-x))
 33 template<class T>inline void OA(const T &a,const int &st,const int &ed) {
 34     if(ed>=st)cout<<a[st];
 35     int i;
 36     FOR(i,st+1,ed)cout<<‘ ‘<<a[i];
 37     puts("");
 38 }
 39 typedef long long LL;
 40 typedef unsigned long long ULL;
 41 typedef pair<int,int> PII;
 42 const double PI=acos(-1.0);
 43 const double EPS=1e-10;
 44 inline int sgn(double &x) {
 45     if(fabs(x) < EPS)return 0;
 46     if(x < 0)return -1;
 47     else return 1;
 48 }
 49 const int INF=0x3f3f3f3f;
 50 const int NINF=0x80000001;
 51 const int MAXN=1111;
 52 const int MAXM=33;
 53 const int MOD = 1000000007;
 54 const int gx[4] = {-1,0,1,0};
 55 const int gy[4] = {0,1,0,-1};
 56 int n,m;
 57 char s[MAXN][MAXN];
 58 int xt,yt,xm,ym;
 59
 60 int save[MAXN][MAXN][4];
 61 char rota[222][4];
 62 char door[222][5];
 63 void init() {
 64     int i,j;
 65     MF1(save);
 66     REP(i,4) {
 67         rota[‘+‘][i]=‘+‘;
 68         rota[‘*‘][i]=‘*‘;
 69     }
 70     for(i=0; i<4; i+=2) {
 71         rota[‘-‘][0+i]=‘-‘;
 72         rota[‘|‘][0+i]=‘|‘;
 73         rota[‘-‘][1+i]=‘|‘;
 74         rota[‘|‘][1+i]=‘-‘;
 75     }
 76     char q[5] = "^>v<";
 77     char w[5] = "URDL";
 78     REP(i,4) {
 79         REP(j,4) {
 80             int l=(i+j)%4;
 81             rota[q[i]][j] = q[l];
 82             rota[w[i]][j] = w[l];
 83         }
 84     }
 85     REP(i,4) {
 86         door[‘+‘][i]=‘1‘;
 87         door[‘*‘][i]=‘0‘;
 88         door[‘|‘][i]=door[‘-‘][i]=‘0‘;
 89     }
 90     door[‘|‘][0]=door[‘|‘][2] = ‘1‘;
 91     door[‘-‘][1]=door[‘-‘][3] =‘1‘;
 92     REP(i,4)REP(j,4) {
 93         door[q[i]][j]=‘0‘;
 94         door[w[i]][j]=‘1‘;
 95     }
 96     REP(i,4) {
 97         door[q[i]][i]=‘1‘;
 98         door[w[i]][i]=‘0‘;
 99     }
100 }
101
102 char rot(char x, int n) {
103     return rota[x][n%4];
104 }
105
106 bool cango(char now , char next, int j) {
107     int k = (j+2)%4;
108 //    door[now][4]=‘\0‘;
109 //    door[next][4]=‘\0‘;
110 //    printf("%c,%c,%s,%s,%d,%d,%c,%c\n",now,next,door[now],door[next],j,k,door[now][j],door[next][k]);
111     if (door[now][j]==‘1‘ && door[next][k]==‘1‘)return true;
112     else return false;
113 }
114
115 bool can(int x,int y,int z,int j) {
116     int xx=x+gx[j];
117     int yy=y+gy[j];
118     if(xx<0 || xx>=n || yy<0 || yy>=m)return false;
119     char now = s[x][y];
120     char next = s[xx][yy];
121 //    printf("%c,%c %d->",now,next,z);
122     now = rot(now, z);
123     next = rot(next,z);
124 //    printf("%c,%c\n",now,next);
125     if(cango(now,next,j))return true;
126     else return false;
127 }
128
129 struct Node {
130     int x,y,z;
131     int step;
132     Node() {}
133     Node(int _x,int _y,int _z,int _s) {
134         x=_x;
135         y=_y;
136         z=_z;
137         step=_s;
138     }
139 };
140
141 int farm() {
142     deque<Node> d;
143     int i,j;
144     d.clear();
145     d.push_back(Node(xt,yt,0,0));
146     save[xt][yt][0]=0;
147     while(!d.empty()) {
148         Node now = d.front();
149         int x=now.x, y=now.y, z=now.z, step=now.step;
150         d.pop_front();
151 //        if(x==19 && z==1)printf("%d,%d,%d,%d\n",x,y,z,step);
152         if(x==xm && y==ym)return step;
153
154         int k = (z+1)%4;
155         if(save[x][y][k]==-1 || save[x][y][k]>step+1) {
156             d.push_back(Node(x, y, k, step+1));
157             save[x][y][k]=step+1;
158         }
159
160         REP(i,4) {
161             int xx=x+gx[i];
162             int yy=y+gy[i];
163 //            printf("%d,%d,%d,%d,%d,[%d,%d,%d],%d\n",x,y,z,i,can(x,y,z,i),xx,yy,z,save[xx][yy][z]);
164             if(can(x,y,z,i)&&(save[xx][yy][z]==-1 || save[xx][yy][z]>step+1)) {
165                 d.push_back(Node(xx,yy,z,step+1));
166                 save[xx][yy][z]=step+1;
167             }
168         }
169     }
170     return -1;
171 }
172
173 int main() {
174     int i;
175     init();
176     RD2(n,m);
177     REP(i,n)scanf(" %s",s[i]);
178     RD2(xt,yt);
179     RD2(xm,ym);
180     xt--;
181     yt--;
182     xm--;
183     ym--;
184     WN(farm());
185     return 0;
186 }

E比赛时没空看,结束后看了,大概是说一个那样的式子,两个玩家交替操作,要是能使式子P(x) =B(x)Q(x),就算人类胜。其中B(x)是任意一种那样的式子,Q(x)=x-k,k为给出的数。给出当前局面,到人类动,问人类是否有必胜策略。好像有100多人过pre,最后只有五十多个ac,好像有点难,我还是先不看了。

不懂能不能抢到百度之星t恤啊!

时间: 2024-10-06 21:22:11

Codeforces Round #354 (Div. 2) ABCD的相关文章

Codeforces Round #258 (Div. 2)[ABCD]

Codeforces Round #258 (Div. 2)[ABCD] ACM 题目地址:Codeforces Round #258 (Div. 2) A - Game With Sticks 题意: Akshat and Malvika两人玩一个游戏,横竖n,m根木棒排成#型,每次取走一个交点,交点相关的横竖两条木棒要去掉,Akshat先手,给出n,m问谁赢. 分析: 水题,很明显不管拿掉哪个点剩下的都是(n-1,m-1),最后状态是(0,x)或(x,0),也就是拿了min(n,m)-1次,

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #Pi (Div. 2) (ABCD题解)

比赛链接:http://codeforces.com/contest/567 听说Round #Pi的意思是Round #314... A. Lineland Mail time limit per test:3 seconds memory limit per test:256 megabytes All cities of Lineland are located on the Ox coordinate axis. Thus, each city is associated with it

Codeforces Round #354 (Div. 2)

5/5 水了场CF,写个水水地题解~~ 题A CodeForces 676A 题意:给你一个排列,问你交换一次,最大最小位置最远是多少? 题解:暴力交换,暴力算,反正数据小.偷懒被hack更惨!! 1 /*zhen hao*/ 2 #include <bits/stdc++.h> 3 using namespace std; 4 5 #define lson l, m, rt*2 6 #define rson m + 1, r, rt*2+1 7 #define xx first 8 #def

Codeforces Round #250 (Div. 2) (ABCD题解)

比赛链接:http://codeforces.com/contest/433 A. Kitahara Haruki's Gift time limit per test:1 second memory limit per test:256 megabytes Kitahara Haruki has bought n apples for Touma Kazusa and Ogiso Setsuna. Now he wants to divide all the apples between th

Codeforces Round #143 (Div. 2) (ABCD 思维场)

题目连链接:http://codeforces.com/contest/231 A. Team time limit per test:2 seconds memory limit per test:256 megabytes One day three best friends Petya, Vasya and Tonya decided to form a team and take part in programming contests. Participants are usually

Codeforces Round #249 (Div. 2) (ABCD题解)

比赛链接:http://codeforces.com/contest/435 A. Queue on Bus Stop time limit per test:1 second memory limit per test:256 megabytes It's that time of the year when the Russians flood their countryside summer cottages (dachas) and the bus stop has a lot of p

Codeforces Round #313 (Div. 2) (ABCD题解)

比赛链接:http://codeforces.com/contest/560 水笔场... A. Currency System in Geraldion time limit per test:2 seconds memory limit per test:256 megabytes A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several va

Codeforces Round #246 (Div. 2) (ABCD详细题解)

比赛链接:http://codeforces.com/contest/432 A. Choosing Teams time limit per test:1 second memory limit per test:256 megabytes The Saratov State University Olympiad Programmers Training Center (SSU OPTC) has n students. For each student you know the numbe