Luo Gu P3507 [POI2010] GRA-The Minima Game

topic

DP, and DP is optimized.

This problem is clearly a DP, the state is better defined,

Definition of DP [i] indicates the maximum difference between the total time to take both the number i, are obtained.

Then the state transition equation can be derived, i.e. \ (dp [i] = max (data [j] -dp [j-1]); j <= i \) time complexity \ (O (n ^ 2) \ )

Then click transfer equation can be simplified, i.e. \ (dp [i] = max (dp [i-1], data [i] -dp [i-1]) \) to obtain \ (O (n) \) time the complexity.

The meaning of the equation is the maximum difference at the i-th, if taken alone, that is, \ (Data [i] -dp [. 1-i] \) , and if the former \ (i-1 \) number taken at the same time, the result is \ (dp [i-1] \)

#include <bits/stdc++.h> 
#define N 1010010
#define int long long
using namespace std;
int n, data[N], dp[N];//dp[i]表示双方共取了i个数,所得到的最大差值。 
bool cmp(int a, int b) {
    return a > b;
}   
signed main()
{   
    scanf("%lld", &n);
    for (int i = 1; i <= n; i++)
        scanf("%lld", &data[i]);
    sort(data + 1, data + 1 + n); 
    for (int i = 1; i <= n; i++)
        dp[i] = max(dp[i - 1], data[i] - dp[i - 1]);
    printf("%lld", dp[n]);
    return 0;
}   
/*  
3   
1 3 1 
2   
*/  

Guess you like

Origin www.cnblogs.com/liuwenyao/p/11585344.html