ACM学习历程—Codeforces Round #354 (Div. 2)
http://codeforces.com/contest/676
在allzysyz学弟和hqwhqwhq的邀请下,打了我的第三场CF。。。
毕竟在半夜。。所以本来想水到12点就去睡觉的。。。结果一下次过了三题,发现第四题是个bfs,就打到了12:30.。。。BC貌似是没有了,不知道后面还会不会有,最近就打CF为主吧。。
A题:
http://codeforces.com/problemset/problem/676/A
应该算是比较水吧。没有什么坑点。直接枚举最大值在最左最右侧和最小值在最左最右侧四种情况。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <string>
#define LL long long using namespace std; int main()
{
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
int n, t, indexone, indexn;
while (scanf("%d", &n) != EOF)
{
for (int i = ; i < n; ++i)
{
scanf("%d", &t);
if (t == ) indexone = i;
if (t == n) indexn = i;
}
printf("%d\n", max(max(indexone, n--indexone), max(indexn, n--indexn)));
}
return ;
}
B题:
http://codeforces.com/problemset/problem/676/B
是ccpc热身赛一题的简化版,首先我可以把所有的酒强行先倒入第一杯,然后让它一层一层往下流。暴力模拟一遍就可以了。上一层的第i杯,会流入下一层的第i杯和第i+1杯。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <string>
#define LL long long using namespace std; double a[][]; int main()
{
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
int n, t, ans;
double tmp;
while (scanf("%d%d", &n, &t) != EOF)
{
bool flag;
ans = ;
a[][] = t;
for (int i = ; i <= n; ++i)
{
memset(a[(i+)%], , sizeof(a[(i+)%]));
flag = true;
for (int j = ; j <= i; ++j)
{
if (a[i%][j] >= )
{
tmp = a[i%][j]-;
ans++;
a[(i+)%][j] += tmp/2.0;
a[(i+)%][j+] += tmp/2.0;
flag = false;
}
}
if (flag) break;
}
printf("%d\n", ans);
}
return ;
}
C题:
http://codeforces.com/problemset/problem/676/C
当时比较直接的想法就先求出把前i段都变成同样字符的代价的所有前缀和suma, sumb。这样就可以求解任意区间变成同样字符的代价了。
然后枚举区间左值,二分区间右值。就可以求解了。这样的复杂度是nlogn。
但是考虑到区间右值其实具有单调性。于是我可以从后往前遍历区间左值,那么区间右值要么不变,要么是上一次的位置往前移动。于是用两个指针就可以2n时间内完成了。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <string>
#define LL long long using namespace std; const int maxN = ;
int n, k;
int suma[maxN], sumb[maxN];
char str[maxN]; void input()
{
scanf("%s", str);
suma[] = sumb[] = ;
for (int i = ; i < n; ++i)
{
if (str[i] == 'a')
{
suma[i+] = suma[i];
sumb[i+] = sumb[i]+;
}
else
{
suma[i+] = suma[i]+;
sumb[i+] = sumb[i];
}
}
} int getLen(int from)
{
int ans, lt = from, rt = n, mid;
while (lt+ < rt)
{
mid = (lt+rt)>>;
if (suma[mid]-suma[from-] > k) rt = mid;
else lt = mid;
}
if (suma[rt]-suma[from-] <= k) ans = rt-from+;
else ans = lt-from+; lt = from; rt = n;
while (lt+ < rt)
{
mid = (lt+rt)>>;
if (sumb[mid]-sumb[from-] > k) rt = mid;
else lt = mid;
}
if (sumb[rt]-sumb[from-] <= k) ans = max(ans, rt-from+);
else ans = max(ans, lt-from+);
return ans;
} void work()
{
int ans = ;
for (int i = ; i <= n; ++i)
ans = max(ans, getLen(i));
printf("%d\n", ans);
} int main()
{
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
while (scanf("%d%d", &n, &k) != EOF)
{
input();
work();
}
return ;
}
D题:
http://codeforces.com/problemset/problem/676/D
这题是个bfs,比较容易看出来。首先我设置一个二进制状态1111,四位,最高位表示上方有门,第二位表示右方有门,第一位表示下方有门,最低位第0位表示左方有门。
然后写个swtich,把map中所有的字符映射到二进制状态。
然后就是bfs了。
bfs带有三个状态x,y,state。x和y即坐标,state表示经过了几次旋转。
然后就是瞎几把搜了。。。
第一种情况(state+1)%4
第二种情况便是上下左右搜,代码我是直接复制粘贴,所以只看往上的情况。
首先这种状态下必须上方有门,而且上面的格子下方有门。
对于上方有门,由于是顺时针旋转,也就是mp的映射二进制位最高位在逆时针旋转后是1。而这个逆时针旋转就是(3+state)%4这一位了。代码里可以看出来。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <string>
#define LL long long using namespace std; //up right down left
int get(char ch)
{
switch (ch)
{
case '+': return ;
case '-': return ;
case '|': return ;
case '^': return ;
case '>': return ;
case '<': return ;
case 'v': return ;
case 'L': return ;
case 'R': return ;
case 'U': return ;
case 'D': return ;
case '*': return ;
}
} bool judge(int p, int index)
{
for (int i = ; i < index; ++i)
p >>= ;
return p&;
} const int maxN = ;
int n, m, fromx, fromy, tox, toy;
char mp[maxN][maxN];
int s[maxN][maxN][]; void input()
{
memset(mp, '*', sizeof(mp));
memset(s, -, sizeof(s));
for (int i = ; i <= n; ++i)
{
scanf("%s", mp[i]+);
mp[i][m+] = '*';
}
scanf("%d%d", &fromx, &fromy);
scanf("%d%d", &tox, &toy);
} struct node
{
int x, y;
int state;
}; int myMin(int x, int y)
{
if (x == -) return y;
if (y == -) return x;
return min(x, y);
} void work()
{
node t, k;
queue<node> q;
t.x = fromx; t.y = fromy; t.state = ;
s[t.x][t.y][t.state] = ;
q.push(t);
while (!q.empty())
{
t = q.front();
q.pop(); k = t;
k.state = (k.state+)%;
if (s[k.x][k.y][k.state] == -)
{
s[k.x][k.y][k.state] = s[t.x][t.y][t.state]+;
q.push(k);
} int dir1, dir2;
dir1 = get(mp[t.x][t.y]); //up
if (judge(dir1, ((+t.state)%+)%))
{
dir2 = get(mp[t.x-][t.y]);
if (judge(dir2, ((+t.state)%+)%))
{
k.x = t.x-;
k.y = t.y;
k.state = t.state;
if (s[k.x][k.y][k.state] == -)
{
s[k.x][k.y][k.state] = s[t.x][t.y][t.state]+;
q.push(k);
}
}
} //right
if (judge(dir1, ((+t.state)%+)%))
{
dir2 = get(mp[t.x][t.y+]);
if (judge(dir2, ((+t.state)%+)%))
{
k.x = t.x;
k.y = t.y+;
k.state = t.state;
if (s[k.x][k.y][k.state] == -)
{
s[k.x][k.y][k.state] = s[t.x][t.y][t.state]+;
q.push(k);
}
}
} //down
if (judge(dir1, ((+t.state)%+)%))
{
dir2 = get(mp[t.x+][t.y]);
if (judge(dir2, ((+t.state)%+)%))
{
k.x = t.x+;
k.y = t.y;
k.state = t.state;
if (s[k.x][k.y][k.state] == -)
{
s[k.x][k.y][k.state] = s[t.x][t.y][t.state]+;
q.push(k);
}
}
} //left
if (judge(dir1, ((+t.state)%+)%))
{
dir2 = get(mp[t.x][t.y-]);
if (judge(dir2, ((+t.state)%+)%))
{
k.x = t.x;
k.y = t.y-;
k.state = t.state;
if (s[k.x][k.y][k.state] == -)
{
s[k.x][k.y][k.state] = s[t.x][t.y][t.state]+;
q.push(k);
}
}
}
}
int ans = -;
ans = myMin(ans, s[tox][toy][]);
ans = myMin(ans, s[tox][toy][]);
ans = myMin(ans, s[tox][toy][]);
ans = myMin(ans, s[tox][toy][]);
printf("%d\n", ans);
} int main()
{
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
while (scanf("%d%d", &n, &m) != EOF)
{
input();
work();
}
return ;
}
ACM学习历程—Codeforces Round #354 (Div. 2)的更多相关文章
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #354 (Div. 2)-D
D. Theseus and labyrinth 题目链接:http://codeforces.com/contest/676/problem/D Theseus has just arrived t ...
- Codeforces Round #354 (Div. 2)-C
C. Vasya and String 题目链接:http://codeforces.com/contest/676/problem/C High school student Vasya got a ...
- Codeforces Round #354 (Div. 2)-B
B. Pyramid of Glasses 题目链接:http://codeforces.com/contest/676/problem/B Mary has just graduated from ...
- Codeforces Round #354 (Div. 2)-A
A. Nicholas and Permutation 题目链接:http://codeforces.com/contest/676/problem/A Nicholas has an array a ...
- Codeforces Round #354 (Div. 2) D. Theseus and labyrinth
题目链接: http://codeforces.com/contest/676/problem/D 题意: 如果两个相邻的格子都有对应朝向的门,则可以从一个格子到另一个格子,给你初始坐标xt,yt,终 ...
- Codeforces Round #354 (Div. 2) C. Vasya and String
题目链接: http://codeforces.com/contest/676/problem/C 题解: 把连续的一段压缩成一个数,对新的数组求前缀和,用两个指针从左到右线性扫一遍. 一段值改变一部 ...
- Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)
题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...
- Codeforces Round #354 (Div. 2) E. The Last Fight Between Human and AI 数学
E. The Last Fight Between Human and AI 题目连接: http://codeforces.com/contest/676/problem/E Description ...
随机推荐
- Nginx 限制php解析、限制浏览器访问
限制php解析 1.有时候会根据目录来限制php解析: location ~ .*(diy|template|attachments|forumdata|attachment|image)/.*\.p ...
- 20145120黄玄曦《网络对抗》MSF基础应用
20145120黄玄曦<网络对抗>MSF基础应用 准备工作 本来决定就是老师提供的XP虚拟机了,做着做着发现因为打补丁以及语言的问题,需要另外的虚拟机. 求来了不那么健壮的虚拟机,环境如下 ...
- 伸展树基础(Splay)
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3948 Solved: 1627 [Submit][St ...
- COS-8文件系统
操作系统(Operating System,简称OS)是管理和控制计算机硬件与软件资源的计算机程序,是直接运行在“裸机”上的最基本的系统软件,任何其他软件都必须在操作系统的支持下才能运行. 操作系 ...
- Linux系统中使用netcat命令的奇技淫巧
netcat是网络工具中的瑞士军刀,它能通过TCP和UDP在网络中读写数据.通过与其他工具结合和重定向,你可以在脚本中以多种方式使用它.使用netcat命令所能完成的事情令人惊讶. netcat所做的 ...
- JSR规范整理
Web Service技术 Java Date与Time API ( JSR 310) Java API for RESTful Web Services (JAX-RS) 1.1 (JSR 311) ...
- java用servlet、cookie实现一个阅读记录
效果如图 代码1 package com.xiaostudy.servlet; import java.io.IOException; import java.io.PrintWriter; impo ...
- 使用IDEA将代码托管到GitHub步骤和错误解决
一.下载并安装Git版本控制工具 下载地址:https://git-scm.com/downloads 注册GitHub账号:https://github.com/ 为什么托管到GitHub要下载Gi ...
- 摘录:Jetty 的工作原理以及与 Tomcat 的比较
引子:Jetty 应该是目前最活跃也是很有前景的一个 Servlet 引擎.本文将介绍 Jetty 基本架构与基本的工作原理:您将了解到 Jetty 的基本体系结构:Jetty 的启动过程:Jetty ...
- 用Java编程计算猴子吃桃问题
猴子吃桃问题:猴子吃桃子问题:猴子第一天摘下N个桃子,当时就吃了一半,还不过瘾,就又吃了一个.第二天又将剩下的桃子吃掉一半,又多吃了一个.以后每天都吃前一天剩下的一半零一个.到第10天在想吃的时候就剩 ...