CF思维联系– Codeforces-987C - Three displays ( 动态规划)
ACM思维题训练集合
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≤109) — 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≤108) — 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
一看到这题,我觉得像是个背包,实际上差不多,只不过就是有了限制条件,后选的序号一定大于之前的序号,且给定的S[i]也需要大于之前选的。然后这个题我觉得数据有点水n2n^2n2的复杂度竟然能这么快。

#include <bits/stdc++.h>
using namespace std;
template <typename t>
void read(t &x)
{
char ch = getchar();
x = 0;
int f = 1;
while (ch < '0' || ch > '9')
f = (ch == '-' ? -1 : f), ch = getchar();
while (ch >= '0' && ch <= '9')
x = x * 10 + ch - '0', ch = getchar();
x *f;
}
#define wi(n) printf("%d ", n)
#define wl(n) printf("%lld ", n)
#define rep(m, n, i) for (int i = m; i < n; ++i)
#define P puts(" ")
typedef long long ll;
#define MOD 1000000007
#define mp(a, b) make_pair(a, b)
//---------------https://lunatic.blog.csdn.net/-------------------//
const int N = 3005;
const int INF = 0x3f3f3f3f;
int s[N], cost[N], maxi[N];
int dp[N][10], weight[N][10];
int main()
{
int n;
read(n);
rep(1, n + 1, i)
{
read(s[i]);
}
rep(1, n + 1, i)
{
read(cost[i]);
}
memset(dp, 0x3f, sizeof(dp));
//rep(0, n + 1, i) weight[i] =s[i];
rep(1, n, i)
{
dp[i][1]=cost[i];
weight[i][1]=s[i];
for (int j =2; j <= 3; j++)
{
for (int k = i+1; k <= n; k++)
if (s[k] > weight[i][j-1])
{
//cout<<1;
if (dp[k][j] > dp[i][j - 1] + cost[k])
{
//cout<<2;
dp[k][j] = dp[i][j - 1] + cost[k];
weight[k][j] = s[k];
}
}
}
}
int ans = INF;
rep(1, n + 1, i)
ans = min(ans, dp[i][3]);
if (ans == INF)
puts("-1");
else
{
wi(ans);
P;
}
}
CF思维联系– Codeforces-987C - Three displays ( 动态规划)的更多相关文章
- CF思维联系–CodeForces - 225C. Barcode(二路动态规划)
ACM思维题训练集合 Desciption You've got an n × m pixel picture. Each pixel can be white or black. Your task ...
- CF思维联系--CodeForces - 218C E - Ice Skating (并查集)
题目地址:24道CF的DIv2 CD题有兴趣可以做一下. ACM思维题训练集合 Bajtek is learning to skate on ice. He's a beginner, so his ...
- CF思维联系– CodeForces - 991C Candies(二分)
ACM思维题训练集合 After passing a test, Vasya got himself a box of n candies. He decided to eat an equal am ...
- CF思维联系–CodeForces -224C - Bracket Sequence
ACM思维题训练集合 A bracket sequence is a string, containing only characters "(", ")", ...
- CF思维联系–CodeForces - 223 C Partial Sums(组合数学的先线性递推)
ACM思维题训练集合 You've got an array a, consisting of n integers. The array elements are indexed from 1 to ...
- CF思维联系–CodeForces - 222 C Reducing Fractions(数学+有技巧的枚举)
ACM思维题训练集合 To confuse the opponents, the Galactic Empire represents fractions in an unusual format. ...
- CF思维联系--CodeForces -214C (拓扑排序+思维+贪心)
ACM思维题训练集合 Furik and Rubik love playing computer games. Furik has recently found a new game that gre ...
- Codeforces 987C. Three displays(o(n^2))
刚开始三重循环tle test11.后来想了个双重循环的方法. 解题思路: 1.双重循环一次,用一个一位数组存j和比j小的i的和的最小值. 2.再双重循环一次,找到比j大的数k,更新结果为ans=mi ...
- CF思维联系– CodeForces -CodeForces - 992C Nastya and a Wardrobe(欧拉降幂+快速幂)
Nastya received a gift on New Year - a magic wardrobe. It is magic because in the end of each month ...
随机推荐
- ssh配置文件最佳实践(伪)
时间:2019-09-11 说明:以下配置是基于常用安全设置,并增加阿里云要求的安全参数而成 版本:第一版 # 1.监听相关 ## 指定ssh端口 Port 1314 ## 只监听网络协议 Addre ...
- 如何增加Tomcat内存
有时候在开发中,常常遇到内存溢出问题,很有可能项目过大,导致Tomcat内存不足问题,那么此时就要给Tomcat添加内存了,如,操作如下 1. 先找到Tomcat目录下的catalina.bat文件
- Github 骚操作
GitHub 竟然有这些骚操作,真是涨姿势 GitHub,不用过多介绍.一个面向开源及私有软件项目的托管平台,因为只支持 git 作为唯一的版本库格式进行托管,故名 GitHub. 作为「全球最大的程 ...
- CH5E07 划分大理石(背包dp+二进制拆分)
传送门 大意: 有价值分别为1..6的大理石各a[1..6]块,现要将它们分成两部分,使得两部分价值之和相等,问是否可以实现.其中大理石的总数不超过20000. 解题思路: 妥妥的多重背包+二 ...
- coding++:都说新的Arraylist 扩容是(1.5倍+1) 看了1.8的源代码发现不是这么回事
都说新的Arraylist 扩容是(1.5倍+1) 看了1.8的源代码发现不是这么回事 就用下面这段代码在jdk的三个版本运行看了下效果: import java.lang.reflect.Fiel ...
- Maybatis的一些总结(三:增删改查)
回顾一个点 之前不懂这句: UserMapper userMapper = sqlSession.getMapper(UserMapper.class); 现在理解了一点点,相当于实现了userMap ...
- git中常用命令的总结
一.git stash 1.git stash 保存当前工作进度,会把暂存区和工作区的改动保存起来.执行完这个命令后,在运行git status命令,就会发现当前是一个干净的工作区,没有任何改动. ...
- 数据结构和算法(Golang实现)(8.1)基础知识-前言
基础知识 学习数据结构和算法.我们要知道一些基础的知识. 一.什么是算法 算法(英文algorithm)这个词在中文里面博大精深,表示算账的方法,也可以表示运筹帷幄的计谋等.在计算机科技里,它表示什么 ...
- F - Pearls HDU - 1300
简单dp. 题目大意:有n种珍珠,这n种珍珠有不同的需求量,不同的价格,价格越高,质量越高,在购买每一种珍珠时,都需要在原来的基础上多买10个.也就是说如果需要买x种珍珠,那就要付x+10个的钱.每一 ...
- CVE-2019-0193 Apache solr velocity模块漏洞
Solr简单介绍 Solr是建立在Apache Lucene ™之上的一个流行.快速.开放源代码的企业搜索平台. Solr具有高度的可靠性,可伸缩性和容错能力,可提供分布式索引,复制和负载平衡查询,自 ...