POJ_2433 Landscaping(贪心)

Landscaping

Time Limit: 1000MS
Memory Limit: 65536K
Problem Description

Farmer John is making the difficult transition from raising mountain goats to raising cows. His farm, while ideal for mountain goats, is far too mountainous for cattle and thus needs to be flattened out a bit. Since flattening is an expensive operation, he wants to remove the smallest amount of earth possible.

The farm is long and narrow and is described in a sort of two-dimensional profile by a single array of N (1 <= N <= 1000) integer elevations (range 1…1,000,000) like this:
1 2 3 3 3 2 1 3 2 2 1 2,

which represents the farm’s elevations in profile, depicted below with asterisks indicating the heights:

A contiguous range of one or more equal elevations in this array is a “peak” if both the left and right hand sides of the range are either the boundary of the array or an element that is lower in elevation than the peak. The example above has three peaks.

Determine the minimum volume of earth (each unit elevation reduction counts as one unit of volume) that must be removed so that the resulting landscape has no more than K (1 <= K <= 25) peaks. Note well that elevations can be reduced but can never be increased.

If the example above is to be reduced to 1 peak, the optimal solution is to remove 2 + 1 + 1 + 1 = 5 units of earth to obtain this set of elevations:

where '-'s indicate removed earth.

Input

Line 1: Two space-separated integers: N and K

Lines 2…N+1: Each line contains a single integer elevation. Line i+1 contains the elevation for index i.

Output

Line 1: The minimum volume of earth that must be removed to reduce the number of peaks to K.

Sample Input

12 1
1
2
3
3
3
2
1
3
2
2
1
2

Sample Output

5

题意

有n个位置,第i个位置的高度为ai。山峰的定义为一端区间,区间内的位置高度相等,相邻的左边和右边为边界或高度低于区间内的高度。每次操作可以将某位置高度减一,求最少进行多少次操作,能使山峰的数量少于等于k。

题解:

贪心。暴力求出每座山峰的位置及其大小。山峰的大小为移除这座山峰需要的操作次数,设山峰区间为[l,r],则移除山峰的代价为 ∑ i = l r m a x ( 0 , a i − m a x ( a l , a r ) ) \sum\limits_{i=l}^r max(0,a_i-max(a_l,a_r)) i=lrmax(0,aimax(al,ar))。当山峰数大于k时,每次移除代价最小的山峰即可。

#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#include<iterator>
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
#define eps 1e-6
 
using namespace std;
typedef long long LL;   
typedef pair<int, int> P;
const int maxn = 1020;
const int mod = 1000000007;
int a[maxn];

int main()
{
    int n, k, i, j, mi, al, ar, num, ans;
    while(~scanf("%d %d", &n, &k))
    {
        ans = 0;
        for(i=1;i<=n;i++)
            scanf("%d", &a[i]);
        a[0] = a[n+1] = 1;
        n++;
        while(1)
        {
            int ks = 0;
            i = 0;
            mi = INF;
            while(i<n){
                ks++;
                for(j=i+1;j<=n;j++)
                    if(a[j] < a[j-1])break;
                for(;j<=n;j++)
                    if(a[j] > a[j-1])break;
                num = 0;
                int b = max(a[i], a[j-1]);
                for(int s=i;s<j;s++)
                    if(a[s] >= b)num += a[s]-b;
                if(num < mi){
                    mi = num;
                    al = i, ar = j-1;
                }
                i = j-1;
            }
            if(ks <= k)break;
            ans += mi;
            int b = max(a[al], a[ar]);
            for(i=al;i<=ar;i++)
                if(a[i] > b)a[i] = b;
        }
        printf("%d\n", ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值