CodeForces Round #286 Div.2
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的更多相关文章
- DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...
- 水题 Codeforces Round #286 (Div. 2) A Mr. Kitayuta's Gift
题目传送门 /* 水题:vector容器实现插入操作,暴力进行判断是否为回文串 */ #include <cstdio> #include <iostream> #includ ...
- 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/ ...
- Codeforces Round #286 (Div. 1) B. Mr. Kitayuta's Technology (强连通分量)
题目地址:http://codeforces.com/contest/506/problem/B 先用强连通判环.然后转化成无向图,找无向图连通块.若一个有n个点的块内有强连通环,那么须要n条边.即正 ...
- Codeforces Round #286 Div.1 A Mr. Kitayuta, the Treasure Hunter --DP
题意:0~30000有30001个地方,每个地方有一个或多个金币,第一步走到了d,步长为d,以后走的步长可以是上次步长+1,-1或不变,走到某个地方可以收集那个地方的财富,现在问走出去(>300 ...
- 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 ...
- Codeforces Round #286 (Div. 1) 解题报告
A.Mr. Kitayuta, the Treasure Hunter 很显然的一个DP,30000的数据导致使用map+set会超时.题解给了一个非常实用的做法,由于每个点有不超过250种状态,并且 ...
- 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 ...
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph
D - Mr. Kitayuta's Colorful Graph 思路:我是暴力搞过去没有将答案离线,感觉将答案的离线的方法很巧妙.. 对于一个不大于sqrt(n) 的块,我们n^2暴力枚举, 对于 ...
随机推荐
- C++ 编写 CorelDRAW CPG 插件例子(2)—ClearFill
这是另一个例子: 贴上主要代码: #include "stdafx.h" #include <tchar.h> #import "libid:95E23C91 ...
- Python OS模块标准库的系统接口及操作方法
Python OS模块标准库的系统接口及操作方法 os.name 返回当前操作系统名,定义了'posix','nt','mac','os2','ce','java'(我使用win7/python3.1 ...
- FIFO、LRU、OPT这三种置换算法的缺页次数
考虑下述页面走向: 1,2,3,4,2,1,5,6,2,1,2,3,7,6,3,2,1,2,3,6 当内存块数量分别为3时,试问FIFO.LRU.OPT这三种置换算法的缺页次数各是多少? 答:缺页定义 ...
- Servlet一次乱码排查后的总结(转)
原文地址:http://my.oschina.net/looly/blog/287255 由来 在写一个小小的表单提交功能的时候,出现了乱码,很奇怪request上来的参数全部是乱码,而从数据库查询出 ...
- sqlplus中"-S"和"-L"用法
Usage: SQLPLUS [option] [logon] [start] <option> ::= -H | -V | [ [-L] [-M ] [-R ] [-S] ] &qu ...
- [译] ASP.NET 生命周期 – ASP.NET 上下文对象(七)
使用 HttpRequest 对象 HttpRequest 对象描述的是一个正在被处理的 HTTP 请求.下表列举了 HttpRequest 中的属性,它们提供了当前请求的相关信息(HttpReque ...
- MVC中Controller和Action讲解上篇
一般我们用mvc开发程序时一般需要三个步骤, 创建模型.创建控制器.创建视图 之前开发程序都是按照这样的步骤来开发的,也没有想过mvc的原理,比如route是怎么找到controller的,contr ...
- EXTJS4.2 控件之Grid 行点击事件
listeners: { 'itemclick': function (view, record, item, index, e) { //Ext.MessageBox.alert("标题& ...
- 如何通过logcat查看系统程序的意图
如果在logcat中不能看到系统程序启动时的意图的类名, 以打开图库(gallery)为例,可以通过在ddms中如图设置,就可以在tomcat中查看到gallery启动时的意图.
- C# 5.0 TAP 模式下的HTTP Get和Post
标题有点瘆人,换了工作之后很少写代码了,之前由于签了保密协议,不敢把代码拿出来分享给大家,只能摘抄网上的, 今斗胆拿出来晒晒,跪求指点,直接上代码吧 public class HTTPHelper : ...