連接php
分析:ios
首先排序後,必定是選的連續的一段。git
f[i]表示前i個位置,先手-後手的最大得分。spa
那麼考慮第i個位置是否選,若是選,先手選的就是從i開始到i的一段,後手在1到i-1就變成了先手,因此就是a[i]-f[i-1]。code
不然第i個位置不選,直接從i-1轉移便可。blog
代碼:排序
#include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #include<cmath> #include<cctype> #include<set> #include<queue> #include<vector> #include<map> #include<bitset> using namespace std; typedef long long LL; inline int read() { int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1; for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f; } const int N = 1000005; LL a[N], dp[N]; int main() { int n = read(); for (int i = 1; i <= n; ++i) a[i] = read(); sort(a + 1, a + n + 1); LL mx = 0, mn = 1e18; dp[1] = a[1]; for (int i = 2; i <= n; ++i) { dp[i] = max(dp[i - 1], a[i] - dp[i - 1]); } cout << dp[n]; return 0; }