BZOJ2091: [Poi2010]The Minima Game

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   ar   for   

2091: [Poi2010]The Minima GameTime Limit: 10 Sec  Memory Limit: 259 MB
Submit: 243  Solved: 163
[Submit][Status]Description

給出N個正整數,AB兩個人輪流取數,A先取。每次可以取任意多個數,直到N個數都被取走。
每次獲得的得分為取的數中的最小值,A和B的策略都是儘可能使得自己的得分減去對手的得分更大。
在這樣的情況下,最終A的得分減去B的得分為多少。

 

Input

第一行一個正整數N (N <= 1,000,000),第二行N個正整數(不超過10^9)。

 

Output


一個正整數,表示最終A與B的分差。

 

Sample Input3
1 3 1

Sample Output2

HINT

 

第一次A取走3,第二次B取走兩個1,最終分差為2。

 

Source

鳴謝 JZP

題解:巧妙的DP!!!動態規劃,一個人一定是從大到小拿牌,dp[i]表示還剩第i小的牌,能夠獲得的最大收益,顯然答案是dp[n]。然後對於一個dp[i]只有兩種情況,第一種是只拿第i大的,獲得收益是a[i]-dp[i-1],要麼不拿第i大的,收益為dp[i-1]取max即可。還是思維方式的問題啊。。。好題!贊!代碼:
 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set>10 #include<queue>11 #include<string>12 #define inf 100000000013 #define maxn 1000000+514 #define maxm 500+10015 #define eps 1e-1016 #define ll long long17 #define pa pair<int,int>18 #define for0(i,n) for(int i=0;i<=(n);i++)19 #define for1(i,n) for(int i=1;i<=(n);i++)20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)22 #define mod 100000000723 using namespace std;24 inline int read()25 {26     int x=0,f=1;char ch=getchar();27     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}28     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}29     return x*f;30 }31 int n,a[maxn],dp[maxn];32 int main()33 {34     freopen("input.txt","r",stdin);35     freopen("output.txt","w",stdout);36     n=read();37     for1(i,n)a[i]=read();38     sort(a+1,a+n+1);39     for1(i,n)dp[i]=max(dp[i-1],a[i]-dp[i-1]);40     printf("%d\n",dp[n]);41     return 0;42 }
View Code

 

BZOJ2091: [Poi2010]The Minima Game

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.