复试训练——动态规划——动态规划问题分析举例

题目1452:搬寝室

时间限制:1 秒

内存限制:128 兆

特殊判题:

提交:1777

解决:724

题目描述:

搬寝室是很累的,xhd深有体会.时间追述2006年7月9号,那天xhd迫于无奈要从27号楼搬到3号楼,因为10号要封楼了.看着寝室里的n件物品,xhd开始发呆,因为n是一个小于2000的整数,实在是太多了,于是xhd决定随便搬2*k件过去就行了.但还是会很累,因为2*k也不小是一个不大于n的整数.幸运的是xhd根据多年的搬东西的经验发现每搬一次的疲劳度是和左右手的物品的重量差的平方成正比(这里补充一句,xhd每次搬两件东西,左手一件右手一件).例如xhd左手拿重量为3的物品,右手拿重量为6的物品,则他搬完这次的疲劳度为(6-3)^2 = 9.现在可怜的xhd希望知道搬完这2*k件物品后的最佳状态是怎样的(也就是最低的疲劳度),请告诉他吧。

输入:

每组输入数据有两行,第一行有两个数n,k(2<=2*k<=n<2000).第二行有n个整数分别表示n件物品的重量(重量是一个小于2^15的正整数).

输出:

对应每组输入数据,输出数据只有一个表示他的最少的疲劳度,每个一行.

样例输入:

2 1
1 3

样例输出:

4

代码:

#include <stdio.h>
#include <algorithm>
using namespace std;
#define INF 0xfffffff
int list[2001];
int dp[1001][2001];
int main(){
	int n,k;
	while(scanf("%d%d",&n,&k)!=EOF){
		int i;
		for(i=1;i<=n;i++){
			scanf("%d",&list[i]);
		}
		sort(list+1,list+n+1);
		for(i=1;i<=n;i++){
			dp[0][i]=0;
		}
		for(i=1;i<=k;i++){
			int j;
			for(j=2*i;j<=n;j++){
				if(j>2*i)
					dp[i][j]=dp[i][j-1];
				else
					dp[i][j]=INF;
				if(dp[i][j]>dp[i-1][j-2]+(list[j]-list[j-1])*(list[j]-list[j-1]))
					dp[i][j]=dp[i-1][j-2]+(list[j]-list[j-1])*(list[j]-list[j-1]);
			}
		}
		printf("%d\n",dp[k][n]);
	}
	return 0;
}

题目1453:Greedy Tino

时间限制:1 秒

内存限制:128 兆

特殊判题:

提交:1038

解决:336

题目描述:

 Tino wrote a long long story. BUT! in Chinese...
So I have to tell you the problem directly and discard his long long story. That is tino want to carry some oranges with "Carrying pole", and he must make two side of the Carrying pole are the same weight. Each orange have its' weight. So greedy tino want to know the maximum weight he can carry.

输入:

The first line of input contains a number t, which means there are t cases of the test data.
for each test case, the first line contain a number n, indicate the number of oranges.
the second line contains n numbers, Wi, indicate the weight of each orange
n is between 1 and 100, inclusive. Wi is between 0 and 2000, inclusive. the sum of Wi is equal or less than 2000.

输出:

For each test case, output the maximum weight in one side of Carrying pole. If you can't carry any orange, output -1. Output format is shown in Sample Output.

样例输入:

1
5
1 2 3 4 5

样例输出:

Case 1: 7

代码:

#include <stdio.h>
#define OFFSET 2000
int dp[101][4001];
int  list[101];
#define INF 0xfffffff
int main(){
	int T;
	int cas=0;
	scanf("%d",&T);
	while(T--!=0){
		int n;
		scanf("%d",&n);
		bool HaveZero=false;
		int cnt=0;
		int i;
		for(i=1;i<=n;i++){
			scanf("%d",&list[++cnt]);
			if(list[cnt]==0){
				cnt--;
				HaveZero=true;
			}
		}
		n=cnt;
		for(i=-2000;i<=2000;i++){
			dp[0][i+OFFSET]=-INF;
		}
		dp[0][0+OFFSET]=0;
		for(i=1;i<=n;i++){
			int j;
			for(j=-2000;j<=2000;j++){
				int tmp1=-INF,tmp2=-INF;
				if(j+list[i]<=2000&&dp[i-1][j+list[i]+OFFSET]!=-INF){
				tmp1=dp[i-1][j+list[i]+OFFSET]+list[i];
				}
				if(j-list[i]>=-2000&&dp[i-1][j-list[i]+OFFSET]!=-INF){
					tmp2=dp[i-1][j-list[i]+OFFSET]+list[i];
				}
				if(tmp1<tmp2){
					tmp1=tmp2;
				}
				if(tmp1<dp[i-1][j+OFFSET]){
					tmp1=dp[i-1][j+OFFSET];
				}
				dp[i][j+OFFSET]=tmp1;
			}
		}
		printf("Case %d: ",++cas);
		if(dp[n][0+OFFSET]==0){
			puts(HaveZero==true ? "0":"-1");
		}else
			printf("%d\n",dp[n][0+OFFSET]/2);
	}
	return 0;
}

 

转载于:https://my.oschina.net/u/1996306/blog/835779

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值