zoukankan      html  css  js  c++  java
  • AtCoder Beginner Contest 163 **E

    AtCoder Beginner Contest 163 E - Active Infants (DP)

    Problem Statement

    There are NN children standing in a line from left to right. The activeness of the ii-th child from the left is AiAi.

    You can rearrange these children just one time in any order you like.

    When a child who originally occupies the xx-th position from the left in the line moves to the yy-th position from the left, that child earns Ax×|x−y|Ax×|x−y| happiness points.

    Find the maximum total happiness points the children can earn.

    题意:

    (mathit n)个物品,第(mathit i)个物品有(a_i)的能量,

    现在你可以重新将这(mathit n)个物品排列,排列的顺序可以是任意的,

    (mathit i)个物品在新排列中如果被安排在第(mathit j)个位置上,可以产生(|j-i|*a_i)的价值,

    问采取最优排列的情况下,所有物品产生的总价值是多少?

    思路:

    首先定义(b_i.first=a_i,b_i.second=i)

    然后按照(b_i.first)进行降序排序,

    定义(dp[i][j])代表当前放了(i+j)个物品,其中(mathit i)个物品放在紧靠着左边,$mathit j $个物品放在紧靠着右边时产生的最大价值。

    然后转移即可。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    #include <iomanip>
    #include <sstream>
    #include <bitset>
    #include <unordered_map>
    // #include <bits/stdc++.h>
    #define ALL(x) (x).begin(), (x).end()
    #define sz(a) int(a.size())
    #define rep(i,x,n) for(int i=x;i<n;i++)
    #define repd(i,x,n) for(int i=x;i<=n;i++)
    #define pii pair<int,int>
    #define pll pair<long long ,long long>
    #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    #define MS0(X) memset((X), 0, sizeof((X)))
    #define MSC0(X) memset((X), '', sizeof((X)))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define eps 1e-6
    #define chu(x)  if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
    #define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
    #define du2(a,b) scanf("%d %d",&(a),&(b))
    #define du1(a) scanf("%d",&(a));
    using namespace std;
    typedef long long ll;
    ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
    ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
    ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
    ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
    void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
    ");}}}
    void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
    ");}}}
    inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
    inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
    void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '
    ' : ' ');}}
    void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '
    ' : ' ');}}
    const int maxn = 2000 + 10;
    const int inf = 0x3f3f3f3f;
    /*** TEMPLATE CODE * * STARTS HERE ***/
    #define DEBUG_Switch 0
    int n;
    int a[maxn];
    pii b[maxn];
    bool cmp(pii & aa, pii & bb )
    {
        return aa.fi > bb.fi;
    }
    ll dp[maxn][maxn];
    int main()
    {
    #if DEBUG_Switch
        freopen("C:\code\input.txt", "r", stdin);
    #endif
        //freopen("C:\code\output.txt","r",stdin);
        n = readint();
        repd(i, 1, n)
        {
            a[i] = readint();
            b[i] = mp(a[i], i);
        }
        sort(b + 1, b + 1 + n, cmp);
        repd(i, 0, n - 1)
        {
            for (int j = 0; j + i < n; ++j)
            {
                int k = i + j + 1;
                dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + 1ll * b[k].fi * abs(b[k].se - (i + 1)) );
                dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + 1ll * b[k].fi * abs(b[k].se - (n - j)) );
            }
        }
        ll ans = 0ll;
        repd(i, 0, n)
        {
            ans = max(ans, dp[i][n - i]);
        }
        printf("%lld
    ", ans );
        return 0;
    }
    
    
    
    
    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    webpack篇
    js 中对于this 的理解的 经典案例
    AMD、CMD、CommonJs和es6对比
    vue import异步加载js
    vscode 保存自动 格式化eslint 代码
    git设置
    面向对象的三大特性之继承
    面向对象 类与对象及其属性与方法 类的组合
    hashlib模块 hash算法
    configparser模块 配置文件的解析操作
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/12745042.html
Copyright © 2011-2022 走看看