题意:给定一个序列表示一群山,要你保留最多 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. 编程,计算data段中的第一组数据的3次方,结果保存在后面一组dword单元中

    assume cs:code data segment dw ,,,,,,, dd ,,,,,,, data ends code segment start: mov ax,data mov ds,a ...

  2. es6问答

    1. 箭头函数的特点 *箭头函数this的指向是定义时所在的对象,而不是使用时所在的对象: * 箭头函数不能做构造函数 * 不能使用argument对象 *不能使用yield命令 2.let cons ...

  3. 【gRPC使用问题3】生成出来无法识别Google.Api.AnnotationsReflection.Descriptor

    1.问题截图: 2.解决方案: Install the package "Google.Api.Gax.Grpc". From the Package Manager Consol ...

  4. C# Request.RawUrl与Request.Url的区别

    RawUrl——不包含域名及端口的地址 Url——包含域名,最全

  5. Ubuntu安装时没注册root用户密码,怎么登录root

    一.其实我个人认为这没有多大必要,因为当你需要 root 的权限时,使用 sudo 便可以了.如果你实在需要在 Ubuntu 中启用 root 帐号的话,那么不妨执行下面的操作: 1.重新设置 roo ...

  6. Vs2015 c# 诊断工具查看程序的占用情况

    windbg用着还不熟悉,dottrace  还要版权,着急查看程序的cpu 的使用情况,因为程序开启之后占用处理器资源较大,问题在哪里呢,于是点开了vs2015自带的诊断工具,以前偶尔打开过,没发现 ...

  7. 在IP网络中,P、PE、CE代表意思

    1.ce , pe属于mpls vpn里的概念.VPN概念中,把整个网络中的路由器分为三类:用户边缘路由器(CE).运营商边缘路由器(PE)和运营商骨干路由器(P):其中,PE充当IP VPN接入路由 ...

  8. Android.PackageManager

    1. Apk的安装和更新过程是怎样的呢? Ref[1] 2. 在安装.apk程序包时, .so是如何选择并安装的? 这里的选择是指,是如何根据CPU_ABI和CPU_ABI2的值来选择合适的.so的. ...

  9. RNA Sequencing

    RNA Sequencing 选择其他单元: RNA Sequencing 国内领先的转录组定序基地 Transcriptome Sequencing可全面性并快速地获得某一物种特定细胞或组织在某一状 ...

  10. web前端学习笔记:文本属性

    今天的web前端笔记主要讲述文本属性,希望能帮助到正在学习web前端开发的初学者们,废话不多说了,一起来看看文本属性的相关内容吧. 文本属性 文本缩进 将Web页面上的一个段落第一行缩进,这是一种最常 ...