zoukankan      html  css  js  c++  java
  • POJ 2433 Landscaping (贪心)

    题意:给定一个序列表示一群山,要你保留最多 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
    ", ans);
      }
      return 0;
    }
    

      

  • 相关阅读:
    前端全链路优化总结
    到底使用字符数组还是字符串常量
    指针的误区
    指针
    函数使用初体验
    C语言函数使用小试牛
    十年研发经验工程师的嵌入式学习书籍大推荐与学习进阶路线
    TCP/IP协议前期的一知半解
    Ubuntu 16.04 LTS安装sogou输入法详解
    1/0信封——数据链路层,ARP及RARP
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/8653699.html
Copyright © 2011-2022 走看看