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 ...
随机推荐
- openwrt 上的 upnp wifi 音频推送 gmediarender
首先是必须启用的模块 Libraries ---> <*> libupnp Sound ---> <*> alsa-utils<*> madplay-a ...
- Asp.Net Core 2.0实现HttpResponse中繁切换
随笔背景:因为项目中有个简单的功能是需要实现中文简体到繁体的切换,数据库中存储的源数据都是中文简体的,为了省事就想着通过HttpHeader的方式来控制Api返回对应的繁体数据. 实现方式:通过Asp ...
- 面试官:HashMap死循环形成的原因是什么?
介绍 HashMap实现原理 之前的文章已经分析了HashMap在JDK1.7的实现,这篇文章就只分析HashMap死循环形成的原因 死循环形成是在扩容转移元素的时候发生的 void resize(i ...
- 扫码枪读取条形码数据(vue)
扫码枪是模拟键盘输入的,所有事件为document.onkeypress = function(){}. 在vue项目中,是没有window.onload的,所以在created钩子函数中做: var ...
- 题解 UVA12186 【工人的请愿书 Another Crisis】
俺太难了 记录一下我调了一个小时的错误 多测不清空 多测清空只清空了\(vector\) 多测全清空了,但是忘了清空\(vector[0]\) \(priority\)_ \(queue\)把\(gr ...
- Html网页链接数据库验证账户密码(新手)
连接代码(其中用到了连接池,不要忘记Jar包.拉入配置文件和工具类): package cn.Wuchuang.Servlet; import org.springframework.jdbc.cor ...
- 题解 P5663 【加工零件【民间数据】】
博客园体验更佳 讲讲我的做法 确定做法 首先,看到这道题,我直接想到的是递归,于是复杂度就上天了,考虑最短路. 如何用最短路 首先,看一张图 我们该如何解决问题? 问题:\(3\)做\(5\)阶段的零 ...
- Java基础语法(7)-数组
title: Java基础语法(7)-数组 blog: CSDN data: Java学习路线及视频 1.数组的概述 数组(Array),是多个相同类型数据按一定顺序排列的集合,并使用一个名字命名,并 ...
- 百道Python面试题实现,搞定Python编程就靠它
对于一般的机器学习求职者而言,最基础的就是掌握 Python 编程技巧,随后才是相关算法或知识点的掌握.在这篇文章中,我们将介绍一个 Python 练习题项目,它从算法练习题到机试实战题提供了众多问题 ...
- Python第六章-函数06-高阶函数
函数的高级应用 二.高阶函数 高级函数, 英文叫 Higher-order Function. 那么什么是高阶函数呢? 在说明什么是=高阶函数之前, 我们需要对函数再做进一步的理解! 2.1 函数的本 ...