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 ...
随机推荐
- phpcms v9二级栏目生成到根目录后三级栏目无法访问的解决办法
这个栏目问题折腾了我一天多,可怜我这个美工又不会程序!!! 我的问题是: 我的栏目设置如下: 一级栏目[生成到根目录(是)]> 二级栏目[生成到根目录(是)] > 三级栏目[生成到根目录( ...
- Using jQuery to add a dynamic “Back To Top” floating button with smooth scroll
Ever read a really long blog post or article and then had to scroll all the way up to the top of the ...
- Django文档——Model字段选项(Field Options)
建立一个简易Model class Person(models.Model): GENDER_CHOICES=( (1,'Male'), (2,'Female'), ) name=models.Cha ...
- fedroa 更换字体
1. 添加RPM包: rpm -Uvh http://www.infinality.net/fedora/linux/infinality-repo-1.0-1.noarch.rpm 2. 安装包 y ...
- oracle游标小试
有时候需要大面积的修改数据,这个时候用循环语句效率不高.而临时表又不能满足点对点修改的时候,游标似一种不错的选择(PS:好像游标也是为循环而生的吧) 现在有两张表 t1(ryid number,nam ...
- PowerDesigner 非数值默认值时会自动增加单引单
在PowerDesigner中,如果默认值是非数值型的,那么 PowerDesigner 会默认加上单引号 因此我们需要把这个默认的单引号干掉,如果是需要设置字符串默认值的时候,就手工加上 单引号 即 ...
- C#基础及记忆概念
在C#中,你给一个方法传输值类型参数时,实际上是使用的这个参数的一个副本,就是将原来的变量复制一份,然后传给一个方法,让其进行操作.所以在方法内部对参数的修改等不会对原来的参数造成影响(这个其实就是值 ...
- MyISAM读写并发优化
MyISAM在读操作占主导的情况下是很高效的.可一旦出现大量的读写并发,同InnoDB相比,MyISAM的效率就会直线下降,而 且,MyISAM和InnoDB的数据存储方式也有显著不同:通常,在MyI ...
- 建立Clojure开发环境-使用IDEA和Leiningen
OS: Mac OS X 10.10 IDEA 14.0.2 Community Edition 安装Leiningen 按照http://leiningen.org/的指南安装lein 阅读Lein ...
- java thread类和runable
java thread 类其实和其他类是一样的.有自己的属性和方法.以及有一个重写的run方法 不同的是:必须重写run()方法. run()方法是线程thread启动后cpu调用运行的程序代码. r ...