zoukankan      html  css  js  c++  java
  • P3035 [USACO11DEC]伞头Umbrellas for Cows

    此题难点在伞的价格没有单调性.
    想到了背包, 但是很明显 (O(NM)) 的复杂度太高.
    其实, 考虑到大的伞假如比小的伞便宜, 小的伞完全可以不考虑.
    所以求一个后缀最小值就有单调性了, 然后dp就行了,,,

    #include <cstdio>
    #include <cstring>
    #include <cassert>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    const int MAXN = 5e3 + 10;
    inline int read(){
        char ch = getchar(); int x = 0;
        while(!isdigit(ch)) ch = getchar();
        while(isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
        return x;
    }
    
    int N, M;
    int a[MAXN], c[(int) 1e5 + 10];
    
    ll f[MAXN];
    
    int main(){
        // freopen("p3035.in", "r", stdin);
        cin>>N>>M;
        for(int i = 1; i <= N; i++) a[i] = read();
        sort(a + 1, a + N + 1);
        for(int i = 1; i <= M; i++) c[i] = read();
        for(int i = M - 1; i >= 1; i--) c[i] = min(c[i], c[i + 1]);
        memset(f, 0x7f, sizeof(f)); f[0] = 0;
        for(int i = 1; i <= N; i++) {
            for(int j = 0; j < i; j++) 
                f[i] = min(f[i], f[j] + c[a[i] - a[j + 1] + 1]);
        }
        cout<<f[N]<<endl; 
        return 0;
    }
    
  • 相关阅读:
    看清爱情的本质,学会受伤。
    美股课堂:美国银行开户亲历记
    京JS 2013
    果皮移动
    果皮移动 移动电商!
    http://www.cutt.com/
    简网APP工场-服务介绍
    Get started
    中科院青年公寓
    c++ replaceAll
  • 原文地址:https://www.cnblogs.com/wsmrxc/p/9836643.html
Copyright © 2011-2022 走看看