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. json 语义分析

    json 中:元素与值用冒号 ":" 隔开元素与元素用逗号 "," 隔开{} 之间是一个对象, 对象可以层层嵌套[] 表示数组, 数组元素用逗号 ", ...

  2. ShowMask

    <html> <head> <script type="text/javascript"> function showMask(){ var a ...

  3. hosts文件的作用 whois查询域名信息

      Whois查询域名信息 在操作系统中的路径:Window98—在Windows目录下Windows 2000/XP—在C:\WINDOWS\system32\drivers\etc目录下 内容:包 ...

  4. iomanip,setw(),setw: undeclared identifier

    今天使用setw(),提示setw: undeclared identifier,上网查了下,原来是没有包含头文件iomanip,现摘录如下: iomanip #include <iomanip ...

  5. How to install Hadoop

    1.How to install Hadoop 3.0.0 http://blog.sina.com.cn/s/blog_4a1f59bf01010kx3.html 2.How to install ...

  6. Oracle的Export用法

    Oracle 的 Export 命令用于导出数据库信息,既可以导出表结构,也可以导出数据,表空间,或者按用户导出等等.按照通常的说法,该命令主要是用于数据库的迁移或者备份的.下面就介绍一下该命令的部分 ...

  7. Contest2037 - CSU Monthly 2013 Oct (Problem J: Scholarship)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?cid=2037&pid=9 [题解]: 这题卡了一下,卡在负数的情况,负数输出 0 这题主要找到一 ...

  8. SSL与TLS的区别以及介绍

    转载 :http://kb.cnblogs.com/page/197396/ SSL:(Secure Socket Layer,安全套接字层),位于可靠的面向连接的网络层协议和应用层协议之间的一种协议 ...

  9. XCode6.1中的ios7.1适配

    在xcode6.1中新创建的项目,运行在我的ios7.1的ipod touch上时(与5s的一样的尺寸, Retina屏幕), 上下出现了黑边,由于没有下载7.1的模拟器,不知道模拟器上有无问题, 查 ...

  10. 解析php中die(),exit(),return的区别

    die()停止程序运行,输出内容exit是停止程序运行,不输出内容return是返回值die是遇到错误才停止exit是直接停止,并且不运行后续代码,exit()可以显示内容.return就是纯粹的返回 ...