您的位置:首页 > 产品设计 > UI/UE

HDU6024 Building Shops 2017中国大学生程序设计竞赛 - 女生专场

2017-07-28 09:50 405 查看


Building Shops

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 1125    Accepted Submission(s): 424


[align=left]Problem Description[/align]
HDU’s
n
classrooms are on a line ,which can be considered as a number line. Each classroom has a coordinate. Now Little Q wants to build several candy shops in thesen
classrooms.

The total cost consists of two parts. Building a candy shop at classroom
i would
have some cost ci.
For every classroom P
without any candy shop, then the distance between
P
and the rightmost classroom with a candy shop on P's
left side would be included in the cost too. Obviously, if there is a classroom without any candy shop, there must be a candy shop on its left side.

Now Little Q wants to know how to build the candy shops with the minimal cost. Please write a program to help him.

 

[align=left]Input[/align]
The input contains several test cases, no more than 10 test cases.

In each test case, the first line contains an integer
n(1≤n≤3000),
denoting the number of the classrooms.

In the following n
lines, each line contains two integers xi,ci(−109≤xi,ci≤109),
denoting the coordinate of the i-th
classroom and the cost of building a candy shop in it.

There are no two classrooms having same coordinate.
 

[align=left]Output[/align]
For each test case, print a single line containing an integer, denoting the minimal cost.
 

[align=left]Sample Input[/align]

3
1 2
2 3
3 4
4
1 7
3 1
5 10
6 1

 

[align=left]Sample Output[/align]

5
11

简单的动态规划题目
题目意思:
n个教室,要在这n个教室中建几个糖果屋,总花费包括两部分,
第一部分:在第i个教室修建糖果屋花费ci
第二部分:如果P 教室没有修建糖果屋,那么把P到P左边第一个糖果屋的距离 算作费用。

每一个点 分为两种状态 修建和不修建 那么状态转移方程即为
dp[i][1]=c[i]+[b]min(dp[i-1][0]+dp[i][1]);[/b]
c[i]表示在第i个教室建糖果屋的时候的花费,min(dp[i-1][0]+dp[i][1]);表示取前一个位置两种状态的最小值。
每一次都取最小值。
dp[i][0]=min(dp[j][1]+sum[j][i],dp[i][0]); 
这个表示 在左边第J个教室建糖果屋,在 j-i之间的教室都不建糖果屋,
花费为  dp[j][1]+sum[j][i]
枚举i左边所有的j,sum[j][i]表示j->i 中距离的求和
比如有5个位置  1 2 3 4 5  sum[1][5]=(5-1)+(4-1)+(3-1)+(2-1)=10;
这样 大题思路就清晰了,注意一下细节问题即可。

#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll maxn=5000;
const ll inf=1e18;
/*
一开始inf取小了,WA了一次,看了下数据,1e-9 -  1e9 所以 inf应该去
1e18 AC
*/

ll dp[maxn][2];
/*
dp[i][0] 表示在第I个位置不修建糖果屋的最小花费
dp[i][1] 表示在第i个位置修建糖果屋的最小花费
*/
ll sum[maxn][maxn];
/*
sum[j][i]表示j->i 中距离的求和
比如有5个位置  1 2 3 4 5  sum[1][5]=(5-1)+(4-1)+(3-1)+(2-1)=10;
*/

ll n;
struct point{
ll x;//教室的位置 x
ll w;//在当前位置修建糖果屋的花费 即权值 weight
}a[maxn];

bool cmp(point a,point b){
return a.x< b.x;
}

int main(){
while(scanf("%d",&n)==1&&n){
for(int i=0;i<n;i++){
scanf("%I64d%I64d",&a[i].x,&a[i].w);
}

/*
考虑到坐标可能不是按照顺序输入,所以要先排序处理一下
*/
sort(a,a+n,cmp);

/*
测试数据输入
for(int i=0;i<n;i++){
printf("%d %d\n",a[i].x,a[i].w);
}
*/

/*
对dp[i][0]  dp[i][1]进行初始化
*/
for(int i=0;i<n;i++){
dp[i][0]=dp[i][1]=inf;
}

/*
求距离
利用累加的思想来做
*/
for(int i=0;i<n;i++){
sum[i][i]=0;
for(int j=i+1;j<n;j++){
sum[i][j]=sum[i][j-1]+a[j].x-a[i].x;
}
}

dp[0][1]=a[0].w;
for(int i=1;i<n;i++){
dp[i][1]=a[i].w + min(dp[i-1][0],dp[i-1][1]);
/*j从i-1 遍历 到 0 求出dp[i][0]的最小值*/
for(int j=i-1;j>=0;j--){
dp[i][0]=min(dp[i][0],dp[j][1]+sum[j][i]);
}
}
/*输出的时候也要注意一下,要对最后的两个状态取Min*/
printf("%I64d\n",min(dp[n-1][0],dp[n-1][1]));
}
return 0;
}


代码参考:http://blog.csdn.net/deepseazbw/article/details/71474208  对代码增加了注释
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐