【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的长方形去填格子的空缺; 如果有填满的方案且方案 ...
随机推荐
- eclipse 去掉Eclipse打开后定期弹出Usage Data Upload对话框
Eclipse 的 UDC 老定期蹦出来说要上传使用数据到 eclipse 官网服务器去除方法: 1.删除 eclipse/plugins 目录下以 org.eclipse.epp.usagedata ...
- Unknown tag (s:property).
Unknown tag (s:property). 在jsp文件中加入此句话:<%@ taglib uri="/struts-tags" prefix="s&quo ...
- Linux下 利用find命令删除所有.svn目录
====================实例============== 删除所有.svn目录 这也是我当初查找 Linux find 命令的目的. 1) find . -type d -name ...
- CF909B Segments
CF909B Segments 题意翻译 题目描述 给你一个整数N.考虑坐标轴上所有可能的部分,在整数点上的端点,坐标在0到N之间,包括它们. 您希望在几个层中绘制这些片段,这样在每个层中这些片段就不 ...
- Grand Central Dispatch(GCD)详解
概述 GCD是苹果异步执行任务技术,将应用程序中的线程管理的代码在系统级中实现.开发者只需要定义想要执行的任务并追加到适当的Dispatch Queue中,GCD就能生成必要的线程并计划执行任务.由于 ...
- 【我所认知的BIOS】—> uEFI AHCI Driver(6) AtaAtapiPassThruSupported的局部变量们
[我所认知的BIOS]-> uEFI AHCI Driver(6) - AtaAtapiPassThruSupported的局部变量们 LightSeed 5/7/2014 前面5个篇文章把EF ...
- android AChartEnginee解说之源代码框架解读
从上周把android ACHartEnginee的源代码check out出来后就一直在看这个东西是怎样使用的,以及底层是怎样实现的,把近期一周对这个东西的了解先发上来,即是给自己做一个总结,也希望 ...
- java根据汉字获取全拼和首字母
import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCase ...
- nyoj--1185--最大最小值(线段树)
最大最小值 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 给出N个整数,执行M次询问. 对于每次询问,首先输入三个整数C.L.R: 如果C等于1,输出第L个数到第R个数 ...
- http server优雅启停原理及验证
问题背景 在 http应用程序重启时,如果直接 kill -9 使程序退出,然后再启动,会存在的问题: 旧的请求未处理完,如果服务端进程直接退出,会造成客户端连接中断(收到 RST) 新请求打过来,服 ...