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. 【HNOI】 攻城略池 tree-dp

    [题目大意] 给定一棵树,边有边权,每个节点有一些兵,现在叶子节点在0时刻被占领,并且任意节点在x被占领,那么从x+1开始,每单位时间产生一个兵,兵会顺着父亲节点一直走到根(1),其中每经过一个节点, ...

  2. javascript 变量类型判断

    一.typeof 操作符 对于Function, String, Number ,Undefined 等几种类型的对象来说,他完全可以胜任,但是为Array时 "); typeof arr ...

  3. hdu 1159 Common Subsequence(最长公共子序列 DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  4. eclipse执行maven install命令时跳过test

    在pom.xml里面配置一下代码,将跳过test. <plugins> <plugin> <groupId>org.apache.maven.plugins< ...

  5. hasOwnProperty()方法与in操作符

    1.hasOwnProperty() 该方法检测属性存在于实例,还是存在于原型,对于存在于实例中的属性则返回true 2.in 使用该操作符时只要通过对象能够访问到的属性都会返回true

  6. 分布式队列Celery

    Celery是什么? Celery 是一个由 Python 编写的简单.灵活.可靠的用来处理大量信息的分布式系统,它同时提供操作和维护分布式系统所需的工具. Celery 专注于实时任务处理,支持任务 ...

  7. Linux 入门记录:四、Linux 系统常用命令

    一.日期时间 命令 date 查看.设置当前系统时间: date -u 格林威治时间 date %Y-%m-%d 显示格式化的时间 date -s "23:00" 使用 -s 参数 ...

  8. python基础===pendulum '''Python datetimes made easy.'''

    https://pypi.python.org/pypi/pendulum Pendulum的一大优势是内嵌式取代Python的datetime类,可以轻易地将它整合进已有代码,并且只在需要的时候才进 ...

  9. .net连接sql server的几种连接字符串的写法

    .net连接sql server的几种连接字符串的写法 1, 混合验证模式登录 server=电脑名 或 电脑IP;database=数据库名;uid=数据库登录名;password=数据库登录密码 ...

  10. C#ActiveX控件开发

    1.新建项目,选择C#,选择.NET Framework2.0,新建一个Windows窗体控件库项目,命名为ActiveXDemo; 2.右击ActiveXDem项目,选择属性——应用程序——程序集信 ...