Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
qq_36480062
Algorithm
提交
284d37fd
A
Algorithm
项目概览
qq_36480062
/
Algorithm
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
Algorithm
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
284d37fd
编写于
1月 30, 2020
作者:
qq_36480062
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
commit
上级
061ef5bc
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
74 addition
and
33 deletion
+74
-33
algorithm/com/shiyu/Main.java
algorithm/com/shiyu/Main.java
+3
-2
algorithm/dp/bag.java
algorithm/dp/bag.java
+14
-25
algorithm/dp/机器人走方格.java
algorithm/dp/机器人走方格.java
+1
-1
algorithm/dp/装箱问题.java
algorithm/dp/装箱问题.java
+1
-1
挑战程序设计竞赛/src/Greedy/切木板.java
挑战程序设计竞赛/src/Greedy/切木板.java
+48
-4
挑战程序设计竞赛/src/dp/多重部分和.java
挑战程序设计竞赛/src/dp/多重部分和.java
+7
-0
未找到文件。
algorithm/com/shiyu/Main.java
浏览文件 @
284d37fd
...
...
@@ -10,9 +10,10 @@ public class Main {
PriorityQueue
<
Integer
>
pq
=
new
PriorityQueue
<
Integer
>();
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
int
a
=
sc
.
nextInt
();
pq
.
add
(
a
);
pq
.
add
(
a
);
}
int
ans
=
0
,
c
=
0
;
long
ans
=
0
;
int
c
=
0
;
if
(
pq
.
size
()
==
1
)
ans
+=
pq
.
poll
();
while
(
pq
.
size
()
>
1
)
{
...
...
algorithm/dp/bag.java
浏览文件 @
284d37fd
...
...
@@ -7,16 +7,15 @@ import java.util.Arrays;
*/
public
class
bag
{
public
static
void
main
(
String
[]
args
)
{
// w(3);
System
.
out
.
println
(
dp
());
System
.
out
.
println
(
dpByOne
());
// System.out.println(dpByWan());
// System.out.println(dp());
// System.out.println(dpByOne());
System
.
out
.
println
(
dpByWan
());
}
public
static
int
n
=
4
;
//物品数量
public
static
int
W
=
10
;
//背包容量
public
static
int
[]
w
=
{
2
,
1
,
3
,
2
};
//重量
public
static
int
[]
v
=
{
3
,
2
,
4
,
2
};
//价值
public
static
int
W
=
9
;
//背包容量
public
static
int
[]
w
=
{
2
,
2
,
3
,
2
};
//重量
public
static
int
[]
v
=
{
3
,
3
,
4
,
2
};
//价值
/**
* 01背包问题
...
...
@@ -47,7 +46,7 @@ public class bag {
public
static
int
dp
()
{
int
[][]
dp
=
new
int
[
n
][
W
+
1
];
//初始化dp的第一行,第一行可选物品只有1个,能装上就装
for
(
int
i
=
0
;
i
<
W
+
1
;
i
++)
{
for
(
int
i
=
0
;
i
<
=
W
;
i
++)
{
if
(
i
>=
w
[
0
])
{
// 每种容量-0号物品
dp
[
0
][
i
]
=
v
[
0
];
}
else
{
...
...
@@ -55,7 +54,7 @@ public class bag {
}
}
for
(
int
i
=
1
;
i
<
n
;
i
++)
{
for
(
int
j
=
0
;
j
<
W
+
1
;
j
++)
{
for
(
int
j
=
0
;
j
<
=
W
;
j
++)
{
if
(
j
>=
w
[
i
])
{
//要的起
//选择当前物品,即i号物品,剩余容量。
int
i1
=
v
[
i
]
+
dp
[
i
-
1
][
j
-
w
[
i
]];
...
...
@@ -77,7 +76,7 @@ public class bag {
* 每次倒序更新,不然数据不能重用。
* 思路:定义一个数组作为辅助空间,初始为n+1,根据dp公式倒序更新
*
* @return
* @return
最大价值
*/
public
static
int
dpByOne
()
{
int
[]
dp
=
new
int
[
W
+
1
];
...
...
@@ -92,16 +91,16 @@ public class bag {
}
/**
* 完全背包,但未经测试不知对错,肯定是错的
* 完全背包
* 单个商品可选多个
*
* @return
* @return
最大价值
*/
public
static
int
dpByWan
()
{
int
[]
dp
=
new
int
[
W
+
1
];
System
.
out
.
println
(
Arrays
.
toString
(
dp
));
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
for
(
int
j
=
w
[
i
+
1
];
j
<=
n
;
j
++)
{
dp
[
j
]
=
Math
.
max
(
dp
[
j
],
v
[
i
+
1
]
+
dp
[
j
-
w
[
i
+
1
]]);
for
(
int
j
=
w
[
i
];
j
<=
W
;
j
++)
{
dp
[
j
]
=
Math
.
max
(
dp
[
j
],
v
[
i
]
+
dp
[
j
-
w
[
i
]]);
}
System
.
out
.
println
(
Arrays
.
toString
(
dp
));
}
...
...
@@ -112,14 +111,4 @@ public class bag {
}
public
static
void
w
(
int
n
)
{
int
c
[]
=
{
1
,
2
,
3
};
int
dp
[]
=
new
int
[
n
+
1
];
for
(
int
i
=
1
;
i
<=
n
;
i
++)
{
for
(
int
j
=
0
;
j
<
3
;
j
++)
{
dp
[
j
]
=
dp
[
j
]
+
dp
[
j
-
c
[
i
]];
}
}
System
.
out
.
println
(
dp
[
n
]);
}
}
algorithm/dp/机器人走方格.java
浏览文件 @
284d37fd
...
...
@@ -20,7 +20,7 @@ import java.util.Arrays;
* 递推方程
* 有了状态,也知道了问题之间的联系,其实递推方程也出来了,就是
* dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
*
有了这些,这道题还没完,我们还要
考虑状态数组的初始化问题,对于上边界和左边界的点,因为它们只能从一个方向过来,需要单独考虑,比如上边界的点只能从左边这一个方向过来,左边界的点只能从上边这一个方向过来,它们的不同路径个数其实就只有 1,提前处理就好。
* 考虑状态数组的初始化问题,对于上边界和左边界的点,因为它们只能从一个方向过来,需要单独考虑,比如上边界的点只能从左边这一个方向过来,左边界的点只能从上边这一个方向过来,它们的不同路径个数其实就只有 1,提前处理就好。
*/
public
class
机器人走方格
{
public
static
void
main
(
String
[]
args
)
{
...
...
algorithm/dp/装箱问题.java
浏览文件 @
284d37fd
...
...
@@ -28,7 +28,7 @@ import java.util.Scanner;
public
class
装箱问题
{
public
static
void
main
(
String
[]
args
)
{
Scanner
sc
=
new
Scanner
(
System
.
in
);
int
v
=
sc
.
nextInt
();
//箱子容量
int
v
=
sc
.
nextInt
();
//箱子容量
消除
int
n
=
sc
.
nextInt
();
//物品数量
int
tiji
[]
=
new
int
[
n
+
1
];
for
(
int
i
=
1
;
i
<=
n
;
i
++)
{
...
...
挑战程序设计竞赛/src/Greedy/切木板.java
浏览文件 @
284d37fd
...
...
@@ -5,8 +5,9 @@ import java.util.PriorityQueue;
/**
* POJ 3253
* Rence Repair
* https://blog.csdn.net/qq_40421671/article/details/83274031
* 农夫约翰为了修理栅栏,要将一块很长的木板分割成N块。
* 准备切成的木板的长度为L1、L2、……、Ln. 未切割木板的长度恰好为
* 准备切成的木板的长度为L1、L2、……、Ln. 未切割
嗯
木板的长度恰好为
* 切割木板的长度和。每次切断木板时,需要的开销为这块木板的长度。
* 例如,长度为21的木板切割成5、8、8的三块木板。
* 长为21的木板切割成13、8时,开销为21.
...
...
@@ -19,15 +20,15 @@ import java.util.PriorityQueue;
* N=3, L={8, 5, 8}
* 输出样例:
* 34
*
利用Huffman思想
,要使总费用最小,那么每次只选取最小长度的两块木板相加,再把这些“和”累加到总费用中即可
*
,要使总费用最小,那么每次只选取最小长度的两块木板相加,再把这些“和”累加到总费用中即可
* 本题虽然利用了Huffman思想,但是直接用HuffmanTree做会超时,可以用优先队列做
* 因为朴素的HuffmanTree思想是:
* (1)先把输入的所有元素升序排序,再选取最小的两个元素,把他们的和值累加到总费用
* (2)把这两个最小元素出队,他们的和值入队,重新排列所有元素,重复(1),直至队列中元素个数<=1,
*
则累计的费用就是最小费用
* 则累计的费用就是最小费用
*/
public
class
切木板
{
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
int
[]
arr
=
{
8
,
5
,
8
,
4
,
3
};
System
.
out
.
println
(
Zui
(
3
,
arr
));
}
...
...
@@ -47,3 +48,45 @@ public class 切木板 {
return
ans
;
}
}
/**
* #include <iostream>
* #include <algorithm>
* #include <queue>
* <p>
* using namespace std;
* <p>
* int n;
* int l[20001];
* <p>
* int main() {
* <p>
* while(~(scanf("%d", &n))) {
* <p>
* priority_queue<int, vector<int>, greater<int> > q;
* <p>
* for(int i=0; i<n; i++) {
* cin >> l[i];
* q.push(l[i]);
* }
* <p>
* long long ans = 0;
* while(q.size() > 1) {
* <p>
* int m1, m2;
* m1 = q.top();
* q.pop();
* m2 = q.top();
* q.pop();
* <p>
* int t = m1 + m2;
* ans += t;
* <p>
* q.push(t);
* }
* <p>
* cout << ans << endl;
* }
* <p>
* return 0;
* }
**/
\ No newline at end of file
挑战程序设计竞赛/src/dp/多重部分和.java
0 → 100644
浏览文件 @
284d37fd
package
dp
;
public
class
多重部分和
{
public
static
void
main
(
String
[]
args
)
{
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录