zoukankan      html  css  js  c++  java
  • 【代填】土地购买「Usaco2008 Mar」(贪心+斜率优化dp)

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int N = 50010;
    template <typename T> void read(T &x) {
    	T f = 1;
    	char ch = getchar();
    	for (; '0' > ch || ch > '9'; ch = getchar()) if (ch == '-') f = -1;
    	for (x = 0; '0' <= ch && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
    	x *= f;
    }
    int n;
    struct node{
    	ll w, l;
    	friend bool operator < (node x, node y) {
    		if (x.w != y.w) return x.w < y.w;
    		return x.l < y.l;
    	}
    }a[N], r[N];
    int top;
    ll dp[N];
    int q[N], head, tail;
    long double Y(int i) {
    	return dp[i];
    }
    long double X(int i) {
    	return r[i + 1].l;
    }
    long double slope(int i, int j) {
    	if (X(i) == X(j)) {
    		return Y(j) >= Y(i) ? 1e18 : -1e18;
    	}
    	return (Y(j) - Y(i)) / (X(j) - X(i));
    }
    int main() {
    	read(n);
    	for (int i = 1; i <= n; i++) read(a[i].w), read(a[i].l);
    	sort(a + 1, a + 1 + n);
    	for (int i = 1; i <= n; r[++top] = a[i], i++) while (top && r[top].l < a[i].l) top--;
    	q[head = tail = 1] = 0;
    	for (int i = 1; i <= top; i++) {
    		while (head < tail && slope(q[head + 1], q[head]) >= -1.0 * r[i].w) head++;
    		dp[i] = r[i].w * r[q[head] + 1].l + dp[q[head]];
    		while (head < tail && slope(q[tail], q[tail - 1]) <= slope(i, q[tail])) tail--;
    		q[++tail] = i;
    	}
    	printf("%lld", dp[top]);
    	return 0;
    }
    
  • 相关阅读:
    几种任务调度的 Java 实现方法与比较
    nginx配置
    生产消费_lock和阻塞队列
    阻塞队列
    countdownlatch+cyclicbarrier+semphore
    01背包
    skiplist
    lru
    按序打印_lock和condition
    按序打印_volatile 无法保证顺序
  • 原文地址:https://www.cnblogs.com/zcr-blog/p/14115650.html
Copyright © 2011-2022 走看看