题意

给定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. Git与GitHub的快速使用

    Git的快速使用 1. Git简介 Git最初是Linus花了两周时间自己用C写了一个分布式版本控制系统 特点: 分布式,每一个主机都有完整版本库 开源免费性能好 注:类似GitHub这种中心环境的存 ...

  2. pat乙级 1021个位数统计

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> ...

  3. AttributeError: module 'requests' has no attribute 'get' 报错分析

    这个报错与代码时没有关系 当文件名与调用模块名重合时,系统找不到我们调用的requests模块. 在命名时,我们要注意不要重合.

  4. Qt中的多窗体编程(续二)

    四.实现子窗体的按钮功能. 1.在显示时间的子窗体中,有两个默认的按钮,都还没有定义其功能,下面就来定义,无论单击哪个按钮,都将线束时钟显示的线程并关闭窗体. 2.在子窗体的可视化设计界面中,在窗体的 ...

  5. No.2.7

    响应式 什么是响应式网页?就是一套代码适配不同的屏幕宽度,不同的适配 媒体查询:能够根据设备宽度的变化,设置差异化样式 开发常用写法: 媒体特性常用写法 max-width min-width @me ...

  6. Python elasticsearch 报错及解决方法

    1. ERROR: [1] bootstrap checks failed [1]: the default discovery settings are unsuitable for product ...

  7. [GKCTF2021]RRRRSA

    [GKCTF2021]RRRRSA 题目 from Crypto.Util.number import * from gmpy2 import gcd flag = b'xxxxxxxxxxxxx' ...

  8. win10bug可导致系统崩溃

    1.使用浏览器访问访问路径:\\.\globalroot\device\condrv\kernelconnect会立刻导致系统崩溃.会影响Windows10 1709及以上版本 2.使用以下代码保存成 ...

  9. html添加公共文件

    html添加公共文件 记录添加公共头尾文件的方法 thymeleaf模板引擎 common.html index.html

  10. JavaSE总结(1)

    Java发展历史.HelloWorld.常量.变量类型转换.运算符.方法(函数)1.jdk版本:    jdk1.2---J2EE/J2SE/J2ME    jdk1.5---JavaSE/JavaE ...