欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POJ_2433 Landscaping(贪心)

程序员文章站 2022-07-12 12:15:46
...

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=lrmax(0,aimax(al,ar))\sum\limits_{i=l}^r max(0,a_i-max(a_l,a_r))。当山峰数大于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;
}
相关标签: 贪心 贪心算法