您的位置:首页 > 其它

2017中国大学生程序设计竞赛 - 女生专场 1002 dp

2017-05-07 17:05 363 查看

Building Shops

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0


[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 these n

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   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个教室的坐标和 当前教室建造糖果商店的代价 若当前教室不建造糖果商店则代价为 与左边最近的糖果商店的距离 第一个位置上的教室必然建造糖果商店 问最少的代价。
题解:dp[i][1]表示第i个教室建造糖果商店 前i个教室的最小代价,dp[i][2]表示第i个教室不建造糖果商店 前i个教室的最小代价,具体看代码。HDU 注意while多组输入 orz。
 

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define esp 0.00000000001
struct node
{
ll x;
ll c;
} N[3005];
bool cmp(struct node a,struct node b)
{
return a.x<b.x;
}
ll dp[3005][5];
ll sum[3005];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=1; i<=n; i++)
scanf("%I64d %I64d",&N[i].x,&N[i].c);
sort(N+1,N+1+n,cmp);
for(int i=1; i<=n; i++)
{
dp[i][1]=5e18;
dp[i][2]=5e18;
}
dp[0][1]=0;
dp[0][2]=0;
for(int i=1; i<=n; i++)
{
dp[i][1]=min(dp[i-1][1],dp[i-1][2])+N[i].c;
ll exm=0;
for(int j=i-1; j>=1; j--)
{
exm=exm+(i-j)*(N[j+1].x-N[j].x);//累加距离
dp[i][2]=min(dp[i][2],dp[j][1]+exm);
}
}
printf("%I64d\n",min(dp
[1],dp
[2]));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐