当前位置:网站首页>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.
边栏推荐
- How to easily realize online karaoke room and sing "mountain sea" with Wang Xinling
- MySQL binlog
- 推荐几款超级实用的数据分析利器
- How to efficiently transfer enterprise business data?
- Paper: Google TPU
- [log service CLS] Tencent cloud log4j/logback log collection best practices
- Remain true to our original aspiration
- The equipment is connected to the easycvr platform through the national standard gb28181. How to solve the problem of disconnection?
- [cloud native | kubernetes chapter] Introduction to kubernetes Foundation (III)
- How to implement SQLSERVER database migration in container
猜你喜欢

Understanding openstack network

Siggraph 2022 | truly restore the hand muscles. This time, the digital human hands have bones, muscles and skin

Several common DoS attacks

Linux record -4.22 MySQL 5.37 installation (supplementary)
![Software test [high frequency] interview questions sorted out by staying up late (latest in 2022)](/img/33/2c2256fd98b908ddaf5573f644ad7f.png)
Software test [high frequency] interview questions sorted out by staying up late (latest in 2022)

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

Mongodb Getting started Practical Tutoriel: Learning Summary Table des matières

Linux记录-4.22 MySQL5.37安装(补充)

【C语言刷题——Leetcode12道题】带你起飞,飞进垃圾堆

一文详解JackSon配置信息
随机推荐
Arrays API
Junit5中的参数化测试(Parameterized Tests)指南
安裝ImageMagick7.1庫以及php的Imagick擴展
New de debugging
The 30 pictures bring the network protocol layer by layer to life. It's really fragrant!
Why is the blackmail virus that shut down half of America's energy system terrible? Interpretation of authoritative reports
2021-04-22: given many line segments, each line segment has two numbers [start, end],
Logging is not as simple as you think
HMM to CRF understanding and learning notes
Database tools in intelij can connect but cannot display schema, tables
Understanding openstack network
I just came back from the Ali software test. I worked for Alibaba P7 in 3+1, with an annual salary of 28*15
Teach you how to view version information with mongodb
Istio FAQ: region awareness does not take effect
Cap: multiple attention mechanism, interesting fine-grained classification scheme | AAAI 2021
Ascinema with asciicast2gif for efficient command line terminal recording
Solution of intelligent all in one machine in expressway service area
leetcode 139. Word break word split (medium)
asciinema 搭配 asciicast2gif 实现高效的命令行终端录制能力
实现领域驱动设计 - 使用ABP框架 - 领域逻辑 & 应用逻辑