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 ...
随机推荐
- 使用jQuery的9个误区
千万别忘记了使用最新的版本哦,毕竟每个版本更新肯定会在功能或性能上有所提升,或者修复了几个Bug,但有时惰性让人不想再去研究新版本的变化,因此,提醒你别忘记了在新项目用新的一定比旧版本要好. AD: ...
- Ubuntu虚拟机与Window、Arm的通信
Ubuntu虚拟机与Window的通信安装有Ubuntu14.04的虚拟机VMware,将虚拟机的网络适配器配置成NAT类型(默认使用VMnet8进行通信),此时将Ubuntu的IP地址设置成与VMn ...
- Linux进程间通信IPC学习笔记
linux下的进程通信手段基本上是从Unix平台上的进程通信手段继承而来的.而对Unix发展做出重大贡献的两大主力AT&T的贝尔实验室及BSD(加州大学伯克利分校的伯克利软件发布中心)在进程间 ...
- Invalid argument supplied for foreach()
将需要被遍历的数组强制转换为数组类型即可 <?php $array = null; foreach((array)$array as $value){ #..code.... } ?>
- oracle 表空间、用户名 相关语句
一.oracle查询表空间文件所在路径 select * from dba_data_files t where t.tablespace_name='FLW' 二.计算出表空间各相关数据 SELE ...
- java当中的定时器的4种使用方式
import java.util.Calendar;import java.util.Date;import java.util.Timer;import java.util.TimerTask; p ...
- hdu 3778
简单的dfs 但繁琐的可以了 0.0 #include<cstdio> #include<cstring> #include<algorithm> using s ...
- ural 1200
推出公式 然后特判两端 代码其实挺烂 但是有人竟然可以直接暴过去的 ...... #include <cstdio> #include <cstring> #in ...
- python繁体中文到简体中文的转换
处理中文字符串遇到了繁体和简体中文的转换,python版: 1.下载zh_wiki.py及langconv zh_wiki.py:https://github.com/skydark/nstool ...
- 深入理解计算机各种类型大小(sizeof)
深入理解计算机各种类型大小(sizeof) // Example of the sizeof keyword size_t i = sizeof( int ); struct align_dep ...