dp[x][s]代表前x组的元素,是区间[1,s]的全部元素
这种模型应用于随着组数增加,所有考虑组包含的元素单调递增的情形 (严格or不严格均可)
这个组啊,有时候不一定是明确的组,也可能是行...
1.uva 1456 - Cellular Network
/**==========================================
* This is a solution for ACM/ICPC problem
*
* @source£ºuva 1456 - Cellular Network
* @type: dp
* @author: wust_ysk
* @blog: http://blog.csdn.net/yskyskyer123
* @email: 2530094312@qq.com
*===========================================*/
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int INF =0x3f3f3f3f;
const int maxn=100 ;
//const int maxV=12 ;
int T,n,w;
int a[maxn+5];
double dp[maxn+5][maxn+5];
bool cmp(double x,double y)
{
return x>y;
}
int tot;
double get(int num,int x)
{
int ans=0;
for(int i=x;i<x+num;i++)
{
ans+=a[i];
}
return ans*1.0/tot;
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&w);
tot=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]),tot+=a[i];
}
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;i++) dp[0][i]=INF;/**double do not use memset 0x3f**/
dp[0][0]=0;/**if for i=1...n dp[0][i]=0 will be wrong **/
for(int ind=1;ind<=w;ind++)
{
for(int ed=ind;ed<=n;ed++)
{
dp[ind][ed]=INF;
for(int k=ind-1;k<ed;k++)
{
int num=ed-(k+1)+1;
dp[ind][ed]=min(dp[ind][ed], dp[ind-1][k]+ed*get(num,k+1) );
}
}
}
printf("%.4f\n",dp[w][n]);
}
return 0;
}
/*
2
5 2
30 5 10 30 25
5 5
30 5 10 30 25
*/
2.uva 1366 - Martian Mining
/**==========================================
* This is a solution for ACM/ICPC problem
*
* @source:uva 1366 - Martian Mining
* @type: dp
* @author: wust_ysk
* @blog: http://blog.csdn.net/yskyskyer123
* @email: 2530094312@qq.com
*===========================================*/
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int INF =0x3f3f3f3f;
const int maxn= 500 ;
int n,m;
int a[maxn+5][maxn+5], b[maxn+5][maxn+5];
int dp[maxn+5][maxn+5];
int sumB[maxn+5][maxn+5];
int main()
{
while(~scanf("%d%d",&n,&m)&&(n||m))//正确的以0结尾的输入不打~是可以的,但有些题目出的不严谨,题目说以0结束,最后不打~却是有问题的。
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
}
}
for(int i=1;i<=n;i++)
{
sumB[i][0]=0;
for(int j=1;j<=m;j++)
{
scanf("%d",&b[i][j]);
sumB[i][j]=sumB[i][j-1]+b[i][j];
}
}
memset(dp[0],0,sizeof dp[0]);
for(int depth=1;depth<=n;depth++)
{
memset(dp[depth],0,sizeof dp[depth]);
int retA=0;
for(int k=0;k<=m;k++)
{
retA+=a[depth][k];
int retB=sumB[depth][m]-sumB[depth][k];
for(int upk=0;upk<=k;upk++)
{
dp[depth][k]=max(dp[depth][k],dp[depth-1][upk] +retA+retB );
}
}
}
int ans=0;
for(int k=0;k<=m;k++)
{
ans=max(ans,dp[n][k]);
}
printf("%d\n",ans);
}
return 0;
}