POJ 2433 Landscaping (贪心)
题意:给定一个序列表示一群山,要你保留最多 K 个山峰,最少要削去多少体积和土。一个山峰是指一段连续的相等的区间,并且左边和右边只能比这个区间低,或者是边界。
析:贪心,每次都寻找体积最小的山峰,然后把它削去,每次削的是最小的,所以是满足贪心的,最后剩下的小于 K 个就可以了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define be begin()
#define ed end()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,n,x) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-10;
const int maxn = 1000 + 10;
const int maxm = 20;
const LL mod = 1000000007;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
}
inline int readInt(){ int x; scanf("%d", &x); return x; }
inline double readDouble(){ double x; scanf("%lf", &x); return x; } int a[maxn]; int main(){
int K;
while(scanf("%d %d", &n, &K) == 2){
for(int i = 1; i <= n; ++i) scanf("%d", a + i); a[n+1] = 0; int ans = 0;
while(true){
int cnt = 0, L, R; m = INF;
for(int i = 1; i <= n; ++i) if(a[i] > a[i+1]){
int l = i, r = i;
while(l >= 1 && a[l-1] <= a[l]) --l;
while(r <= n && a[r+1] <= a[r]) ++r;
if(l >= 1 || r <= n){
++cnt;
int t = max(a[l], a[r]);
int sum = 0;
for(int j = l+1; j < r; ++j) if(a[j] > t) sum += a[j] - t;
if(sum < m){ m = sum; L = l; R = r; }
}
i = r;
}
if(cnt <= K) break;
ans += m;
int t = max(a[L], a[R]);
for(int i = L+1; i < R; ++i) if(a[i] > t) a[i] = t;
}
printf("%d\n", ans);
}
return 0;
}
POJ 2433 Landscaping (贪心)的更多相关文章
- POJ 1456(贪心)
#include <string.h> #include <iostream> #include <queue> #include <stdio.h> ...
- poj -3614 Sunscreen(贪心 + 优先队列)
http://poj.org/problem?id=3614 有c头奶牛在沙滩上晒太阳,每头奶牛能忍受的阳光强度有一个最大值(max_spf) 和最小值(min_spf),奶牛有L种防晒霜,每种可以固 ...
- POJ 3614 Sunscreen 贪心
题目链接: http://poj.org/problem?id=3614 Sunscreen Time Limit: 1000MSMemory Limit: 65536K 问题描述 to avoid ...
- POJ 1456 - Supermarket - [贪心+小顶堆]
题目链接:http://poj.org/problem?id=1456 Time Limit: 2000MS Memory Limit: 65536K Description A supermarke ...
- poj 3404&&poj1700(贪心)
Bridge over a rough river Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4143 Accept ...
- poj 2287(贪心)
Tian Ji -- The Horse Racing Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 12490 Acc ...
- poj 2431 Expedition 贪心 优先队列 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2431 题解 朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪 ...
- poj 2431 Expedition 贪心
简单的说说思路,如果一开始能够去到目的地那么当然不需要加油,否则肯定选择能够够着的油量最大的加油站加油,,不断重复这个贪心的策略即可. #include <iostream> #inclu ...
- poj 3154 Graveyard 贪心
//poj 3154 //sep9 #include <iostream> #include <cmath> using namespace std; double a[204 ...
随机推荐
- mac navicat premium 使用技巧
快捷键 CMD-I:对象信息 CMD-L:查询日志 CMD-Y:新建查询 SHIFT-CMD-T:数据传输 SHIFT-CMD-C:命令列界面
- sqlserver DBA面试题
1.sqlserver 2008 R2 on windows server 2008 R2群集中,有节点A.B,现在需要停机新添加一个节点C进来替换现有节点B,请列出必要的步骤. 2.sqlserve ...
- Debug的使用
1.什么是Debug? Debug是DOS.Windows都提供的实模式(8086方式)程序的调试工具.使用它,可以查看CPU各种寄存器的内容.内存的情况和在机器码级跟踪程序的运行. 我用的是Mac机 ...
- 弹框alertView
// 创建一个弹框UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“标题” message:@“显示的具体内容” delegate:s ...
- 关于transform-style:preserve-3d的些许明了
父元素要添加属性transform-style:preserve-3d;和transform:perspective(800px);还有相对定位 首先设置子元素 具有3D属性,然后再设置视角与3D元素 ...
- hdu 3415(单调队列) Max Sum of Max-K-sub-sequence
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=3415 大意是给出一个有n个数字的环状序列,让你求一个和最大的连续子序列.这个连续子序列的长度小于等于k. ...
- 839A Arya and Bran
A. Arya and Bran time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- BZOJ1226或洛谷2157 [SDOI2009]学校食堂
BZOJ原题链接 洛谷原题链接 注意到\(B[i]\)很小,考虑状压\(DP\). 设\(f[i][j][k]\)表示前\(i - 1\)个人已经拿到菜,第\(i\)个人及其后面\(7\)个人是否拿到 ...
- 转载(windows下安装mysql)
转载请声明出处:http://blog.csdn.net/u013067166/article/details/49951577 最近重装了系统,去MySQL官网下载了最新的M ...
- Tinyos学习笔记(一)
简述:发送和接受数据的程序分别烧录到两个节点上,发送方发送流水灯数据,接受方接受数据并实现流水灯 1.发送和接受程序用到的组件及其接口如图(通过make telosb docs获得)所示: 2.发 ...