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= ...
随机推荐
- CoreCLR
CoreCLR 在这篇中我将讲述GC Collector内部的实现, 这是CoreCLR中除了JIT以外最复杂部分,下面一些概念目前尚未有公开的文档和书籍讲到. 为了分析这部分我花了一个多月的时间,期 ...
- java课后思考问题(七)
1.继承条件下的构造方法调用 package parent;class Grandparent { public Grandparent() { System.out.println("Gr ...
- PHP正则表达式 - 附录(常用正则表达式)
常用正则表达式附录I 平时做网站经常要用正则表达式,下面是一些讲解和例子,仅供大家参考和修改使用: "^\d+$" //非负整数(正整数 + 0) "^[0-9]*[1- ...
- C#基础之运行环境
这是我的第一篇博文,目前这一阶段我的目标是先把C#掌握好,C#是一门语言,是基于C风格语言(C.C++和Java)的特性而设计的.所以在我的博客里,我会记录下我的学习笔记,这不仅仅是笔记,还是所学过的 ...
- bjut193E 吃饭时的怪癖
题目: http://bjutacm.openjudge.cn/lianxi/193E/ 思路: n的所有质因数之和等于phi(n) * n / 2, phi(n)为欧拉函数. 实现: #includ ...
- H5的storage(sessionstorage&localStorage)简单存储删除
众所周知,H5的storage有sessionstorage&localStorage,其中他们的共同特点是API相同 下面直接上代码,storage中的存储与删除: <!DOCTYPE ...
- java 通过文件后缀名查找文件
最近开发项目的时候需要过滤出一些指定的文件,所以有了以下的一些代码: /** **该类主要是过滤得到指定后缀名的文件 **/ public class DataFileFilter implement ...
- selenium+python之python多线程
程序.进程及线程的区别 计算机程序是磁盘中可执行的二进制数据(或者其他类型)他们只有在被读取到内存中,被操作系统调用才开始他们的生命周期. 进程是程序的一次执行,每个进程都有自己的地址空间,内存,数据 ...
- 【Python图像特征的音乐序列生成】解析ABC格式的文件(修改版)
ABC格式,是一个音乐标准,ABC Plus Project最新的标准是2.x. ABC格式的音乐长成这样: X: T:Abacus % Nottingham Music Database S:By ...
- MySql5.7主从配置
记录 环境:ubuntu16.04,mysql5.7 主机:192.168.1.240,192.168.1.241:241为Salve 1.安装mysql sudo apt-get install m ...