CF987C Three displays (#线性dp)

题意翻译

题目大意:

nn个位置,每个位置有两个属性s,cs,c,要求选择3个位置i,j,ki,j,k,使得s_i<s_j<s_ksi​<sj​<sk​,并使得c_i+c_j+c_kci​+cj​+ck​最小

输入格式:

一行一个整数,nn,3<=n<=30003<=n<=3000

一行nn个整数,即ss

再一行nn个整数,即cc

输出格式:

输出一个整数,即最小的c_i+c_j+c_k

感谢@守望 提供翻译

题目描述

It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.

There are nn displays placed along a road, and the ii -th of them can display a text with font size s_isi​ only. Maria Stepanovna wants to rent such three displays with indices i < j < ki<j<k that the font size increases if you move along the road in a particular direction. Namely, the condition s_i < s_j < s_ksi​<sj​<sk​ should be held.

The rent cost is for the ii -th display is c_ici​ . Please determine the smallest cost Maria Stepanovna should pay.

输入输出格式

输入格式:

The first line contains a single integer nn ( 3 \le n \le 3\,0003≤n≤3000 ) — the number of displays.

The second line contains nn integers s_1, s_2, \ldots, s_ns1​,s2​,…,sn​ ( 1 \le s_i \le 10^91≤si​≤109 ) — the font sizes on the displays in the order they stand along the road.

The third line contains nn integers c_1, c_2, \ldots, c_nc1​,c2​,…,cn​ ( 1 \le c_i \le 10^81≤ci​≤108 ) — the rent costs for each display.

输出格式:

If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices i < j < ki<j<k such that s_i < s_j < s_ksi​<sj​<sk​ .

输入输出样例

输入样例#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

说明

In the first example you can, for example, choose displays 1 , 44 and 55 , because s_1 < s_4 < s_5s1​<s4​<s5​ ( 2 < 4 < 102<4<10), and the rent cost is 40 + 10 + 40 = 9040+10+40=90 .

In the second example you can't select a valid triple of indices, so the answer is -1.


思路

给你n个数,每个数有两个权值(a,b),问取三个数,要求这三个数的a值递增,满足要求的最小三个数的和。

这题可以转换成线性dp,类似于最大子序列和。

先求出s[i]后面比s[i]大的c[i]的最小值,然后枚举前两个数c[i],c[j]以及j后面递增并且有最小值的dp[j]。

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#define inf 0x3f3f3f3f+1
using namespace std;
int n,cnt,c[3001],s[3001],dp[3001];
//定义dp[i]代表s[i]后面比s[i]大的对应c[i]的最小值 
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	register int i,j;
	cin>>n;
	cnt=inf;
	for(i=1;i<=n;i++)
	{
		cin>>s[i];
	}
	for(i=1;i<=n;i++)
	{
		cin>>c[i];
	}
	memset(dp,inf,sizeof dp);
	for(i=1;i<=n;i++)
	{
		for(j=i+1;j<=n;j++)//只在后面找 
		{
			if(s[j]>s[i])//如果后面的比前面的大 
			{
				dp[i]=min(dp[i],c[j]);//对应每个c[i]的最小值 
			}
		}
	}
	for(i=1;i<=n;i++)//枚举每个c[i]和c[j] 
	{
		for(j=i+1;j<=n;j++)
		{
			if(s[j]>s[i] && dp[i]!=inf)
			{
				cnt=min(cnt,c[i]+c[j]+dp[j]);//c[i]+c[j]+对应的每个c的最小值 
			}
		}
	}
	if(cnt==inf) cout<<-1<<endl;
	else
	{
		cout<<cnt<<endl;
	}
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值