Description
Farmer John has invented a new way of feeding his cows. He lays out N (1 <= N <= 700,000) hay bales conveniently numbered 1..N in a long line in the barn. Hay bale i has weight W_i (1 <= W_i <= 2,000,000,000). A sequence of six weights might look something like: 17 5 9 10 3 8 A pair of cows named Bessie and Dessie walks down this line after examining all the haybales to learn their weights. Bessie is the first chooser. They take turns picking haybales to eat as they walk (once a haybale is skipped, they cannot return to it). For instance, if cows Bessie and Dessie go down the line, a possible scenario is: * Bessie picks the weight 17 haybale * Dessie skips the weight 5 haybale and picks the weight 9 haybale * Bessie picks the weight 10 haybale * Dessie skips the weight 3 haybale and picks the weight 8 haybale Diagrammatically: Bessie | | 17 5 9 10 3 8 Dessie | | This scenario only shows a single skipped bale; either cow can skip as many as she pleases when it's her turn.Each cow wishes to maximize the total weight of hay she herself consumes (and each knows that the other cow has this goal).Furthermore, a cow will choose to eat the first bale of hay thatmaximimizes her total weight consumed. Given a sequence of hay weights, determine the amount of hay that a pair of cows will eat as they go down the line of hay. 一排数,两个人轮流取数,保证取的位置递增,每个人要使自己取的数的和尽量大,求两个人都在最优策略下取的和各是多少。
Input
* Line 1: A single integer: N * Lines 2..N+1: Line i+1 contains a single integer: W_i
Output
* Line 1: Two space-separated integers, the total weight of hay consumed by Bessie and Dessie respectively
Sample Input
6
17
5
9
10
3
8
17
5
9
10
3
8
Sample Output
27 17
http://blog.sina.com.cn/s/blog_6114627a0100h3tx.html
OTL有点晕不想想了就搬运了一下。。鸣谢【鼓掌】
以下是翻译的c++程序
#include<cstdio>
#include<string>
#include<cstring>
long long f[3][700001];
long long x[700001];
int main()
{
int n;
scanf("%d",&n);
int i;
for(i=1;i<=n;i++)
scanf("%lld",&x[i]);
int reach=n;
long long max=0;
for(i=n;i>=1;i--)
{
f[1][i]=max;
f[2][i]=f[2][reach];
if(f[1][i]<=f[2][reach]+x[i])
{
f[1][i]=f[2][reach]+x[i];
max=f[1][i];
reach=i;
f[2][i]=f[1][i+1];
}
}
printf("%lld %lld\n",f[1][1],f[2][1]);
return 0;
}