zoukankan      html  css  js  c++  java
  • HDU 3507 Print Article

    Print Article

    Time Limit: 3000ms
    Memory Limit: 65536KB
    This problem will be judged on HDU. Original ID: 3507
    64-bit integer IO format: %I64d      Java class name: Main
     
    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.
     

    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 5
    5
    9
    5
    7
    5

    Sample Output

    230

    Source

     
    解题:斜率优化dp。。。
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 const int maxn = 500010;
    18 LL sum[maxn],dp[maxn];
    19 int n,m,q[maxn],head,tail;
    20 LL G(int j,int k){
    21     LL a = dp[j]+sum[j]*sum[j];
    22     LL b = dp[k]+sum[k]*sum[k];
    23     return a-b;
    24 }
    25 LL S(int j,int k){
    26     return (sum[j] - sum[k])*2;
    27 }
    28 int main() {
    29     int i;
    30     while(~scanf("%d %d",&n,&m)){
    31         sum[0] = 0;
    32         for(i = 1; i <= n; i++){
    33             scanf("%I64d",sum+i);
    34             sum[i] += sum[i-1];
    35         }
    36         q[0] = dp[0] = head = tail = 0;
    37         for(i = 1; i <= n; i++){
    38             while(head < tail && G(q[head+1],q[head]) <= sum[i]*S(q[head+1],q[head])) ++head;
    39             dp[i] = dp[q[head]] + (sum[i] - sum[q[head]])*(sum[i] - sum[q[head]])+m;
    40             while(head < tail && G(q[tail-1],q[tail])*S(q[tail],i) >= G(q[tail],i)*S(q[tail-1],q[tail])) --tail;
    41             q[++tail] = i;
    42         }
    43         printf("%I64d
    ",dp[n]);
    44     }
    45     return 0;
    46 }
    View Code
  • 相关阅读:
    带最小值操作的栈 &#183; Min Stack
    删除排序数组中的重复数字 II · Remove Duplicates from Sorted Array II
    K组翻转链表 · Reverse Nodes in k-Group
    HTML DOM Style borderBottomLeftRadius 属性
    PHP For 循环
    PHP copy() 函数
    CSS border-left-color 属性
    ASP.NET Web Pages 文件夹
    no-empty-label (Rules) – Eslint 中文开发手册
    PHP ksort() 函数
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3916845.html
Copyright © 2011-2022 走看看