51nod 1307绳子和重物


第1行:1个数N,表示绳子的数量(1 <= N <= 50000)。
第2 - N + 1行:每行3个数,Ci, Wi, Pi,Ci表示最大负重,Wi表示重物的重量,Pi表示挂在哪个绳子上,如果直接挂在钩子上则Pi = -1(1 <= Ci <= 10^9,1 <= Wi <= 10^9,-1 <= Pi <= N - 2)。
输出1个数,最多挂到第几个绳子,不会出现绳子断掉的情况。
5
5 2 -1
3 3 0
6 1 -1
3 1 0
3 2 3
3 思路:1.二分,二分枚举最多能挂到第k个绳子,从第k个往第一个推,若发现当前可以绳子可以承载下所有重物则将k增大,否则将k减小。
2.并查集,并查集自底向上维护每一个绳子,若当前绳子挂的负重大于承重则从最后一个重物开始往前删,直到负重小于承重,这样下去等挂到根节点的时候所有的重物都已经挂完了,剩下没有删掉的个数即为最多的个数 二分:
#include<iostream>
#include<algorithm>
#include<cstring> using namespace std;
typedef long long LL;
const int maxn = ;
struct node {
LL w, c, pre, cost;
}e[maxn];
LL n;
void reset()
{
for (int i = ; i <= n; i++)
e[i].cost = ;
}
int main()
{
ios::sync_with_stdio(false);
while (cin >> n) {
for (int i = ; i <= n; i++) {
cin >> e[i].c >> e[i].w >> e[i].pre;
e[i].pre++;
}
int l = , r = n; bool flag;
while (l <= r) {
int mid = (l + r) >> ;
flag = true;
reset();
for (int i = mid; i >= ; i--) {
e[i].cost += e[i].w;
if (e[i].cost > e[i].c) {
flag = false; break;
}
e[e[i].pre].cost += e[i].cost;
}
if (flag) l = mid + ;
else r = mid - ;
}
cout << r << endl;
}
return ;
}
并查集:
#include<iostream>
#include<algorithm>
#include<cstring> using namespace std;
const int maxn = ;
typedef long long LL;
struct Task {
int w, c, pre, cost;
}e[maxn];
int n, f[maxn];
int Find(int x)
{
if (f[x] != x)
f[x] = Find(f[x]);
return f[x];
}
int main()
{
ios::sync_with_stdio(false);
while (cin >> n) {
for (int i = ; i <= n; i++) {
cin >> e[i].c >> e[i].w >> e[i].pre;
e[i].cost += e[i].w; e[i].pre++;
f[i] = i;
}
int ans = n;
for (int i = n; i >= ; i--) {
while (e[i].cost > e[i].c) {
int tmp = Find(ans);
e[tmp].cost -= e[ans].w;
ans--;
}
e[e[i].pre].cost += e[i].cost;
f[i] = e[i].pre;
}
cout << ans << endl;
}
return ;
}
51nod 1307绳子和重物的更多相关文章
- 51nod 1307 绳子与重物 (标记父节点更新即可)
1307 绳子与重物 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有N条绳子编号 0 至 N - 1,每条绳子后面栓了一个重物重量为Wi,绳子的最大负重为Ci. ...
- 51nod 1307 绳子与重物(并查集水了一发)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1307 题意: 思路: 可以直接二分答案,然后dfs. 因为标签是并查集, ...
- 1307 绳子与重物(DFS)
1307 绳子与重物 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有N条绳子编号 0 至 N - 1,每条绳子后面栓了一个重物重量 ...
- 51NOD 1639 绑鞋带 数学
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1639 假如一开始有一根绳子. 那么增加一根的时候,可以插在它的尾部,也可 ...
- 51nod 1243 排船的问题(二分)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1243 题意: 思路: 二分来做,每次贪心的把船安排到能安排的最左边即可. ...
- 51nod 1243 二分+贪心
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1243 1243 排船的问题 题目来源: Codility 基准时间限制: ...
- 51nod 1449 贪心
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1449 1449 砝码称重 题目来源: CodeForces 基准时间限制 ...
- 【51Nod 1244】莫比乌斯函数之和
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 模板题... 杜教筛和基于质因子分解的筛法都写了一下模板. 杜教筛 ...
- 51Nod 1268 和为K的组合
51Nod 1268 和为K的组合 1268 和为K的组合 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 给出N个正整数组成的数组A,求能否从中选出若干个,使 ...
随机推荐
- jquery 日期和时间的逻辑,比较大小
HTML:<ul> <li> <span>到达</span> <img class="date-s" src="/p ...
- 对象无法注册到Spring容器中,手动从spring容器中拿到我们需要的对象
当前对象没有注册到spring容器中,此时无法new object() 的方式创建对象,否则所有@Autowired 注入的对象都为null; 处理方式: 手动创建一个类@Component注册到S ...
- Mac查看Python安装路径和版本
目录 #查看当前所有Python版本路径 appledeMBP:~ apple$ which python2.7 /usr/local/bin/python2.7 appledeMBP:~ apple ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 全书总结
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 全书总结 本系列文章中可能有很多翻译有问题或者错误的地方:并且有些章节 ...
- Oracle使用——impdp导入数据时数据表已经存在
背景 在做数据迁移时,需要将不同地方的dmp文件整合到一个数据库中,在导入时,目标表已经存在,该如何把数据追加进入目标表中 方法介绍 当使用IMPDP完成数据库导入时,如遇到表已存在时,Oracle提 ...
- MaxCompute Mars开发指南
Mars 算法实践 人脸识别 Mars 是一个基于矩阵的统一分布式计算框架,而且 Mars 已经在 GitHub 中开源.当你看完 Mars 的介绍可能会问它能做什么,这几乎取决于你想做什么,因为 M ...
- 当pip安装因为网络超时而无法安装的时候慢
2.4 尝试pip --default-timeout=1000 install https://download.pytorch.org/whl/cu100/torch-1.1.0-cp36-cp ...
- 修改Mariadb存储路径
大部分基于此文章操作:http://lddyw.blog.51cto.com/4151746/1684364 找个好久的资料,都打算源码安装了,最后终于更改成功了. 环境:CentOS6.6 64位虚 ...
- 树状数组(Binary Index Tree)
一维BIT(单点更新,区间求和): Problem - 1166 #include <iostream> #include <algorithm> #include <c ...
- 2019-7-1-VisualStudio-快速设置启动项目
title author date CreateTime categories VisualStudio 快速设置启动项目 lindexi 2019-07-01 14:37:38 +0800 2019 ...