zoukankan      html  css  js  c++  java
  • ZJOI2007仓库建设

    斜率优化

    # include <stdio.h>
    # include <stdlib.h>
    # include <iostream>
    # include <string.h>
    # include <algorithm>
    # define IL inline
    # define RG register
    # define Fill(a, b) memset(a, b, sizeof(a))
    using namespace std;
    typedef long long ll;
    
    IL ll Read(){
        RG char c = getchar(); RG ll x = 0, z = 1;
        for(; c > '9' || c < '0'; c = getchar()) z = c == '-' ? -1 : 1;;
        for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + c - '0';
        return x * z;
    }
    
    const int MAXN(1000010);
    const ll INF(1e18);
    int n, cnt, Q[MAXN], tail, head;
    ll f[MAXN], c[MAXN], p[MAXN], x[MAXN], s[MAXN], g[MAXN];
    
    IL double Calc(RG int i, RG int j){
        return 1.0 * (f[i] + g[i] - f[j] - g[j]) / (p[i] - p[j]);
    }
    
    int main(){
        n = Read();
        for(RG int i = 1; i <= n; i++){
            x[i] = Read(); p[i] = Read(); c[i] = Read();
            p[i] += p[i - 1];
            s[i] = s[i - 1] + p[i - 1] * (x[i] - x[i - 1]);
            g[i] = x[i] * p[i] - s[i];
        }
        for(RG int i = 1; i <= n; i++){
            while(head < tail && Calc(Q[head], Q[head + 1]) <= x[i]) head++;
            f[i] = f[Q[head]] + c[i] + s[i] + g[Q[head]] - x[i] * p[Q[head]];
            while(head < tail && Calc(Q[tail - 1], Q[tail]) > Calc(Q[tail], i)) tail--;
            Q[++tail] = i;
        }
        printf("%lld
    ", f[n]);
        return 0;
    }
  • 相关阅读:
    mysql中lock tables与unlock tables(锁表/解锁)使用总结
    php开始,html应用的一些不错收藏
    正则表达总结
    yii2 安装
    Nginx出现413 Request Entity Too Large错误解决方法
    jQuery中使用ajaxSubmit提交表单
    Go Log模块生成日志文件
    Go sync模块
    Golang html encoding解析
    Go struct tag
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8206394.html
Copyright © 2011-2022 走看看