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 ...
随机推荐
- Git应用详解第三讲:本地分支的重要操作
前言 前情提要:Git应用详解第二讲:Git删除.修改.撤销操作 分支是git最核心的操作之一,了解分支的基本操作能够大大提高项目开发的效率.这一讲就来介绍一些分支的常见操作及其基本原理. 一.分支概 ...
- MySQL入门,第二部分,必备基础知识点
一.数据类型 日期和时间数据类型 date 字节 日期,格式:2014-09-18 日期和时间数据类型 time 字节 时间,格式:08:42:30 日期和时间数据类型 datetime 字节 日期时 ...
- 奥卡姆剃刀原则在ERP项目的应用
一向崇信“奥卡姆剃刀原则”,如非必要,绝不新增. 在我所实施的项目中,自定义字段.自定义报表非常少.很极端的一个例子是,曾经有一家工厂,生产打印机的部件,产品百分之百外销. 在项目实施完成,成功上线后 ...
- awk线程号
for i in `ps|grep [a]out|awk '{print $1}'` do kill -9 "$i" done
- 【python实现卷积神经网络】上采样层upSampling2D实现
代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...
- 动手学Transformer
动手实现Transformer,所有代码基于tensorflow2.0,配合illustrated-transformer更香. 模型架构 Encoder+Decoder Encoder Decode ...
- Xshell远程连接Linux系统
一般来说我们连接Linux,会使用到一些远程连接工具 比如:Xshell和Xftp Xshell:远程连接linux系统 Xftp:远程在Linux系统中上传或下载文件 Xshell和Xftp百度云链 ...
- How Many Answers Are Wrong HDU - 3038 (经典带权并查集)
题目大意:有一个区间,长度为n,然后跟着m个子区间,每个字区间的格式为x,y,z表示[x,y]的和为z.如果当前区间和与前面的区间和发生冲突,当前区间和会被判错,问:有多少个区间和会被判错. 题解:x ...
- Problem D. Ice Cream Tower
题解:二分加贪心,,,二分答案,然后进行判断,判断的时候,首先给每一组配一个最大的球,然后在向每一组里面填球,注意填球的时候要按组进行,每一组先填一个,然后更新每一组内的最小值,方便下一次寻找. #i ...
- E - Aladdin and the Flying Carpet
It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a ...