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;
}

  

最新文章

  1. Flume NG安装部署及数据采集测试
  2. 转来的emacs配置文件,自动安装插件
  3. multiple definition of `err_sys&#39; 《UNIX环境高级编程》
  4. Android NDK开发入门实例
  5. nginx如何限速?
  6. Android基础之Activity launchMode详解
  7. 百度,你家云管家能靠谱点不?替你脸红!Shame on you!
  8. ie6/7/8中span右浮动折行问题的解决方案
  9. [转] 三步将你的 React Native 项目运行在 Web 浏览器上面
  10. angularjs使用ng-messages的注册表单实例
  11. kettle表输入条件参数设置
  12. inline使用
  13. mac IntelliJ Idea添加schema和dtd约束提示
  14. windows下安装memcached,报错:Failed to ignore SIGHUP RESULT too large
  15. Hadoop 综合揭秘——HBase的原理与应用
  16. Java -- POI -- 入门使用以及简单介绍
  17. web-day5
  18. apicloud 自定义模块的开发与上架注意事项
  19. gnuplot生成MySQL QPS图形
  20. (全排列)Ignatius and the Princess II -- HDU -- 1027

热门文章

  1. WEB渗透 - XSS
  2. 洛谷P1000超级马里奥的神奇解法
  3. 在vscode中怎样debug调试go程序
  4. 学习scrapy爬虫框架的一些经验和教训
  5. [UWP]抄抄《CSS 故障艺术》的动画
  6. IntegerCache缓存占用堆、栈、常量池的问题,自动拆装箱的基本概念,Integer==int时的问题说明
  7. 跟面试官侃半小时MySQL事务隔离性,从基本概念深入到实现
  8. 【Unity游戏开发】跟着马三一起魔改LitJson
  9. OpenLDAP 多主复制(基于docker容器模式部署)
  10. angular中$q用法, $q多个promise串行/同步/等待), $q.all用法,使用