Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
SRQuestions
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张敏捷
SRQuestions
Commits
329c989c
Commit
329c989c
authored
Oct 29, 2019
by
majexh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加 度小满 第二题的解法
parent
f0714c0f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
12 deletions
+38
-12
Second.java
度小满/second/Second.java
+38
-12
No files found.
度小满/second/Second.java
View file @
329c989c
packa
ge
second
;
packa
ge
second
;
...
...
@@ -17,7 +17,7 @@ public class Second {
public
void
add
(
List
<
Pair
<
Integer
,
Integer
>>
list
,
int
time
,
int
weight
)
{
list
.
add
(
new
Pair
<>(
time
,
weight
));
// list 已经是一个小顶堆 重新排序
for
(
int
i
=
list
.
size
()
-
1
;
i
>
=
0
;)
{
for
(
int
i
=
list
.
size
()
-
1
;
i
>
0
;)
{
// 父节点的序号
int
father
=
i
%
2
==
0
?
(
i
-
2
)
/
2
:
i
/
2
;
// 与父节点比较
...
...
@@ -31,10 +31,25 @@ public class Second {
}
/**
* 弹出值 并且在弹出之后 重新从上向下构建 小顶堆
* 弹出
最小
值 并且在弹出之后 重新从上向下构建 小顶堆
*/
public
void
pop
()
{
public
Pair
<
Integer
,
Integer
>
pop
(
List
<
Pair
<
Integer
,
Integer
>>
list
)
{
swap
(
list
,
0
,
list
.
size
()
-
1
);
Pair
<
Integer
,
Integer
>
temp
=
list
.
remove
(
list
.
size
()
-
1
);
int
i
=
0
;
// 从上向下 重新构建小顶堆
while
(
i
<
list
.
size
())
{
// 子树的左边那个
int
k
=
2
*
i
+
1
;
// 左子树大于右子树
if
(
k
<
list
.
size
()
-
1
&&
list
.
get
(
k
).
getKey
()
>
list
.
get
(
k
+
1
).
getKey
())
k
++;
if
(
k
>=
list
.
size
()
||
list
.
get
(
i
).
getKey
()
<
list
.
get
(
k
).
getKey
())
break
;
swap
(
list
,
i
,
k
);
i
=
k
;
}
return
temp
;
}
...
...
@@ -43,27 +58,38 @@ public class Second {
int
n
=
scanner
.
nextInt
(),
w
=
scanner
.
nextInt
();
int
[]
t
=
new
int
[
n
],
weights
=
new
int
[
n
];
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
t
[
i
]
=
scanner
.
nextInt
();
weights
[
i
]
=
scanner
.
nextInt
();
}
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
weights
[
i
]
=
scanner
.
nextInt
();
t
[
i
]
=
scanner
.
nextInt
();
}
Second
second
=
new
Second
();
List
<
Pair
<
Integer
,
Integer
>>
heap
=
new
LinkedList
<>();
// 用堆排序的话就需要在出去的时候
int
currentTime
=
0
,
currentWeight
=
w
,
index
=
0
;
while
(
true
)
{
while
(
currentWeight
>=
0
)
{
if
(
index
>=
n
)
break
;
while
(
index
<
n
&&
currentWeight
-
weights
[
index
]
>=
0
)
{
// 入堆 并调整
second
.
add
(
heap
,
currentTime
+
t
[
index
],
weights
[
index
]);
currentWeight
-=
weights
[
index
];
index
++;
}
// 出堆 直到下一个车能够入堆为止
// while () {
//
// }
if
(
index
==
n
)
{
// 等于n的时候 表示已经全部入完 这个时候需要全部出完即可
while
(
heap
.
size
()
!=
0
)
{
currentTime
=
second
.
pop
(
heap
).
getKey
();
}
break
;
}
else
{
// 出堆 直到下一个车能够入堆为止
while
(
currentWeight
<
weights
[
index
])
{
Pair
<
Integer
,
Integer
>
temp
=
second
.
pop
(
heap
);
currentWeight
+=
temp
.
getValue
();
currentTime
=
temp
.
getKey
();
}
}
}
System
.
out
.
println
(
currentTime
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment