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. BeatSaber节奏光剑插件开发官方教程1-创建一个插件模板

    原文:https://wiki.assistant.moe/modding/intro 一.简介 Beat Saber 开发环境:unity2018.C#..NET framework 4.6. 此教 ...

  2. echo指令

    1.在Linux中echo命令用来在标准输出上显示一段字符,比如:echo "the echo command test!" 这个就会输出“the echo command tes ...

  3. menubar下面的选项不可以输入中文

    这是一个QT5的bug. 1.不用中文,使用英文: 2.先输入中文,然后在属性Action里面的text里改成中文.

  4. Oracle中清除BIN$开头的垃圾表的解决办法 [转]

    oracle drop table的时候,不会彻底删除该表,它将drop的表放到了自己的回收站里,放到回收站的表就是我们看到的形如bin$/rt62vkdt5wmrjfcz28eja==$0的表,其中 ...

  5. hive 数据清理--数据去重

    hive> select * from (select *,row_number() over (partition by id) num from t_link) t where t.num= ...

  6. Linux新手常用命令 - 转载

    开始→运行→cmd命令 集锦 cls------------命令窗清屏eqit-----------退出当前命令ping ip--------检查网络故障ipconfig-------查看IP地址wi ...

  7. QT 进度条 QProgressDialog

    //默认构造函数 参数依次为,对话框正文,取消按钮名称,进度条范围,及所属 QProgressDialog *progressDlg=new QProgressDialog( QStringLiter ...

  8. C语言排序算法之简单交换法排序,直接选择排序,冒泡排序

    C语言排序算法之简单交换法排序,直接选择排序,冒泡排序,最近考试要用到,网上也有很多例子,我觉得还是自己写的看得懂一些. 简单交换法排序 /*简单交换法排序 根据序列中两个记录键值的比较结果来对换这两 ...

  9. LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List

    题目中文:没有重复字符的最长子串 题目难度:Medium 题目内容: Given a string, find the length of the longest substring without ...

  10. CSS设置小技巧

    水平居中 对于元素的水平居中,有三种情况: 行内元素(文字.图片等):text-align: center; 定宽块状元素(有设置宽度的block元素):margin: 0 auto; 不定宽块状元素 ...