The 17th Zhejiang Provincial Collegiate Programming Contest B.Bin Packing Problem
题意
给定n个物品,和一个容量为C的桶
需要求出为了装下这些物品,分别使用首次适应算法(FF)、最佳适应算法(BF)需要的桶的数量
\(n \leq 10^6\)
思路
BF:容易想到可以用set维护,每次二分查找能放进去的第一个位置,如果都放不下就另外开一个桶存放
注意因为可能存在重复元素,所以应该使用multiset
FF:容易想到利用数据结构维护区间最大值(我用的是线段树
首先想到了 \(O(nlog^2n)\) 做法,对于每个ai,二分[1, mid]最大能放下ai的位置,又因为查找也是log的,所以是log方
然而怎么改都tle
正解是一个log的做法,线段树update的时候就可以更新最大值、统计答案;查找哪个位置能放下ai时,query里判断一下,如果t[rt << 1]能放下,就往左区间询问,否则往右子树区间询问,当l=r时返回l
code
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, m, T, C;
int a[N];
int t[N << 2];
int mp[N], vis[N], Ans;
inline int maxx (int x, int y) {
return x > y ? x : y;
}
inline void pushup(int rt) {
t[rt] = maxx(t[rt << 1], t[rt << 1 | 1]);
}
inline void build(int l, int r, int rt) {
if(l == r) {
t[rt] = C;
mp[l] = rt;
vis[l] = 0;
return;
}
int mid = (l + r) >> 1;
build(l, mid, rt << 1);
build(mid + 1, r, rt << 1 | 1);
pushup(rt);
}
inline void update(int i, int l, int r, int x, int val) {
if (l == r) {
t[i] = val;
if(val < C && !vis[l]) {
vis[l] = 1;
Ans++;
}
return;
}
int mid = (l + r) >> 1;
if (x <= mid) update(i << 1, l, mid, x, val);
else update(i << 1 | 1, mid + 1, r, x, val);
pushup(i);
}
inline int query(int i, int l, int r, int v) {
if (l == r) return l;
int mid = (l + r) >> 1;
if(t[i << 1] >= v) return query(i << 1, l, mid, v);
else return query(i << 1 | 1, mid + 1, r, v);
}
inline void solve_1() {
build(1, n, 1);
Ans = 0;
for(int i = 1; i <= n; i++) {
int pos = query(1, 1, n, a[i]);
int v = t[mp[pos]];
update(1, 1, n, pos, v - a[i]);
}
printf("%d ", Ans);
}
multiset<int> S;
inline void solve_2() {
S.clear();
int tot = 0;
for (int i = 1; i <= n; ++i) {
if (S.empty()) {if(C > a[i]) S.insert(C - a[i]); ++tot; continue;}
auto tmp = S.lower_bound(a[i]);
if (tmp == S.end()) {
if(C > a[i]) S.insert(C - a[i]); ++tot;
} else {
int V = *tmp; S.erase(tmp);
if (a[i] < V) S.insert(V - a[i]);
}
}
printf("%d\n", tot);
}
int main() {
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &C);
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
solve_1();
solve_2();
}
system("pause");
return 0;
}
The 17th Zhejiang Provincial Collegiate Programming Contest B.Bin Packing Problem的更多相关文章
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Capture the Flag
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5503 The 12th Zhejiang Provincial ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5494 The 12th Zhejiang Provincial ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Beauty of Array
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5496 The 12th Zhejiang Provincial ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Lunch Time
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5499 The 12th Zhejiang Provincial ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Convert QWERTY to Dvorak
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5502 The 12th Zhejiang Provincial ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest May Day Holiday
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5500 The 12th Zhejiang Provincial ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Demacia of the Ancients
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5504 The 12th Zhejiang Provincial ...
- zjuoj The 12th Zhejiang Provincial Collegiate Programming Contest Ace of Aces
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5493 The 12th Zhejiang Provincial ...
- 140 - The 12th Zhejiang Provincial Collegiate Programming Contest(第二部分)
Floor Function Time Limit: 10 Seconds Memory Limit: 65536 KB a, b, c and d are all positive int ...
- The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - L Doki Doki Literature Club
Doki Doki Literature Club Time Limit: 1 Second Memory Limit: 65536 KB Doki Doki Literature Club ...
随机推荐
- Postgresql实现不同用跨模式访问
1.修改参数 2.创建两个用户 创建a1 create user a1 connection limit-1 password '123456'; alter user a1 SUPERUSER; g ...
- 自定义顺序表ArrayList
1.简介 顺序表是在计算机内存中以数组的形式保存的线性表,线性表的顺序存储是指用一组地址连续的存储单元依次存储线性表中的各个元素.使得线性表中在逻辑结构上相邻的数据元素存储在相邻的物理存储单元中,即通 ...
- pat乙级 1019 数字黑洞
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> ...
- wwise 音频引擎介绍
https://blog.csdn.net/GJQI12/article/details/108007696
- 微信小程序与微信公众号之间支付问题解决方案
前言 大家好,我是一名对编程有兴趣的小伙子,IT届称我为xiager,工作中叫我jake 就好了,如果此文对你有帮助希望多多关注哦. 准备 微信公众平台 微信支付平台 微信开放平台 一. 小程序 ...
- 解决点击el-dialog对话框,body抖动问题
在vue中点击el-dialog对话框有时候body样式会加上padding-right:8px. 百度了一下多数是增加padding,然后他们的解决办法是 全局给body加样式 padding-ri ...
- Python3 时间戳格式化和减法运算
import datetime import time # 获取当前时间(2023-02-16 16:41:36) now_date = datetime.datetime.now().strftim ...
- Linux中使用Makefile来运行QuestaSim
环境:Win7x64,VMware15.0,centOS7.0,QuestaSim10.7c 假设已经编辑好了一个全加器还有运行这个DUT的testbech,代码如下: 点击查看代码 // filen ...
- [UnityShader]说厌了的遮挡显示
首先是效果 这个其实有相当多的地方在说了,我就不嚼舌根了 大概原理:一个Shader两个Pass,第一个Pass不管深度,都渲染,但是不写入深度,第二个Pass几乎没有改动,因为后执行,会覆盖掉第一个 ...
- N63050 第十一周运维作业
第十一周 就业和全程班小伙伴本周学习内容: 二十一.Mysql数据库二 1.MySQL的视图函数存储过程触发器和事件管理(64分钟) 2.MySQL用户和权限管理(40分钟) 3.MySQL架构和存储 ...