当前位置:网站首页>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.
边栏推荐
- Pay from 0 to 1
- CAS:122567-66-2_DSPE-生物素_DSPE-Biotin
- CAS:153162-70-0_N-BOC-6-Biotinamidohexylamine
- 数据一致性:双删为什么要延时?
- StoneDB 助力 2022 开放原子全球开源峰会
- YOLO之父宣布退出CV界,坦言无法忽视自己工作带来的负面影响
- 2022年全国职业院校技能大赛网络安全 B模块 任务十windows操作系统渗透测试 国赛原题
- LyScript 实现应用层钩子扫描器
- Soft exam system analysts note experience sharing: theory of protracted war
- CAS: 773888-45-2_BIOTIN ALKYNE_Biotin-alkynyl
猜你喜欢
随机推荐
互联网用户账号信息管理规定今起施行:必须严打账号买卖灰产
CAS:908007-17-0_Biotin-azide _生物素叠氮化物
CAS:1192802-98-4_UV 裂解的生物素-PEG2-叠氮
【云原生实用技巧】使用 skopeo 批量同步 helm chart 依赖镜像
好朋友离职了,一周面试了20多场,我直呼内行
软件测试人员必备的60个测试工具清单,建议收藏一波~
IO线程进程->线程同步互斥机制->day6
YOLO之父宣布退出CV界,坦言无法忽视自己工作带来的负面影响
template string
嵌入式开发:嵌入式基础——代码和数据空间揭秘
七夕快乐!
buildscript和allprojects的作用和区别是什么?
中国企业构建边缘计算解决方案的最佳实践
嵌入式系统:时钟
21天打卡挑战学习MySQL——《Window下安装MySql》第一周 第三篇
Data_web(八)mysql增量同步到mongodb
趣链的产品构架
Kubernetes入门到精通-Operator 模式
C. Array Elimination-- Codeforces Round #751 (Div. 2)
StoneDB 开源社区月刊 | 202207期