【HDU5951 2016ACM ICPC亚洲区沈阳站-重现赛 D】【博弈 DP】Recursive sequence n物品A元B元连续竞价的平衡点

Winning an Auction

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 47    Accepted Submission(s): 3


Problem Description
Alice and Bob play an auction game. Alice has A dollars and Bob has B dollars initially. There are N items on sale. In each round, an item will be sold by the following way. Alice writes down an integer a (0 ≤ a ≤ A) and Bob writes down an integer b (0 ≤ b ≤ B), which are the amount of dollars they want to pay for the item. If a > b, then Alice gets the item and pays a dollars to the seller. If a < b, then Bob gets the item and pays b dollars to the seller. If a = b, then for the 1st, 3rd, 5th, 7th ... round, Alice gets the item and pays a dollars; for the 2rd, 4th, 6th, 8th ... round, Bob gets the item and pays b dollars. Since all the items have the same value, the goal of the auction game is to get as many items as possible. Both Alice and Bob know the values of N,A and B. Your task is to calculate how many items they will get if both of them play optimally.
 

Input
The first line is the number of test cases. Each test case contains 3 integers N,A and B, which are no larger than 255.
 

Output
For each test case, output the number of items Alice and Bob will get if both of them play optimally.
 

Sample Input
  
  
3 1 1 2 2 4 2 3 3 3
 

Sample Output
  
  
Alice 0 Bob 1 Alice 1 Bob 1 Alice 2 Bob 1
 

Source

//http://blog.csdn.net/snowy_smile/article/details/52981616
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
#define MS(x, y) memset(x, y, sizeof(x))
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b > a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b < a)a = b; }
const int N = 256, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
int casenum, casei;
int n, A, B;
short f[2][N][N][N];
void DP()
{
    for (int o = 0; o < 2; ++o)
    {
        for (int a = 0; a <= 255; ++a)
        {
            for (int b = 0; b <= 255; ++b)
            {
                if (a > b)f[o][1][a][b] = 1;
                else if (b > a)f[o][1][a][b] = 0;
                else f[o][1][a][b] = o == 1 ? 1 : 0;
            }
        }
    }
    for (int o = 0; o < 2; ++o)
    {
        for (int p = 2; p <= 255; ++p)
        {
            for (int a = 0; a <= 255; ++a)
            {
                for (int b = 0; b <= 255; ++b)
                {
                    if (a == b)
                    {
                        if (p & 1)f[o][p][a][b] = p / 2 + (o == 1);
                        else f[o][p][a][b] = p / 2;
                        continue;
                    }
                    if ((o - p + 1) & 1)//Alice占据优势
                    {
                        int va = f[o][p - 1][a][b] + 1;                            //初始a买的状态
                        int vb;
                        for (int u = 1; ; ++u)
                        {
                            if (b < u || (vb = f[o][p - 1][a][b - u]) >= va)    //b多花钱没意义
                            {
                                f[o][p][a][b] = va;
                                break;
                            }
                            if (a < u || (va = f[o][p - 1][a - u][b] + 1) <= vb)//a多花钱没意义
                            {
                                f[o][p][a][b] = vb;
                                break;
                            }
                        }
                    }
                    else//Bob占据优势
                    {
                        int vb = f[o][p - 1][a][b];                                //初始b买的状态
                        int va;
                        for (int u = 1; ; ++u)
                        {
                            if (a < u || (va = f[o][p - 1][a - u][b] + 1) <= vb)//a多花钱没意义
                            {
                                f[o][p][a][b] = vb;
                                break;
                            }
                            if (b < u || (vb = f[o][p - 1][a][b - u]) >= va)    //b多花钱没意义
                            {
                                f[o][p][a][b] = va;
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
}
int main()
{
    DP();
    scanf("%d", &casenum);
    for (casei = 1; casei <= casenum; ++casei)
    {
        scanf("%d%d%d", &n, &A, &B);
        int Alice = f[n & 1][n][A][B];
        int Bob = n - Alice;
        printf("Alice %d Bob %d\n", Alice, Bob);
    }
    return 0;
}
/*
【trick&&吐槽】
1,记忆化搜索是比DP慢一些,甚至可能达到2倍常数,所以记忆化搜索这里会TLE儿DP可以AC
2,使用short类型才可以满足这种做法的空间要求
3,如果离线询问的话是可以对空间再次降低一半的

【题意】
http://acm.hdu.edu.cn/showproblem.php?pid=5951

【分析】
用f[o][i][j][k]
表示初始n的奇偶性为o,现在考虑到倒数第i件物品,Alice还剩下j元钱,Bob还剩下k元钱
对于第1 ~ i的所有物品,Alice在最优策略下所能获得的最大物品数。
那么我们可以从0开始枚举物品的实际购买价格。
例如假设现是Alice的奇偶性优势点,Alice可以以x元购买这个物品,那么Alice的收益会是f[o][i - 1][j - x][k] + 1
而Bob如果以x + 1元购买这个物品,使得Alice的收益会是f[o][i - 1][j][k - x - 1]
如果Bob的这个"x + 1元购买"的决策,相比于Alice的"x元购买"策略,能使得Alice的收益变少,则Bob肯定会花这x + 1元
同理,如果Alice的"x + 1"元购买策略,相比于Bob的"x + 1元购买策略",能使得Alice的收益增加,则Alice肯定会花这x + 1元
……
两人一直进行到,其中一人无力出更高价或者即使出更高价也不优。

【时间复杂度&&优化】
O(2 * n ^ 3 * k)

*/


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值