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=;
int n,i,a[N];
long long f[][N];
int main()
{
scanf("%d",&n);
for(i=;i<=n;i++)
scanf("%d",&a[i]);
for(i=n;i>=;i--)
{
f[][i]=f[][i+];
f[][i]=f[][i+];
if(f[][i+]+a[i]>=f[][i])//注意一下等于
{
f[][i]=f[][i+]+a[i];
f[][i]=f[][i+];
}
}
cout<<f[][]<<' '<<f[][];
return ;
}

优化版:

#include<stdio.h>
#include<iostream>
using namespace std;
const int N=;
int n,i,a[N];
long long t,x,y;
int main()
{
scanf("%d",&n);
for(i=;i<=n;i++)
scanf("%d",&a[i]);
for(i=n;i>=;i--)
if(y+a[i]>=x)
{
t=x;
x=y+a[i];
y=t;
}
cout<<x<<' '<<y;
return ;
}

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

  1. bzoj 1783: [Usaco2010 Jan]Taking Turns【贪心+dp】

    不知道该叫贪心还是dp 倒着来,记f[0][i],f[1][i]分别为先手和后手从n走到i的最大值.先手显然是取最大的,当后手取到比先手大的时候就交换 #include<iostream> ...

  2. [bzoj1783] [Usaco2010 Jan]Taking Turns

    题意: 一排数,两个人轮流取数,保证取的位置递增,每个人要使自己取的数的和尽量大,求两个人都在最优策略下取的和各是多少. 注:双方都知道对方也是按照最优策略取的... 傻逼推了半天dp......然后 ...

  3. BZOJ 2020 [Usaco2010 Jan]Buying Feed,II:贪心【定义价值】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2020 题意: FJ开车去买K份食物. 如果他的车上有X份食物,每走一里就花费X元. FJ的 ...

  4. BZOJ 2021 [Usaco2010 Jan]Cheese Towers:dp + 贪心

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2021 题意: John要建一个奶酪塔,高度最大为m. 他有n种奶酪.第i种高度为h[i]( ...

  5. BZOJ 2021 Usaco2010 Jan Cheese Towers 动态规划

    题目大意:全然背包.假设最顶端的物品重量≥k,那么以下的全部物品的重量变为原来的45 考虑一些物品装进背包,显然我要把全部重量大于≥k的物品中重量最小的那个放在最顶端.才干保证总重量最小 那么我们给物 ...

  6. BZOJ2021: [Usaco2010 Jan]Cheese Towers

    2021: [Usaco2010 Jan]Cheese Towers Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 184  Solved: 107[Su ...

  7. 2020: [Usaco2010 Jan]Buying Feed, II

    2020: [Usaco2010 Jan]Buying Feed, II Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 220  Solved: 162[ ...

  8. [bzoj 1782] [Usaco2010 Feb]slowdown慢慢游

    [bzoj 1782] [Usaco2010 Feb]slowdown慢慢游 Description 每天Farmer John的N头奶牛(1 <= N <= 100000,编号1-N)从 ...

  9. [bzoj 3048] [Usaco2013 Jan]Cow Lineup

    [bzoj 3048] [Usaco2013 Jan]Cow Lineup Description 给你一个长度为n(1<=n<=100,000)的自然数数列,其中每一个数都小于等于10亿 ...

随机推荐

  1. hdu 1281 棋盘游戏(二分匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281 棋盘游戏 Time Limit: 2000/1000 MS (Java/Others)    M ...

  2. Java 中的静态内部类

    静态内部类是 static 修饰的内部类,这种内部类的特点是: 1. 静态内部类不能直接访问外部类的非静态成员,但可以通过 new 外部类().成员 的方式访问 2. 如果外部类的静态成员与内部类的成 ...

  3. perl登录ssh

    use warnings; use strict; use Net::SSH::Perl; my $host = '192.168.255.128'; my $username = 'root'; m ...

  4. vuejs怎么在服务器部署?

    通过npm run build 把生成的dist文件夹(不要上传文件夹)里的内容上传到http服务器上就可以通过 http来访问了,开发机上正常,上传以后 程序出现错误不能运行的原因99.99%的可能 ...

  5. DTW 算法(转)

    DTW为(Dynamic Time Warping,动态时间归准)的简称.应用很广,主要是在模板匹配中,比如说用在孤立词语音识别,计算机视觉中的行为识别,信息检索等中.可能大家学过这些类似的课程都看到 ...

  6. [c++,bson] linux 使用 BSON 编程[www]

    [c++,bson] linux 使用 BSON 编程 http://blog.chinaunix.net/uid-28595538-id-4987410.html 1.js db=db.getSib ...

  7. 【Educationcal Codeforces Round 21】

    这场edu我原本以为能清真一点…… 后来发现不仅是七题 还有各种奇奇怪怪的骚操作…… A. 随便枚举 #include<bits/stdc++.h> using namespace std ...

  8. C 实现有追求的线程池 后续

    引言 -_- 还是老套路开局 很久以前写过一个有追求的线程池 -> C 实现有追求的线程池 探究 讲述的是一种思路, 并且实现了. 可以一用. 最近在详细搞simplec 框架. 准备发布个正式 ...

  9. 给自己立一个flag

    工作理念:做完!做对!做好!做优! 1.请教问题方面 遇到问题先自己想办法解决(限定时长为30分钟). 请教问题的时候,明确:“问题是什么,为什么错在那里,结果是什么” 2.博客 一周两篇左右:对工作 ...

  10. HDU-1083

    Courses Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...