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,求能否从中选出若干个,使 ...
随机推荐
- 一键制作启动elasticsearch和kibana启动的脚本可执行程序
1.测试环境 测试环境: . windows10专业版 . elasticsearch6.5.4 . kibana6.5.4 2.启动的脚本run.py import os import time i ...
- Silverlight 2.5D RPG游戏技巧与特效处理:(五)HLSL渲染动画
原文:Silverlight 2.5D RPG游戏技巧与特效处理:(五)HLSL渲染动画 或许大家依旧对上一节中的“黑夜”及“梦回过去”记忆犹新,追问下去HLSL到底是何方神圣能实现如此炫酷之效果?层 ...
- SFINAE and enable_if
There's an interesting issue one has to consider when mixing function overloading with templates in ...
- bzoj1624 寻宝之路
Description 农夫约翰正驾驶一条小艇在牛勒比海上航行. 海上有N(1≤N≤100)个岛屿,用1到N编号.约翰从1号小岛出发,最后到达N号小岛.一张藏宝图上说,如果他的路程上经 ...
- C++简单读取 & 写入实例
#include <fstream> #include <iostream> using namespace std; int main () { ]; // 以写模式打开文件 ...
- win10下Anaconda3配置环境变量
有时候在win10安装好Anaconda3后,使用conda命令时依然会出现: C:\Users\dell\PycharmProjects\pytorch>conda list 'conda' ...
- mysql 语句的查询过程解析
select * from tb where ID = 1 下面解析的查询过程都是基于上面的简单查询,该系列的所有素材都来自于丁奇的mysql的45讲 1.建立连接 a.客户端发出请求,请求首先到达连 ...
- QT_OPENGL-------- 2.shader
用可编程管线绘制一个三角形 1.以上一节window为基准,进行绘制. 2.下载编译glew,并在.pro添加动态链接,并在头文件中引用. LIBS +=-L/usr/lib64 -lGLEW 可能根 ...
- 基于Tablestore Tunnel的数据复制实战
前言 数据复制主要指通过互联的网络在多台机器上保存相同数据的副本,通过数据复制方案,人们通常希望达到以下目的:1)使数据在地理位置上更接近用户,进而降低访问延迟:2)当部分组件出现故障时,系统依旧可以 ...
- ORA错误查询手册
ORA-00910: 指定した長さがデータ型に対して長すぎます 原因: データ型CHARまたはRAWに対して指定した長さは.2000を超える値または4000を超える値であるため無効です. 処置: 指定 ...