E - Active Infants
Posted asunayi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了E - Active Infants相关的知识,希望对你有一定的参考价值。
Time Limit: 2 sec / Memory Limit: 1024 MB
Score : 500 points
ps:一道dp,这次真的是我自己写的!一直没过,把int都改成ll过了= =没注意数据范围qaq
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 Ai .
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 y -th position from the left, that child earns Ax×|x−y| happiness points.
Find the maximum total happiness points the children can earn.
Constraints
- 2≤N≤2000
- 1≤Ai≤109
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
NA1 A2 ...... AN
Output
Print the maximum total happiness points the children can earn.
Sample Input 1 Copy
4 1 3 4 2
Sample Output 1 Copy
20
If we move the 1 -st child from the left to the 3 -rd position from the left, the 2 -nd child to the 4 -th position, the 3 -rd child to the 1 -st position, and the 4 -th child to the 2 -nd position, the children earns 1×|1−3|+3×|2−4|+4×|3−1|+2×|4−2|=20 happiness points in total.
Sample Input 2 Copy
6 5 5 6 1 1 1
Sample Output 2 Copy
58
Sample Input 3 Copy
6 8 6 9 1 2 1
Sample Output 3 Copy
85
#include <bits/stdc++.h> using namespace std; #define ll long long const int mod=1e9+7; const int N=2e3+5; ll dp[N][N]; ll maxn; struct nobe{ ll v,p; }a[N]; bool cmp(nobe a,nobe b){ return a.v>b.v; } int main(){ ll n; cin>>n; for(ll i=0;i<n;i++){ cin>>a[i].v; a[i].p=i+1; } sort(a,a+n,cmp); for(ll i=0;i<=n;i++) for(ll j=0;j<=n;j++){ if(i+j==n) { maxn=max(maxn,dp[i][j]); break; } int k=i+j; dp[i+1][j]=max(dp[i+1][j],dp[i][j]+abs(a[k].p-i-1)*a[k].v); dp[i][j+1]=max(dp[i][j+1],dp[i][j]+abs(a[k].p-n+j)*a[k].v); } cout<<maxn<<endl; return 0; }
以上是关于E - Active Infants的主要内容,如果未能解决你的问题,请参考以下文章
错误记录Flutter 混合开发获取 BinaryMessenger 报错 ( FlutterActivityAndFragmentDelegate.getFlutterEngine() )(代码片段