gems gems gems

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem Description
Now there are n gems, each of which has its own value. Alice and Bob play a game with these n gems.
They place the gems in a row and decide to take turns to take gems from left to right. 
Alice goes first and takes 1 or 2 gems from the left. After that, on each turn a player can take k or k+1 gems if the other player takes k gems in the previous turn. The game ends when there are no gems left or the current player can't take k or k+1 gems.
Your task is to determine the difference between the total value of gems Alice took and Bob took. Assume both players play optimally. Alice wants to maximize the difference while Bob wants to minimize it.
 
Input
The first line contains an integer T (1≤T≤10), the number of the test cases. 
For each test case:
the first line contains a numbers n (1≤n≤20000);
the second line contains n numbers: V1,V2…Vn. (−100000≤Vi≤100000)
 
Output
For each test case, print a single number in a line: the difference between the total value of gems Alice took and the total value of gems Bob took.
 
Sample Input
1
3
1 3 2
 
Sample Output
4
 
Source

思路:dp,蜜汁题意;滚动数组优化空间;

#include<bits/stdc++.h>
using namespace std; const int N=2e4+,M=2e6+,inf=1e9+; int dp[][][],n,sum[N]; int main()
{
int T,x;
scanf("%d",&T);
while(T--)
{
memset(dp,,sizeof(dp));
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&x),sum[i]=sum[i-]+x;
for(int i=n;i>=;i--)
{
for(int j=;j>=;j--)
{
if(i+j<=n)
{
dp[][i%][j]=max(sum[i+j-]-sum[i-]+dp[][(i+j)%][j],sum[i+j]-sum[i-]+dp[][(i+j+)%][j+]);
dp[][i%][j]=min(-sum[i+j-]+sum[i-]+dp[][(i+j)%][j],-sum[i+j]+sum[i-]+dp[][(i+j+)%][j+]);
}
else if(i+j-<=n)
{
dp[][i%][j]=dp[][(i+j)%][j]+sum[i+j-]-sum[i-];
dp[][i%][j]=dp[][(i+j)%][j]-sum[i+j-]+sum[i-];
}
}
}
printf("%d\n",dp[][][]);
}
return ;
}

gems gems gems

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 625    Accepted Submission(s): 77

Problem Description
Now there are n gems, each of which has its own value. Alice and Bob play a game with these n gems.
They place the gems in a row and decide to take turns to take gems from left to right. 
Alice goes first and takes 1 or 2 gems from the left. After that, on each turn a player can take k or k+1 gems if the other player takes k gems in the previous turn. The game ends when there are no gems left or the current player can't take k or k+1 gems.
Your task is to determine the difference between the total value of gems Alice took and Bob took. Assume both players play optimally. Alice wants to maximize the difference while Bob wants to minimize it.
 
Input
The first line contains an integer T (1≤T≤10), the number of the test cases. 
For each test case:
the first line contains a numbers n (1≤n≤20000);
the second line contains n numbers: V1,V2…Vn. (−100000≤Vi≤100000)
 
Output
For each test case, print a single number in a line: the difference between the total value of gems Alice took and the total value of gems Bob took.
 
Sample Input
1
3
1 3 2
 
Sample Output
4
 
Source

hdu 6199 gems gems gems dp的更多相关文章

  1. HDU 6199 DP 滚动数组

    强行卡内存 这题在CF上好像有道极相似的题 可以想到状态设计为dp[f][i][k]表示f在取完i-1时,此时可以取k个或k+1个的状态下的最大值.之前以为n是1e5,自己想不到怎么设计状态真的辣鸡, ...

  2. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  3. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  4. hdu 2829 Lawrence(斜率优化DP)

    题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...

  5. hdu 4568 Hunter 最短路+dp

    Hunter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  6. HDU 1231.最大连续子序列-dp+位置标记

    最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  7. HDU 1078 FatMouse and Cheese ( DP, DFS)

    HDU 1078 FatMouse and Cheese ( DP, DFS) 题目大意 给定一个 n * n 的矩阵, 矩阵的每个格子里都有一个值. 每次水平或垂直可以走 [1, k] 步, 从 ( ...

  8. HDOJ(HDU).1284 钱币兑换问题 (DP 完全背包)

    HDOJ(HDU).1284 钱币兑换问题 (DP 完全背包) 题意分析 裸的完全背包问题 代码总览 #include <iostream> #include <cstdio> ...

  9. HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛

    普通的数位DP计算回文串个数 /* HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 2-36进制下回文串个数 */ ...

  10. hdu 6199 沈阳网络赛---gems gems gems(DP)

    题目链接 Problem Description Now there are n gems, each of which has its own value. Alice and Bob play a ...

随机推荐

  1. jquery遍历获取每一行数据进行对比

    $("#dtlTable tr:gt(0)").each(function(i){ var orderQtyBy = $("input[name='orderQtyBys ...

  2. shell expr match

    expr match "$pwrdm_stat" ".*,\($pwr_state:[0-9]*\)" 不理解 从字符串开始的位置匹配子串的长度   expr ...

  3. ELK日志分析系统

    部署环境 192.168.1.147 kibana.logstash.Escluster-node-1 192.168.1.151 filebeat.Escluster-node-2.nginx 软件 ...

  4. 细谈 Web Api 图片上传,在使用 Task.ContinueWith 变量无法赋值问题的解决办法!

    在使用Asp.Net Web Api 图片上传接口的时候,到网上找了一些个例子,但大多数找到都是这个版本! [HttpPost] public Task<Hashtable> ImgUpl ...

  5. 12: nginx原理及常用配置

    1.1 nginx基本介绍 1.nginx高并发原理( 多进程+epoll实现高并发 ) 1. Nginx 在启动后,会有一个 master 进程和多个相互独立的 worker 进程. 2. 每个子进 ...

  6. es数据迁移脚本(python)

    #!/usr/bin/python # -*- coding: UTF-8 -*- # 文件名:indiceCreate.py import sys import base64 import time ...

  7. 20145221高其_Web基础

    20145221高其_Web基础 目录 实践目标 Apache 前端编程 后端编程 PHP MYSQL & 后端 简单SQL注入与XSS 发帖和会话管理 总结 实践目标 (1)Web前端HTM ...

  8. 动态规划之91 decode ways

    题目链接:https://leetcode-cn.com/problems/decode-ways/description/ 参考:https://www.jianshu.com/p/5a604070 ...

  9. Junit中AssertTrue的使用

    assertTrue public static void assertTrue(String message, boolean condition) Asserts that a condition ...

  10. linux内核中的hisi_sas是什么?

    答: 是一个HISILICON SAS 控制器驱动(HISILICON SAS controller driver)