Three displays CodeForces - 987C (dp)
1 second
256 megabytes
standard input
standard output
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.
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.
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.
5
2 4 5 4 10
40 30 20 10 40
90
3
100 101 100
2 4 5
-1
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
33
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.
题意:拿三样物品,价值要和顺序都递增,cost总和最少
题解:dp,改一下板子就可以了
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=; int main()
{
int n;
int ans=INF;
int val[maxn],cost[maxn],f[maxn][];
cin>>n;
for(int i=;i<n;i++)
cin>>val[i];
for(int i=;i<n;i++)
cin>>cost[i];
memset(f, INF, sizeof(f));
for (int i = ; i < n; i++)
{
f[i][] = cost[i];
for (int j = ; j < i; j++)
if (val[j] < val[i])
f[i][] =min(f[i][], f[j][] + cost[i]);
for (int j = ; j < i; j++)
if (val[j] < val[i])
f[i][] =min(f[i][], f[j][] + cost[i]); }
for(int i=;i<n;i++)
ans=min(ans,f[i][]);
if(ans!=INF)
cout<<ans<<endl;
else
cout<<-<<endl; }
Three displays CodeForces - 987C (dp)的更多相关文章
- Codeforces 1142D(dp)
题目传送 先给出设计dp的结论: dp[i][j]:以第i个位置.以rankj的数拓展出去的方案数.意会一下,我实在想不好语言-- 其中所谓rankj=真·rank%11 找到拓展的规律,转移也就顺理 ...
- Codeforces 1131G(dp)
传送门 与Codeforces1107G一起食用 思路 想到要用dp--然后常规地设dp[i]为推倒前i个牌的最小花费 有两种情况:一是当前这个推,二是不推而被别人推.对于第一种,需要找到这个左推(因 ...
- Codeforces 1107F(dp)
怎么就没人解释一下为啥用b排序可以保证正确性呢……太菜了,理解了好久. 时间流逝价值会丢失的背包,类似题洛谷1417 本题与洛谷1417不同之处在于流逝是有截止的. 1.这个dp[j]的含义是:最后跑 ...
- Codeforces 1107G(dp)
1.答案要取连续的区间疯狂暗示线段树. 2.外层枚举r,内层枚举l显然过于暴力. 3.考虑内层的优化.dp[i]:以第i位为结尾的答案(长度大于1的).dp[i] = max(第一种情况,第二种情况) ...
- codeforces 682D(DP)
题目链接:http://codeforces.com/contest/682/problem/D 思路:dp[i][j][l][0]表示a串前i和b串前j利用a[i] == b[j]所得到的最长子序列 ...
- codeforces 666A (DP)
题目链接:http://codeforces.com/problemset/problem/666/A 思路:dp[i][0]表示第a[i-1]~a[i]组成的字符串是否可行,dp[i][1]表示第a ...
- Codeforces 1144G(dp)
据说这题是种dp的套路?然后被我国红名神仙(南大Roundgod)贪心了,不过思路上非常相近了,故而可贪吧. 设的dp[i][0]是:如果把第i个数放在上升序列里了,那么下降序列结尾的那个最大是多少: ...
- Codeforces 1152D(dp)
要点 寻找最多边的匹配的结论:贪心地从叶子开始找,最后答案都是奇数层下边的那条边. 设\(dp[i][j]\)表示当前长度为\(i\),平衡度为\(j\),平衡度为(数量减去)数量. 增加左右括号转移 ...
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
随机推荐
- RTX51 Tiny
参考文档 :RTX51 Tiny 2.02 中文手册.doc.Keil_Rtx51_tiny_RTOS中文版.pdf RTX-51 有 2 个版本:Full 和 Tiny.类似的国人写的 Small ...
- LinkedList源码及原理
简介 内部结构分析 LinkedList源码分析 构造方法 添加(add)方法 根据位置取数据的方法 根据对象得到索引的方法 检查链表是否包含某对象的方法: 删除(remove/pop)方法 Link ...
- Git操作(及操作github)
一.git操作: 1.创建一个项目: 2.打开项目,在项目目录下右键单击--打开 git bash: 3.初始化项目仓库 创建项目仓库 命令: git init ; git status 查看文 ...
- Swing编程概述
Swing作为AWT组件的“强化版”,它的产生主要是为了克服AWT构建的GUI,无法在所有平台都通用的问题.允许编程人员跨平台时指定统一的GUI显示风格也是Swing的最大优势.Swing是AWT的补 ...
- python生成器简单代码了理解。
__author__ = 'ZHHT' #!/usr/bin/env python # -*- coding:utf-8 -*- #返回当前执行到的函数的返回值.并保持当前执行的状态.这时候先执行别的 ...
- 20170405JDBC数据查询
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...
- PHP的加解密:如何安装ioncube扩展?
一.下载loader-wizard.php(支持php5.3.php5.4.php5.5.php5.6版本) ioncube提供了一个安装的向导程序,可以非常方便的帮助检测php的运行环境,自动给出提 ...
- 编写sql查询语句思路
编写查询语句思路/* 1.首先确定最终输出结果的列,包括几个方面:A.首先这些列来自于一个 表.还是多个表,如果是多个表则可能用到多表查询的(等值连接.不等值 连接.外连接.自连接):B.这些列是直接 ...
- python+selenium之中类/函数/模块的简单介绍和方法调用
# coding=utf-8 class ClassA (object): string1 = "这是一个字符串." def instancefunc(self): print ( ...
- WinForm form属性
一.布局 Autostroll 控件内容大于可见区域是否自动显示滚动条 Maximumsize 窗体可调到最大尺寸 minimumsize 窗体可调到最小尺寸 size 窗体看到的尺寸 Sta ...