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)的更多相关文章

  1. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

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

    D. Theseus and labyrinth 题目链接:http://codeforces.com/contest/676/problem/D Theseus has just arrived t ...

  3. Codeforces Round #354 (Div. 2)-C

    C. Vasya and String 题目链接:http://codeforces.com/contest/676/problem/C High school student Vasya got a ...

  4. Codeforces Round #354 (Div. 2)-B

    B. Pyramid of Glasses 题目链接:http://codeforces.com/contest/676/problem/B Mary has just graduated from ...

  5. Codeforces Round #354 (Div. 2)-A

    A. Nicholas and Permutation 题目链接:http://codeforces.com/contest/676/problem/A Nicholas has an array a ...

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

    题目链接: http://codeforces.com/contest/676/problem/D 题意: 如果两个相邻的格子都有对应朝向的门,则可以从一个格子到另一个格子,给你初始坐标xt,yt,终 ...

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

    题目链接: http://codeforces.com/contest/676/problem/C 题解: 把连续的一段压缩成一个数,对新的数组求前缀和,用两个指针从左到右线性扫一遍. 一段值改变一部 ...

  8. Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)

    题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...

  9. 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 ...

随机推荐

  1. CSS Id 和 Class选择器

    CSS Id 和 Class选择器 如果你要在HTML元素中设置CSS样式,你需要在元素中设置"id" 和 "class"选择器. 一.id 选择器 id 选择 ...

  2. github Git-fork-别人的项目后更新代码的方法

     用github还处于菜的阶段,遇到问题简单记录.   举个例子,需要 fork 这个项目 https://github.com/tarobjtu/WebFundamentals.git 点击 for ...

  3. pxe-kickstart批量部署文档

    #PXE安装: yum install syslinux xinetd tftp-server httpd -y yum install dhcp -y yum install system-conf ...

  4. JavaWeb HTML

    1. HTML介绍 1.1. 什么是HTML HTML的全称为Hyper Text Markup Language,译为超文本标记语言. 超文本,就是指页面内可以包含图片.链接,甚至音乐.程序等非文字 ...

  5. ubuntu 致命错误: zlib.h:没有那个文件或目录【转】

    本文转载自:https://blog.csdn.net/u013359794/article/details/44922685?locationnum=15&fps=1 编译时,出现错误,提示 ...

  6. linux下字典生成工具-crunch与rtgen

    所谓的密码字典主要是配合密码破解软件所使用,密码字典里包括许多人们习惯性设置的密码.这样可以提高密码破解软件的密码破解成功率和命中率,缩短密码破解的时间.当然,如果一个人密码设置没有规律或很复杂,未包 ...

  7. gulp前端构建工具的搭建

    我的机器: 3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux 1 ...

  8. Effective C++ 条款03:尽可能使用const

    场景一 用于修饰指针 char greeting[] = "Hello"; char* p = greeting; // non-const pointer, non-const ...

  9. Buildbot初探

    什么是Buildbot Buildbot是一个持续集成和自动化测试框架,我在毕业刚进VMware不久的一个和以色列人合作的项目中接触到Buildbot,当时我真的恨死它了...经常随意的提交了一些代码 ...

  10. java8 函数接口 Predicate例子

    import java.util.HashSet; import java.util.Collection; import java.util.function.Predicate; public c ...