• 1475 建设国家 DP


    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1475

    这题转化过来就是,给定n个点,每个点都有一个过期时间,一个价值。现在安排他们成一直线,使得总和最大。

    一开始就是贪心,这是一个很经典的贪心。

    http://www.cnblogs.com/liuweimingcprogram/p/6358456.html

    和这题差不多,然后这个比较特别,有一个地方是可以接两个,我就暴力枚举了。然后蜜汁wa

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <assert.h>
    #define IOS ios::sync_with_stdio(false)
    using namespace std;
    #define inf (0x3f3f3f3f)
    typedef long long int LL;
    
    
    #include <iostream>
    #include <sstream>
    #include <vector>
    #include <set>
    #include <map>
    #include <queue>
    #include <string>
    #include <bitset>
    const int maxn = 1e3 + 20;
    struct node {
        int val, cost;
        node(int a, int b) : val(a), cost(b) {}
        node() {}
        bool operator < (const struct node & rhs) const {
            if (val != rhs.val) return val > rhs.val;
            else return cost < rhs.cost;
        }
    }a[maxn];
    int w[maxn];
    bool del[maxn];
    vector<struct node>vc;
    void work() {
        int n, H;
        cin >> n >> H;
        for (int i = 1; i <= n; ++i) {
            cin >> a[i].cost >> a[i].val;
            a[i].cost = H - a[i].cost;
        }
        sort(a + 1, a + 1 + n);
        int ans = 0;
        for (int i = 1; i <= n; ++i) {
            int t = a[i].cost;
            if (t > n) {
                ans += a[i].val; //必定可以,空位也少了一个了,就放去第n天
                del[i] = true;
                continue;
            }
            while (t >= 1 && w[t]) {
                t--;
            }
            if (t) {
                w[t] = a[i].val;
                ans += a[i].val;
                del[i] = true;
            }
        }
        for (int i = 1; i <= n; ++i) {
            if (del[i]) continue;
            vc.push_back(a[i]);
        }
    //    cout << w[1] << endl;
    //    cout << w[2] << endl;
    //    cout << w[3] << endl;
    //    cout << w[4] << endl;
    //    cout << ans << endl;
        int tans = 0;
        for (int i = 1; i <= n; ++i) {
            tans += w[i];
            int tofind = -inf;
            if (w[i] == 0) continue;
            for (int j = 0; j < vc.size(); ++j) {
                if (vc[j].cost < i) continue;
                tofind = max(tofind, vc[j].val);
            }
            ans = max(ans, tans + tofind);
        }
        cout << ans << endl;
    }
    int main() {
    #ifdef local
        freopen("data.txt", "r", stdin);
    //    freopen("data.txt", "w", stdout);
    #endif
        work();
        return 0;
    }
    View Code

    看了题解后居然是dp

    用dp[i][j]表示前i个点中,选出了j个点,的最大答案。

    那么,要求解dp[i][j],则有dp[i][j] = dp[i - 1][j],就是不选第i个点。

    如果要选,那么有两种情况,

    1、把这个点作为结束点,也就是两个点放在一起。这需要这个点的保质期 >= j - 1即可。因为现在求解的是选了j个点,那么以前就是选了j - 1个点,排在一起,所以只需要保质期 >= j - 1,然后更新答案,不更新dp数组

    2、接在后一排,那么需要保质期 >= j

    特别地,一开始就过期的,就不能选。

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <assert.h>
    #define IOS ios::sync_with_stdio(false)
    using namespace std;
    #define inf (0x3f3f3f3f)
    typedef long long int LL;
    
    
    #include <iostream>
    #include <sstream>
    #include <vector>
    #include <set>
    #include <map>
    #include <queue>
    #include <string>
    #include <bitset>
    const int maxn = 1e3 + 20;
    int dp[maxn][maxn];
    struct node {
        int cost, val;
        bool operator < (const struct node & rhs) const {
            if (cost != rhs.cost) return cost < rhs.cost;
            else return val > rhs.val;
        }
    }a[maxn];
    void work() {
        int n, h;
        cin >> n >> h;
        for (int i = 1; i <= n; ++i) {
            cin >> a[i].cost >> a[i].val;
            a[i].cost = h - a[i].cost;
        }
        sort(a + 1, a + 1 + n);
    //    memset(dp, -0x3f, sizeof dp);
        dp[0][0] = 0;
        int ans = 0;
        for (int i = 1; i <= n; ++i) {
            if (a[i].cost == 0) continue;
            for (int j = 1; j <= i; ++j) {
                dp[i][j] = dp[i - 1][j];
                if (a[i].cost >= j - 1) {
                    ans = max(ans, dp[i - 1][j - 1] + a[i].val);
                }
                if (a[i].cost >= j) {
                    dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + a[i].val);
                }
            }
        }
        cout << ans << endl;
    }
    
    int main() {
    #ifdef local
        freopen("data.txt", "r", stdin);
    //    freopen("data.txt", "w", stdout);
    #endif
        work();
        return 0;
    }
    View Code
  • 相关阅读:
    集合set() 和 深浅copy
    Python 数据类型的操作——字典
    Python()- 面向对象
    面向对象的软件开发
    Python数据类型的操作——列表、元组
    Python 数据类型的操作——字符串
    Linux下386中断处理
    任务的休眠与唤醒
    Linux下SIGSTOP的特殊特征和实现
    内核线程对信号的处理策略
  • 原文地址:https://www.cnblogs.com/liuweimingcprogram/p/6395089.html
Copyright © 2020-2023  润新知