A. Mr. Kitayuta's Gift (枚举)

题意:

给一个长度不超过10的串,问能否通过插入一个字符使得新串成为回文串。

分析:

因为所给的串很多,所以可以枚举 “在哪插入” 和 “插入什么”,写一个二重循环枚举新串,判断是否为回文串。时间复杂度为O(n3)

还可只枚举插入位置(在那个位置用一个特殊字符表示),在判断的时候,如果遇到特殊字符,则所插入的字符一定为镜像的字符。

 #include <cstdio>
#include <cstring> char s[], s1[];
int l; bool judge()
{
for(int i = ; i < (l+)/; ++i)
if(s1[i] == ) s1[i] = s1[l-i];
else if(s1[l-i] == ) s1[l-i] = s[i];
else if(s1[i] != s1[l-i]) return false; return true;
} int main()
{
scanf("%s", s);
l = strlen(s);
bool flag = false;
for(int pos = ; pos <= l; ++pos)
{//枚举所加入字符的位置,s1为得到的新串
int i;
for(i = ; i < pos; ++i) s1[i] = s[i];
s1[i++] = ;
for(; i <= l; ++i)s1[i] = s[i-];
if(judge()) { flag = true; break; }
} if(!flag) puts("NA");
else
{
for(int i = ; i <= l; ++i)
if(s1[i] == ) printf("a");
else printf("%c", s1[i]);
} return ;
}

代码君

B. Mr. Kitayuta's Colorful Graph (并查集)

题意:

有一个多种颜色的无向图,给出每条边的两个顶点以及该边的颜色。

然后有若干次询问,每次询问两点间共有多少种颜色的通路。

分析:

这道题可以理解为二维的并查集。如果没有颜色的区分,可以判断两点是否连通。

现在给边染上颜色,则我们可以分开处理。最后在询问的时候枚举所有颜色,判断两点是否连通。

 #include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ; int G[maxn][maxn];
int n, m;
int p[maxn][maxn];
//找颜色为color的边中,x的父节点
int findp(int color, int x) { return x == p[color][x] ? x : p[color][x] = findp(color, p[color][x]); } struct Edge
{
int u, v;
Edge(int u=, int v=):u(u), v(v) {}
}; vector<Edge> edges[maxn]; int main()
{
//freopen("in.txt", "r", stdin);
int n, m, Mc = ;
scanf("%d%d", &n, &m);
//并查集初始化
for(int i = ; i <= m; ++i)
for(int j = ; j <= n; ++j)
p[i][j] = j; for(int i = ; i < m; ++i)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
Mc = max(Mc, c);
int pa = findp(c, a);
int pb = findp(c, b);
if(pa != pb) p[c][pa] = pb;
} int Q;
scanf("%d", &Q);
for(int i = ; i < Q; ++i)
{
int u, v, cnt = ;
scanf("%d%d", &u, &v);
for(int c = ; c <= Mc; ++c) //枚举所有颜色
if(findp(c, u) == findp(c, v))
cnt++;
printf("%d\n", cnt);
} return ;
}

代码君

C. Mr. Kitayuta, the Treasure Hunter (DP)

题意:

参见原文吧,=_=||

Mr. Kitayuta, the Treasure Hunter

分析:

设dp(i, j)表示跳到第i个岛步长为j,wi表示第i个岛的宝石数,最大能取到的宝石数。

状态转移方程为 dp(i, j) = wi + max{dp(i+j-1, j-1), dp(i+j, j), dp(i+j+1, j+1)}

上式是从后往前推的,最终答案是dp(d, d)。

从前往后推也是一样的,不过要维护一个最大值。

还有一个很严重的问题就是:30000×30000的空间太大了!

再深入分析,其实步长的变化范围没有3w那么大。

假设每一步步长为前面步长加一,可以求出最大步长。类似地,求出最小步长。

假设d=1,最大步长不会超过245 + d,因为

同样的,如果d≤244,则最小步长为0.

如果d≥245,

所以,最小步长不会小于max{0, d - 245}.

 #include <cstdio>
#include <algorithm>
using namespace std; int max3(int a, int b, int c)
{ return max(max(a, b), c); } int w[], dp[][]; int main()
{
//freopen("in.txt", "r", stdin); int n, d;
scanf("%d%d", &n, &d);
for(int i = ; i < n; ++i)
{
int x;
scanf("%d", &x);
w[x]++;
} int maxp, minp = ;
for(int n = ; ; n++) if(d*(n+)+n*(n+)/ >= )
{
maxp = d + n; //最大步长
break;
}
for(int n = ; d >= ; n++) if(d*(n+)-n*(n+)/ >= )
{
minp = d - n; //最小步长
break;
} for(int x = ; x >= d; --x)
for(int l = minp; l <= maxp; ++l)
dp[x][l-minp+] = w[x] + max3(dp[x+l][l-minp+], dp[x+l+][l+-minp], dp[x+l-][l-minp]); printf("%d\n", dp[d][d+-minp]); return ;
}

代码君

CodeForces Round #286 Div.2的更多相关文章

  1. DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph

    题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...

  2. 水题 Codeforces Round #286 (Div. 2) A Mr. Kitayuta's Gift

    题目传送门 /* 水题:vector容器实现插入操作,暴力进行判断是否为回文串 */ #include <cstdio> #include <iostream> #includ ...

  3. Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph 并查集

    D. Mr. Kitayuta's Colorful Graph Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/ ...

  4. Codeforces Round #286 (Div. 1) B. Mr. Kitayuta&#39;s Technology (强连通分量)

    题目地址:http://codeforces.com/contest/506/problem/B 先用强连通判环.然后转化成无向图,找无向图连通块.若一个有n个点的块内有强连通环,那么须要n条边.即正 ...

  5. Codeforces Round #286 Div.1 A Mr. Kitayuta, the Treasure Hunter --DP

    题意:0~30000有30001个地方,每个地方有一个或多个金币,第一步走到了d,步长为d,以后走的步长可以是上次步长+1,-1或不变,走到某个地方可以收集那个地方的财富,现在问走出去(>300 ...

  6. Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph dfs

    B. Mr. Kitayuta's Colorful Graph time limit per test 1 second memory limit per test 256 megabytes in ...

  7. Codeforces Round #286 (Div. 1) 解题报告

    A.Mr. Kitayuta, the Treasure Hunter 很显然的一个DP,30000的数据导致使用map+set会超时.题解给了一个非常实用的做法,由于每个点有不超过250种状态,并且 ...

  8. Codeforces Round #286 (Div. 2) B 并查集

    B. Mr. Kitayuta's Colorful Graph time limit per test 1 second memory limit per test 256 megabytes in ...

  9. Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph

    D - Mr. Kitayuta's Colorful Graph 思路:我是暴力搞过去没有将答案离线,感觉将答案的离线的方法很巧妙.. 对于一个不大于sqrt(n) 的块,我们n^2暴力枚举, 对于 ...

随机推荐

  1. 修改wamp的apache默认端口80以及www目录

    修改wamp的apache默认端口80以及www目录 以修改为8088端口和D:/workphp目录为例. 修改为8088端口 左键托盘图标,在“Apache”里可以直接打开httpd.conf,查找 ...

  2. The content of element type "sqlMapConfig" is incomplete,

    The content of element type "sqlMapConfig" is incomplete, it must match "(properties? ...

  3. 1079. Total Sales of Supply Chain (25)

    时间限制 250 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A supply chain is a network of r ...

  4. Mysql 如何删除数据表中的重复数据!

    1.使用distinct查询所有不重复的记录 2.创建数据表相同结构的临时表,将第一步的数据复制进去 create temporary table if not exists student_temp ...

  5. 2016 系统设计第一期 (档案一)MVC 引用 js css

    @Styles.Render("~/Bootstrap/css/bootstrap-theme.css") @Scripts.Render("~/jQuery/jquer ...

  6. AOP和IOC个人理解

    14:18 2014/5/5 IOC inversion of control 控制反转  将new对象的权力由调用者转移到spring容器(即xml文件),Struts2与Spring整合(scop ...

  7. SwfUpload vs里运行可以上传文件,放到iis上上传就报404错误。

    网上的答案都是说swfupload 的upload_url 路径要设置成绝对路径,但是我也设置了,但是还是不行,然后又找了方法,终于找到了,点击这里查看 解决办法: <system.webSer ...

  8. bnuoj 31796 键盘上的蚂蚁(搜索模拟)

    http://www.bnuoj.com/bnuoj/contest_show.php?cid=2876#problem/31796 [题意]: 如题,注意大小写情况 [code]: #include ...

  9. poj 2226 Muddy Fields (转化成二分图的最小覆盖)

    http://poj.org/problem?id=2226 Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  10. 如何使用 XSD

    如何使用 XSD 一个简单的 XML 文档: 请看这个名为 "note.xml" 的 XML 文档: <?xml version="1.0"?> & ...