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= ...
随机推荐
- 后台任务利器之Hangfire
后台任务利器之Hangfire 一.简述 Hangfire作为一款高人气且容易上手的分布式后台执行服务,支持多种数据库.在.net core的环境中,由Core自带的DI管理着生命周期,免去了在NF4 ...
- scrollHelper
(function ($) { var mouseScroll = function (e) { try { var origEvent = e.originalEvent; origEvent.pr ...
- css3响应式表格
<h1>极客学院相关课程</h1> <table class="responsive"> <thead> <tr> &l ...
- 结合源码看nginx-1.4.0之nginx内存管理详解
目录 0. 摘要 1. nginx内存结构设计 2. nginx内存数据结构 3. nginx内存管理原理 4. 一个简单的内存模型 5. 小结 6. 参考资料 0. 摘要 内存管理,是指软件运行时对 ...
- MUI获取文本框的值
MUI事件绑定注意父节点.子节点(也可以是标签选择器) js部分 html部分
- malloc/free函数
一.malloc 函数原型:void *malloc(unsigned int size); 功 能:在内存的动态存储区中分配一个长度为size的连续空间. 返 回 值:指向所分配的连续 ...
- ztree的CheckBox不显示问题解决办法
问题: 在使用ztree插件时需要设置 zTree 的节点上是否显示 checkbox / radio,但设置后不显示复选框/单选框,如下图所示 设置方法: var setting = { check ...
- CORS的原理及应用
CORS的原理及应用 CORS是跨站资源共享,同样是解决浏览器的同源策略 其本质是设置响应头,使得浏览器允许跨域请求. 第三方网站返回数据的时候在浏览器的响应头中添加允许的域名,允许所有的用* 1 简 ...
- Spring中c3p0连接池的配置 及JdbcTemplate的使用 通过XML配置文件注入各种需要对象的操作 来完成数据库添加Add()方法
通过配置文件XML方法的配置 可以使用非常简练的Service类 UserService类代码如下: package com.swift; public class UserService { pri ...
- cocos2dx 使用XMLHttpRequest时回调status为0的问题
今天使用cocos连接http访问时,使用XMLHttpRequest在pc上反问时正常的返回了status=0,但是在android上去返回status是0,看了一下底层代码, 发现status只有 ...