CodeForces Round #290 Div.2
A. Fox And Snake
代码可能有点挫,但能够快速A掉就够了。
#include <cstdio> int main()
{
//freopen("in.txt", "r", stdin); int n, m;
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++i)
{
if(i % == || i % == ) for(int j = ; j < m; ++j) printf("#");
else if(i % == )
{
for(int j = ; j < m; ++j) printf(".");
printf("#");
}
else
{
printf("#");
for(int j = ; j < m; ++j) printf(".");
}
printf("\n");
} return ;
}
代码君
B. Fox And Two Dots (DFS)
题意:
一个n×m的矩阵,给每个格子上都染上色。问是否能找到一条长度不小于4的相同颜色的回路。
分析:
在DFS的时候我们可以记录每个格子递归时的深度,如果下一个格子曾经走过而且与当前格子深度相差大于1,说明找到回路。
代码中的小错误还真不容易发现,归根结底还是自己对DFS理解得不够深刻。
#include <cstdio> const int maxn = + ;
int n, m;
int vis[maxn][maxn];
char map[maxn][maxn];
int dx[] = {, , , -};
int dy[] = {, , -, }; bool dfs(int x, int y, int d, char color)
{
vis[x][y] = d;
for(int i = ; i < ; ++i)
{
int xx = x + dx[i];
int yy = y + dy[i];
if(xx>=&&xx<n&&yy>=&&yy<m && map[xx][yy] == color)
{
if(vis[xx][yy] && vis[x][y] - vis[xx][yy] > ) return true;
else if(!vis[xx][yy])
//return dfs(xx, yy, d+1, color);
if(dfs(xx, yy, d+, color)) return true;
}
}
return false;
} int main()
{
//freopen("in.txt", "r", stdin); scanf("%d%d", &n, &m);
for(int i = ; i < n; ++i)
scanf("%s", map[i]); for(int i = ; i < n; ++i)
for(int j = ; j < m; ++j) if(!vis[i][j])
if(dfs(i, j, , map[i][j]))
{
printf("Yes\n");
return ;
} printf("No\n"); return ;
}
代码君
C. Fox And Names (拓扑排序)
题意:
要求重排26个英文字母,使得所给出的n个名字满足从小到大的字典序。
分析:
根据字典序的比较规则,可以得到一些字母之间的小于关系。然后通过拓扑排序扩展成一个全序关系输出即可。
#include <cstdio>
#include <cstring>
#include <algorithm> const int maxn = + ;
int n, len[maxn], t, c[maxn];
char names[maxn][maxn], ans[];
bool G[][]; bool dfs(int u)
{
c[u] = -;
for(int v = ; v < ; ++v) if(G[u][v])
{
if(c[v] < ) return false;
else if(!c[v] && !dfs(v)) return false;
}
c[u] = ; ans[--t] = u + 'a';
return true;
} bool topo()
{
t = ;
memset(c, , sizeof(c));
for(int u = ; u < ; ++u) if(!c[u]) if(!dfs(u)) return false;
return true;
} int main()
{
//freopen("in.txt", "r", stdin); scanf("%d", &n);
for(int i = ; i < n; ++i) { scanf("%s", names[i]); len[i] = strlen(names[i]); }
for(int i = ; i < n; ++i)
{
int l1 = len[i];
for(int j = i+; j < n; ++j)
{
int l2 = len[j];
int p = ;
while(names[i][p] == names[j][p] && p < l1 && p < l2) p++;
if(p == l1) continue;
else if(p == l2)
{//bbba无论怎样都不会比bb字典序小
puts("Impossible");
return ;
}
else
{
char c1 = names[i][p], c2 = names[j][p];
G[c1-'a'][c2-'a'] = true;
}
}
} if(topo()) printf("%s\n", ans);
else puts("Impossible"); return ;
}
代码君
D. Fox And Jumping (GCD + DP)
题意:
有一排无限多的格子,编号从负无穷到正无穷。有n张卡片,第i个卡片的花费为ci,购买这张卡片后可以向前或者向后跳li个格子,使用次数不限。
问是否能用通过购买这些卡片跳到任意格子,如果可以最小花费是多少。
分析:
如果有某种方法能够向前或者向后走一步,则可以到达任意格子。
考虑两张卡片的情况,l1x + l2y = g,根据数论知识有正整数g的最小值是gcd(l1, l2)
把问题转化成了:用最小的花费选取若干个数使得他们的GCD = 1
第一个想法就是DP,但是数据太大,于是用map来解决这个问题。
还有就是学习一下map的新用法,貌似是C++11里的标准,确实好用。
#include <bits/stdc++.h>
using namespace std; typedef pair<int, int> pii;
const int maxn = + ;
int n, l[maxn], c[maxn];
map<int, int> d; int main()
{
//freopen("in.txt", "r", stdin); scanf("%d", &n);
for(int i = ; i < n; ++i) scanf("%d", &l[i]);
for(int i = ; i < n; ++i) scanf("%d", &c[i]);
d[] = ;
for(int i = ; i < n; ++i)
for(pii p: d)
{ //遍历整个map
int g = __gcd(p.first, l[i]);
if(!d.count(g) || d[g] > p.second + c[i]) d[g] = p.second + c[i];
} if(!d.count()) puts("-1");
else printf("%d", d[]); return ;
}
代码君
CodeForces Round #290 Div.2的更多相关文章
- Codeforces Round #290 (Div. 2) D. Fox And Jumping dp
D. Fox And Jumping 题目连接: http://codeforces.com/contest/510/problem/D Description Fox Ciel is playing ...
- Codeforces Round #290 (Div. 2) C. Fox And Names dfs
C. Fox And Names 题目连接: http://codeforces.com/contest/510/problem/C Description Fox Ciel is going to ...
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots dfs
B. Fox And Two Dots 题目连接: http://codeforces.com/contest/510/problem/B Description Fox Ciel is playin ...
- Codeforces Round #290 (Div. 2) A. Fox And Snake 水题
A. Fox And Snake 题目连接: http://codeforces.com/contest/510/problem/A Description Fox Ciel starts to le ...
- Codeforces Round #290 (Div. 2) E. Fox And Dinner 网络流建模
E. Fox And Dinner time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)
http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring&qu ...
- Codeforces Round #290 (Div. 2) B (dfs)
题目链接:http://codeforces.com/problemset/problem/510/B 题意:判断图中是否有某个字母成环 思路:直接dfs就好了,注意判断条件:若下一个字母与当前字母相 ...
- Codeforces Round #290 (Div. 2) _B找矩形环的三种写法
http://codeforces.com/contest/510/status/B 题目大意 给一个n*m 找有没有相同字母连起来的矩形串 第一种并查集 瞎搞一下 第一次的时候把val开成字符串了 ...
- DFS Codeforces Round #290 (Div. 2) B. Fox And Two Dots
题目传送门 /* DFS:每个点四处寻找,判断是否与前面的颜色相同,当走到已走过的表示成一个环 */ #include <cstdio> #include <iostream> ...
- 找规律 Codeforces Round #290 (Div. 2) A. Fox And Snake
题目传送门 /* 水题 找规律输出 */ #include <cstdio> #include <iostream> #include <cstring> #inc ...
随机推荐
- hdu 5343 MZL's Circle Zhou SAM
MZL's Circle Zhou 题意:给定两个长度不超过a,b(1 <= |a|,|b| <= 90000),x为a的连续子串,b为y的连续子串(x和y均可以是空串):问x+y形成的不 ...
- Mysql 配置主从服务自动同步功能
1.修改主服务器master: #vi /etc/my.cnf [mysqld] log-bin=mysql-bin //[必须]启用二进制日志 serve ...
- buffer busy wait在RAC环境下出现
昨天运维组的同时反映有套系统用户反映很慢,需要协助帮忙检查什么原因引起的性能问题.导出了从8点到11点的AWR报告进行分析,发现等待事件里大部分的指标都正常,就是buffer busy wait的平均 ...
- 【多路复用】I/O多路复用
http://www.tuicool.com/articles/RBvqUz C#下用select方法实现socket服务端
- Mac 启用http-dav功能(WebDAV服务器)
启用Mac的WebDAV可以实现文件的上传以及Windows.Linux和Mac之间的数据互传. 客户端使用:windows下使用网上邻居 --> 添加一个网上邻居 --> 输入uplo ...
- 研读代码必须掌握的Eclipse快捷键
1. Ctrl+左键 和F3 这个是大多数人经常用到的,用来查看变量.方法.类的定义跳到光标所在标识符的定义代码.当按执行流程阅读时,F3实现了大部分导航动作. 2 Ctrl+Shift+G在工作空间 ...
- 对.net orm工具Dapper在多数据库方面的优化
Dapper是近2年异军突起的新ORM工具,它有ado.net般的高性能又有反射映射实体的灵活性,非常适合喜欢原生sql的程序员使用,而且它源码很小,十分轻便.我写本博客的目的不是为了介绍Dapper ...
- 1064: [Noi2008]假面舞会 - BZOJ
Description 一年一度的假面舞会又开始了,栋栋也兴致勃勃的参加了今年的舞会.今年的面具都是主办方特别定制的.每个参加舞会的人都可以在入场时选择一 个自己喜欢的面具.每个面具都有一个编号,主办 ...
- Spring MVC控制层的返回类型--String类型与Bean类型
SpringMVC控制层的返回类型形式多样,现拿其中的两种--String类型与Bean类型作以说明. 一.测试项目的结构 说明:(jsp的名字没起好) 控制层:UserController.java ...
- BZOJ 4036 [HAOI2015] Set 解题报告
首先我们不能一位一位的考虑,为什么呢? 你想想,你如果一位一位地考虑的话,那么最后就只有 $n$ 个数字,然而他给了你 $2^n$ 个数字,怎么看都不对劲呀.(我是因为这样子弄没过样例才明白的) 所以 ...