当前位置:网站首页>4518. 最低票价
4518. 最低票价
2022-07-28 14:15:00 【追寻远方的人】
在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行。
在接下来的一年里,你要旅行的日子将以一个名为 days 的数组给出。
每一项是一个从 1 到 365 的整数。
火车票有三种不同的销售方式:
一张为期一天的通行证售价为 costs[0] 美元;
一张为期七天的通行证售价为 costs[1] 美元;
一张为期三十天的通行证售价为 costs[2] 美元。
通行证允许数天无限制的旅行。
例如,如果我们在第 2 天获得一张为期 7 天的通行证,那么我们可以连着旅行 7 天:第 2 天、第 3 天、第 4 天、第 5 天、第 6 天、第 7 天和第 8 天。
返回你想要完成在给定的列表 days 中列出的每一天的旅行所需要的最低消费。
输入格式
第一行包含一个整数 n,表示 days 数组的长度。
第二行包含 n 个整数,表示 days[i]。
第三行包含 3 个整数,表示 costs[i]。
输出格式
一个整数,表示旅行所需要的最低消费。
数据范围
1≤n≤365,
1≤days[i]≤365,
days 按顺序严格递增,
1≤costs[i]≤1000。
输入样例:
6
1 4 6 7 8 20
2 7 15
输出样例:
11
代码:
/* f [ i ] 表示对于前 i 个天数,所需要的最小花费 我们可以将集合分成三类: (1)当前这一天 days [ i ] 单独买一张一天的票,那答案就是 f [ i ] = f [ i - 1 ] + costs [ 0 ]; (2)找到对于当前 i 最近的且刚好满足天数相差超过 7 天的下标 j,则前面的花费就是f [ j ], 再加上这最近七天内的花费costs [ 1 ], 即 f [ i ] = f [ j ] + costs [ 1 ]; (3)找到对于当前 i 最近的且刚好满足天数相差超过 30 天的下标 k,则前面的花费就是f [ k ], 再加上这最近三十天内的花费costs [ 2 ], 即 f [ i ] = f [ k ] + costs [ 2 ]; 对 2 中的三种情况,我们取一个最小值就是答案 */
#include <bits/stdc++.h>
using namespace std;
const int N = 400;
int n;
int d[N], f[N], c[3];
int find(int r, int limit)
{
int l = r;
while (l && d[r] - d[l] + 1 <= limit)// 往回找到与当前下标最接近的且刚好满足天数相差超过limit的下标, 但极限就是 1
l--;
return l;
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
cin >> d[i];
for (int i = 0; i < 3; i++)
cin >> c[i];
int res1, res2;
for (int i = 1; i <= n; i++)
{
f[i] += f[i - 1] + c[0];
res1 = f[find(i, 7)] + c[1];
res2 = f[find(i, 30)] + c[2];
int t = min(res1, res2);
f[i] = min(f[i], t);
}
cout << f[n] << endl;
return 0;
}
边栏推荐
- Matlab load usage
- Three pop-up boxes commonly used in JS
- Compose learning notes 1-compose, state, flow, remember
- 7、 Detailed explanation of C language function definition
- 35道MySQL面试必问题图解,小白都能看懂
- PS how to crop photos
- PS modify the length and width pixels and file size of photos
- Wonderful frog -- how simple can it be to abandon the float and use the navigation bar set by the elastic box
- C language program: judging triangles
- 安全与隐私计算在国内发展现状
猜你喜欢

SSRF vulnerability

Find papers and their open source code

边缘技术和小程序容器在智能家居中的应用
Node.js+express realizes the operation of MySQL database

企业微信客服链接,企业微信客服聊天

Establishment and traversal of binary tree (implemented in C language)

Instant experience | further improve application device compatibility with cts-d

Buuctf partial solution

Simple data analysis using Weka and excel

SQL labs detailed problem solving process (less1-less10)
随机推荐
Solve blast database error: error pre fetching sequence data
Install biological sequence de redundancy software CD hit
6、 C language circular statement
Establishment and traversal of binary tree (implemented in C language)
Instant experience | further improve application device compatibility with cts-d
The third pre class exercise
Development status of security and privacy computing in China
Second class exercise
【游戏测试工程师】初识游戏测试—你了解它吗?
Third class exercise
Application of edge technology and applet container in smart home
URP下使用GL进行绘制方法
Iframe tag
Xiaobai can understand the 35 necessary questions in MySQL interview
VTK vtkcontourwidget extracts regions of interest
Machine learning related concepts
Node.js+express realizes the operation of MySQL database
Three pain points of software development! How to solve the applet container?
Chapter 3 stack, queue and array
苹果iPhone手机APP应用图标隐藏怎么找回恢复显示在iPhone苹果手机桌面显示被隐藏的应用APP图标到iPhone苹果手机桌面?