bzoj 1783: [Usaco2010 Jan]Taking Turns

1783: [Usaco2010 Jan]Taking Turns

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

Sample Output

27 17

题解:

又是博弈问题。。又是DP。。。

我们是不知道第一个人从哪个开始选,但是肯定在最后一个结束。

所以我们定义f[0][i]表示先手从i到n的最大值,f[1][i]为后手。

显然f[0][i]=f[0][i+1],f[1][i]=f[1][i+1]

对于f[0][i],还有一种情况是选i,那么值为a[i]+f[1][i+1](仔细想想吧)

当然,如果选i,f[1][i]就为f[0][i+1]了

#include<stdio.h>
#include<iostream>
using namespace std;
const int N=700005;
int n,i,a[N];
long long f[2][N];
int main()
{
    scanf("%d",&n);
    for(i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(i=n;i>=1;i--)
    {
        f[0][i]=f[0][i+1];
        f[1][i]=f[1][i+1];
        if(f[1][i+1]+a[i]>=f[0][i])//注意一下等于
        {
            f[0][i]=f[1][i+1]+a[i];
            f[1][i]=f[0][i+1];
        }
    }
    cout<<f[0][1]<<‘ ‘<<f[1][1];
    return 0;
}

优化版:

#include<stdio.h>
#include<iostream>
using namespace std;
const int N=700005;
int n,i,a[N];
long long t,x,y;
int main()
{
    scanf("%d",&n);
    for(i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(i=n;i>=1;i--)
        if(y+a[i]>=x)
        {
            t=x;
            x=y+a[i];
            y=t;
        }
    cout<<x<<‘ ‘<<y;
    return 0;
}
时间: 2024-10-26 21:41:51

bzoj 1783: [Usaco2010 Jan]Taking Turns的相关文章

BZOJ 2021 Usaco2010 Jan Cheese Towers 动态规划

题目大意:完全背包,如果最顶端的物品重量≥k,那么下面的所有物品的重量变为原来的45 考虑一些物品装进背包,显然我要把所有重量大于≥k的物品中重量最小的那个放在最顶端,才能保证总重量最小 那么我们给物品排个序,第一键值为重量是否≥k(≥k的放在前面),第二键值为重量(从小到大) 然后依次加入背包,令fi表示没有重量≥k的物品放在最顶端时重量为i的最大价值,gi表示有重量≥k的物品放在最顶端是重量为i的最大价值,DP即可 时间复杂度O(nT) #include <cstdio> #include

BZOJ1783: [Usaco2010 Jan]Taking Turns

n<=700000个数,两人轮流取数,位置必须单增,输出两人都按最优策略得到的最大答案. 一开始看不懂"最优策略",后来发现没有必要知道,f[i][1/0]--先/后手取数i最优答案,f[i][1]=a[i]+f[maxi][0],f[i][0]=f[maxi][1],maxi>i,因为先手取完后手必定转移到后面最优的先手策略. 然后WA了一发.这两人还是挺友好的不会陷害对方,就是说,有多个f[maxi][1]时,去下标最小的一个,让游戏进行地更持久! 1 #include

BZOJ2021: [Usaco2010 Jan]Cheese Towers

2021: [Usaco2010 Jan]Cheese Towers Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 184  Solved: 107[Submit][Status] Description Farmer John wants to save some blocks of his cows' delicious Wisconsin cheese varieties in his cellar for the coming winter.

2020: [Usaco2010 Jan]Buying Feed, II

2020: [Usaco2010 Jan]Buying Feed, II Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 220  Solved: 162[Submit][Status] Description (buying.pas/buying.in/buying.out 128M 1S) Farmer John needs to travel to town to pick up K (1 <= K <= 100) pounds of feed

BZOJ 2101: [Usaco2010 Dec]Treasure Chest 藏宝箱( dp )

dp( l , r ) = sum( l , r ) - min( dp( l + 1 , r ) , dp( l , r - 1 ) ) 被卡空间....我们可以发现 l > r 是无意义的 , 所以可以省下一半的空间 -------------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<a

[BZOJ] 1614: [Usaco2007 Jan]Telephone Lines架设电话线

1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1806  Solved: 773[Submit][Status][Discuss] Description Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N <= 1,000)根按1..N顺次编号的废

BZOJ 1778: [Usaco2010 Hol]Dotp 驱逐猪猡

1778: [Usaco2010 Hol]Dotp 驱逐猪猡 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 563  Solved: 216[Submit][Status][Discuss] Description 奶牛们建立了一个随机化的臭气炸弹来驱逐猪猡.猪猡的文明包含1到N (2 <= N <= 300)一共N个猪城.这些城市由M (1 <= M <= 44,850)条由两个不同端点A_j和B_j (1 <= A_j<

bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队 分块

1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MB Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. John 准备了Q (1 <= Q <= 180,000) 个可能的牛的

BZOJ 3887[Usaco2015 Jan]Grass Cownoisseur

题面: 3887: [Usaco2015 Jan]Grass Cownoisseur Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 237  Solved: 130[Submit][Status][Discuss] Description In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-way cow pat