【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的长方形去填格子的空缺; 如果有填满的方案且方案 ...
随机推荐
- maven tomcat 热部署
一.什么是热部署 就是在 tomcat 启动的时候进行部署 二.配置 tomcat 1.需要修改 tomcat 的 conf/tomcat-users.xml 配置文件.添加用户名.密码.权限. &l ...
- CodeForcesGym 100548G The Problem to Slow Down You
The Problem to Slow Down You Time Limit: 20000ms Memory Limit: 524288KB This problem will be judged ...
- struts配置 WEB得拷贝要注意的事项
原始WEB文件tutoral===>新文件tutoralTest 要将靠拷贝的文件的contextRoot修改(一定要修改)
- 【百度语音识别】JavaAPI方式语音识别示例
https://ai.baidu.com/forum/topic/show/496730
- C#中的public protected internal private
1.常见的四种方位修饰符关系下图中的protected internal是并集的关系,意思是在命名空间内是internal的,在命名空间外是protected的 2.sealed final seal ...
- G - Arctic Network
G - Arctic Network #include<cmath> #include<cstdio> #include<cstring> #include&l ...
- java连接数据库核心代码
一.oracle String driver = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:Oracle ...
- 详解Mysql分布式事务XA(跨数据库事务)
详解Mysql分布式事务XA(跨数据库事务) 学习了:http://blog.csdn.net/soonfly/article/details/70677138 mysql执行XA事物的时候,mysq ...
- Newton均差插值性质证明
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemh1cWl1aHVp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...
- 0x22 迭代加深
poj2248 真是个新套路.还有套路剪枝...大到小和判重 #include<cstdio> #include<iostream> #include<cstring&g ...