Building Shops
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 379 Accepted Submission(s): 144
Problem Description
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 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.
Input
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.
Output
For each test case, print a single line containing an integer, denoting the minimal cost.
Sample Input
Sample Output
5
11
// 一条直线上有 n 的教室,想要在这些点上建一些糖果店,建设糖果店的成本分为 2 部分,建设费,右边的非糖果店到这个糖果店的距离差的和(累加到是一个糖果店为止)
//典型DP题
dp[i] 为在 i 建造最后一个糖果店的最小花费的话
丛左到右 dp[i] = min(dp[i],dp[j]+shop[i].v-(n-i+1)*(shop[i].p-shop[j].p)) (1<=j<i) p是位置,v为建造费
还有就是需要排序,还有需要 long long 型
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
#define LL long long
#define MX 3005
struct Shop
{
LL p,v;
bool operator < (const Shop& b)const
{
return p<b.p;
}
}shop[MX];
LL dp[MX]; int main()
{
int n;
while (scanf("%d",&n)!=EOF)
{
for (int i=;i<=n;i++)
scanf("%I64d%I64d",&shop[i].p,&shop[i].v);
sort(shop+,shop++n);
LL total = ;
for (int i=;i<=n;i++)
total += shop[i].p - shop[].p; dp[]=shop[].v+total;
for (int i=;i<=n;i++)
{
for (int j=;j<i;j++)
{
if (j==) dp[i] = dp[j] + shop[i].v - (n-i+)*(shop[i].p-shop[j].p);
else dp[i] = min(dp[i],dp[j]+shop[i].v-(n-i+)*(shop[i].p-shop[j].p));
}
}
LL ans = dp[];
for (int i=;i<=n;i++)
ans = min(dp[i],ans);
printf("%I64d\n",ans);
}
return ;
}
Building Shops的更多相关文章
- HDU6024 Building Shops 2017-05-07 18:33 30人阅读 评论(0) 收藏
Building Shops Time Limit: 2000/1000 MS ...
- HDU6024:Building Shops(简单DP)
Building Shops Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- (hdu 6024) Building Shops
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6024 Problem Description HDU’s n classrooms are on a ...
- hdu6024 Building Shops(区间dp)
https://cn.vjudge.net/problem/HDU-6024 分开考虑某一点种与不种,最后取二者的最小值. dp[i][1] = min(dp[i-1][0], dp[i-1][1]) ...
- HDU 6024 Building Shops
$dp$. $dp[i]$表示到$i$位置,且$i$位置建立了的最小花费,那么$dp[i] = min(dp[k]+cost[i+1][k-1])$,$k$是上一个建的位置.最后枚举$dp[i]$,加 ...
- 【HDU6024】Building Shops
题意 有n个教室排成一排,每个教室都有一个坐标,现在,小Q想建一些糖果商店,在这n个教室里面.总的花费有两部分,在教室i建一个糖果屋需要花费ci,对于没有任何糖果屋的P,需要的花费为这个教室到它左边有 ...
- HDU6024:Building Shops(DP)
传送门 题意 在一条直线上有n个教室,现在要设置糖果店,使得最后成本最小,满足以下两个条件: 1.若该点为糖果店,费用为cost[i]; 2.若不是,则为loc[i]-最近的糖果店的loc 分析 dp ...
- HDU 6024 Building Shops (简单dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6024 题意:有n个room在一条直线上,需要这这些room里面建造商店,如果第i个room建造,则要总 ...
- 2017中国大学生程序设计竞赛 - 女生专场 1002 dp
Building Shops Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
随机推荐
- 模板:什么是Traits
Traits不是一种语法特性,而是一种模板编程技巧.Traits在C++标准库,尤其是STL中,有着不可替代的作用. 如何在编译期间区分类型 下面我们看一个实例,有四个类,Farm.Worke ...
- DEB方式在UBUNTU安装ODOO 8.0
odoo在ubuntu最简单最快速安装方式是deb方式,基本无需再去改数据库配置文件,全自动化了,odoo中文网推荐新手采用此方法 1 安装数据库:sudo apt-get install postg ...
- 基于Opencv自带BP网络的车标简易识别
代码地址如下:http://www.demodashi.com/demo/12966.html 记得把这几点描述好咯:代码实现过程 + 项目文件结构截图 + 演示效果 1.准备工作 1.1 训练集和测 ...
- 基于iOS 10、realm封装的下载器
代码地址如下:http://www.demodashi.com/demo/11653.html 概要 在决定自己封装一个下载器前,我本以为没有那么复杂,可在实际开发过程中困难重重,再加上iOS10和X ...
- Android设置拍照或者上传本地图片
效果例如以下: 看代码: MainActivity类中: package com.example.ceshidemo; import java.io.ByteArrayOutputStream; im ...
- 在Windows Python3.5 安装LightGBM
LightGBM是微软旗下DMTK推出的Gradient Boosting框架,因为其快速高效,以后或许会成为数据挖掘竞赛中的又一个大杀器.地址:https://github.com/Microsof ...
- Anaconda(Spyder)使用Tensorflow
按照上篇文安装成功后,每次使用TensorFlow的时候需要激活conda环境. 在正常情况下,是Anaconda的bin路径在环境变量中,但激活conda-tensorflow环境后,环境变量中存储 ...
- opencv中各种矩阵乘的差别
尊重原创,转载请注明:http://blog.csdn.net/tangwei2014 OpenCV中每次遇到矩阵乘法就乱,各种翻各种查. 这次总结了一下.为了简单明了,还是让样例说话. 1. Mat ...
- php使用N层加密eval gzinflate str_rot13 base64 破解方法汇总
php使用N层加密eval gzinflate str_rot13 base64 破解方法汇总 来源:本站转载 作者:佚名 时间:2011-02-14 TAG: 我要投稿 PHP使用eval(gzin ...
- java-ApiDemo
"飞机大战"玩了一段时间,稍后补上飞机大战完全代码. 赶紧继续后续课程 API String相关方法: 注意:涉及截取/删除/插入字符串位置时,指的是下标x的位置:如果是范围,则带 ...