Codeforces 1322D Reality Show (DP)

题目链接

https://codeforces.com/contest/1322/problem/D
题面写得非常模糊,很容易读错题,建议参考翻译:https://www.luogu.com.cn/problem/CF1322D

题解

(最大的难点是读题?读错题看了一天题解没看懂的蒟蒻枯了)
考虑假设没有选出的 (l_i) 不增这个限制,那么答案是和顺序无关的,因为在选出的集合固定后每一位对答案贡献的次数都等于所有小于等于这一位的数之和除以位权下取整的值。
那么可以写出如下 DP: (f[j][k]) 表示考虑了第 (0) 位至第 (j) 位,且恰好有 (k) 个等于第 (j) 位 (就相当于第 (j) 位贡献为 (k)),转移可以枚举选多少个。
现在要求选出的 (l_i) 不增,那么只需要从后往前扫 (因为原来的 DP 也都是从低位转移到高位),每次刷表即可。(f[l[i]][k]) 可以转移到 (f[l[i]+j][lfloorfrac{k+1}{2^j} floor]).
时间复杂度按我的实现应该是 (O((n^2+nm)log n)).
哪位神仙教教我 (O(n^2+nm)) 的写法啊 /kel

代码

#include<bits/stdc++.h>
#define llong long long
#define mkpr make_pair
#define riterator reverse_iterator
#define y1 Lorem_ipsum_dolor
using namespace std;

inline int read()
{
	int x = 0,f = 1; char ch = getchar();
	for(;!isdigit(ch);ch=getchar()) {if(ch=='-') f = -1;}
	for(; isdigit(ch);ch=getchar()) {x = x*10+ch-48;}
	return x*f;
}

const int mxN = 2e3;
int c[mxN+3],a[mxN+3],w[mxN+mxN+3];
int f[mxN+mxN+3][mxN+3];
int mx[mxN+mxN+3];
int n,m;

void updmax(int &x,int y) {x = max(x,y);}

int main()
{
	n = read(),m = read();
	for(int i=1; i<=n; i++) c[i] = read();
	for(int i=1; i<=n; i++) a[i] = read();
	for(int i=1; i<=n+m; i++) w[i] = read();
	memset(f,213,sizeof(f));
	for(int i=1; i<=n+m; i++) f[i][0] = 0;
	for(int i=n; i>=1; i--)
	{
		for(int k=mx[c[i]]; k>=0; k--)
		{
			int tmp = k+1,val = -a[i]+w[c[i]];
			for(int j=0; tmp; j++,tmp>>=1,val+=w[c[i]+j]*tmp)
			{
				updmax(mx[c[i]+j],tmp);
				updmax(f[c[i]+j][tmp],f[c[i]][k]+val);
			}
		}
		for(int j=1; j<=n+m; j++) {updmax(f[j][0],max(f[j-1][0],f[j-1][1]));}
	}
	printf("%d
",f[n+m][0]);
	return 0;
}
原文地址:https://www.cnblogs.com/suncongbo/p/12561232.html