codeforce(等....) dp好題集選

2019年到了,馬上又是省賽區域賽的一年了,但是我的dp水平還是堪憂,所以在衆多網站(主要codeforce)蒐羅一下dp的好題刷一下,並在此發一下解題報告來督促自己....

MemSQL Start[c]UP 3.0 - Round 1 C.Pie Rules

題意:有n個派,每個派有一個大小,Alice和Bob有一個令牌,誰有令牌可以選擇當前派給自己或對方,同時下一回合沒有得到派的人將得到令牌,已知每個人都會選擇最優的策略,Alice和Bob最終得到的派的體積分別是多少?

題解:因爲第一回合是Bob擁有令牌,我們設dp[i]代表第i回合選擇的人的最大價值,那麼dp[1]就是Bob的答案,然後逆推進行轉移就可以了,很不錯的dp思維題~~

#include<bits/stdc++.h>
using namespace std;
#define Sheryang main
const int maxn=1e6+7;
typedef long long ll;
const int mod=1e9+7;
#define IO cin.tie(0),ios::sync_with_stdio(false);
#define pi acos(-1)
#define PII pair<ll,ll>
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();if(c == '-')Nig = -1,c = getchar();while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();return Nig*x;}
#define read read()
/** keep hungry and keep calm! **/

int a[maxn],dp[maxn],sum[maxn];
int Sheryang(){

    int n=read;
    for(int i=1;i<=n;i++){
        a[i]=read;
    }
    for(int i=n;i>=1;i--){
        sum[i]=sum[i+1]+a[i];
        dp[i]=max(dp[i+1],sum[i+1]-dp[i+1]+a[i]);
    }

    printf("%d %d\n",sum[1]-dp[1],dp[1]);
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章