当前位置:网站首页>UVa 10003 - Cutting Sticks(白书,区间DP)
UVa 10003 - Cutting Sticks(白书,区间DP)
2022-08-03 22:03:00 【51CTO】
题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=12&problem=944&mosmsg=Submission+received+with+ID+17137424
思路:比较裸的区间DP吧,状态转移方程为dp[i][j]=min(dp[i][k]+dp[k][j])+a[j]-a[i],可以用四边形不等式进行优化
AC代码1:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const
int
inf
=
0x3f3f3f3f;
//1061109567
typedef
long
long
LL;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using
namespace
std;
int
a[
55],
dp[
55][
55];
int
main()
{
int
l;
while(
scanf(
"%d",
&
l)
&&
l)
{
memset(
dp,
inf,
sizeof(
dp));
int
n;
scanf(
"%d",
&
n);
a[
0]
=
0;
for(
int
i
=
1;
i
<=
n;
i
++)
scanf(
"%d",
&
a[
i]);
a[
n
+
1]
=
l;
for(
int
i
=
0;
i
<=
n;
i
++)
{
dp[
i][
i
+
1]
=
0;
}
for(
int
r
=
3;
r
<=
n
+
2;
r
++)
//注意这里的长度是n+2
{
for(
int
i
=
0;
i
<=
n
-
r
+
2;
i
++)
//注意这里i的取值范围
{
int
j
=
i
+
r
-
1;
for(
int
k
=
i
+
1;
k
<
j;
k
++)
{
if(
dp[
i][
j]
>
dp[
i][
k]
+
dp[
k][
j])
dp[
i][
j]
=
dp[
i][
k]
+
dp[
k][
j];
}
dp[
i][
j]
+=
a[
j]
-
a[
i];
//printf("%d %d %d\n",i,j,dp[i][j]);
}
}
printf(
"The minimum cutting is %d.\n",
dp[
0][
n
+
1]);
}
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
AC代码2:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const
int
inf
=
0x3f3f3f3f;
//1061109567
typedef
long
long
LL;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using
namespace
std;
int
a[
55],
dp[
55][
55];
int
main()
{
int
l;
while(
scanf(
"%d",
&
l)
&&
l)
{
memset(
dp,
inf,
sizeof(
dp));
int
n;
scanf(
"%d",
&
n);
a[
0]
=
0;
for(
int
i
=
1;
i
<=
n;
i
++)
scanf(
"%d",
&
a[
i]);
a[
n
+
1]
=
l;
for(
int
i
=
0;
i
<=
n;
i
++)
{
dp[
i][
i
+
1]
=
0;
}
for(
int
r
=
3;
r
<=
n
+
2;
r
++)
{
for(
int
i
=
0;
i
<=
n
-
r
+
3;
i
++)
//比上一个多算了一次,其实是没有必要的
{
int
j
=
i
+
r
-
1;
for(
int
k
=
i
+
1;
k
<
j;
k
++)
{
if(
dp[
i][
j]
>
dp[
i][
k]
+
dp[
k][
j])
dp[
i][
j]
=
dp[
i][
k]
+
dp[
k][
j];
}
dp[
i][
j]
+=
a[
j]
-
a[
i];
}
}
printf(
"The minimum cutting is %d.\n",
dp[
0][
n
+
1]);
//注意这里是dp[0][n+1]
}
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
边栏推荐
猜你喜欢
随机推荐
中国企业构建边缘计算解决方案的最佳实践
How to deal with commas in the content of the CSV file of the system operation and maintenance series
【kali-漏洞扫描】(2.1)Nessus解除IP限制、扫描快无结果、插件plugins被删除(中)
JPA Native Query(本地查询)及查询结果转换
376. Wiggle Subsequence
CAS: 773888-45-2_BIOTIN ALKYNE_生物素-炔基
一体化HTAP数据库如此难,为什么他们还要做?
[kali-vulnerability exploitation] (3.2) Metasploit basics (on): basic knowledge
488. Zuma Game
C. Array Elimination-- Codeforces Round #751 (Div. 2)
dataframe multi-level index replace index df.swaplevel(axis=1)
AI首席架构师13-AICA-智能文档分析技术在行业场景中的应用
template string
Pay from 0 to 1
IDaaS 是什么?一文说清它的价值
Makefile
关于GPIO你真的懂了吗?这篇文章都给你整理好了
LitJson报错记录
Security Fundamentals 8 --- XSS
mysql如何将表结构导出到excel









