zoukankan      html  css  js  c++  java
  • 1272 最大距离 只想到了dp

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

    离散化后,用dp[i]表示向右,大于等于i这个数字的最大位置

    dp[i] = max(dp[i + 1], dp[i])

    #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 = 50000 + 20;
    int a[maxn], dp[maxn], id[maxn];
    int n;
    vector<int>da;
    void work() {
        scanf("%d", &n);
        int top = 0;
        da.push_back(0);
        for (int i = 1; i <= n; ++i) {
            scanf("%d", &a[i]);
            da.push_back(a[i]);
        }
        sort(da.begin(), da.end());
        for (int i = 1; i <= n; ++i) {
            id[i] = lower_bound(da.begin(), da.end(), a[i]) - da.begin();
        }
        for (int i = n; i >= 1; --i) {
            dp[id[i]] = max(dp[id[i]], i);
        }
    //    for (int i = 1; i <= n; ++i) {
    //        cout << id[i] << " ";
    //    }
        for (int i = n; i >= 1; --i) {
            dp[i] = max(dp[i], dp[i + 1]);
        }
        int ans = 0;
        for (int i = 1; i <= n; ++i) {
            ans = max(ans, dp[id[i]] - i);
        }
        cout << ans << endl;
    }
    
    int main() {
    #ifdef local
        freopen("data.txt", "r", stdin);
    //    freopen("data.txt", "w", stdout);
    #endif
        work();
        return 0;
    }
    View Code

    然后这题排个序,然后记录下最小的id就好。

    把思维转过来,每个i,去右边找,相当于每个i,向左边找,一样的。

  • 相关阅读:
    bzoj 5455
    hdu 6705
    hdu 6706
    斜率优化
    bzoj3672
    bzoj1367
    bzoj2118
    bzoj2337
    Codeforces 1077D Cutting Out(二分答案)
    Codeforces 1079C Playing Piano(记忆化搜索)
  • 原文地址:https://www.cnblogs.com/liuweimingcprogram/p/6361952.html
Copyright © 2011-2022 走看看