标签:int names cup mat als copy ssis ppi ons
Time Limit: 2 sec / Memory Limit: 1024 MB
Score : 500 points
ps:一道dp,这次真的是我自己写的!一直没过,把int都改成ll过了= =没注意数据范围qaq
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.
Input is given from Standard Input in the following format:
NA1 A2 ...... AN
Print the maximum total happiness points the children can earn.
4 1 3 4 2
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.
6 5 5 6 1 1 1
58
6 8 6 9 1 2 1
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;
}
标签:int names cup mat als copy ssis ppi ons
原文地址:https://www.cnblogs.com/asunayi/p/12773702.html