lightoj 1031 被区间dp虐哭...看题解才学会

You are playing a two player game. Initially there are n integer numbers in an array and player A and B get chance to take them alternatively. Each player can take one or more numbers from the left or right end of the array but cannot take from both ends at a time. He can take as many consecutive numbers as he wants during his time. The game ends when all numbers are taken from the array by the players. The point of each player is calculated by the summation of the numbers, which he has taken. Each player tries to achieve more points from other. If both players play optimally and player A starts the game then how much more point can player A get than player B?

Input
Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains a blank line and an integer N (1 ≤ N ≤ 100) denoting the size of the array. The next line contains N space separated integers. You may assume that no number will contain more than 4 digits.

Output
For each test case, print the case number and the maximum difference that the first player obtained after playing this game optimally.

Sample Input
Output for Sample Input
2

4
4 -10 -20 7

4
1 2 3 4
Case 1: 7
Case 2: 10

这个题我只能想到最垃圾的那种转移,开三维….
前2是区间..第三是取的次数….
就很2…
而且因为多了1位没办法记忆化…
几乎就是裸的搜索…或者说是枚举……………………….
我他妈的好菜啊………………………………………………………………………
想想都炸一脸…
就没敢写…
也幸亏没写…….
dalao的思路是随便取一下,剩下的就可以进入递归….
想想确实是这样的….
毕竟a-(b-c)=a+c-b……、
这个虽然是小学学的东西
但是我完全没有想到可以用在这里…..
用在记忆化搜索里….
用在区间dp里……..
真的是要好好体会体会……………………
思路真是比我强的不知道哪里去了

#include<iostream>
#include<memory.h>
#include<algorithm>
#include<cstdio>
using namespace std;
int tu[101],qz[101],dp[101][101];
int dfs(int z, int y)
{
    if (dp[z][y] > -1000000)return dp[z][y];
    dp[z][y] = qz[y] - qz[z - 1];
    for (int a = z;a <= y;a++)
    {
        dp[z][y] = max(dp[z][y], qz[a] - qz[z - 1] - dfs(a+1 , y));
        dp[z][y] = max(dp[z][y], qz[y] - qz[a - 1] - dfs(z, a-1));
    }
    return dp[z][y];
}
int main()
{
    int T;
    cin >> T;
    int u=0;
    while (T--)
    {
        memset(qz, 0, sizeof(qz));
        memset(dp, 0, sizeof(dp));
        int n;
        cin >> n;
        for (int a = 0;a <= n;a++)for (int b = a+1;b <= n;b++)dp[a][b] = -1000000;
        for (int a = 1;a <= n;a++)
        {
            cin >> tu[a];
            qz[a] =qz[a - 1] + tu[a];
            dp[a][a] = tu[a];
        }
        printf("Case %d: %d\n", ++u, dfs(1, n));
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sigma函数是指一个数字的所有因子之和。给定一个数字n,需要求出有多少个数字的Sigma函数是偶数。\[2\] 为了解决这个问题,可以先筛选出n范围内的素数(范围在10^6即可),然后对n进行素因子分解。对于每个因子,如果它的Sigma函数中连乘的每一项都是偶数,那么整个Sigma函数就是偶数。具体实现中,可以判断每个因子的平方根是否为偶数,如果是偶数,则减去(平方根+1)/2。\[1\] 另外,还可以使用O(1)的做法来解决这个问题。根据观察,所有的完全平方数及其两倍的值都会导致Sigma函数为偶数。因此,可以直接计算n的平方根,然后减去(平方根+1)/2即可得到结果。\[3\] #### 引用[.reference_title] - *1* [Sigma Function](https://blog.csdn.net/PNAN222/article/details/50938232)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [【LightOJ1336】Sigma Function(数论)](https://blog.csdn.net/qq_30974369/article/details/79009498)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值