zoukankan      html  css  js  c++  java
  • gems gems gems

    Now there are nn gems, each of which has its own value. Alice and Bob play a game with these nn 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 kk or k+1k+1 gems if the other player takes kk gems in the previous turn. The game ends when there are no gems left or the current player can't take kk or k+1k+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.

    InputThe first line contains an integer TT (1T101≤T≤10 ), the number of the test cases.
    For each test case:
    the first line contains a numbers nn (1n200001≤n≤20000 );
    the second line contains n numbers: V1,V2VnV1,V2…Vn . (100000Vi100000−100000≤Vi≤100000 )
    OutputFor 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
    #include<bits/stdc++.h>
    using namespace std;
    const long long INF = 1e18;
    const int N = 20000 + 10;
    const int K = 210;
    const int TN = 255 + 10;
    const int MOD = 255;
    int T, n;
    int v[N], pre[N], dp[2][TN][K];
    int main()
    {
        scanf("%d", &T);
        while(T-- && scanf("%d", &n)!=EOF)
        {
            memset(dp, 0, sizeof(dp));
            for(int i=1;i<=n;i++)
            {
                scanf("%d", &v[i]);
                pre[i] = pre[i-1] + v[i];
            }
            int lmt = sqrt(n*2.0) + 1;
            for(int idx=n;idx;idx--)
            for(int k=lmt;k;k--)
            {
                if(idx+k <= n) {
                    dp[0][idx&MOD][k] = pre[idx+k-1] - pre[idx-1] +
                        max(dp[1][(idx+k)&MOD][k], dp[1][(idx+k+1)&MOD][k+1] + v[idx+k]);
                    dp[1][idx&MOD][k] = -pre[idx+k-1] + pre[idx-1] + 
                        min(dp[0][(idx+k)&MOD][k], dp[0][(idx+k+1)&MOD][k+1] - v[idx+k]);
                }   
                else if(idx+k-1 <= n) {
                    dp[0][idx&MOD][k] = dp[1][(idx+k)&MOD][k] + pre[idx+k-1] - pre[idx-1];
                    dp[1][idx&MOD][k] = dp[0][(idx+k)&MOD][k] - pre[idx+k-1] + pre[idx-1];
                }
            }
            printf("%d
    ", dp[0][1][1]);
        }
    }
    

      

  • 相关阅读:
    Linux基础命令---arch
    JSON漫谈
    django中外键关联表的查询随笔
    <django中render_to_response的可选参数和使用方法>
    有趣的Redis:缓存被我写满了,该怎么办?
    2020全球C++及系统软件技术大会成功落下帷幕
    AWS 宣布创建 Elasticsearch 和 Kibana 分支
    Flutter开发指南之理论篇:Dart语法05(单线程模型,事件循环模型,Isolate)
    自定义注解!绝对是程序员装逼的利器!!
    Java8 Stream
  • 原文地址:https://www.cnblogs.com/--lr/p/7587333.html
Copyright © 2011-2022 走看看