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 ...
随机推荐
- 06#Web 实战:可滑动的标签页
实现效果图 本随笔只是记录一下大概的实现思路,如果感兴趣的小伙伴可以通过代码和本随笔的说明去理解实现过程.我的 Gitee 和 GitHub 地址.注意哦:这个只是 PC 上的标签页,手机端的没用,因 ...
- OpenLayers点聚合
1. 引言 当页面加载的数据量过大时,拖拽.缩放时往往会产生卡顿 然而,页面实现的内容是有限的,人眼可见范围也是有限的,过于微小的部分是可以不予显示的 聚合是解决这种问题的一个办法,当数据比较多,单个 ...
- 基于C++的OpenGL 01 之Hello Triangle
1. 引言 本文基于C++语言,描述OpenGL的绘制流程,这里描述的是OpenGL的核心模式(Core-profile) 本文基于Ubuntu 20.04.3 LTS系统,使用CMake构建程序,O ...
- html(Angular) 调用本地安装exe程序
1.写注册表 新建 .reg文件 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\creoparametric] "URL P ...
- Tensorflow 1.X 在windows上的安装
参考:https://blog.csdn.net/weixin_42326479/article/details/105539110 pip install -i https://pypi.douba ...
- user-agent反反爬
title: user-agent反反爬 author: 杨晓东 permalink: user-agent反反爬 date: 2021-10-02 11:27:04 categories: - 投篮 ...
- Prometheus学习笔记之设置存储时间为30天不生效
0x00 概述 Prometheus升级到2.23进行测试发现,设置存储时间为30天未生效,根据官方说明手册,实际运行后发现数据只能存储几个小时.... --storage.tsdb.retentio ...
- Git版本管理工具详细教程
一 Git初始化 下载安装, 下载地址: https://git-scm.com/downloads 每个系统的都有(linux.mac.windows等),看官网的安装教程,很详细,此处我以wind ...
- ES-DSL
GET index_name/_search{ "track_total_hits":true} 可以查询总记录数,不加只能展示最多10000条
- 如何写出不可维护的Vue代码
前言 不止一次接手过复杂业务功能模块,开端总是深陷其中难以自拔,无数个深夜抚摸着头皮在内心暗暗咒骂. 相信你也有过类似的经历,面对复杂的业务逻辑,看代码俩小时,写代码五分钟,没有点胆识和谋略都不敢下手 ...