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 ...
随机推荐
- 数据结构和算法day1(Java)
1.什么是数据结构? 数据结构就是把数据元素按照一定的关系组织起来的集合,用来组织和存储数据. 1.2.数据结构的分类: 逻辑结构 和物理结构 逻辑结构: 集合结构(无关系).线性结构(一对一).树形 ...
- 几种排序(c#实现)
代码: int[] arr = { 2, 3, 4, 6, 1, 5, 4 }; // 冒泡排序:把最小的往前冒 O(n2) //int temp1; //for (int i = 0; i < ...
- 问题集锦 ~ javascript
#使用PHP输出json数据给js变量 SyntaxError: unexpected token: identifier const data = <?php echo json_encode ...
- Android悬浮窗全屏不获取焦点但是可以漏出软键盘的办法
addFlags( WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);的作用是在不获取焦点的时候显示软键盘. 接触这个标记 getWindow().c ...
- vue-element-admin框架连接yapi配置vue.config.js
devServer: { host:'localhost', port:port, proxy:{ [process.env.VUE_APP_BASE_API + '/admin']: { //配置p ...
- vscode格式化
1.tslint TypeScript的格式化 2.esLint 3.prettier 4.vetur 格式化html,css,vue
- python发送钉钉消息通用脚本
1.使用shell生成需要发送的内容. 2.调用该脚本发送文本内容python3,其中的文件 /wj/sbjk,需要改成直接需要发送的文件. [root@manager dingding]# more ...
- cisco-RIPv2重分发缺省路由
项目设计: 网络拓扑图: IP地址规划表 设备 端口 接口模式 绑定vlan IP地址 对端设备 端口 IP地址 R1 Fa0/0 无 无 172.16.1.1/30 R2 Fa0/0 172.16. ...
- Django 之RestFramework
1. 从request先说起 在Django原生的request里,请求的数据可以从request.GET或者request.POST里面取到. 需要注意的是,如果是POST请求,request.PO ...
- Linux,Lnmp配置Index of /索引页
其实很简单就是修改conf配置文件 进入域名对应的vhost修改conf文件: 第一种方法 cd /usr/local/nginx/vhost/ 在清楚当前域名所对用的conf文件可以试用 ll 列出 ...