CF 987C Three displays 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.
题意: 给你n个数,每个数有两个权值(a,b),问取三个数,要求这三个数的a值递增,满足要求的最小三个数的和,没有满足要求的条件输出NO
暴力解法:
遍历中间一个数,然后两个循环分别找比他大的和比他小的,然后记录最小值
dp解法:
dp[i][j],i为第几个选择的数,j是选择的数的位置
这样的话,状态转移方程是:
dp[1][i] 每个位置的数
dp[2][j]这个位置与前面任意位置组合成的递增的两个的数
dp[3][i]这个位置与前面两个位置的组合成递增的三个的数
dp[2][j]=min(dp[2][j],dp[1][i]+dp[1][j]);
dp[3][j]=min(dp[3][j],dp[2][i]+dp[1][j]);
暴力代码:
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 1e6 + ;
const int mod = 1e9 + ;
typedef long long ll;
ll a[maxn], b[maxn];
int main(){
std::ios::sync_with_stdio(false);
ll n;
while( cin >> n ) {
for( ll i = ; i < n; i ++ ) {
cin >> a[i];
}
for( ll i = ; i < n; i ++ ) {
cin >> b[i];
}
ll ans = 1e12;
bool flag = false;
for( ll i = ; i < n; i ++ ) {
ll min1 = 1e12, min2 = 1e12;
for( ll j = i+; j < n; j ++ ) {
if( a[i] < a[j] ) {
min1 = min( min1, b[j] );
}
}
for( ll j = ; j < i; j ++ ) {
if( a[i] > a[j] ) {
min2 = min( min2, b[j] );
}
}
if( min1 != 1e12 && min2 != 1e12 ) {
flag = true;
ans = min( ans, min1 + min2 + b[i] );
}
}
if( flag ) {
cout << ans << endl;
} else {
cout << - << endl;
}
}
return ;
}
dp代码:
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = *1e3 + ;
const int mod = 1e9 + ;
typedef long long ll;
ll a[maxn], b[maxn], dp[maxn][maxn];
int main(){
std::ios::sync_with_stdio(false);
ll n;
while( cin >> n ) {
for( ll i = ; i < n; i ++ ) {
cin >> a[i];
dp[][i] = 1e12, dp[][i] = 1e12;
}
for( ll i = ; i < n; i ++ ) {
cin >> b[i];
}
ll ans = 1e12;
for( ll j = ; j < n; j ++ ) {
for( ll i = ; i < j; i ++ ) {
if( a[i] < a[j] ) {
dp[][j] = min( dp[][j] , b[i] + b[j] );
}
}
}
for( ll j = ; j < n; j ++ ) {
for( ll i = ; i < j; i ++ ) {
if( a[i] < a[j] ) {
dp[][j] = min( dp[][j], dp[][i] + b[j] );
}
}
ans = min( ans, dp[][j] );
}
if( ans != 1e12 ) {
cout << ans << endl;
} else {
cout << - << endl;
}
}
return ;
}
CF 987C Three displays DP或暴力 第十一题的更多相关文章
- CF C. Three displays(DP+思维)
http://codeforces.com/contest/987/problem/C 题意:给你两个n的序列要你根据第一个序列(严格单调递增的方式)在第二个序列里找3个数加起来,输出最小的一个. 思 ...
- CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)
问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...
- 一道看似dp实则暴力的题 Zombie's Treasure Chest
Zombie's Treasure Chest 本题题意:有一个给定容量的大箱子,此箱子只能装蓝宝石和绿宝石,假设蓝绿宝石的数量无限,给定蓝绿宝石的大小和价值,要求是获得最大的价值 题解:本题看似是 ...
- [SHOI2001]化工厂装箱员(dp?暴力:暴力)
118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有3种不同的纯度,A:100%,B:1%,C:0.01%,为了出售方便,必须把不同纯度 ...
- codeforces 797 E. Array Queries【dp,暴力】
题目链接:codeforces 797 E. Array Queries 题意:给你一个长度为n的数组a,和q个询问,每次询问为(p,k),相应的把p转换为p+a[p]+k,直到p > n为 ...
- VIJOS1476 旅行规划(树形Dp + DFS暴力乱搞)
题意: 给出一个树,树上每一条边的边权为 1,求树上所有最长链的点集并. 细节: 可能存在多条最长链!最长链!最长链!重要的事情说三遍 分析: 方法round 1:暴力乱搞Q A Q,边权为正-> ...
- [cf 1015f] Bracket Substring (dp+kmp)
传送门 Solution 设dp方程dp[now][pos][red][fla]表示还有now个位置,pos表示匹配到第几位,red表示左括号数-右括号数,fla表示是否已经是给定串的字串 暴力转移即 ...
- CF 256D. Good Sequences(DP)
题目链接 主要是标记前面素数的最大的DP值,要认真一些.没想到居然写了一个很难发现的错误. #include <cstdio> #include <cstring> #incl ...
- CF 335B - Palindrome 区间DP
335B - Palindrome 题目: 给出一个字符串(均有小写字母组成),如果有长度为100的回文子串,输出该子串.否则输出最长的回文子串. 分析: 虽然输入串的长度比较长,但是如果存在单个字母 ...
随机推荐
- Unity场景和代码合并以及UnityYAMLMerge的使用
1.首先是.gitignore的配置. # Folder config file Desktop.ini # Recycle Bin used on file shares $RECYCLE.BIN/ ...
- Netty学习(十)-Netty文件上传
今天我们来完成一个使用netty进行文件传输的任务.在实际项目中,文件传输通常采用FTP或者HTTP附件的方式.事实上通过TCP Socket+File的方式进行文件传输也有一定的应用场景,尽管不是主 ...
- 消息中间件-activemq实战整合Spring之Topic模式(五)
这一节我们看一下Topic模式下的消息发布是如何处理的. applicationContext-ActiveMQ.xml配置: <?xml version="1.0" enc ...
- 【C/C++】随机数的生成
C/C++:rand()函数 rand()函数的头文件:#include<stdlib.h> 该函数产生的随机数随机性差,速度慢,周期小(0-32767) 用法如下所示: #include ...
- 单纯的xlistview
public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener{ private ...
- 调用链系列(1):解读UAVStack中的贪吃蛇
一.背景 对于分布式在线服务,一个请求需要经过多个系统中多个模块,可能多达上百台机器的协作才能完成单次请求.这种场景下单靠人力无法掌握整个请求中各个阶段的性能开销,更无法快速的定位系统中性能瓶颈.当发 ...
- 配置Windows Server 2008环境
上一章已经把Windows Server2008操作系统安装完毕,接下来配置一下Windows Server环境.配置网络和共享中心.配置桌面环境.配置用户IE设置.安装Telnet远程工具.配置文件 ...
- Mysql: 图解 inner join、left join、right join、full outer join、union、union all的区别
转载来源 对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚. ...
- 源码编译OpenJdk 8,Netbeans调试Java原子类在JVM中的实现(Ubuntu 16.04)
一.前言 前一阵子比较好奇,想看到底层(虚拟机.汇编)怎么实现的java 并发那块. volatile是在汇编里加了lock前缀,因为volatile可以通过查看JIT编译器的汇编代码来看. 但是原子 ...
- 完美解决迅雷极速版强制升级到迅雷X
虽然迅雷已死,但是还是软件还是有点点用的.废话不好多说,直接上解决办法: 1. 找到桌面的迅雷图标,右键选择打开文件位置; 2. 根据路径找到: 相对路径:Thunder Network\Thunde ...