首页 > 代码库 > hdu3507_斜率dp
hdu3507_斜率dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3507
Print Article
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 9699 Accepted Submission(s): 3066
Problem Description
Zero has an old printer that doesn‘t work well sometimes. As it is antique, he still like to use it to print articles. But it is too old to work for a long time and it will certainly wear and tear, so Zero use a cost to evaluate this degree.
One day Zero want to print an article which has N words, and each word i has a cost Ci to be printed. Also, Zero know that print k words in one line will cost
M is a const number.
Now Zero want to know the minimum cost in order to arrange the article perfectly.
One day Zero want to print an article which has N words, and each word i has a cost Ci to be printed. Also, Zero know that print k words in one line will cost
M is a const number.
Now Zero want to know the minimum cost in order to arrange the article perfectly.
Input
There are many test cases. For each test case, There are two numbers N and M in the first line (0 ≤ n ≤ 500000, 0 ≤ M ≤ 1000). Then, there are N numbers in the next 2 to N + 1 lines. Input are terminated by EOF.
Output
A single number, meaning the mininum cost to print the article.
Sample Input
5 559575
Sample Output
230
推荐博客:http://blog.csdn.net/azheng51714/article/details/8214165
1 //dp[i]=dp[j]+M+(sum[i]-sum[j])^2 2 //设k<j<i, j比k决策好 3 //dp[j]+M+(sum[i]-sum[j])^2<dp[k]+M+(sum[i]-sum[k])^2 4 //(dp[j]+num[j]^2-(dp[k]+num[k]^2))/(2*(num[j]-num[k]))<sum[i] 5 //dp[j]+num[j]^2-(dp[k]+num[k]^2)) GetUp() 6 //2*(num[j]-num[k]) GetDown() 7 #include <algorithm> 8 #include <iostream> 9 #include <cstring>10 #include <cstdlib>11 #include <cstdio>12 #include <vector>13 #include <ctime>14 #include <queue>15 #include <list>16 #include <set>17 #include <map>18 using namespace std;19 #define INF 0x3f3f3f3f20 typedef long long LL;21 22 int dp[500010], n, m, sum[500010], q[500010];23 int GetDp(int i, int j)24 {25 return dp[j] + m + (sum[i]-sum[j])*(sum[i]-sum[j]);26 }27 int GetUp(int j, int k)//yj-yk的部分28 {29 return dp[j] + sum[j]*sum[j] - (dp[k]+sum[k]*sum[k]);30 }31 int GetDown(int j, int k)//xj-xk的部分32 {33 return 2 * (sum[j] - sum[k]);34 }35 int main()36 {37 while(~scanf("%d %d", &n, &m))38 {39 sum[0] = dp[0] = 0;40 for(int i = 1; i <= n; i++){41 scanf("%d", &sum[i]);42 sum[i] += sum[i-1];43 }44 int head = 0, tail = 0;45 q[tail++] = 0;46 for(int i = 1; i <=n; i++)47 {48 while(head+1<tail && GetUp(q[head+1],q[head])<=sum[i]*GetDown(q[head+1],q[head]))49 head++;50 dp[i] = GetDp(i, q[head]);51 while(head+1<tail && GetUp(i, q[tail-1])*GetDown(q[tail-1],q[tail-2])<=GetUp(q[tail-1],q[tail-2])*GetDown(i,q[tail-1]))52 tail--;53 q[tail++] = i;54 }55 printf("%d\n", dp[n]);56 }57 return 0;58 }
hdu3507_斜率dp
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。