• 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;
    }
    
  • 相关阅读:
    随笔 JS 字符串 分割成字符串数组 并动态添加到指定ID的DOM 里
    phpmyadmin查看创建表的SQL语句
    thinkphp 3.2响应头 x-powered-by 修改
    Centos 7.0查看硬盘使用情况 命令
    centos 7.0安装花生壳
    centos 7.0 菜鸟接触命令 记录
    centos 7.0 ssh 登陆
    Sphinx 2.2.6 window下安装全过程 未完 持续标记~~~~
    c#多线程编程实战(原书第二版)文摘
    分布式锁的几种实现方式(转)
  • 原文地址:https://www.cnblogs.com/Nepenthe8/p/14289798.html
Copyright © 2020-2023  润新知