当前位置:网站首页>C. Three displays(动态规划)Codeforces Round #485 (Div. 2)
C. Three displays(动态规划)Codeforces Round #485 (Div. 2)
2022-06-24 15:35:00 【51CTO】
原题链接: https://codeforces.com/problemset/problem/987/C

测试样例:
样例输入1
5
2 4 5 4 10
40 30 20 10 40
样例输出1
90
样例输入2
3
100 101 100
2 4 5
样例输出2
-1
样例输入3
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
样例输出3
33
提示:
不要暴力哦。
解题思路: 学长都提醒不要暴力了,还是暴力尝试了两发。赛后发现简直了,dp解决就好了。好了,来说一下思路,这道题我们是要求解能让的最小值
,其中
,那么我们可以先探讨其中一部分也就是
,这里我们就要用到dp来解决了,用
表示满足
的
的最小值,我们不用在意
的值,我们只要求得最小值,同时
满足
就行。那么我们求出这个最小值之后我们不就可以再求一个最小值吗?我们将这个比较式拆成两个,那么我们原来的这三层暴力for循环经过这样拆成了两个,OK,我们具体看代码。
AC代码:
/*
*
*/
//POJ不支持
//i为循环变量,a为初始值,n为界限值,递增
//i为循环变量, a为初始值,n为界限值,递减。
using
namespace
std;
const
int
inf
=
0x3f3f3f3f;
//无穷大
const
int
maxn
=
1e5;
//最大值。
typedef
long
long
ll;
typedef
long
double
ld;
typedef
pair
<
ll,
ll
>
pll;
typedef
pair
<
int,
int
>
pii;
//*******************************分割线,以上为自定义代码模板***************************************//
int
n;
int
a[
maxn],
b[
maxn],
dp[
maxn];
//dp[i]表示的是a[i]>a[j]使得b[i]+b[j]最小的值,其中1<=j<i。
int
main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
while(
cin
>>
n){
rep(
i,
1,
n)
cin
>>
a[
i];
rep(
i,
1,
n)
cin
>>
b[
i];
memset(
dp,
inf,
sizeof(
dp));
rep(
i,
2,
n){
rep(
j,
1,
i
-
1){
if(
a[
i]
>
a[
j]){
dp[
i]
=
min(
dp[
i],
b[
i]
+
b[
j]);
}
}
}
//我们再获取b[i]+dp[j]的最小值,这即是我们想要的答案。
int
ans
=
inf;
rep(
i,
2,
n){
rep(
j,
1,
i
-
1){
if(
a[
i]
>
a[
j]){
ans
=
min(
ans,
b[
i]
+
dp[
j]);
}
}
}
if(
ans
==
inf)
cout
<<-
1
<<
endl;
else
cout
<<
ans
<<
endl;
}
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.
- 52.
- 53.
- 54.
- 55.
- 56.
边栏推荐
- Siggraph 2022 | truly restore the hand muscles. This time, the digital human hands have bones, muscles and skin
- Two problems of qtreewidget returning as DLL in singleton mode
- Implement Domain Driven Design - use ABP framework - domain logic & application logic
- 如何在Thymeleaf3标签中使用嵌套标签
- 2021-04-24: handwriting Code: topology sorting.
- Istio FAQ: region awareness does not take effect
- Build go command line program tool chain
- Paper: Google TPU
- Mysql之Binlog
- Three solutions for Jenkins image failing to update plug-in Center
猜你喜欢

Intelij 中的 Database Tools可以连接但是无法显示SCHEMA, TABLES

高速公路服务区智能一体机解决方案

nifi从入门到实战(保姆级教程)——环境篇

The equipment is connected to the easycvr platform through the national standard gb28181. How to solve the problem of disconnection?

Build go command line program tool chain

How to easily realize online karaoke room and sing "mountain sea" with Wang Xinling

Why is it easy for enterprises to fail in implementing WMS warehouse management system
![clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]](/img/f0/42f394dbc989d381387c7b953d2a39.jpg)
clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]

Using alicloud RDS for SQL Server Performance insight to optimize database load - first understanding of performance insight

熬夜整理出的软件测试【高频】面试题大全(2022最新)
随机推荐
Cap: multiple attention mechanism, interesting fine-grained classification scheme | AAAI 2021
asciinema 搭配 asciicast2gif 实现高效的命令行终端录制能力
打破内存墙的新利器成行业“热搜”!持久内存让打工人也能玩转海量数据+高维模型
MySQL development specification
Mysql之Binlog
Hardware security threats of cloud infrastructure
[application recommendation] the hands-on experience and model selection suggestions of apifox & apipost in the recent fire
Efficient tools commonly used by individuals
[log service CLS] Tencent cloud log4j/logback log collection best practices
Install the imagemagick7.1 library and the imageick extension for PHP
[cloud native | kubernetes chapter] Introduction to kubernetes Foundation (III)
Wechat official account debugging and natapp environment building
Poor remote code execution in Alien Swarm
Parameterized tests guide in junit5
60 个神级 VS Code 插件!!
From practical teaching to competition exercise, Tencent experts personally teach Ti-One platform operation strategy!
"Industry foresight" future development trend of intelligent security monitoring industry
D. Solve The Maze(思维+bfs)Codeforces Round #648 (Div. 2)
Improving the classification of motor imagery by combining EEG and MEG signals in BCI
How to implement SQLSERVER database migration in container