Codeforces 1322D Reality Show (DP)

Topic Link

https://codeforces.com/contest/1322/problem/D
title written in a very fuzzy surface, it is easy to misread the question, the proposed reference translation: https://www.luogu.com.cn/problem/CF1322D

answer

(The biggest difficulty is to read the question? Read the wrong question did not understand the solution to a problem looking for a day of konjac withered)
consider assuming no elected \ (l_i \) does not increase the limit, then the answer is and order independent, because in election after a fixed set out a number of times each answer all contributions are equal to or less rounded value of this bit divided by the number of bits right.
You can write the following the DP: \ (F [J] [K] \) represents consideration of the \ (0 \) -th bit to \ (J \) bits and exactly \ (K \) th equal to the \ ( j \) bit (equivalent to the first \ (j \) bits contributions \ (k \) ), the transfer can choose how many to enumerate.
Now requires selected \ (L_i \) does not increase, then only the forward sweep from the (DP are also because the original transition from low to high), each table can brush. \ (f [l [i] ] [k] \) may be transferred to \ (F [L [I] + J] [\ lfloor \ FRAC {K +. 1} {2 ^ J} \ rfloor] \) .
Time complexity of implementation should be as I \ (O ((the n-^ 2 + nm) \ the n-log) \) .
Which fairy teach me \ (O (n ^ 2 + nm) \) the wording ah / kel

Code

#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\n",f[n+m][0]);
	return 0;
}

Guess you like

Origin www.cnblogs.com/suncongbo/p/12561232.html