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. 如何分析SpringBoot源码模块及结构?--SpringBoot源码(二)

    注:该源码分析对应SpringBoot版本为2.1.0.RELEASE 1 前言 本篇接 如何搭建自己的SpringBoot源码调试环境?--SpringBoot源码(一). 前面搭建好了自己本地的S ...

  2. 数组去重--hash方法

    hash方法我以前百度找到的,经常用性能好速度快,本文章主要是一步步解释hash方法的过程(其实没多少步) 在这里就能看出每个自定义下标都是独一无二的,其实就相当于数组arr已经去重了 剩下我们就需要 ...

  3. Ubuntu 18 安装 cuda 10

    1.把预先下好的cuda放到某个目录,如Download. 2.Crtl + Alt + F3 进入tty,使用tty登录. 关闭用户图形界面,sudo systemctl set-default m ...

  4. 记一次在新服务器上搭建lnmp的过程

    背景: 前不久阿里云在做活动,200+一台服务器三年,于是果断入手了一台. 今天有空就在服务器上把lnmp环境给装了,之前为了了解安装过程,在别的机器上尝试过单独安装nginx.mysql.php,虽 ...

  5. Python 3.9 新特性:任意表达式可作为装饰器!

    一个月前(2月20日),一则新的 PEP 没有受到任何阻碍就被官方采纳了,这么快的速度,似乎并不多见. 然而,更为高效率的是,仅在半个月内,它的实现就被合入了代码仓.也就是说,我们最快有望在 3 天后 ...

  6. 一文带你熟悉SpringIOC

    Spring的IOC: IOC是Spring的一个核心组件,理解IOC是迈向Spring大门的重要一步 现实生活中,我们写字用的笔会有多种颜色,为了做不同的标记,需要用不同颜色的笔.如果只是使用一两种 ...

  7. F版本SpringCloud 3—大白话Eureka服务注册与发现

    引用:服务注册与发现,就像是租房子一样 前言 今天洛阳下雨了,唉,没有想到有裹上了羽绒服,不穿冷穿了热的尴尬温度.上学工作这么多年都在外面,家里竟然没有一件春天的外套. 日常闲聊之后,开始今天的芝士环 ...

  8. RTSP协议进行视频取流的方法、注意点及python实现

    在视频应用中,我们一般都需要基于摄像头或录像机的视频流进行二次开发,那么就涉及到如何将视频流取出来. 在摄像机安装好之后,一般是通过局域网与本地的服务器进行连接,要取录像机的视频流就要在局域网范围内进 ...

  9. 破解WIFI教程

    今日主题:如何破解WIFI 准备工具 笔记本一台 usb无线网卡[我用的是小米的] kali系统[可以在虚拟机里装,建议用2019年及以下版本] VMware Workstation15虚拟机安装 可 ...

  10. Servlet(四)----HTTP、Response、servletContent

    ##  HTTP协议: 1.请求消息:客户端发送给服务器端的数据 *  数据格式: 1.请求行 2.请求头     3.请求空行 4.请求体 2.响应消息:服务器端发送给客户端的数据 *  数据格式: ...