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 ...
随机推荐
- MySQL操作数据库和表的基本语句(DDL
1.创建数据库: CREATE DATABASE 数据库名; eg.CREATE DATABASE test_ddl;122.创建表 CREATE TABLE 表名(列名 数据类型 约束,...); ...
- ceph对接openstack环境
环境准备: 保证openstack节点的hosts文件里有ceph集群的各个主机名,也要保证ceph集群节点有openstack节点的各个主机名 一.使用rbd方式提供存储如下数据: (1)image ...
- 【LeetCode】3 、Longest Substring Without Repeating Characters
题目等级:Medium 题目描述: Given a string, find the length of the longest substring without repeating chara ...
- Linux系统学习之 一:新手必须掌握的Linux命令1
2018-10-03 16:04:12 一.常用系统工作命令 1.wget 命令 作用:用于在终端中下载网络文件. 格式:wget [参数] 下载地址 参数及作用: -b : 后台下载模式 -d:显示 ...
- RESTful API - 介绍
目录 RESTful API 介绍 RESTful介绍 RESTful API设计指南 API与用户的通信协议 域名 版本(Versioning) 路径(Endpoint) 浏览器请求方式(metho ...
- PL/SQL Challenge 每日一题:2014-3-14 11gR2中带RELIES_ON子句的RESULT_CACHE函数
PL/SQL Challenge 每日一题:2014-3-14 11gR2中带RELIES_ON子句的RESULT_CACHE函数 最先答对且答案未经编辑的puber将获得纪念章一枚(答案不可编辑但可 ...
- C# WPF 无窗体传递消息
WPF如果存在窗体(或至少,在任务栏有图标显示),互相传递消息是很容易的. 寻找目标窗体句柄->WindowsAPI SendMessage/PostMessage->目标窗体AddHoo ...
- 1414 冰雕 51nod 暴力
1414 冰雕 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 白兰大学正在准备庆祝成立256周年.特别任命副校长 ...
- 子序列 NYOJ (尺取法+队列+hash) (尺取法+离散化)
子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 给定一个序列,请你求出该序列的一个连续的子序列,使原串中出现的所有元素皆在该子序列中出现过至少1次. 如2 8 ...
- [转]SQLSERVER一些公用DLL的作用解释
转自:Leo_wlCnBlogs SQLSERVER一些公用DLL的作用解释 如果你的SQLSERVER安装在C盘的话,下面的路径就是相应SQLSERVER版本的公用DLL的存放路径 SQL2005 ...