【codeforces 234F】Fence
【题目链接】:http://codeforces.com/problemset/problem/234/F
【题意】
你有n块板要凃油漆;
然后每块板有高度h[i];(宽度都为1)
然后每块板只能凃同一种颜色;(不同板则可以涂不同颜色);
板是按下标顺序放着的;
相邻两个板i和j之间;
相同的高度,如果都有板,且它们的颜色不同,则不好的程度+1;
然后;
你能涂a平方单位的红漆,b平方单位的绿漆;
问你把这n块板涂满的最小“不好程度”;
或输出不能涂满;
【题解】
记忆化搜索;
在记录状态的时候;
只要记录前i个板,两种颜色中的一种还剩下多少就可以了,另外一种是能够确定的(只知道一种颜色用了多少,和涂了多少个板,能够知道也即唯一确定另外一种颜色还剩多少);
所以;记搜的数组开成
f[200][4e4][2]就好;
这里的最后一维用于记录前一个涂了什么颜色;
然后根据当前这一列要涂成什么颜色;
做相应的转移就好;
(好古老,要读文件才能提交,不然显示WA)
【Number Of WA】
0
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 210;
const int A = 4e4+100;
const int oo = 0x3f3f3f3f;
int n,a,b,h[N],ans;
int f[N][A][2];
int solve(int idx,int red,int green,int pre){
if (red<0 || green <0) return oo;
if (idx>n) return 0;
int & ret = f[idx][red][pre];
if (ret!=-1) return ret;
ret = min(
solve(idx+1,red-h[idx],green,0)+(pre==0?0:min(h[idx],h[idx-1])),
solve(idx+1,red,green-h[idx],1)+(pre==1?0:min(h[idx],h[idx-1]))
);
return ret;
}
int main(){
//Open();
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
Close();//scanf,puts,printf not use
//init??????
cin >> n;
cin >> a >> b;
rep1(i,1,n){
cin >> h[i];
}
ms(f,-1);
ans = solve(1,a,b,0);
if (ans>=oo){
cout <<-1<<endl;
}
else{
cout <<ans<<endl;
}
return 0;
}
【codeforces 234F】Fence的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
- 【codeforces 515D】Drazil and Tiles
[题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...
随机推荐
- 【hiho一下 第九周】 状态压缩·二
[题目链接]:http://hihocoder.com/problemset/problem/1048 [题意] [题解] 按从左到右然后从上到下的顺序; 依次枚举每个格子是竖条还是横条; 然后在搜索 ...
- CF786A - Berzerk
/* CF786A - Berzerk http://codeforces.com/contest/786/problem/A 博弈论 直接搜出NP状态图.记得要记忆化剪枝. * */ #includ ...
- [SharePoint2010开发入门经典]10、使用SPS2010构建面向服务的应用程序
本章概要: 1.使用SPS自带的web service 2.构建自定义web service 3.使用不同的客户端解决方案部署自定义站点
- Android 进程常驻(5)----开机广播的简单守护以及总结
这是一个轻量级的库,配置几行代码.就能够实如今android上实现进程常驻,也就是在系统强杀下,以及360获取root权限下.clean master获取root权限下都无法杀死进程 支持系统2.3到 ...
- 【前端福利】用grunt搭建自己主动化的web前端开发环境-完整教程
jQuery在使用grunt,bootstrap在使用grunt,百度UEditor在使用grunt,你没有理由不学.不用! 1. 前言 各位web前端开发者.假设你如今还不知道grunt或者听说过. ...
- 《Java虚拟机原理图解》 1.2.3、Class文件里的常量池具体解释(下)
NO9.类中引用到的field字段在常量池中是如何描写叙述的?(CONSTANT_Fieldref_info, CONSTANT_Name_Type_info) 一般而言.我们在定义类的过程中会定义一 ...
- java基础——transient
今天在看struts1源代码的时候,发如今ActionForm中首先声明了两个transient类型的protected变量. 之前没有接触过该transient类型,所以就查了查. transien ...
- invalidate
转载请注明出处: 前言: 本文是我读<Android内核剖析>第13章----View工作原理总结而成的,在此膜拜下作者 .同时真挚地向渴望了解 A ...
- DB-MySQL:MySQL 连接的使用
ylbtech-DB-MySQL:MySQL 连接的使用 1.返回顶部 1. Mysql 连接的使用 在前几章节中,我们已经学会了如何在一张表中读取数据,这是相对简单的,但是在真正的应用中经常需要从多 ...
- Weex学习与实践(一):Weex,你需要知道的事
Weex学习与实践(一):Weex,你需要知道的事 http://coderyi.com/posts/weex1/ 1.命令行工具:weex-toolkit https://github.com/w ...