zoukankan      html  css  js  c++  java
  • Luogu P3545 [POI2012]HUR-Warehouse Store (带反悔的贪心)

    题目大意:

    n天。第i天上午会进货(A_i)件商品,中午的时候会有顾客需要购买(B_i)件商品,可以选择满足顾客的要求,或是无视掉他。

    如果要满足顾客的需求,就必须要有足够的库存。问最多能够满足多少个顾客的需求。

    思路:

    典型的带反悔的贪心。

    策略是能满足顾客的需求则满足他,但这样的的局部最优不一定是全局最优,比如

    4
    3 0 0 0
    1 2 1 1
    

    这时我们就要考虑反悔。

    我们记录下已满足的顾客的需求和编号,在一个以需求排序的大根堆当中更新最大需求。

    Code:
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    typedef pair<int, int> PI;
    const double eps = 1e-6;
    const int N = 250010;
    const int INF = 0x3f3f3f3f;
    const int mod = 1000000007; //998244353
    LL powmod(LL a, LL b) { LL res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1)res = res * a % mod; a = a * a % mod; }return res; }
    
    bool tg[N];
    struct Node {
    	LL a, b, id;
    	friend bool operator<(const Node& x, const Node& y) {
    		return x.b < y.b;
    	}
    }e[N];
    LL n, ans, sum;
    
    int main() {
    	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    	cin >> n;
    	for (int i = 1; i <= n; i++) {
    		cin >> e[i].a;
    		e[i].id = i;
    	}
    	for (int i = 1; i <= n; i++) cin >> e[i].b;
    	priority_queue<Node> q;
    	for (int i = 1; i <= n; i++) {
    		sum += e[i].a;
    		if (e[i].b <= sum) {
    			tg[e[i].id] = 1;
    			q.push(e[i]);
    			sum -= e[i].b;
    			ans++;
    		} else if (!q.empty() && q.top().b > e[i].b) {
    			tg[q.top().id] = 0;
    			tg[e[i].id] = 1;
    			sum += q.top().b - e[i].b;
    			q.pop();
    			q.push(e[i]);
    		}
    	}
    	cout << ans << endl;
    	for (int i = 1; i <= n; i++) {
    		if (tg[i]) cout << i << " ";
    	}
    	cout << endl;
    	return 0;
    }
    
  • 相关阅读:
    Enum, Generic and Templates
    Writing A Threadpool in Rust
    A First Look at Rust Language
    Closures in OOC
    Complexity Behind Closure
    Introduction to OOC Programming Language
    OOC,泛型,糟糕的设计。
    Enlightenment笔记
    Machine Learning/Random Projection
    Machine Learning/Introducing Logistic Function
  • 原文地址:https://www.cnblogs.com/Nepenthe8/p/14289798.html
Copyright © 2011-2022 走看看