【CodeForces - 987C 】Three displays (dp,最长上升子序列类问题,三元组问题)

题干:

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 sisi 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 si<sj<sksi<sj<sk should be held.

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

Input

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

The second line contains nn integers s1,s2,…,sns1,s2,…,sn (1≤si≤1091≤si≤109) — the font sizes on the displays in the order they stand along the road.

The third line contains nn integers c1,c2,…,cnc1,c2,…,cn (1≤ci≤1081≤ci≤108) — the rent costs for each display.

Output

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 si<sj<sksi<sj<sk.

Examples

Input

5
2 4 5 4 10
40 30 20 10 40

Output

90

Input

3
100 101 100
2 4 5

Output

-1

Input

10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13

Output

33

Note

In the first example you can, for example, choose displays 11, 44 and 55, because s1<s4<s5s1<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.

题目大意:

解题报告:

   三元组问题。

   类似最长上升子序列的做法,但是维护的不是长度,而是的最小值。

AC代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX = 3000 + 5;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll a[MAX];
ll b[MAX];
ll dp[MAX];
int main()
{int n;ll ans = INF;cin>>n;for(int i = 1; i<=n; i++) {scanf("%lld",&a[i]);}for(int i = 1; i<=n; i++) {scanf("%lld",&b[i]);}memset(dp,INF, sizeof(dp));for(int i =2; i<=n-1; i++) {//其实不用到n,因为还要留一个给第三个display,所以这里递归到n-1.for(int j = 1; j<i; j++) {if(a[i] > a[j])dp[i] = min(dp[i], b[i] + b[j]);}}
//    printf("%lld\n",dp[3]);for(int i = 1; i<=n-1; i++) {for(int j = i+1; j<=n; j++) {if(a[j] > a[i])ans = min(ans ,dp[i]+b[j]);
//            printf("%lld %lld ==== %d %d\n",dp[i],b[j] ,i,j);}}if(ans == INF) printf("-1\n");else cout << ans<<endl;return 0 ;
}

或者直接dp解:

详见博客https://blog.csdn.net/xs18952904/article/details/80504296

 

20191003

忽然发现今天随便刷codeforce的C题又刷到这道题了,特此更新:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
ll dp1[MAX],dp2[MAX],a[MAX],c[MAX];
int n;
int main()
{cin>>n;for(int i = 1; i<=n; i++) scanf("%lld",a+i),dp1[i]=1e18;for(int i = 1; i<=n; i++) scanf("%lld",c+i),dp2[i]=1e18;for(int i = 1; i<=n; i++) {for(int j = 1; j<i; j++) {if(a[i] > a[j]) dp1[i] = min(dp1[i],c[j]+c[i]);}}for(int i = 1; i<=n; i++) {for(int j = 1; j<i; j++) {if(a[i] > a[j]) dp2[i] = min(dp2[i],dp1[j]+c[i]);}}ll ans = *min_element(dp2+1,dp2+n+1);if(ans == (ll)1e18) ans = -1;printf("%lld\n",ans);return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441433.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

git object 很大_这才是真正的Git——Git内部原理

本文以一个具体例子结合动图介绍了Git的内部原理&#xff0c;包括Git是什么储存我们的代码和变更历史的、更改一个文件时&#xff0c;Git内部是怎么变化的、Git这样实现的好处等等。TL;DR本文以一个具体例子结合动图介绍了Git的内部原理&#xff0c;包括Git是什么储存我们的代码…

【CodeForces - 195D】Analyzing Polyline (思维,卡精度的处理方式)

题干&#xff1a; As Valeric and Valerko were watching one of the last Euro Championship games in a sports bar, they broke a mug. Of course, the guys paid for it but the barman said that he will let them watch football in his bar only if they help his son …

【CodeForces - 985D】Sand Fortress (二分,贪心,思维构造,技巧,有坑)

题干&#xff1a; You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbe…

scrapy 分布式 mysql_Scrapy基于scrapy_redis实现分布式爬虫部署的示例

准备工作1.安装scrapy_redis包,打开cmd工具,执行命令pip install scrapy_redis2.准备好一个没有BUG,没有报错的爬虫项目3.准备好redis主服务器还有跟程序相关的mysql数据库前提mysql数据库要打开允许远程连接,因为mysql安装后root用户默认只允许本地连接,详情请看此文章部署过程…

(精)DEVC++的几个实用小技巧

依赖 DEV C 5.11 最新版 下载安装DEV C后&#xff0c;使用DEV C打开一个随便的cpp文件&#xff0c;你看到的应该是这样的界面。&#xff08;为了节约读者的流量&#xff0c;图片进行了有损压缩&#xff0c;但是字看得清楚&#xff09; 重点是确认工具栏有AStyle选项。 相信…

win10一按右键就闪屏_升级Win10正式版后屏幕一直闪烁正确的解决办法

Win10正式版屏幕一直闪烁怎么办呢&#xff1f;升级到Win10正式版并进入Windows桌面后&#xff0c;发现屏幕一直不断的闪烁&#xff0c;此时无法执行任务操作。小编最近在升级到Win10正式版后才遇到了这个问题&#xff0c;后台经过反复思考和探索&#xff0c;终于解决了问题&…

*【CodeForces - 195B】After Training (多解,模拟)

题干&#xff1a; After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from l…

pandas打印全部列_python——pandas练习题1-5

练习1-开始了解你的数据探索Chipotle快餐数据相应数据集&#xff1a;chipotle.tsvimport pandas as pd chipopd.read_csv("exercise_data/chipotle.tsv",sept) chipo.head(5)chipo.shape[0] #查看有多少行4622chipo.shape[1] #查看有多少列5chipo.columns #打印所…

【CodeForces - 689B】Mike and Shortcuts(Dijkstra最短路,或者bfs跑状态类似spfa)

题干&#xff1a; Recently, Mike was very busy with studying for exams and contests. Now he is going to chill a bit by doing some sight seeing in the city. City consists of n intersections numbered from 1 to n. Mike starts walking from his house located a…

java utf-8 gbk_Java 字符转码之UTF-8转为GBK/GB2312

java跟python类似的做法&#xff0c;在java中字符串的编码是java修改过的一种Unicode编码&#xff0c;所以看到java中的字符串&#xff0c;心理要默念这个东西是java修改过的一种Unicode编码的编码。packagestring;importjava.nio.charset.Charset;public classUTF82GBK {publi…

java好的博客_推荐5个万博爆款Java开源博客,是我目前用过最好用的博客系统

1.OneBlog一个简洁美观、功能强大并且自适应的Java博客&#xff0c;使用springboot开发&#xff0c;前端使用Bootstrap&#xff0c;支持移动端自适应&#xff0c;配有完备的前台和后台管理功能。功能简介多种编辑器、自动申请友情链接、百度推送、评论系统、权限管理、SEO、实时…

中介者模式java_图解Java设计模式之中介者模式

智能家庭项目1)智能家庭包括各种设备&#xff0c;闹钟、咖啡机、电视机、窗帘等2)主人要看电视时&#xff0c;各个设备可以协同工作&#xff0c;自动完成看电视的准备工作&#xff0c;比如流程为 &#xff1a;闹铃响起 - 》咖啡机开始做咖啡 -》窗帘自动落下 -》电视机开始播放…

【POJ - 2398】Toy Storage (计算几何,二分找位置,叉积,点和直线的位置关系)

题干&#xff1a; Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing with them. They gave Reza a rectangular box to put his toys in. Unfortunately, Reza is rebellious and obeys his parents by simply throwing …

java servlet 转发和重定向_JavaWeb(一)Servlet中乱码解决与转发和重定向的区别

前言前面其实已经把Servlet中所有的内容都介绍完了&#xff0c;这篇讲补充一点乱码和重定向与转发之间的区别&#xff01;一、request请求参数出现乱码问题1.1、get请求1)乱码示例get请求的参数是在url后面提交过来的&#xff0c;也就是在请求行中。结果&#xff1a;Servlet_de…

java jsp导出pdf文件_JSP页面导出PDF格式文件

JSP页面导出PDF格式文件基本在前端页面可以全部完成添加下载链接的点击事件var downPdf document.getElementById("downLoad");downPdf.onclick function() {downPdf.parentNode.removeChild(downPdf);html2canvas(document.body, {onrendered:function(canvas) {v…

java unsafe park_Java中Unsafe类详解

http://www.cnblogs.com/mickole/articles/3757278.htmlJava不能直接访问操作系统底层&#xff0c;而是通过本地方法来访问。Unsafe类提供了硬件级别的原子操作&#xff0c;主要提供了以下功能&#xff1a;1、通过Unsafe类可以分配内存&#xff0c;可以释放内存&#xff1b;类中…

【qduoj - 夏季学期创新题】骑士游历(递推dp)

题干&#xff1a; 描述 输入 输入包含多组数据&#xff0c;第一行T表示数据组数接下来每行六个整数n&#xff0c;m&#xff0c;x1&#xff0c;y1&#xff0c;x2&#xff0c;y2(分别表示n&#xff0c;m&#xff0c;起点坐标&#xff0c;终点坐标) 输出 输出T行&#xff0c;表示…

java ee 6 源码_Java EE 6开发手册·高级篇(第4版)

资源名称&#xff1a;Java EE 6开发手册高级篇(第4版)内容简介&#xff1a;《Java EE 6 开发手册?高级篇(第4 版)》是一本面向实战、以示例为驱动、在Java 平台企业版6(Java EE 6)上开发企业级应用的指南。该指南基于The Java EE 6 Tutorial: Basic Concepts&#xff0c;Fourt…

【qduoj - 夏季学期创新题】矩形剖分(递归,dp)

题干&#xff1a; 描述 对一个给定的矩形&#xff0c;将其划分成尽可能少的正方形&#xff0c;输出正方形的最少个数。例如&#xff0c;如下图所示的情况&#xff0c;则输入为3和4&#xff0c;输出为4。 输入 输入两个整数中间用空格分开。 输出 输出最少分割成的正方形的个…

学分绩点计算编程java_方便我们计算学分绩点的JavaScript

基于目前我们学校教务处的管理系统, 依靠Javascript的帮忙, 我们可以很方便地计算成绩.测试用HTML:style"width: 100%; border-collapse: collapse;">课程代码课程名称课程性质成绩补考成绩重修成绩学分绩点辅修标记24109505数据库系统实验专业基础课优秀0.54.502…