2091: [Poi2010]The Minima Game

2091: [Poi2010]The Minima Game

链接

分析:

  首先排序后,一定是选的连续的一段。

  f[i]表示前i个位置,先手-后手的最大得分。

  那么考虑第i个位置是否选,如果选,先手选的就是从i开始到i的一段,后手在1到i-1就变成了先手,所以就是a[i]-f[i-1]。

  否则第i个位置不选,直接从i-1转移即可。

代码:

#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;
}

 

posted @ 2019-03-29 14:30  MJT12044  阅读(248)  评论(0编辑  收藏  举报