U - Three displays
Problem description
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 n displays placed along a road, and the i-th of them can display a text with font size si only. Maria Stepanovna wants to rent such three displays with indices i<j<k that the font size increases if you move along the road in a particular direction. Namely, the condition si<sj<sk should be held.
The rent cost is for the i-th display is ci. Please determine the smallest cost Maria Stepanovna should pay.
Input
The first line contains a single integer n (3≤n≤3000) — the number of displays.
The second line contains n integers s1,s2,…,sn (1≤si≤10^9) — the font sizes on the displays in the order they stand along the road.
The third line contains n integers c1,c2,…,cn (1≤ci≤10^8) — 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<k such that si<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 1, 4 and 5, because s1<s4<s5(2<4<10), and the rent cost is 40+10+40=90.
In the second example you can't select a valid triple of indices, so the answer is -1.
解题思路:题目的意思就是找出连续递增的三个数si,sj,sk,使得si<sj<sk,并且使得ci+cj+ck的和最小。做法:从二个数开始循环到倒数第二个数,每循环到当前值sj就向两边遍历查找符合条件的si、sk对应的最小ci,ck值,如果找得到即m1、m2都≠INF,就将三个c值相加再和原来的最小值mincost进行比较替换;最后如果mincost还是INF,说明找不到这样符合条件的情况,此时输出"-1"。时间复杂度为是O(n2),暴力即过。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int INF=3e8+;//这里INF要设置成比3e8大一点,因为三个c值相加有可能刚好为3e8
struct NODE{int s,c;}node[];
int main(){
int n,m1,m2,mincost=INF;cin>>n;
for(int i=;i<n;++i)cin>>node[i].s;
for(int i=;i<n;++i)cin>>node[i].c;
for(int i=;i<n-;++i){
m1=m2=INF;
for(int j=i-;j>=;--j)
if(node[j].s<node[i].s&&node[j].c<m1)m1=node[j].c;
for(int j=i+;j<n;++j)
if(node[j].s>node[i].s&&node[j].c<m2)m2=node[j].c;
if(m1!=INF&&m2!=INF)mincost=min(mincost,node[i].c+m1+m2);
}
if(mincost<INF)cout<<mincost<<endl;
else cout<<"-1"<<endl;
return ;
}
U - Three displays的更多相关文章
- Codeforces Round #485 (Div. 2) C. Three displays
Codeforces Round #485 (Div. 2) C. Three displays 题目连接: http://codeforces.com/contest/987/problem/C D ...
- CVE-2018-14424 use-after-free of disposed transient displays 分析报告
漏洞描述 GDM守护进程不能正确的取消导出在D-Bus 接口上已经被销毁的display对象,这造成本地用户可以触发UAF,从而使系统崩溃或造成任意代码执行. 调试环境 gdm版本: 3.14.2(通 ...
- Three displays CodeForces - 987C (dp)
C. Three displays time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- DE2之7-segment displays
以前课题用的是友晶的DE2-70,现在重拾FPGA,选了一款性价比高的DE2.恰逢闲来无事,于是尝试将各个Verilog模块翻译成VHDL,半算回顾以前的知识,半算练习VHDL. Verilog 01 ...
- How Chromium Displays Web Pages: Bottom-to-top overview of how WebKit is embedded in Chromium
How Chromium Displays Web Pages This document describes how web pages are displayed in Chromium from ...
- CF 987C Three displays DP或暴力 第十一题
Three displays time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- CF思维联系– Codeforces-987C - Three displays ( 动态规划)
ACM思维题训练集合 It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in ...
- USB Tethering always displays grey when USB tethering type is Linux(EEM)
USB Tethering always displays grey when USB tethering type is Linux(EEM) 1.Problem DESCRIPTION USB T ...
- imshow() displays a white image for a grey image
Matlab expects images of type double to be in the 0..1 range and images that are uint8 in the 0..255 ...
随机推荐
- [forward]警惕UNIX下的LD_PRELOAD环境变量
From: https://blog.csdn.net/haoel/article/details/1602108 警惕UNIX下的LD_PRELOAD环境变量 前言 也许这个话题并不新鲜,因为LD_ ...
- js 阻止冒泡事件和默认事件
阻止事件冒泡 window.enent ? window.enent.cancelBubble = true : e.stopPropagation() function stopBubble(eve ...
- hdu 5176 The Experience of Love
The Experience of Love Accepts: 11 Submissions: 108 Time Limit: 4000/2000 MS (Java/Others) Memor ...
- PAT 1115 Counting Nodes in a BST
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT 1060. Are They Equal
If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered ...
- 字符串拼接原理 javac 和 javap
一.新建一个 Test1 类 public class Test1 { public static void main(String[] args) { String a = "a" ...
- noip模拟赛 PA
分析:很显然这是一道搜索题,可能是由于我的搜索打的太不美观了,这道题又WA又T......如果对每一个询问都做一次bfs是肯定会T的,注意到前70%的数据范围,N的值都相等,我们可以把给定N的所有情况 ...
- SSM(spring mvc+spring+mybatis)学习路径——1-1、spring入门篇
目录 1-1 Spring入门篇 专题一.IOC 接口及面向接口编程 什么是IOC Spring的Bean配置 Bean的初始化 Spring的常用注入方式 专题二.Bean Bean配置项 Bean ...
- linux系统读写缓存
1. 操作系统缓存 在linux世界里,一切可读写设备都可看作是文件.文件cache设计的好坏直接影响着文件系统和磁盘的性能.最直观的是使用free命令看到的cached列. 这里面的cached列 ...
- MVC.Net5:添加Tinymce的图片上传功能
Tinymce是目前几个主流的Web文本编辑器之一,不过它的图片上传功能是要收费的,而其它几个免费的上传图片的插件支持的都是PHP.那么就只能自己动手写一个了(源代码下载). 准备工作如下:1. ...