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 ...
随机推荐
- ubuntu14.0开机guest账号禁用方法
在终端里进入/usr/share/lightdm/lightdm.conf.d/目录 sudo vim 50-unity-greeter.conf 然后在文件里输入: [SeatDefaults] a ...
- CodeCombat代码全记录(Python学习利器)--Kithgard地牢代码1
Kithgard地牢注意:在调用函数时,要在函数的后面加上括号内容,否则在python中,将不会认为你在调用这个函数内容,而你的英雄将像木头一样站在原地不会执行上左下右的移动!!! hero.move ...
- 洛谷——P1613 跑路
P1613 跑路 题目大意: 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资,小A买了一个十分牛B ...
- Luogu P2970 [USACO09DEC]自私的放牧
https://www.luogu.org/problemnew/show/P2970 P2970 [USACO09DEC]自私的放牧 题目描述 Each of Farmer John's N (1 ...
- 第一节:初识pandas之Series(上)
Series线性的数据结构, 也是一个一维数组. 声明:本人Python小白,以下代码只是个人学习的过程,仅仅记录一下学习的点点滴滴,若有错误,还望指正. (注:该代码均在jupyter notebo ...
- Arduino 串口通讯参考笔记 - Serial 类库及相关函数介绍
声明: 本ID发布的所有文章及随笔均为原创,可随意转载,单转载文章必须注明作者 aiyauto 及包含原文出处地址 http://www.cnblogs.com/aiyauto/p/7071712.h ...
- bupt summer training for 16 #7 ——搜索与DP
https://vjudge.net/contest/174962#overview A.我们发现重点在于x,y只要累加就ok了 在每个x上只有上下两种状态,所以可以记忆化搜索 f[0/1][i]表示 ...
- Codeforces Round #404 (Div. 2)——ABCDE
题目这里 A.map裸题 #include <bits/stdc++.h> using namespace std; map <string, int> p; string s ...
- ActiveMQ学习总结(5)——Java消息服务JMS详解
JMS: Java消息服务(Java Message Service) JMS是用于访问企业消息系统的开发商中立的API.企业消息系统可以协助应用软件通过网络进行消息交互. JMS的编程过程很简单,概 ...
- poj 3621最优比例生成环(01分数规划问题)
/* 和求最小生成树差不多 转载思路:http://www.cnblogs.com/wally/p/3228171.html 思路:之前做过最小比率生成树,也是属于0/1整数划分问题,这次碰到这道最优 ...