题意

让你解决一个装箱问题,给你两种算法,让你求用这两种算法解决问题得到的方案

解题思路

对于第一种方法,用线段树维护区间最大值,要注意需要先判断左边以保证找到最前面的。
对于d第二种方法,使用lower_bound直接模拟即可

#include <bits/stdc++.h>using namespace std;const int N = 1e6 + 5;int a[N];
int n, c, T;
struct node {int l, r;int maxx;int tag;
}t[N<<2];void push_up(int u) {t[u].maxx = max(t[u<<1].maxx, t[u<<1|1].maxx);
}void push_down(int u) {if (t[u].tag != 0) {t[u<<1].tag = t[u].tag;t[u<<1|1].tag = t[u].tag;t[u<<1].maxx += t[u].tag;t[u<<1|1].maxx += t[u].tag;t[u].tag = 0;}
}void build(int l, int r, int u) {t[u].l = l, t[u].r = r;t[u].maxx = c;t[u].tag = 0;if (t[u].l == t[u].r) {return;}int mid = (l + r) >> 1;build(l, mid, u<<1);build(mid+1, r, u<<1|1);push_up(u);
}void update(int pos, int u, int v) {if (t[u].l == t[u].r) {t[u].maxx += v;t[u].tag += v;return;}push_down(u);int mid = (t[u].l + t[u].r) >> 1;if (pos <= mid) update(pos, u<<1, v);else update(pos, u<<1|1, v);push_up(u);
}int query(int val, int u) {if (t[u].l == t[u].r) {return t[u].l;}push_down(u);if (val <= t[u<<1].maxx) return query(val, u<<1);else return query(val, u<<1|1);
}int main() {scanf("%d", &T);while (T--) {scanf("%d %d", &n, &c);for (int i = 1; i <= n; i++) scanf("%d", &a[i]);build(1, n, 1);int Max_bot = 0;for (int i = 1; i <= n; i++) {int pos = query(a[i], 1);Max_bot = max(Max_bot, pos);update(pos, 1, -a[i]);}map<int, int> mp;mp[c] = 1;for (int i = 1; i <= n; i++) {auto it = mp.lower_bound(a[i]);if (it == mp.end()) {mp[c-a[i]]++;}else {mp[it->first - a[i]] ++;mp[it->first]--;if (mp[it->first] == 0) mp.erase(it);}}int Max_bot_2 = 0;for (auto x : mp) Max_bot_2 += x.second;printf("%d %d\n", Max_bot, Max_bot_2);}
}

Bin Packing Problem相关推荐

  1. java实现 一维装箱问题_装箱问题(Bin packing problem)

    问题的定义 装箱问题(Bin packing problem),又称集装优化,是一个利用运筹学去解决实际生活的的经典问题.在维基百科的定义如下: In the bin packing problem, ...

  2. B - Bin Packing Problem (线段树+multiset)

    第一个算法用线段树来进行快速查询第一个可以装下item的bin 第二个算法用multiset的二分快速找到能装下item的最小的bin(也就是lower_bound()) 线段树可以对无序的序列实现快 ...

  3. 【补题】Bin Packing Problem

    题意:我们现在用一种容量为c的容器分两种方法去装下n个物品,n个物品的体积给定,必须按照顺序来存放物品,求两种方法分别用多少容器. 第一种方法:我们每次找能放下目前的容器,都放不下就新开一个容器 第二 ...

  4. 【论文阅读】(2022)A goal-driven ruin and recreate heuristic for the 2D variable-sized bin packing prob...

    文章目录 一.Abstract 摘要 二.Introduction 介绍 三.Solution representation 解决方案的表示 四.Ruin and recreate heuristic ...

  5. HDU - 1613 Ecological Bin Packing

    问题描述: Background Bin packing, or the placement of objects of certain weights into different bins sub ...

  6. 【算法理论】bin packing 装箱问题

    Bin packing(装箱问题) Problem:给定n件物品和k个箱子,每一个箱子的容量为1,每一件物品的大小w为(0,1),要求使用最少数目的箱子来装上全部的物品. 这个问题是NPC问题,只有a ...

  7. Google OR-Tools(六) 装箱问题 Bin Packing

    本文参考Google OR-Tools官网文档介绍OR-Tools的使用方法. 装箱问题的描述是要将一组给定尺寸的物品放置到具有固定容量的容器中,一般情况下由于容器有容量限制,不可能放入所有物品,那么 ...

  8. OR-Tools:2-包装问题,箱包问题(bin packing)

    OR-Tools 解决的问题类型: Linear optimization Constraint optimization Mixed-integer optimization Bin packing ...

  9. UVa1149 - Bin Packing

    //UVa1149 - Bin Packing //贪心,背包,乘船问题 #include<iostream> #include<algorithm> using namesp ...

最新文章

  1. iOS手机 相册 相机(Picker Write)
  2. js高级教程阅读笔记 第五章-引用类型(5.6)
  3. Zabbix监控zookeeper
  4. Le Chapitre VI
  5. 有个产品经理女朋友是一种什么样的体验?
  6. 抽奖的箱子_王者荣耀近期问题不断,昭君星元箱子开出空气,瑶新皮涉嫌抄袭...
  7. 【概念信息】成本中心计划
  8. java中pagex_Java/5_get和post比较.md at master · zaoshangyaochifan/Java · GitHub
  9. pyinstaller使用方法及案例
  10. LeetCode 2 Keys Keyboard
  11. (一)梳理前端知识体系,搞定大厂必考面试题
  12. 【.NET进程通信】初探.NET中进程间通信的简单的实现
  13. github最值得收藏的Bootstrap3后台管理框架
  14. python画多层网络_绘制多层n
  15. fscapture设置中文_Fscapture是什么软件?有没有功能介绍?
  16. 关于实时协同编辑的架构思考
  17. 哈尔滨华夏计算机学院分数线,黑龙江2008年高职(专科)批录取院校录取最低分数线(九)...
  18. html5 cuplayer,PlayerLite免费flv与mp4网页视频播放器
  19. 基于Laravel的会计财务系统
  20. 电脑qq传到我的android文件在哪里,手机QQ传文件到我的电脑功能(QQ数据线)的使用方法...

热门文章

  1. 判断是否为USD格式
  2. 刘振飞:做云计算比保障双十一要难得多
  3. EOJ #3452 唐纳德先生和假骰子【暴力】
  4. 变革中的思索之重读《孙子兵法》
  5. 简历修订中,下载打开需密码
  6. Spring源码深度解析(郝佳)-学习-Spring Boot体系原理
  7. 海外游戏代投需要注意的
  8. mysql:百度地图经纬度,按照经纬度坐标排序查询
  9. 国产“芯”时代 盘点国内十大IC卡制卡企业
  10. fastadmin列表页展示分类名称通用搜索按分类名称搜索