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. Python面试题之Python的Super方法

    我们最常见的,可以说几乎唯一能见到的使用super的形式是: class SubClass(BaseClass): def method(self): super(SubClass, self).me ...

  2. 运行报警告UserWarning: Unknown extension is not supported and will be removed warn(msg)

    运行python代码,出现如下警告: C:\Users\niko\PycharmProjects\python_new\venv\lib\site-packages\openpyxl\reader\w ...

  3. Zabiix 监控图形乱码问题

    Zabiix切换为中文 配置中文乱码问题 在C:\Windows\Fonts中复制想要的字体,后缀为ttf,若本身问大写,请改成小写的文件后缀ttf,并上传至zabbix服务器的/usr/local/ ...

  4. knudson hypothesis 二次突变假说

    二次突变假说是由诺丁在1953年提出的,他发现似乎随着年龄的增长,患有癌症的概率有上升.对这种现象有一种解释,即癌症的发生需要多个突变的累积. 克努森在1971通过研究正式地提出该观点.他对具有遗传性 ...

  5. 配置内核 Makefile:1449: *** mixed implicit and normal rules. Stop.【转】

    本文转载自:https://blog.csdn.net/bitowang/article/details/8446494 在编译内核的时候提示Makefile:1449: *** mixed impl ...

  6. centos上mailx通过465端口发送邮件

    最近在看zabbix发送邮件的时候,发现自己的邮件总是无法发送,这里可能是外网防火墙禁止25端口,那么如何绕过25端口呢?  我使用的是163邮箱的TSL加密协议465端口 由于mailx基本配置很简 ...

  7. 平滑重启php

    kill -USR2 `cat /usr/local/webserver/php/var/run/php-fpm.pid`

  8. v-model和sync修饰符

    场景: 在用vue开发的过程中我们经常会遇到父子组件共用同一变量的情况,那么在这种情况下,我们肯定会想直接 把变量传过来用,因为是双向绑定的所以子组件就会修改这个变量,这样在vue中时会报错的. 问题 ...

  9. Android两个Activity之间的数据交换

    1. 不带数据 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceS ...

  10. poj1986带权lca

    lca求距离,带权值 的树上求lca,我是用倍增法求的,求两点之间的距离转化为到根节点之间的距离 (de了一个小时 的bug,重打居然就过了....) #include<map> #inc ...