1. 程式人生 > >hdu6323(單調決策性DP)

hdu6323(單調決策性DP)

題意:給定一個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