题意:给定一个序列表示一群山,要你保留最多 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. 如何开发简单的javaweb项目,jsp+javabean+servlet

    一.相关的软件下载和环境配置 1.下载并配置JDK. 2.下载eclipse. 3.下载并配置apache-tomcat(服务器). 4.下载MySQL(数据库). 5.下载Navicat for M ...

  2. linux查看本服务端口开放情况

    在Linux使用过程中,需要了解当前系统开放了哪些端口,并且要查看开放这些端口的具体进程和用户,可以通过netstat命令进行简单查询. 1.netstat命令各个参数说明如下: -t : 指明显示T ...

  3. Bootstrap(6)图标菜单按钮组件

    一.小图标组件 Bootstrap 提供了免费的 263 个小图标(数了两次),具体可以参考中文官网的组件链接:http://v3.bootcss.com/components/#glyphicons ...

  4. JS 获取屏幕的宽度和高度,各种方式

      Javascript: 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document ...

  5. Failed to read artifact descriptor for xxx:jar的问题解决

    在开发的过程中,尤其是新手,我们经常遇到Maven下载依赖jar包的问题,也就是遇到“Failed to read artifact descriptor for xxx:jar”的错误. 对于这种非 ...

  6. 云计算之 PaaS详解

    PaaS是Platform-as-a-Service的缩写,意思是平台即服务. Paas - 概述 计算机技术 PaaS(Platform-as-a-Service:平台即服务) 全称:(Platfo ...

  7. ALV界面显示

    PERFORM ADD.   IS_LAYOUT_LVC-CWIDTH_OPT = 'X'.   IS_LAYOUT_LVC-SEL_MODE = 'A'. CALL FUNCTION 'REUSE_ ...

  8. sqlserver数据库的备份与还原——完整备份与还原

    sqlserver提供四种数据库备份方式 完整备份:备份整个数据库的所有内容包括书屋和日志 差异备份:只备份上次完整备份后更高的数据部分 事务日志备份:只备份事务日志里的内容 文件或文件组备份:只备份 ...

  9. js的urlencode

    function urlencode (str) { str = (str + '').toString(); return encodeURIComponent(str).replace(/!/g, ...

  10. idea spring-boot gradle mybatis 搭建开发环境

    使用工具idea 2017.2开发,gradle构建项目,使用的技术有spring-boot.mybatis 1.新建项目 说明:1.src为源码路径,开发主要在src下 2.src/main/jav ...