题目来源: Codility
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题
 收藏
 关注
有N条绳子编号 0 至 N - 1,每条绳子后面栓了一个重物重量为Wi,绳子的最大负重为Ci。每条绳子或挂在别的绳子下或直接挂在钩子上(编号-1)。如果绳子下所有重物的重量大于绳子的最大负重就会断掉(等于不会断)。依次给出每条绳子的负重Ci、重物的重量Wi以及绳子会挂在之前的哪条绳子的下面,问最多挂多少个绳子而不会出现绳子断掉的情况。
 
例如下图:
 
5, 2, -1
3, 3, 0
6, 1, -1
3, 1, 0
3, 2, 3
 
 

 
挂到第4个时会有绳子断掉,所以输出3。
 
 
Input
第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)。
Output
输出1个数,最多挂到第几个绳子,不会出现绳子断掉的情况。
Input示例
5
5 2 -1
3 3 0
6 1 -1
3 1 0
3 2 3
Output示例
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绳子和重物的更多相关文章

  1. 51nod 1307 绳子与重物 (标记父节点更新即可)

    1307 绳子与重物 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有N条绳子编号 0 至 N - 1,每条绳子后面栓了一个重物重量为Wi,绳子的最大负重为Ci. ...

  2. 51nod 1307 绳子与重物(并查集水了一发)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1307 题意: 思路: 可以直接二分答案,然后dfs. 因为标签是并查集, ...

  3. 1307 绳子与重物(DFS)

    1307 绳子与重物 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有N条绳子编号 0 至 N - 1,每条绳子后面栓了一个重物重量 ...

  4. 51NOD 1639 绑鞋带 数学

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1639 假如一开始有一根绳子. 那么增加一根的时候,可以插在它的尾部,也可 ...

  5. 51nod 1243 排船的问题(二分)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1243 题意: 思路: 二分来做,每次贪心的把船安排到能安排的最左边即可. ...

  6. 51nod 1243 二分+贪心

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1243 1243 排船的问题 题目来源: Codility 基准时间限制: ...

  7. 51nod 1449 贪心

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1449 1449 砝码称重 题目来源: CodeForces 基准时间限制 ...

  8. 【51Nod 1244】莫比乌斯函数之和

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 模板题... 杜教筛和基于质因子分解的筛法都写了一下模板. 杜教筛 ...

  9. 51Nod 1268 和为K的组合

    51Nod  1268  和为K的组合 1268 和为K的组合 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 给出N个正整数组成的数组A,求能否从中选出若干个,使 ...

随机推荐

  1. JS原生实现五角星评价详情demo

    <style> *{margin: 0;padding: 0;} .pingfen{ width: 195px; margin:10px auto; height:20px; positi ...

  2. 2019.11.12htmlhomework1

    ex: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...

  3. D3D10/11中的遮挡查询的使用

    原文:D3D10/11中的遮挡查询的使用       在D3D10/11中,有D3D10_QUERY/D3D11_QUERY接口,通过QUERY接口,我们可以查询GPU的一些状态,比如GPU的时间戳信 ...

  4. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十八章:立方体贴图

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十八章:立方体贴图 代码工程地址: https://github.c ...

  5. 配置一个Oracle共享服务器进程环境需要哪两项参数

    SHARED_SERVERS和DISPATCHERS. PROTOCOL(pro或prot): 调度程序要监听的网络协议.这是唯一必需的属性 ADDRESS(ADD或者ADDR): 指定调度程序正在上 ...

  6. Oracle基础知识点——Oracle常用权限理解:SYSDBA、SYSOPER、Normal、dba、connect、resource

    权限介绍 系统权限 含义:系统规定用户使用数据库的权限,系统权限是针对用户对数据库的操作而言(登录数据库:读取数据表.视图:删除数据库).它只是概念上的role,只是一种登录认证时的身份标识而已. S ...

  7. char和achar互转

    #pragma once#include "stdafx.h" #ifndef _Convert_H_#define _Convert_H_ //定义转换类class Conver ...

  8. HDU_1061:Rightmost Digit

    Problem Description Given a positive integer N, you should output the most right digit of N^N.   Inp ...

  9. BZOJ 1834网络扩容题解

    一道不算太难的题目 但是真的很恶心 显然,对于第一问,我们直接无脑打模板就好了 第二问也不是很难,我们将每条边再连一条容量为inf,费用为w的边 但是流量只要小于第一问的答案加k就行了 所以我们增加一 ...

  10. Flask——向博客文章中添加图片

    未添加图片样式 添加图片设置: 1.允许渲染img标签 在数据库文章模型allowed_tags中添加img 2.给clean函数加个参数attributes=attrs, attrs = { '*' ...