hdu6323(单调决策性DP)

版权声明:如果看得起就随便拿去用吧QWQ https://blog.csdn.net/qkoqhh/article/details/84107763

题意:给定一个n*m的矩阵,可以在矩阵内将A个元素改成0,现在要在矩阵内选出B个不相交的宽为m的矩阵,使得这3个矩阵的和最大

这个可以将所有行压成一个点然后变成序列上的问题,修改可以预处理,显然对每行来说,应优先修改最小的元素,那么就可以预处理出a[i][j],代表第i行修改j次后的权值。显然修改的效果会越来越小,所以f(j)=a[i][j]是一个上凸函数。。

然后设d[p][i][j]为到第i行,选了p个矩形(第p个矩形以第i行结尾),修改了j次的最大值

那么d[p][i][j]=max{max{d[p][i-1][k]+a[i][j-k]},max{d[p-1][v][k]+a[i][j-k]}}

=max{max(d[p][i-1][k],d[p-1][v][k])+a[i][j-k]}

那么设g[p][i][k]=max(d[p][i][k],d[p-1][v][k])

d[p][i][j]=max{g[p][i][k]+a[i][j-k]}

复杂度是O(TnABm),显然会T

而由于a的性质比较特殊,所以这个决策具有单调性

对于2个决策点k<v<j,如果g[p][i][k]+a[i][j-k]<g[p][i][v]+a[i][j-v],由于a[i][x]为上凸函数,所以随着j增加,v一直比k优,因此可以用单调队列维护决策,用二分找到决策区间就可以了。。

/*
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 105
#define nm 10005 
#define pi 3.1415926535897931
using namespace std;
const ll inf=1e9+7;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}
 
 
 




int q[nm],qh,qt,b[NM],n,tot,m,_p,v[nm],f[nm];
ll a[NM][3005],g[NM][nm],d[NM][nm],ans;

int main(){
    //freopen("data.in","r",stdin);
    int _=read();while(_--){
	n=read();tot=read();m=read();_p=read();
	inc(i,1,n){
	    a[i][0]=0;
	    inc(j,1,tot)b[j]=read(),a[i][0]+=b[j];
	    sort(b+1,b+tot+1);
	    inc(j,1,tot)a[i][j]=a[i][j-1]+max(0,-b[j]);
	}
	//inc(i,1,n){inc(j,0,tot)printf("%lld ",a[i][j]);putchar('\n');}
	mem(g);ans=0;
	inc(i,1,n)inc(j,0,m)d[i][j]=-inf;
	inc(p,1,_p){
	    inc(i,1,n){
		q[qh=qt=1]=0;mem(f);mem(v);
		d[i][0]=max(d[i][0],g[i-1][0]+a[i][0]);
		g[i][0]=max(g[i][0],d[i][0]);
		inc(j,1,m){
		    f[j]=max(f[j],f[j-1]);
		    while(qh<=qt&&f[j]!=q[qh])qh++;
		    int s=j;
		    while(qh<=qt){
			s=q[qt]+tot+1;
			for(int x=j,y=min(m,q[qt]+tot);x<=y;)
			    if(g[i-1][q[qt]]+a[i][mid-q[qt]]<=g[i-1][j]+a[i][mid-j])s=mid,y=mid-1;else x=mid+1;
			if(s<=v[q[qt]])qt--;else break;
		    }
		    if(s<=m)v[j]=s,f[s]=j,q[++qt]=j;
		    while(qh<=qt&&f[j]!=q[qh])qh++;
		    //printf("%d %d:%d\n",i,j,q[qh]);
		    d[i][j]=max(d[i][j],g[i-1][q[qh]]+a[i][j-q[qh]]);
		    g[i][j]=max(g[i][j],d[i][j]);
		}
	    }
	    //inc(i,1,n){inc(j,0,m)printf("%lld ",d[i][j]);putchar('\n');}putchar('\n');
	    inc(i,1,n)inc(j,0,m)g[i][j]=max(g[i][j],g[i-1][j]);
	}
	inc(j,0,m)ans=max(ans,g[n][j]);
	printf("%lld\n",ans);
    }
    return 0;
}

Problem E. Find The Submatrix

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 43    Accepted Submission(s): 15


 

Problem Description

Little Q is searching for the submatrix with maximum sum in a matrix of n rows and m columns. The standard algorithm is too hard for him to understand, so he (and you) only considers those submatrixes with exactly m columns.
It is much easier now. But Little Q always thinks the answer is too small. So he decides to reset no more than A cells' value to 0, and choose no more than B disjoint submatrixes to achieve the maximum sum. Two submatrix are considered disjoint only if they do not share any common cell.
Please write a program to help Little Q find the maximum sum. Note that he can choose nothing so the answer is always non-negative.

Input

The first line of the input contains an integer T(1≤T≤10), denoting the number of test cases.
In each test case, there are 4 integers n,m,A,B(1≤n≤100,1≤m≤3000,0≤A≤10000,1≤B≤3).
Each of the following n lines contains m integers, the j-th number on the i-th of these lines is wi,j(|wi,j|≤109), denoting the value of each cell.

Output

For each test case, print a single line containing an integer, denoting the maximum sum.

Sample Input

 

2 5 1 0 1 3 -1 5 -1 -2 5 1 1 1 3 -1 5 -1 -2

Sample Output

 

7 8

Source

2018 Multi-University Training Contest 3

Recommend

chendu

Statistic | Submit | Discuss | Note

猜你喜欢

转载自blog.csdn.net/qkoqhh/article/details/84107763