• [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;
    }
    
  • 相关阅读:
    关于 L3 缓存行 cacheLIne 的研究!还是对程序有举足轻重的作用!
    所谓的科学,根本就没有解决问题的根本。如框架,框架再好,也需要内容。
    编译器开发,手动把汇编转 机器码
    测试HTML
    神奇的经历,顶上去保证上帝保佑你!!!!!
    ECS框架研究 ,unity 以及缓存行的研究
    AVX 指令详解 ,还有SSE指令
    关于 Visual Studio 2017 ,或2019 ,Installer 没检测到已安装的程序.以及C++ 创建项目失败
    TCP 协议 精解
    opc 相关组件
  • 原文地址:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/6337479.html
Copyright © 2020-2023  润新知