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 ...
随机推荐
- Oracle11g:分区表数据操作出现ORA-14400异常处理
Oracle11g:分区表数据操作出现ORA-14400异常处理 问题: 当对已分区的表数据进行操作(例如新增,修改),出现异常提示: ORA: 插入的分区关键字未映射到任何分区 分析: 意思说的是插 ...
- 20145109 《Java程序设计》第七周学习总结
20145109 <Java程序设计>第七周学习总结 Chapter 13 Time & Date Date System.currentTimeMillis() return L ...
- Oracle loop、while、for循环
Loop循环 Declare p_sum ; p_i number; Begin p_i :; Loop p_sum := p_sum + p_i; p_i :; ) then SYS.Dbms_Ou ...
- shell printf命令:格式化输出语句
printf 命令用于格式化输出, 是echo命令的增强版.它是C语言printf()库函数的一个有限的变形,并且在语法上有些不同. 注意:printf 由 POSIX 标准所定义,移植性要比 ech ...
- QT 进度条 QProgressDialog
//默认构造函数 参数依次为,对话框正文,取消按钮名称,进度条范围,及所属 QProgressDialog *progressDlg=new QProgressDialog( QStringLiter ...
- pandas 数据处理
1. 查看数值数据的整体分布情况 datafram.describe() 输出: agecount 1463.000000mean 22.948052std 8.385384min 13.000000 ...
- org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 语法分析器在此文档中遇到多个 "64,000" 实体扩展; 这是应用程序施加的限制
使用SAX解析XML文件.XML文件有1.5G,程序抛出了这个问题: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 语法 ...
- WPF关于改变ListBoxItem的颜色的注意事项以及如何找到ListBox中的ItemsPanel
在ListBox中碰到过几个问题,现在把它写出来: 第一个就是在ListBoxItem中当我用触发器IsSelected和IsMouseOver来设置Background和Foreground的时候, ...
- 关于pytest的一些问题
一. 测试模块内部使用fixture和测试模块调用外部公共的fixture 1. unittest框架下的测试用例模块 from selenium import webdriver import un ...
- 简短的perl程序
简短的perl程序能够实现大功能. perl是如何做到的呢? 1. 默认变量 如果没有向函数提供参数值,则默认参数为$_: 如果没有变量用于接收一个表达式的值,则默认接收变量为 ...