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. Spark小课堂Week4 从控制台看Spark逻辑结构

    Spark小课堂Week4 从控制台看Spark逻辑结构 层级关系: 从监控控制台,我们可以看到如下关系: 一个 Job 包含 n Stage 一个 Stage 包含 n Task Job0解决什么问 ...

  2. Chrome浏览器安装插件提示(net::ERR_NAME_NOT_RESOLVED)

    在chrome的webstore中安装currently插件.使用goagentFQ后能正常访问,但出现"net::ERR_NAME_NOT_RESOLVED"错误. 该错误的含义 ...

  3. PIL 安装

    1.安装依赖包 1.1 ubuntu安装 apt-get install python-devapt-get install libjpeg-dev apt-get install libjpeg8- ...

  4. envi中多波段图层叠加layer stacking

    Basic Tools——layer stacking 选择投影和输出的文件 波段1-7波段图层都叠加在一个文件中了

  5. 另一个 SqlParameterCollection 中已包含 SqlParameter

    出处:http://www.cnblogs.com/OldYongs/archive/2011/03/12/1982021.html#2742742 一般情况下,我们定义的一个SqlParameter ...

  6. 在Qt Creator中添加OpenCV库

    在项目的pro文件中添加如下代码:INCLUDEPATH += D:/opencv/build/include win32:CONFIG(debug, debug|release): {LIBS += ...

  7. EXTJS 4.2 资料 控件之Grid 添加行,编辑行,删除行

    //SiteVariableConfigValue类,创立一个模型类 Ext.define("SiteVariableConfigValue", { extend: "E ...

  8. 微软Hololens设备 浅分析

    微软Hololens的定位是一款MR 设备(Mixed reality).MR与AR的不同我认为是MR能够将真实环境的场景信息与虚拟对象进行完美的融合,它是基于SLAM(SimultaneousLoc ...

  9. 拼音操作工具类 - PinyinUtil.java

    拼音操作工具类,提供字符串转换成拼音数组.汉字转换成拼音.取汉字的首字母等方法. 源码如下:(点击下载 -PinyinUtil.java.pinyin4j-2.5.0.jar ) import net ...

  10. BZOJ 1706: [usaco2007 Nov]relays 奶牛接力跑

    Description FJ的N(2 <= N <= 1,000,000)头奶牛选择了接力跑作为她们的日常锻炼项目.至于进行接力跑的地点 自然是在牧场中现有的T(2 <= T < ...