题意:给定一个序列表示一群山,要你保留最多 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 (贪心)的更多相关文章

  1. POJ 1456(贪心)

    #include <string.h> #include <iostream> #include <queue> #include <stdio.h> ...

  2. poj -3614 Sunscreen(贪心 + 优先队列)

    http://poj.org/problem?id=3614 有c头奶牛在沙滩上晒太阳,每头奶牛能忍受的阳光强度有一个最大值(max_spf) 和最小值(min_spf),奶牛有L种防晒霜,每种可以固 ...

  3. POJ 3614 Sunscreen 贪心

    题目链接: http://poj.org/problem?id=3614 Sunscreen Time Limit: 1000MSMemory Limit: 65536K 问题描述 to avoid ...

  4. POJ 1456 - Supermarket - [贪心+小顶堆]

    题目链接:http://poj.org/problem?id=1456 Time Limit: 2000MS Memory Limit: 65536K Description A supermarke ...

  5. poj 3404&&poj1700(贪心)

    Bridge over a rough river Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4143   Accept ...

  6. poj 2287(贪心)

    Tian Ji -- The Horse Racing Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 12490   Acc ...

  7. poj 2431 Expedition 贪心 优先队列 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2431 题解 朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪 ...

  8. poj 2431 Expedition 贪心

    简单的说说思路,如果一开始能够去到目的地那么当然不需要加油,否则肯定选择能够够着的油量最大的加油站加油,,不断重复这个贪心的策略即可. #include <iostream> #inclu ...

  9. poj 3154 Graveyard 贪心

    //poj 3154 //sep9 #include <iostream> #include <cmath> using namespace std; double a[204 ...

随机推荐

  1. 联想G510安装win7系统

    1.插入系统盘重启(要按Fn+f12) 2默认下一步 3.选择自定义 4.选择需要将系统安装到的分区,格式化,然后下一步 5.默认下一步 安装到这里就成功了 6.问题处理: 第4步时出现 Window ...

  2. String.format的用法

    有些时候,对于一些东西,不是没有简单的方法,而是我们没有接触到过 String.format();即创建格式化的字符串,里面有很多的通配使用符号,我这里说一下我接触到的,以后接触到其他的再填坑 它的内 ...

  3. 模块math和cmath

    python使用特殊命令import导入模块,再以module.function的方式使用模块 python标准库提供了一个专门用于处理复数的模块cmath,math处理数据 模块math常用的函数有 ...

  4. certificate verify failed (https://gems.ruby-china.org/specs.4.8.gz)

    redis集群配置中 >gem sources -a https://ruby.taobao.org/ Error fetching https://gems.ruby-china.org/: ...

  5. Java项目生成可执行jar包、exe文件以及在Windows下的安装文件

    1.如何通过eclipse将Java项目生成可执行jar包 首先把在eclipse下的java项目导出jar file 下一步 下一步 下一步 最后点击完成,便生成了可执行的jar文件.可以在刚刚选择 ...

  6. HashMap从源码分析数据结构

    1. HashMap在链表中存储的是键值对 2. 数组是一块连续的固定长度的内存空间,再好的哈希函数也不能保证得到的存储地址绝对不发生冲突.那么哈希冲突如何解决呢?哈希冲突的解决方案有多种:开放定址法 ...

  7. day7 回顾

    1,is == == 比较的是值. is 内存地址.对于int str 来说:存在小数据池的概念.为什么存在小数据池? 节省内存. int -5 ~ 256 str 全部字母,数字与单个字母想乘 数字 ...

  8. c# usercontrol 用户自定义控件无法显示在工具箱的解决办法

    由于最开始x86平台,后来换成anyCPU 重新编译也没有删除x86目录下的dll ,也没重新生成 费x86 下的dll , 导致工具箱无法找到目录下的dll 无法加载自定义控件 右击工具栏中的内容选 ...

  9. instanceof 和 typeof

    instanceof 运算符用来检测 constructor.prototype 是否存在于参数 object 的原型链 function Person(){ Person.prototype.dan ...

  10. linux使用笔记4

    运行已经安装好的软件: cd到这个软件的安装目录 输入命令/:./软件名称 就可以了 运行geoserver 1.首先需要添加JAVA_HOME的环境变量: 在服务器没有设置JAVA_HOME的情况下 ...