CodeForces ZeptoLab Code Rush 2015
拖了好久的题解,想想还是补一下吧。
A. King of Thieves
直接枚举起点和5个点之间的间距,进行判断即可。
#include <bits/stdc++.h>
using namespace std; char s[]; int main()
{
//freopen("in.txt", "r", stdin); int n;
bool ans = false;
scanf("%d%s", &n, s);
for(int q = ; q < n && !ans; q++) if(s[q] == '*')
for(int l = ; q + *l < n; l++)
{
int i;
for(i = ; i <= ; i++) if(s[q + i*l] == '.') break;
if(i > ) { ans = true; break; }
} printf("%s\n", ans ? "yes" : "no"); return ;
}
代码君
B. Om Nom and Dark Park (DFS)
给一个完全二叉树,增加若干条边的权值,使得从根到每个叶节点的权值之和相同。
直接从下往上维护这颗树,累加所增加的权值。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ( << ) + ;
LL a[maxn], num[maxn], ans, sum[maxn]; int n; void dfs(int o, int d)
{
if(d > n) return;
int lc = o*, rc = o*+;
dfs(lc, d+); dfs(rc, d+);
int m = max(sum[lc] + a[lc], sum[rc] + a[rc]);
ans += m - (sum[lc] + a[lc]);
ans += m - (sum[rc] + a[rc]);
sum[o] = m;
} int main()
{
//freopen("in.txt", "r", stdin); scanf("%d", &n);
for(int i = ; i < (<<(n+)); i++) scanf("%I64d", &a[i]);
dfs(, );
printf("%I64d\n", ans); return ;
}
代码君
C. Om Nom and Candies (部分贪心)
关于部分贪心可以看高逸涵的09年国家队论文。
这道题可以分成两种情况,用不同的办法来解决。
- 有一种糖果的质量大于等于1000,那么我们枚举这种糖果的数量来求最大值。根据题目数据范围,循环的次数不会超过109 / 103 = 106.
- 两种糖果的质量都小于1000,然后选一个性价比比较高的糖果。不妨设
,那么我们说蓝色糖果的数量一定小于Wr。这样贪心的理由就是,如果有Wr个蓝色糖果,我们完全可以换成Wb个红色糖果。因为这种方案的糖果质量是一样的,但是总价值是HbWr < HrWb。所以我们可以枚举蓝色糖果的数量,来求最优解,并且循环的次数不超过1000.
#include <bits/stdc++.h>
using namespace std;
typedef long long LL; int main()
{
LL c, hr, hb, wr, wb, ans = ;
scanf("%I64d%I64d%I64d%I64d%I64d", &c, &hr, &hb, &wr, &wb);
const int M = ;
if(wb > wr) { swap(hr, hb); swap(wr, wb); }
if(wr > M)
{
for(LL i = ; i * wr <= c; i++)
{
LL j = (c - i * wr) / wb;
ans = max(ans, (LL)i*hr + j*hb);
}
cout << ans << endl;
return ;
} if(hb*wr > hr*wb) { swap(hr, hb); swap(wr, wb); }
for(LL i = ; i <= wr && i * wb <= c; i++)
{
LL j = (c - i * wb) / wr;
ans = max(ans, i*hb+j*hr);
}
cout << ans << endl; return ;
}
代码君
D. Om Nom and Necklace (KMP)
题解转自:http://cyberzhg.github.io/blog/Online-Judge/CF526ABCDE/
这道题最终还是并没有看明白 r mod k是怎么来的,(⊙﹏⊙)b
#include <cstdio> const int maxn = + ;
char s[maxn];
int f[maxn], m, k; void getFail()
{
f[] = , f[] = ;
for(int i = ; i < m; i++)
{
int j = f[i];
while(j && s[i] != s[j]) j = f[j];
f[i+] = s[i] == s[j] ? j+ : ;
}
} int main()
{
//freopen("in.txt", "r", stdin);
scanf("%d%d%s", &m, &k, s);
getFail(); for(int i = ; i <= m; i++)
{
int l = i - f[i];
int R = i / l;
if(i % l == )
printf("%c", R / k >= R % k ? '' : '');
else
printf("%c", R / k > R % k ? '' : '');
}
printf("\n"); return ;
}
代码君
E. Transmitting Levels
在CF上发现了一种非常神的解法。首先预处理一下前缀和,然后用一个链表head[i]记录以第i个节点为结尾的,每段上的数之和不超过b的第一个数的下标。当这个跨度大于等于n的时候就说明找到答案了。
len就是记录分成了多少段。
我说的可能不太清楚,但是代码十分好懂,而且十分简练。感觉比官方题解要好很多。
但是我不太能证明它的正确性,为什么一旦找到i - head[i] >= n就输出答案了呢,也就是为什么后面不可能有最优解了呢?
#include <cstdio> typedef long long LL;
const int maxn = + ;
LL a[maxn], sum[maxn], len[maxn], head[maxn]; int main()
{
//freopen("in.txt", "r", stdin); int n, q;
scanf("%d%d", &n, &q);
for(int i = ; i <= n; i++) { scanf("%I64d", &a[i]); a[i+n] = a[i]; }
for(int i = ; i <= n * ; i++) { sum[i] = sum[i-] + a[i]; head[i] = i; }
while(q--)
{
int b; scanf("%d", &b);
for(int i = n + , j = ; i <= n * ; i++)
{
while(sum[i] - sum[j] > b) j++;
head[i] = head[j];
len[i] = len[j] + ;
if(i - head[i] >= n) { printf("%I64d\n", len[i]); break; }
}
} return ;
}
代码君
CodeForces ZeptoLab Code Rush 2015的更多相关文章
- Codeforces - ZeptoLab Code Rush 2015 - D. Om Nom and Necklace:字符串
D. Om Nom and Necklace time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces ZeptoLab Code Rush 2015 D.Om Nom and Necklace(kmp)
题目描述: 有一天,欧姆诺姆发现了一串长度为n的宝石串,上面有五颜六色的宝石.他决定摘取前面若干个宝石来做成一个漂亮的项链. 他对漂亮的项链是这样定义的,现在有一条项链S,当S=A+B+A+B+A+. ...
- ZeptoLab Code Rush 2015 A. King of Thieves 暴力
A. King of Thieves Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/526/pr ...
- ZeptoLab Code Rush 2015 C. Om Nom and Candies 暴力
C. Om Nom and Candies Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/526 ...
- ZeptoLab Code Rush 2015 B. Om Nom and Dark Park DFS
B. Om Nom and Dark Park Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
- ZeptoLab Code Rush 2015
A 题意:给出一串由.*组成的字符串,如果有等间距的五个及五个以上的*存在,则输出yes 直接枚举就可以了 看题一定要仔细啊,做的时候看成必须有五个等间距的".*"才可以跳跃= = ...
- ZeptoLab Code Rush 2015 C. Om Nom and Candies [ 数学 ]
传送门 C. Om Nom and Candies time limit per test 1 second memory limit per test 256 megabytes input sta ...
- ZeptoLab Code Rush 2015 B. Om Nom and Dark Park
Om Nom is the main character of a game "Cut the Rope". He is a bright little monster who l ...
- Codeforces Zepto Code Rush 2014 -C - Dungeons and Candies
这题给的一个教训:Codeforces没有超时这个概念.本来以为1000*(1000+1)/2*10*10要超时的.结果我想多了. 这题由于k层都可能有关系,所以建一个图,每两个点之间连边,边权为n* ...
随机推荐
- Unity3D 错误,nativeVideoFrameCallback解决方法。
原地址:http://blog.csdn.net/alking_sun/article/details/23684733 Unity3D在打包安卓应用的时候,一打开游戏就闪退,接入LogCat之后发现 ...
- C#&java重学笔记(泛型)
C#部分: 1.泛型的出现主要用于解决类.接口.委托.方法的通用性,通过定义泛型类.接口.委托.方法,可以让不同类型的数据使用相同运算规则处理数据,方便了开发. 2.利用System.Nullable ...
- 初步体验javascript try catch机制
javascript在ECMAScript3中引入了try catch finally机制,大致原理和其他语言一样. 我们也可以自定义错误事件. 但是事先声明:我们自定义的错误事件,只支持对name. ...
- HDU4966 GGS-DDU(最小树形图)
之前几天想着补些算法的知识,学了一下最小树形图的朱刘算法,不是特别理解,备了份模板以备不时之需,想不到多校冷不丁的出了个最小树形图,没看出来只能表示对算法不太理解吧,用模板写了一下,然后就过了.- - ...
- HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)
题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...
- [你必须知道的.NET]第三十三回,深入.NET 4.0之,Lazy<T>点滴
发布日期:2009.10.29 作者:Anytao © 2009 Anytao.com ,Anytao原创作品,转贴请注明作者和出处. 对象的创建方式,始终代表了软件工业的生产力方向,代表了先进软件技 ...
- poj 3311(floyd+状态压缩)
题目链接:http://poj.org/problem?id=3311 思路:Floyd + 状态压缩DP 题意是有N个城市(1~N)和一个PIZZA店(0),要求一条回路,从0出发,又回到0,而且 ...
- C语言学习资料(转载)
◆经典C源程序100例:http://post.baidu.com/f?kz=8618367 ◆时钟的驻留程序:http://post.baidu.com/f?kz=10822377 ◆数据结构暨若干 ...
- 20个最受欢迎的Linux命令
http://code.csdn.net/news/2819566 1. 以 root 帐户执行上一条命令 sudo !! 2. 利用 Python 搭建一个简单的 Web 服务器,可通过 ht ...
- C编译器剖析PDF文档及UCC编译器162.3
http://blog.csdn.net/sheisc/article/details/42387857 http://blog.csdn.net/sheisc/article/details/455 ...