Bags Game

题目传送门

这种博弈问题挺经典的,第一时间就应该想到 区间 D P 区间DP 区间DP ,小小地积累一下吧

解法

设计出 D P DP DP
f l . r : 考虑区间 [ l , r ] . 先手可以获得的最大差值 f_{l.r} : 考虑区间 [l,r] .先手可以获得的最大差值 fl.r:考虑区间[l,r].先手可以获得的最大差值
应该有些题也可以定义为 最大值 , 计算答案时用 总和 算出 差值 , 但是这道不行,计算中途可能减去 A A A C C C 不好记录

考虑转移

  1. 直接取两端:
    显然有 f l , r ← max ⁡ ( a l − f l + 1 , r , a r − f l , r − 1 ) f_{l,r} \gets \max(a_l-f_{l+1,r},a_r-f_{l,r-1}) fl,rmax(alfl+1,r,arfl,r1)

  2. S n u k e Snuke Snuke
    f l , r ← s l , r − A / C − ( f i , i − 1 + B / D + s i , i − 1 + B / D ) f_{l,r}\gets s_{l,r}-A/C-(f_{i,i-1+B /D}+s_{i,i-1+B /D}) fl,rsl,rA/C(fi,i1+B/D+si,i1+B/D)
    用个数据结构或单调队列维护好 f i , i − 1 + B / D + s i , i − 1 + B / D f_{i,i-1+B /D}+s_{i,i-1+B /D} fi,i1+B/D+si,i1+B/D 的最小值就好了

Code

用的单调队列

#include<iostream>
#define S(p,q) s[p+q-1]-s[q-1]
#define V(p,q) (S(p,q)+f[p][q])

using ll = long long ;
using namespace std;

const int N=3e3+7;
ll f[N][N],n,A,B,C,D,s[N],x[N],q[N];
void calc(ll k,ll i,ll Z){
	for(ll j=1,h=1,t=0;j<=n+1-k;j++){
		while(h<=t&&V(k,q[t])>=V(k,j))t--;
		q[++t]=j;
		while(h<t&&q[h]<j+k-i)h++;
		if(j+k>i)f[i][j+k-i]=max(f[i][j+k-i],S(i,j+k-i)-Z-V(k,q[h]));
	}
}
int main(){
	scanf("%d %lld %lld %lld %lld",&n,&A,&B,&C,&D);
	for(ll i=1;i<=n;i++) scanf("%lld",&x[i]),s[i]=s[i-1]+x[i];
	for(ll i=1;i<=n;i++){
		for(ll j=1;j<=n+1-i;j++)f[i][j]=max(x[j]-f[i-1][j+1],x[i+j-1]-f[i-1][j]);
		calc(i-min(i,B),i,A);calc(i-min(i,D),i,C);
	}
	printf("%lld\n",f[n][1]);
}

好难呀。。

Score : 350 points    Problem Statement On the poker table, there are tea bags of N different flavors. The flavors are numbered from 1 through N, and there are A i ​ tea bags of flavor i ( 1≤i≤N). You will play a game using these tea bags. The game has a parameter called difficulty between 1 and A 1 ​ +⋯+A N ​ , inclusive. A game of difficulty b proceeds as follows: You declare an integer x. Here, it must satisfy b≤x≤A 1 ​ +⋯+A N ​ . The dealer chooses exactly x tea bags from among those on the table and gives them to you. You check the flavors of the x tea bags you received, and choose b tea bags from them. If all b tea bags you chose are of the same flavor, you win. Otherwise, you lose. The dealer will do their best to make you lose. You are given Q queries, so answer each of them. The j-th query is as follows: For a game of difficulty B j ​ , report the minimum integer x you must declare at the start to guarantee a win. If it is impossible to win, report −1 instead. DeepL 翻译    问题陈述 扑克桌上有 N 种不同口味的茶包。这些茶包的编号从 1 到 N ,其中有 A i ​ 个茶包的口味是 i ( 1≤i≤N )。 您将用这些茶包做一个游戏。游戏的参数为难度,介于 1 和 A 1 ​ +⋯+A N ​ 之间。难度为 b 的游戏过程如下: 你声明一个整数 x 。这里,它必须满足 b≤x≤A 1 ​ +⋯+A N ​ 。 庄家从桌子上的茶包中恰好选出 x 个茶包给你。 你检查你收到的 x 袋泡茶的味道,从中选择 b 袋泡茶。 如果你选择的所有 b 茶叶都是同一种口味,你就赢了。否则,你输了。 庄家会尽力让你输。 你会得到 Q 个问题,请逐一回答。 j 个问题如下: 对于难度为 B j ​ 的游戏,请报告你必须在游戏开始时宣布的保证获胜的最小整数 x 。如果不可能获胜,则报告 −1 。    Constraints 1≤N≤3×10 5 1≤Q≤3×10 5 1≤A i ​ ≤10 6 ( 1≤i≤N) 1≤B j ​ ≤min(10 6 ,A 1 ​ +⋯+A N ​ ) ( 1≤j≤Q) All input values are integers. DeepL 翻译    限制因素 1≤N≤3×10 5 1≤Q≤3×10 5 1≤A i ​ ≤10 6 ( 1≤i≤N ) 1≤B j ​ ≤min(10 6 ,A 1 ​ +⋯+A N ​ ) ( 1≤j≤Q ) 所有输入值均为整数。    Input The input is given from Standard Input in the following format: N Q A 1 ​ ⋯ A N ​ B 1 ​ ⋮ B Q ​ DeepL 翻译    输入 输入内容由标准输入法提供,格式如下 N Q A 1 ​ ⋯ A N ​ B 1 ​ ⋮ B Q ​    Output Print Q lines. The j-th line ( 1≤j≤Q) should contain the answer to the j-th query. DeepL 翻译    输出 打印 Q 行。 j -th 行( 1≤j≤Q )应包含对 j -th 查询的回答。    Sample Input 1 Copy 4 5 4 1 8 4 1 8 5 2 10 DeepL 翻译    输入样本 1 4 5 4 1 8 4 1 8 5 2 10    Sample Output 1 Copy 1 17 14 5 -1 For the 1-st query, if you declare x=1, then no matter which 1 bag the dealer chooses, you can satisfy the winning condition by choosing appropriate 1 bag among them. Since you cannot choose an integer x less than 1, the answer is 1. For the 2-nd query, if you declare x=17, then no matter which 17 bags the dealer chooses, you can satisfy the winning condition by choosing appropriate 8 bags among them. Conversely, if x<17, the dealer can choose bags to prevent your victory. Thus, the answer is 17. For the 3-rd query, if you declare x=14, then no matter which 14 bags the dealer chooses, you can satisfy the winning condition by choosing appropriate 5 bags among them. Conversely, if x<14, the dealer can choose bags to prevent your victory. Thus, the answer is 14. For the 4-th query, if you declare x=5, then no matter which 5 bags the dealer chooses, you can satisfy the winning condition by choosing appropriate 2 bags among them. Conversely, if x<5, the dealer can choose bags to prevent your victory. Thus, the answer is 5. For the 5-th query, no matter what x you declare, the dealer can choose bags to prevent your victory. Thus, print −1. DeepL 翻译    样本输出 1 1 17 14 5 -1 对于 1 -st疑问句,如果你声明 x=1 ,那么无论庄家选择哪一个 1 包,你都可以通过在其中选择合适的 1 包来满足获胜条件。由于你不能选择小于 1 的整数 x ,所以答案是 1 。 对于 2 /nd这个疑问句,如果你声明 x=17 ,那么无论庄家选择哪一个 17 包,你都可以通过在其中选择适当的 8 包来满足获胜条件。反之,如果 x<17 ,庄家可以选择袋子阻止你获胜。因此,答案是 17 。 对于 3 (rd)这个疑问句,如果你宣布 x=14 ,那么无论庄家选择哪一个 14 包,你都可以通过在其中选择适当的 5 包来满足获胜条件。反之,如果 x<14 ,庄家可以选择袋子阻止你获胜。因此,答案是 14 。 对于 4 -th这个疑问句,如果你宣布 x=5 ,那么无论庄家选择哪一个 5 个袋子,你都可以通过在其中选择适当的 2 个袋子来满足获胜条件。反之,如果 x<5 ,庄家可以选择袋子阻止你获胜。因此,答案是 5 。 对于 5 -th这个疑问句,无论你下出什么 x ,庄家都可以选择袋子来阻止你获胜。因此,请打印 −1 。    Sample Input 2 Copy 5 3 13 13 13 13 2 5 12 13 DeepL 翻译    输入样本 2 5 3 13 13 13 13 2 5 12 13    Sample Output 2 Copy 19 47 51 DeepL 翻译    输出示例 2 19 47 51
08-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值