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,求能否从中选出若干个,使 ...
随机推荐
- ConvertJavaMiliSecondToDateTime
private static DateTime ConvertJavaMiliSecondToDateTime(long javaMS) { DateTime UT ...
- 阿里云容器Kubernetes监控(九) - Kubernetes事件离线工具kube-eventer正式开源
前言 监控是保障系统稳定性的重要组成部分,在Kubernetes开源生态中,资源类的监控工具与组件百花齐放.除了社区自己孵化的metrics-server,还有从CNCF毕业的Prometheus等等 ...
- Please upgrade the installed version of powershell to the minimum required version and run the command again.
版权声明:本文为博主原创文章,转载请注明出处.谢谢 https://blog.csdn.net/cow66/article/details/77993908 我的系统是windows 7 安装了vag ...
- JavaScript —— 常用数据类型隐式转换
公用方法: let checkType = (data) => { if(data){ console.log(true); }else{ console.log(false); } } 一.字 ...
- Cython保护Python代码
注:.pyc也有一定的保护性,容易被反编译出源码... 项目发布时,为防止源码泄露,需要对源码进行一定的保护机制,本文使用Cython将.py文件转为.so进行保护.这一方法,虽仍能被反编译,但难度会 ...
- mysql带有子查询的like查询
SELECT * FROM by_app_categories WHERE c_name LIKE CONCAT('%', (SELECT `name` FROM b_catelist WHERE t ...
- qt中窗口绘制——图片的绘制
在qt 中,QPixmap 用于表示一张图片,支持png,jpg格式的加载. QPixmap pm("c:/test.png"); 或者 QPixmap pm; pm.load(& ...
- 开发板ping通虚拟机与主机
刚因为虚拟机与主机没法互相ping通的事情,奋战到将近凌晨一点.现在把这个过程总结一下,以方便后加入该行业的广大IT精英. VMWare提供了三种工作模式:bridged(桥接模式).NAT(网络地址 ...
- Istio on ACK集成生态(1): 集成TSDB助力可观测性存储
阿里云容器服务Kubernetes(简称ACK)支持一键部署Istio,可以参考文档在ACK上部署使用Isito.Istio on ACK提供了丰富的监控能力,为网格中的服务收集遥测数据,其中Mixe ...
- 阿里云DataWorks正式推出Stream Studio:为用户提供大数据实时计算的数据中台
5月15日 阿里云DataWorks正式推出Stream Studio,正式为用户提供大数据的实时计算能力,同时标志着DataWorks成为离线.实时双计算领域的数据中台. 据介绍,Stream St ...