题意

给定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的更多相关文章

  1. 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 ...

  2. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5494 The 12th Zhejiang Provincial ...

  3. 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 ...

  4. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Lunch Time

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5499 The 12th Zhejiang Provincial ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. PPMM

    代码 #include<cstdio> using namespace std; const int N = 1000000 , INF = 2e9; int n , m , x , he ...

  2. slate源码解析(二)- 基本框架与数据模型

    源码架构 首先来看下最核心的slate包下的目录: 可以看到,作为一个开源富文本库,其源码是相当之少.在第一篇文章中说过,Slate没有任何开箱即用的功能,只提供给开发者用于构建富文本所需的最基本的一 ...

  3. 一台电脑连接多个不同IP段

    1.首先让电脑能联网,则在ipv4里面设置好能上网的ip地址,dns则为路由器ip 备用dns8.8.8.8 2.点击高级依次添加其他内网ip段 例如192.168.0.66 ----------19 ...

  4. 跨平台C++ DLL导出宏

    #pragma once #if defined(__GNUC__) #define _DEPRECATED_ __attribute__((deprecated)) #define _FORCE_I ...

  5. java学习日记20230301-API文档

    JAVA API java application programming interinterface  应用程序编程接口,是java提供的基本编程接口 在线文档:https://www.matoo ...

  6. 以图搜图(demo创建流程)

    window10添加向量数据库以及调用 创建docker 1,在windows功能中打开Hyper-V 和 容器 2,进入https://www.docker.com/ ,下载windows版本进行安 ...

  7. VMware Workstation Pro 16、docker和Mysql相关

    VMware Workstation Pro 16安装参考 docker容器的使用参考 Docker 容器使用 Docker Hub资源 Docker Hub Mysql数据库安装参考 Mysql数据 ...

  8. Dependency Analyzer

    Dependency Analyzer  idea插件  查找maven依赖 1.Setting---->Plugins------>Dependency Analyzer 2.使用 po ...

  9. 关于我在安装2.6.9版本bochs虚拟机时遇到的问题以及解决过程

    更新于:2019.7.2 在阅读<一个64位操作系统的设计与实现>过程中,搭建实验环境遇到的诸多困难. 本人的实验环境:vmware15.0 下安装有kali-liunx虚拟机里进行的安装 ...

  10. 各版本JDK及镜像仓库

    Adoptium Marketplace 毕昇(华为) Dragonwell(阿里) Tencent Kona OpenJDK OracleJDK RedHatOpenJDK Temurin(ecli ...