zoukankan      html  css  js  c++  java
  • [BZOJ3156]防御准备

    题面在这里

    题意

    一条战线上共有(n)个检查点,每个检查点必须放置一个木偶或者放置一个守卫塔,
    放置守卫塔的费用为(a_i),放置木偶的费用为该位置到右边第一个守卫塔的距离,
    求最小费用。

    数据范围

    [1le nle10^6,1le a_ile 10^9 ]

    sol

    (f[i])表示强制在第(i)处建守卫塔时前(i)个检查点所需的最小费用,有

    [f[i]=min_{j=0}^{i-1}(f[j]+sum_{k=j+1}^{i-1}(i-k)+a[i]) ]

    考虑斜率优化:

    [=min_{j=0}^{i-1}(f[j]+sum_{k=1}^{i-j-1}k+a[i]) ]

    [=min_{j=0}^{i-1}(f[j]+frac{1}{2}j(j+1)-ij)+a[i]+frac{1}{2}i(i-1) ]

    斜率(k=i)单增,插点((i,f[i]+frac{1}{2}i(i+1)))即可

    代码

    #include<bits/stdc++.h>
    #include<algorithm>
    #include<iostream>
    #include<cstdlib>
    #include<iomanip>
    #include<cstring>
    #include<complex>
    #include<vector>
    #include<cstdio>
    #include<string>
    #include<bitset>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<map>
    #include<set>
    #define mp make_pair
    #define pb push_back
    #define RG register
    #define il inline
    using namespace std;
    typedef unsigned long long ull;
    typedef vector<int>VI;
    typedef long long ll;
    typedef double dd;
    const dd eps=1e-10;
    const int mod=1e8;
    const int N=1000010;
    il ll read(){
    	RG ll data=0,w=1;RG char ch=getchar();
    	while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    	if(ch=='-')w=-1,ch=getchar();
    	while(ch<='9'&&ch>='0')data=data*10+ch-48,ch=getchar();
    	return data*w;
    }
    
    il void file(){
    	freopen(".in","r",stdin);
    	freopen(".out","w",stdout);
    }
    
    ll n,a[N],f[N];
    struct node{ll x,y;}Q[N];ll L=1,R;
    il void insert(node q){
    	while(L<R&&(Q[R].y-Q[R-1].y)*(q.x-Q[R].x)>(q.y-Q[R].y)*(Q[R].x-Q[R-1].x))R--;
    	Q[++R]=q;
    }
    il ll query(ll k){
    	while(L<R&&k*(Q[L+1].x-Q[L].x)>(Q[L+1].y-Q[L].y))L++;
    	return Q[L].y-k*Q[L].x;
    }
    
    int main()
    {
    	n=read();for(RG int i=1;i<=n;i++)a[i]=read();
    	insert((node){0,0});
    	for(RG int i=1;i<=n;i++){
    		f[i]=query(i)+1ll*(i-1)*i/2+a[i];
    		insert((node){i,f[i]+1ll*(i+1)*i/2});
    		//一定要记得开1ll!!!
    	}
    	printf("%lld
    ",f[n]);
    	return 0;
    }
    
    
  • 相关阅读:
    JVM系列-001-JVM监控工具
    Java8-CompletableFuture
    Java8-ConcurrentUtils
    Java8-LongAccumulator
    Java8-Atomic
    Java8-ConcurrentHashMap
    Java8-Synchronized-No.02
    Java8-Synchronized-No.01
    Java8-Executors-No.03
    Java8-Executors-No.02
  • 原文地址:https://www.cnblogs.com/cjfdf/p/8646260.html
Copyright © 2011-2022 走看看