HDU6024:Building Shops(简单DP)
Building Shops
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 4446 Accepted Submission(s): 1551
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.
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.
1 2
2 3
3 4
4
1 7
3 1
5 10
6 1
11

#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)的更多相关文章
- hdu6024 Building Shops(区间dp)
https://cn.vjudge.net/problem/HDU-6024 分开考虑某一点种与不种,最后取二者的最小值. dp[i][1] = min(dp[i-1][0], dp[i-1][1]) ...
- HDU6024 Building Shops 2017-05-07 18:33 30人阅读 评论(0) 收藏
Building Shops Time Limit: 2000/1000 MS ...
- HDU6024:Building Shops(DP)
传送门 题意 在一条直线上有n个教室,现在要设置糖果店,使得最后成本最小,满足以下两个条件: 1.若该点为糖果店,费用为cost[i]; 2.若不是,则为loc[i]-最近的糖果店的loc 分析 dp ...
- HDU 1087 简单dp,求递增子序列使和最大
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Codeforces Round #260 (Div. 1) A. Boredom (简单dp)
题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...
- 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 ...
- 简单dp --- HDU1248寒冰王座
题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...
- poj2385 简单DP
J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit ...
- hdu1087 简单DP
I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32768KB ...
随机推荐
- LinkedHashMap源码解读
1. 前言 还是从面试中来,到面试中去.面试官在面试 Redis 的时候经常会问到,Redis 的 LRU 是如何实现的?如果让你实现 LRU 算法,你会怎么实现呢?除了用现有的结构 LinkedHa ...
- TypeScript Jest 调试
本文简要介绍了如何在 Jest 单元测试中利用 Chrome Node DevTools 来辅助调试. 背景 代码是 TS 写的 所测功能无 UI 界面,出现Bug后不容易定位 用 console 式 ...
- 自动控制理论的MATLAB仿真实例(一)
拉普拉斯变换及其反变换 Laplace变换及其反变换的定义为:
- iframe 父框架调用子框架的函数
1.父框架定义: <iframe name="mainframe" id="mainframe" width="100%" scrol ...
- vue基础----过滤器filter
1.用的场景:一个功能在每个组件都能用,而computed虽然有缓存,但不能用在每一个组件,需要的话的每一个都需要写. 2.特点:改变数据的展示形式,不改变原有的形式 分为全局与局部的 <di ...
- Crypto++ 无法解析的外部符号 CryptoPP::AssignIntToInteger
预处理器定义中添加:CRYPTOPP_NO_ASSIGN_TO_INTEGER 方案出自:https://github.com/weidai11/cryptopp/issues/389
- File的获取功能(新手)
//导入包.import java.io.File;/*File的获取功能*///创建的一个类.public class zylx2 { //公共静态的主方法. public static void ...
- Vulnhub靶场 DC-2 WP
DC-2简介 描述 与DC-1一样,DC-2是另一个专门构建的易受攻击的实验室,目的是获得渗透测试领域的经验. 与原始DC-1一样,它在设计时就考虑了初学者. 必须具备Linux技能并熟悉Linux命 ...
- Linux服务器(Centos)上安装jexus
哈子是Jexus Jexus是一款Linux平台上的高性能WEB服务器和负载均衡网关,Jexus Web Service,简称JWS,以支持ASP.NET.ASP.NET CORE.PHP为特色, 同 ...
- Journal of Proteome Research | Mining the Proteome Associated with Rheumatic and Autoimmune Diseases(挖掘风湿和自身免疫疾病相关的蛋白组)(解读人:黄旭蕾)
期刊名:JPR 发表时间:(2019年12月) IF:3.780 单位:Grupo de Investigación de Reumatología (GIR), Unidad de Proteó ...