hdu 6024 Building Shops(dp)

题目链接:hdu 6024 Building Shops

题意:

要在n个点上建若干小卖店,每个点建造小卖店的消费为cost[i],如果这个点没有建造小卖店,那么这个点的费用为和左边最近的点的坐标差。

求总的最小费用。

题解:

首先,第一个点必须建造,然后对于第i个点:

dp[i]=min(dp[i],sum[i]-sum[j]-(i-j)*(id[i]-id[j]+dp[j-1]+cost[j]);(1<=j<=i)

其中:

id[i]表示第i个点的坐标。

sum[i]=id[i]-id[1]+sum[i-1](即所有点与第一个点距离的前缀和)。

 1 #include<bits/stdc++.h>
 2 #define F(i,a,b) for(int i=a;i<=b;i++)
 3 using namespace std;
 4 typedef pair<int,int> P;
 5 const int N=1e5+7;
 6 int n;
 7 P a[N];
 8 long long dp[N],sum[N],inf=1ll<<62;
 9
10 int main()
11 {
12     while(~scanf("%d",&n))
13     {
14         F(i,1,n)scanf("%d%d",&a[i].first,&a[i].second);
15         sort(a+1,a+1+n);
16         F(i,1,n)sum[i]=a[i].first-a[1].first+sum[i-1],dp[i]=inf;
17         dp[1]=a[1].second;
18         F(i,2,n)F(j,1,i)
19         dp[i]=min(dp[i],sum[i]-sum[j]-1ll*(i-j)*(a[j].first-a[1].first)+dp[j-1]+a[j].second);
20         printf("%lld\n",dp[n]);
21     }
22     return 0;
23 }

时间: 2024-09-29 19:42:39

hdu 6024 Building Shops(dp)的相关文章

HDU 6024 Building Shops

$dp$. $dp[i]$表示到$i$位置,且$i$位置建立了的最小花费,那么$dp[i] = min(dp[k]+cost[i+1][k-1])$,$k$是上一个建的位置.最后枚举$dp[i]$,加上最后一段的花费,取个最小值即可. #include <bits/stdc++.h> using namespace std; const long long mod=1e9+7; const long long inf=1e18; int n; struct X { long long pos;

HDU 4960 (水dp)

Another OCD Patient Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) patient. This morning, his children played with plasticene. They broke the plasticene into N pieces, and put them in a line. Each piece has a volume Vi. Since Xi

HDU 1087 &amp;&amp; POJ 2533(DP,最长上升子序列).

~~~~ 两道题的意思差不多,HDU上是求最长上升子序列的和,而POJ上就的是其长度. 貌似还有用二分写的nlogn的算法,不过这俩题n^2就可以过嘛.. ~~~~ 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1087 http://poj.org/problem?id=2533 ~~~~ HDU1087: #include<cstdio> #include<cstring> #include<algorithm> #

hdu 2296 aC自动机+dp(得到价值最大的字符串)

Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3180    Accepted Submission(s): 1033 Problem Description For the hope of a forever love, Steven is planning to send a ring to Jane with a rom

hdu 2457 AC自动机+dp

DNA repair Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2004    Accepted Submission(s): 1085 Problem Description Biologists finally invent techniques of repairing DNA that contains segments c

[ACM] hdu 3555 Bomb (数位DP,统计1-N中含有“49”的总数)

Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 7187 Accepted Submission(s): 2512 Problem Description The counter-terrorists found a time bomb in the dust. But this time the terrorists impro

HDU 4833 Best Financing DP

Best Financing Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 120    Accepted Submission(s): 24 Problem Description 小 A想通过合理投资银行理财产品达到收益最大化.已知小A在未来一段时间中的收入情况,描述为两个长度为n的整数数组dates和earnings,表示在

hdu 1011(树形dp)

Mark.看着吴神博客写的,还未完全懂. 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 #include <set> 8 #include <map> 9 #include <string>

HDU 2294 Pendant (DP+矩阵快速幂降维)

HDU 2294 Pendant (DP+矩阵快速幂降维) ACM 题目地址:HDU 2294 Pendant 题意: 土豪给妹子做首饰,他有K种珍珠,每种N个,为了炫富,他每种珍珠都要用上.问他能做几种长度[1,N]的首饰. 分析: 1 ≤ N ≤ 1,000,000,000简直可怕. 首先想dp,很明显可以想到: dp[i][j] = (k-(j-1))*dp[i-1][j-1] + j*dp[i-1][j](dp[i][j]表示长度为i的并且有j种珍珠的垂饰有多少个) 然后遇到N太大的话,