20152016-acmicpc-neerc-northern-subregional-contest J:Journey to the "The World's Start"(单调队列+DP+二分)
来源:互联网 发布:销售智慧软件 编辑:程序博客网 时间:2024/05/16 16:14
题意:给出n-1张不同的票,票价分别为 pi,每张票每次最多可以坐 r 个站(1<=r<n),并且票是可以无限用并且只能买一张,如果坐到限定的距离了,要出站再重新进站,这里要花费 di 的时间(2<=i<=n-1),并且每坐一个站花费 1 min,一个人坐地铁要从第一个站坐到最后一个站,问在规定时间 t 里面可以买到的最低票价是多少。
思路:首先可以确定的是,我们对票价都要进行一次DP,我自己推出的方程是dp[i] = min(dp[i], (dp[i-j] + j) + time[i]),(1<=j<=r),接着发现无论怎么样,那个人肯定要坐 n-1 个站,所以直接让 t 减去 n - 1,然后方程变成dp[i] = min(dp[i], dp[i-j]+time[i]),这样的话DP时间复杂度还是O(n^2)。
由于是 f[k] + g[i] 型的 dp(只有形如 dp[i]=max/min (f[k]) + g[i] (k<i && g[i]是与k无关的变量)才能用到单调队列进行优化。),我们可以使用单调队列来优化这个DP,化成dp[i] = min(dp[i], dp[que[head]] + time[i])这样的形式。这样优化之后时间复杂度就直接变成了O(n)了。
只有这样还是不够的,因为我们要枚举每种票,通过题意可以发现每种票的距离 r 是递增的,因此我们可以通过二分搜索来枚举,这样复杂度就变成了O(nlogn)了,还有一点就是可能一种票的路程比搜索出来的临界值大,但是价格较小,我们应该枚举一下后面的取较小者。
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>#include <map>#include <cstdlib>typedef long long LL;using namespace std;#define N 50005#define INF 0x3f3f3f3f3f//单调队列优化DP + 二分搜索LL price[N];LL time[N];LL dp[N];int que[N];int n, t;bool solve(int r){ if(r == 0) return false; dp[1] = 0; int head = 1, tail = 0; que[++tail] = 1; for(int i = 2; i <= n; i++) { dp[i] = dp[que[head]] + time[i]; // 如果队尾的时间大于当前时间,就删除,然后把当前的站插入 while(head <= tail && dp[que[tail]] >= dp[i]) tail--; que[++tail] = i; // 当前距离和之前的距离不能超过 r while(head <= tail && i - que[head] >= r) head++; } if(dp[n] > t) return false; return true;}int main(){// freopen("journey.in", "r", stdin);// freopen("journey.out", "w", stdout); scanf("%d%d", &n, &t); for(int i = 1; i < n; i++) scanf("%I64d", &price[i]); for(int i = 2; i <= n-1; i++) scanf("%I64d", &time[i]); //总共坐n-1个站要n-1 min t -= n-1; int l = 0, r = n - 1; while(l <= r) { int mid = (l+r) / 2; if(solve(mid)) { r = mid - 1; } else { l = mid + 1; } } int ans = price[l]; for(int i = l + 1; i < n; i++) if(price[i] < ans) ans = price[i]; printf("%d\n", ans); return 0;}
阅读全文
0 0
- 20152016-acmicpc-neerc-northern-subregional-contest J:Journey to the "The World's Start"(单调队列+DP+二分)
- 【2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest J】【二分答案+multiset】Journey to the “The World’
- GYM 100801J Journey to the “The World's Start”【二分+Dp+单调队列】
- 2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest Problem J 【二分+DP+单调队列】
- Problem J. Journey to the “The World’s Start”
- GYM 100801J Journey to the “The World's Start”
- Journey to the The World's Start
- Journey to the The World's Start
- ACM ICPC 2011–2012, NEERC, Northern Subregional Contest J. John’s Inversions(合并排序求逆序数对数)
- alpcOj Journey to the The World's Start
- 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest J dp
- John’s Inversions 2011-2012 ACM-ICPC, NEERC, Northern Subregional Contest
- 2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest D:Distribution in Metagonia(构造)
- 2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest G:Graph (构造,拓扑)
- Class(ACM ICPC 2008–2009, NEERC, Northern Subregional Contest)
- 2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest
- 2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest【9/12】
- 2017-2018 ACM-ICPC, NEERC, Northern Subregional Contest
- 数组和指针的区别、字符数组和字符串指针区别
- Servlet中的web.xml
- Redis安装使用
- 【kmp算法next数组-串的最小循环节/循环周期】Period HDU
- 汇编语言(第三版)检测点重点题目整理
- 20152016-acmicpc-neerc-northern-subregional-contest J:Journey to the "The World's Start"(单调队列+DP+二分)
- PAT 甲级 1094. The Largest Generation (25)
- OSI七层模型与TCP/IP五层模型
- JavaSe复习笔记
- 欢迎使用CSDN-markdown编辑器
- 《阿里巴巴Java开发手册》终极版更新
- 82.Remove Duplicates from Sorted List II
- Java编程思想读书笔记——内部类
- 文化,大脑与幸福