【题目链接】: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的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  3. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  4. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  5. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  6. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  7. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  8. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

  9. 【codeforces 515D】Drazil and Tiles

    [题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...

随机推荐

  1. BZOJ 3674 可持久化并查集加强版(路径压缩版本)

    /* bzoj 3674: 可持久化并查集加强版 http://www.lydsy.com/JudgeOnline/problem.php?id=3674 用可持久化线段树维护可持久化数组从而实现可持 ...

  2. HDU 4341

    分组背包而已.注意的是,每个时间T,要把一组的全加进去比较一次. #include <iostream> #include <cstdio> #include <cstr ...

  3. 前端 自定义format函数

    为字符串创建format方法,用于字符串格式化  {# 前端没有字符串占位符%s的替代方法,以下是自定义字符串替换的方法,以后前端拓展方法都可以使用下面的形式 #} String.prototype. ...

  4. UNIX环境高级编程(5):文件I/O(1)

    UNIX系统中的大多数文件I/O仅仅须要用到5个函数:open.read.write.lseek以及close.本章说明的函数常常称为"不带缓冲的I/0",术语不带缓冲指的是每一个 ...

  5. UESTC--1262--Memory(dfs)

    Memory Time Limit: 1000MS   Memory Limit: 65535KB   64bit IO Format: %lld & %llu SubmitStatus De ...

  6. poj--2631--Roads in the North(树的直径 裸模板)

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2389   Accepted: 117 ...

  7. 蓝桥杯-- 历届试题 核桃的数量 (gcd)

      历届试题 核桃的数量   时间限制:1.0s   内存限制:256.0MB        问题描述 小张是软件项目经理,他带领3个开发组.工期紧,今天都在加班呢.为鼓舞士气,小张打算给每个组发一袋 ...

  8. c++面向对象程序设计 课后题 答案 谭浩强 第四章

    c++面向对象程序设计课后题答案 谭浩强 第四章 1: #include <iostream> using namespace std; class Complex {public: Co ...

  9. 修改mysql连接的密码

    mysql8.0修改密码: ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '你的password'; msyql开启实现 ...

  10. xBIM 学习与应用系列目录

        xBIM 实战04 在WinForm窗体中实现IFC模型的加载与浏览   xBIM 实战03 使用WPF技术实现IFC模型的加载与浏览   xBIM 实战02 在浏览器中加载IFC模型文件并设 ...