题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6024

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 test cases.
In each test case, the first line contains an integer n(≤n≤), denoting the number of the classrooms.
In the following n lines, each line contains two integers xi,ci(−≤xi,ci≤), 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 Sample Output Source
2017中国大学生程序设计竞赛 - 女生专场

题目大意:有N个教室,需要在教室建糖果店,所需要的花费有两种情况

1.建糖果店所需要花费的费用。

2.这个点离左边最近的糖果店的距离。

分析:这个点有两种可能  建糖果店  不建糖果店 所以是道dp题

设dp【i】【0】表示在这个点建立糖果店所需要花费的最小费用,dp【i】【0】在这个点不建糖果店所需要花费的最小费用

dp【i】【1】=min(dp[i-1][1],dp[i-1][0])+ci;

dp[i][0]的情况需要从起点到开始枚举,所以时间复杂度为O(n*n);

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include <vector>
#include<iostream>
using namespace std;
#define N 50005
#define INF 0x3f3f3f3f
#define LL long long
LL dp[N][];
struct node
{
LL x,c;
}s[N];
int cmp(node a,node b)
{
return a.x<b.x;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
scanf("%lld %lld",&s[i].x,&s[i].c);
sort(s+,s+n+,cmp);
memset(dp,,sizeof(dp));
dp[][] = s[].c;///东边肯定有糖果店 所以第一个教室是必须建糖果店的
dp[][] = INF;
for(int i=;i<=n;i++)
{
dp[i][] = min(dp[i-][],dp[i-][])+s[i].c;
///在这个教室建立糖果店所需要的最小花费
LL sum=;
dp[i][] = INF;
///找到前面花费最小的糖果店
for(int j=i-;j>=;j--)
{
sum+=(i-j)*(s[j+].x-s[j].x);
dp[i][] = min(dp[i][],dp[j][]+sum);
}
}
printf("%lld\n",min(dp[n][],dp[n][]));
///取建糖果店与不建糖果店的其中的小值
}
return ;
}

(hdu 6024) Building Shops的更多相关文章

  1. 2道acm编程题(2014):1.编写一个浏览器输入输出(hdu acm1088);2.encoding(hdu1020)

    //1088(参考博客:http://blog.csdn.net/libin56842/article/details/8950688)//1.编写一个浏览器输入输出(hdu acm1088)://思 ...

  2. Bestcoder13 1003.Find Sequence(hdu 5064) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5064 题目意思:给出n个数:a1, a2, ..., an,然后需要从中找出一个最长的序列 b1, b ...

  3. 2013 多校联合 F Magic Ball Game (hdu 4605)

    http://acm.hdu.edu.cn/showproblem.php?pid=4605 Magic Ball Game Time Limit: 10000/5000 MS (Java/Other ...

  4. (多线程dp)Matrix (hdu 2686)

    http://acm.hdu.edu.cn/showproblem.php?pid=2686     Problem Description Yifenfei very like play a num ...

  5. War Chess (hdu 3345)

    http://acm.hdu.edu.cn/showproblem.php?pid=3345 Problem Description War chess is hh's favorite game:I ...

  6. 2012年长春网络赛(hdu命题)

    为迎接9月14号hdu命题的长春网络赛 ACM弱校的弱菜,苦逼的在机房(感谢有你)呻吟几声: 1.对于本次网络赛,本校一共6名正式队员,训练靠的是完全的自主学习意识 2.对于网络赛的群殴模式,想竞争现 ...

  7. BestCoder Round #69 (div.2) Baby Ming and Weight lifting(hdu 5610)

    Baby Ming and Weight lifting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  8. BestCoder Round #68 (div.2) geometry(hdu 5605)

    geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  9. 2013多校联合2 I Warm up 2(hdu 4619)

    Warm up 2 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total ...

随机推荐

  1. javascript基础修炼(7)——Promise,异步,可靠性

    开发者的javascript造诣取决于对[动态]和[异步]这两个词的理解水平. 一. 别人是开发者,你也是 Promise技术是[javascript异步编程]这个话题中非常重要的,它一度让我感到熟悉 ...

  2. DSAPI多功能组件编程应用-网络相关(下)

    [DSAPI.DLL下载地址] 在本篇,我将重点介绍DSAPI.DLL中Socket编程的使用.众所周知,Socket用起来不难,但是写起来麻烦.我对Socket进行了封装,进行了高度简化.下面我将通 ...

  3. 【转载】Windows Server 2012服务器删除IIS方法

    在Windows Server2012版本的服务器系统中,我们可以通过服务器管理器中的"添加角色和功能"来添加IIS的Web服务器,当我们不再使用IIS功能时候,我们也可以通过删除 ...

  4. aspx 页面中 js 引用与页面后台的数据交互 --【 后台调用 js 】

    js 中调用后台方法   一.用Response.Write方法 Response.Write("<script type='text/javascript'>alert(&qu ...

  5. ASP.NET SignalR介绍

    是什么? 简单来说,ASP.NET SignalR是一个开源的实时通讯(real-time)库,有了ASP.NET SignalR,我们可以在 详细介绍参考:https://docs.microsof ...

  6. 关于git的简单操作

    首先这篇随笔我是不太想写的,因为网上有很多教程,我也是看廖雪峰大神的git教程自学的.还是一个小学生,就当一个学习笔记了,如果你想看大神的原版,请点击这里.我们原来都是用svn的,但是越来越觉得svn ...

  7. React的组件模式

    组件是 React 的核心,因此了解如何利用它们对于创建优秀的设计结构至关重要. 什么是组件 根据 React 官网的介绍,"组件让你可以将 UI 分割成独立的.可重用的部分,并独立管理每个 ...

  8. java框架之springboot

    快速入门 一.helloworld示例 二.springboot单元测试 三.springboot热部署 web开发 整合redis thymeleaf使用 spring-data-jpa使用 整合m ...

  9. noi.ac #289. 电梯(单调队列)

    题意 题目链接 Sol 傻叉的我以为给出的\(t\)是单调递增的,然后\(100\rightarrow0\) 首先可以按\(t\)排序,那么转移方程为 \(f[i] = min_{j=0}^{i-1} ...

  10. Xutils, OKhttp, Volley, Retrofit对比

    Xutils这个框架非常全面,可以进行网络请求,可以进行图片加载处理,可以数据储存,还可以对view进行注解,使用这个框架非常方便,但是缺点也是非常明显的,使用这个项目,会导致项目对这个框架依赖非常的 ...