Building Shops

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

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
3
1 2
2 3
3 4
4
1 7
3 1
5 10
6 1
 
Sample Output
5
11
 
Source
 
 
题意:
在n个在一条线的教室里面开店,给出每个教室的位置和开店需要的钱,若该教室开店就消耗开店花的钱,不开店则消耗该教室到左边最近的开店的教室的距离的钱,不开店的教室左边一定有开店的教室,求最少花费多少钱。
题解:
由题意可得第一个教室一定会开店,由题目的两个状态类似背包,因此可以用DP来做,设DP[n][2],DP[n][0]代表店铺n-1没有开店花费最少的钱,DP[0][1]代表店铺n-1开店花费最少的钱。
因此可以得一转移方程为dp[i][1] = classes[i].price + min(dp[i - 1][0], dp[i - 1][1]);。而关于dp[i][0]则可以枚举来得到最小值。
重点:m += (i - f) * (classes[f + 1].point - classes[f].point);
注意:该题数据应用long long!我因为这个卡了十分钟……
具体见如下代码:
#define _CRT_SECURE_NO_DepRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <algorithm>
#include <bitset>
#include <cstdlib>
#include <cctype>
#include <iterator>
#include <vector>
#include <cstring>
#include <cassert>
#include <map>
#include <queue>
#include <set>
#include <stack>
#define ll long long
#define INF 0x3f3f3f3f
#define ld long double
const ld pi = acos(-1.0L), eps = 1e-8;
int qx[4] = { 0,0,1,-1 }, qy[4] = { 1,-1,0,0 }, qxx[2] = { 1,-1 }, qyy[2] = { 1,-1 };
using namespace std;
struct node
{
ll point, price;
}classes[5000];
bool cmp(node x, node y)
{
return x.point < y.point;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
ll dp[5000][2];
while (cin >> n)
{
memset(dp, 0, sizeof(dp));
for (int i = 0; i < n; i++)
{
cin >> classes[i].point >> classes[i].price;
}
sort(classes, classes + n, cmp);//题目没说按照顺序输入,所以需要先排序
dp[0][1] = classes[0].price;
dp[0][0] = INF;
for (int i = 1; i < n; i++)
{
dp[i][1] = classes[i].price + min(dp[i - 1][0], dp[i - 1][1]);
dp[i][0] = INF;
ll m = 0;
for (int f = i - 1; f >= 0; f--)//枚举出i店铺左边第一个店铺为哪个店铺的时候花费最少,从i-1开始枚举
{
m += (i - f) * (classes[f + 1].point - classes[f].point);//重点,可画图理解,m为每次往前推一点的时候增加的距离花费
dp[i][0] = min(dp[i][0], dp[f][1] + m);
}
}
cout << min(dp[n - 1][0], dp[n - 1][1]) << endl;
}
return 0;
}

  

HDU6024:Building Shops(简单DP)的更多相关文章

  1. hdu6024 Building Shops(区间dp)

    https://cn.vjudge.net/problem/HDU-6024 分开考虑某一点种与不种,最后取二者的最小值. dp[i][1] = min(dp[i-1][0], dp[i-1][1]) ...

  2. HDU6024 Building Shops 2017-05-07 18:33 30人阅读 评论(0) 收藏

    Building Shops                                                             Time Limit: 2000/1000 MS ...

  3. HDU6024:Building Shops(DP)

    传送门 题意 在一条直线上有n个教室,现在要设置糖果店,使得最后成本最小,满足以下两个条件: 1.若该点为糖果店,费用为cost[i]; 2.若不是,则为loc[i]-最近的糖果店的loc 分析 dp ...

  4. HDU 1087 简单dp,求递增子序列使和最大

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  5. Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

    题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...

  6. codeforces Gym 100500H A. Potion of Immortality 简单DP

    Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...

  7. 简单dp --- HDU1248寒冰王座

    题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...

  8. poj2385 简单DP

    J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit ...

  9. hdu1087 简单DP

    I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     ...

随机推荐

  1. 还在使用集合类完成这些功能?不妨来看看 Guava 集合类!!!

    日常开发中,小黑哥经常需要用到 Java 提供集合类完成各种需求.Java 集合类虽然非常强大实用,但是提供功能还是有点薄弱. 举个例子,小黑哥最近接到一个需求,从输入一个文档中,统计一个关键词出现的 ...

  2. web测试喜事连连--草稿箱功能

    “草稿箱”功能很常见吧,编辑内容后,不想发布的话,就先存为草稿.啥时候想公开了,发布即可. 今天发生个啥事呢,让作为Tester的我,哭笑不得. 开发部经理老F,反馈一个客户需求,发到群里让大家讨论. ...

  3. Redis05——Redis Cluster 如何实现分布式集群

    前面一片文章,我们已经说了Redis的主从集群及其哨兵模式.本文将继续介绍Redis的分布式集群. 在高并发场景下,单个Redis实例往往不能满足业务需求.单个Redis数据量过大会导致RDB文件过大 ...

  4. python常用软件包

    redis数据库: 链接:https://pan.baidu.com/s/1X_ndCj0scF2WZd2FPLRKag 提取码:e3zm git安装包: 链接:https://pan.baidu.c ...

  5. Pocket+Evernote 打造个人知识库体系

    俗话说巧妇难为无米之炊,还是那个不太恰当的例子. 写作就好比人类的消化系统,想要持续的输出...那么就要持续的输入... 今天就来说一说如何进行持续有效的输入. 信息处理过程 先放一张图,这是我的整个 ...

  6. vue基础----过滤器filter

    1.用的场景:一个功能在每个组件都能用,而computed虽然有缓存,但不能用在每一个组件,需要的话的每一个都需要写. 2.特点:改变数据的展示形式,不改变原有的形式  分为全局与局部的 <di ...

  7. tensorflow一些API的基本理解

    1.tf.Session self._session = None opts = tf_session.TF_NewSessionOptions(target=self._target, config ...

  8. 关于MySQL数据库事务的机制学习总结

    这几天面试多次被问到了数据库事务机制.隔离级别.乐观锁悲观锁类的问题,之前对这些只能说有所了解,有些概念还停留在记忆层面,没有理解,所以回答的不好.后面翻书学习了下,理解了一些东西,在此做一个记录. ...

  9. ProjectTool写白包工具,秒级别写H5游戏壳包,可视化操作,极易使用,支持Swift、Objecive-C双语言

    这是自动写白包工具,秒级别写H5游戏壳包,可视化操作,极易使用,支持Swift.Objecive-C双语言 扣扣交流群:811715780 [ 官网下载 ] 这是一个白包目录示例 ProjectToo ...

  10. Servlet(三)----Servlet体系与HTTP

    ## Servlet的体系结构 Servlet  ---  接口 | | GenericServlet  ---  抽象类 | | HttpServlet  --  抽象类 GenericServle ...