zoukankan      html  css  js  c++  java
  • [HDU3507]Print Article

    [HDU3507]Print Article

    试题描述

    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.

    输入

    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.

    输出

    A single number, meaning the mininum cost to print the article.

    输入示例

    5 5
    5
    9
    5
    7
    5

    输出示例

    230

    数据规模及约定

    见“输入

    题解

    题目描述需要做题人自行朝正确的方向想象,其含义是隐晦的,并不是字面意思。

    如果我告诉你这是一道裸斜率优化的 dp,那么你也许能够很容易的猜出题目描述的意思。

    最后提醒:Ci 有可能为 0,也就是可能出现重复的点,那么在维护凸壳时需要注意一些什么就不言自明了。

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <stack>
    #include <vector>
    #include <queue>
    #include <cstring>
    #include <string>
    #include <map>
    #include <set>
    using namespace std;
    #define LL long long
    
    #define maxn 500010
    struct Point {
    	LL x, y;
    	Point() {}
    	Point(LL _, LL __): x(_), y(__) {}
    } ps[maxn];
    LL S[maxn], f[maxn];
    int n, M, q[maxn], hd, tl;
    
    LL sqr(LL x) { return x * x; }
    
    bool isup(int a, int b, int c) {
    	LL x1 = ps[b].x - ps[a].x, y1 = ps[b].y - ps[a].y, x2 = ps[c].x - ps[b].x, y2 = ps[c].y - ps[b].y;
    	return y1 * x2 - x1 * y2 >= 0;
    }
    
    int main() {
    	while(scanf("%d%d", &n, &M) == 2) {
    		for(int i = 1; i <= n; i++) {
    			int x; scanf("%d", &x);
    			S[i] = S[i-1] + x;
    		}
    		
    		hd = 1; tl = 0;
    		q[++tl] = 0;
    		for(int i = 1; i <= n; i++) {
    			LL slop = S[i] << 1;
    			while(hd < tl && slop * (ps[q[hd+1]].x - ps[q[hd]].x) > ps[q[hd+1]].y - ps[q[hd]].y) hd++;
    			f[i] = ps[q[hd]].y - slop * ps[q[hd]].x + sqr(S[i]) + M;
    			ps[i] = Point(S[i], f[i] + sqr(S[i]));
    			while(hd < tl && isup(q[tl-1], q[tl], i)) tl--;
    			q[++tl] = i;
    		}
    		printf("%lld
    ", f[n]);
    	}
    	
    	return 0;
    }
    
  • 相关阅读:
    Go语言 0 前言
    SVM支持向量机的基本原理
    支持向量机通俗介绍
    线程句柄和线程ID的区别
    分布式事务一致性,事务补偿实战
    ResultSet is from UPDATE. No Data.
    linux 批量kill进程
    mysql数据表拷贝
    Maven assembly 打包
    JS 删除对象属性
  • 原文地址:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/6337479.html
Copyright © 2011-2022 走看看