ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

「Snackdown 2021 Final」Robbery 题解

2022-01-26 10:33:44  阅读:137  来源: 互联网

标签:int 题解 void tot read 2021 ind inline Final


link

Solution

首先我们可以发现,对于任意一个选出来的物品序列,如果我们排序之后奇偶分类,那么分成的两堆一定相差 \(\le n-1\) 。

然后我们考虑dp,我们可以设 \(f_{a,w}\) 表示选 \(a\) 个物品,重量和为 \(w\) 的价值和最大值,那么对于 \(a\) 为偶数,则有转移式:

\[f_{a,w}=\max_{i} \{f_{a/2,i}+f_{a/2,w-i}\} \]

根据我们发现的性质,\(i\) 的范围就会变为 \([(w-n)/2,(w+n)/2]\)。

对于 \(a\) 为奇数,我们就可以直接选一个把 \(a\) 变为偶数。

不难发现复杂度 \(\Theta(n^2\log k)\)。

Code

#include <bits/stdc++.h>
using namespace std;

#define Int register int
#define int long long
#define MAXM 1000005
#define MAXN 1005

template <typename T> inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;}
template <typename T,typename ... Args> inline void read (T &t,Args&... args){read (t);read (args...);}
template <typename T> inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');}
template <typename T> inline void chkmax (T &a,T b){a = max (a,b);}
template <typename T> inline void chkmin (T &a,T b){a = min (a,b);}

int n,k,W,val[MAXN],ind[MAXM],tot[MAXM],f[35][5005];

int F (int a,int w){
	if (w < a || w > a * n) return -1e18;
	if (~f[ind[a]][w - tot[a] + 2500]) return f[ind[a]][w - tot[a] + 2500];
	if (a == 1) return f[ind[a]][w - tot[a] + 2500] = (w <= n ? val[w] : -1e18);
	if (a & 1){
		int &t = f[ind[a]][w - tot[a] + 2500] = -1e18;
		for (Int i = 1;i <= n && i <= w;++ i) chkmax (t,F (a - 1,w - i) + val[i]);
		return t; 
	}
	else{
		int &t = f[ind[a]][w - tot[a] + 2500] = -1e18;
		for (Int i = (w - n) / 2;i <= (w + n) / 2;++ i) chkmax (t,F (a / 2,i) + F (a / 2,w - i));
		return t;
	}
}

signed main(){
	read (n,k,W);int cnt = 0;tot[k] = W,memset (f,-1,sizeof (f));
	for (Int i = k;i >= 1;) if (i & 1) ind[i] = ++ cnt,tot[i - 1] = tot[i],i --;else ind[i] = ++ cnt,tot[i / 2] = tot[i] / 2,i >>= 1; 
	for (Int i = 1;i <= n;++ i) read (val[i]);
	write (F (k,W)),putchar ('\n');
	return 0;
}

标签:int,题解,void,tot,read,2021,ind,inline,Final
来源: https://www.cnblogs.com/Dark-Romance/p/15845625.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有