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 ...
随机推荐
- 什么是PRD、MRD与BRD
什么是PRD? 什么是MRD? 什么是BRD? 一.PRD的含义 英文简称,PRD(Product Requirement Document),PRD文档中文意思是:产品需求文档. PRD文档是产品项 ...
- delphi 仅带下划线的TEdit控件
在做录入框的时候,很希望有一个只带下划线的文本框,网上介绍的很多,有自己做组件的,须不知Delphi下只需要简单设置几个属性即可达到目的.
- Delphi2010中DataSnap技术网摘
一.为DataSnap系统服务程序添加描述 这几天一直在研究Delphi 2010的DataSnap,感觉功能真是很强大,现在足有理由证明Delphi7该下岗了. DataSnap有三种服务模式,其中 ...
- xml学习总结(四)
命名空间 (1)产生 问题:在不同的约束文档中,有不同好安逸的相同标记名称 解决办法 每个约束模式人当被赋予一个唯一的名称空间,每个名称空间可用一个唯一的URI表示 在XML实例中为来自不同模式文档的 ...
- 支持异步通知的globalfifo平台设备驱动程序及其测试代码
驱动: #include <linux/module.h> #include <linux/types.h> #include <linux/fs.h> #incl ...
- MyEclipse 8.5配置Tomcat7.0
MyEclipse 8.5配置默认没有Tomcat7.0, 如果想使用怎么办.? window>>Preferences>>MyEclipse Enterprise Workb ...
- Qt智能指针简明说明
下面的智能指针分别对应boost库,Qt库,c++11的智能指针 boost::scoped_ptr QScopedPointer unique_ptr 在其生命期结束后会自动删除它所指的对象(确定 ...
- 【数位DP】bzoj1026: [SCOI2009]windy数
1026: [SCOI2009]windy数 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 4163 Solved: 1864[Submit][Sta ...
- sql删除wordpress没用的postmeta记录
支持多作者的wordpress博客,有时需要审核他们的文章内容,虽然UGC(User-generated content)整体是好的,但是也要控制一下质量,实在不相关或spam的文章就要毫不手软的删除 ...
- Fiddler 日志
Fiddler 日志(Logging) 在开发扩展插件及编写FiddlerScript时对调试程序非常有用. 1.输出日志 在FiddlerScript脚本中,你可以这样输出输出日志: Fiddler ...