zoukankan      html  css  js  c++  java
  • loj#2483. 「CEOI2017」Building Bridges(dp cdq 凸包)

    题意

    题目链接

    Sol

    [f[i], f[j] + (h[i] - h[j])^2 + (w[i - 1] - w[j])) ]

    然后直接套路斜率优化,发现(k, x)都不单调

    写个cdq就过了

    辣鸡noi.ac居然出裸题&&原题

    #include<bits/stdc++.h> 
    #define Pair pair<double, double>
    #define MP(x, y) make_pair(x, y)
    #define fi first
    #define se second
    #define int long long 
    #define LL long long 
    #define db  double
    #define Fin(x) {freopen(#x".in","r",stdin);}
    #define Fout(x) {freopen(#x".out","w",stdout);}
    using namespace std;
    const int MAXN = 1e6 + 10, mod = 1e9 + 7, INF = 1e18 + 10;
    const double eps = 1e-9;
    template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
    template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
    template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
    template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
    template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
    template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
    template <typename A> inline void debug(A a){cout << a << '
    ';}
    template <typename A> inline LL sqr(A x){return 1ll * x * x;}
    inline int read() {
        char c = getchar(); int x = 0, f = 1;
        while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
        while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
        return x * f;
    }
    int N;
    struct Sta {
        int id;
        db h, w, x, y, f, ad;
        void Get() {
            x = 2 * h;
            y = f + h * h - w;
        }
    }a[MAXN], st[MAXN];
    vector<Pair> v;
    int comp(const Sta &a, const Sta &b) {
    	return a.id < b.id;
    }
    double GetK(Pair a, Pair b) {
        if((b.fi - a.fi) < eps) return INF;
        return (b.se - a.se) / (b.fi - a.fi);
    }
    
    int sid[MAXN], cur;
    
    void GetConvexHull(int l, int r) {
    	while(cur) sid[cur--] = 0;
        v.clear();
        for(int i = l; i <= r; i++) {
            double x = a[i].x, y = a[i].y;
            while((v.size() > 1 && ((GetK(v[v.size() - 1], MP(x, y)) < GetK(v[v.size() - 2], v[v.size() - 1])))) ||
    			  ((v.size() > 0) && (v[v.size() - 1].fi == x) && (v[v.size() - 1].se >= y))) 
    			v.pop_back(), sid[cur--] = 0;
            v.push_back(MP(x, y));	sid[++cur] = a[i].id;
        }
    }
    int cnt = 0;
    db Find(int id, db k) {
    	int tmp = v.size();
        int l = 0, r = v.size() - 1, ans = 0;
        while(l <= r) {
            int mid = l + r >> 1;
            if((mid == v.size() - 1) || (GetK(v[mid], v[mid + 1]) > k)) r = mid - 1, ans = mid;
            else l = mid + 1;
        }
        return v[ans].se - k * v[ans].fi;
    }
    void CDQ(int l, int r) {
        if(l == r) {
            a[l].Get(); 
    		return ;   
    	}
        int mid = l + r >> 1;
      	CDQ(l, mid); 
        GetConvexHull(l, mid);
        for(int i = mid + 1; i <= r; i++) {
    		chmin(a[i].f, Find(i, a[i].h) + a[i].ad);
    	}
    	CDQ(mid + 1, r);
        int tl = l, tr = mid + 1, tot = tl - 1;
        while(tl <= mid || tr <= r) {
            if((tr > r) || (tl <= mid && a[tl].x < a[tr].x)) st[++tot] = a[tl++];//?????tl <= mid 
            else st[++tot] = a[tr++];
        }
        for(int i = l; i <= r; i++) a[i] = st[i]; 
    }
    signed main() {
        N = read(); 
        for(int i = 1; i <= N; i++) a[i].h = read();
        for(int i = 1; i <= N; i++) {
    		a[i].w = read() + a[i - 1].w; a[i].id = i;
    		a[i].f = a[i - 1].f + sqr(a[i].h - a[i - 1].h);
    		a[i].ad = sqr(a[i].h) + a[i - 1].w;
        	if(i == 1) a[1].f = 0;
        }
        CDQ(1, N);
        sort(a + 1, a + N + 1, comp);
    //	for(int i = 1; i <= N; i++) cout << i << ' ' << (LL)a[i].f << '
    ';
    	cout << (LL)a[N].f;
    	return 0;
    }
    
  • 相关阅读:
    bzoj1432_[ZJOI2009]Function
    Luogu1681_ 最大正方形II
    初等数论-Base-2(扩展欧几里得算法,同余,线性同余方程,(附:裴蜀定理的证明))
    [bzoj2456] mode
    初等数论-Base-1(筛法求素数,欧拉函数,欧几里得算法)
    小程序之Tab切换
    vue-axios基本用法
    vue-过渡动画
    vue-router实例
    永恒之蓝漏洞利用复现
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/10541978.html
Copyright © 2011-2022 走看看