D - Travel Card

思路:dp,类似于单调队列优化。

其实可以写的更简单。。。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int, int> using namespace std; const int N = 1e5 + ;
const int M = 1e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 +; int n, t[N], dp[N][], mn[N], a[N];
int st1[N], st2[N], head1, head2, rear1, rear2; int main() {
memset(dp, inf, sizeof(dp));
memset(mn, inf, sizeof(mn));
mn[] = ;
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%d", &t[i]);
} dp[][] = ;
dp[][] = ;
dp[][] = ; st1[rear1++] = ;
st2[rear2++] = ; for(int i = ; i <= n; i++) {
for(int j = ; j < ; j++) mn[i - ] = min(mn[i - ], dp[i - ][j]);
dp[i][] = mn[i - ] + ;
dp[i][] = mn[i - ] + ;
dp[i][] = mn[i - ] + ;
while(head1 < rear1 && t[i] - t[st1[head1]] >= ) head1++;
while(head2 < rear2 && t[i] - t[st2[head2]] >= ) head2++; if(head1 < rear1) dp[i][] = min(dp[i][], dp[st1[head1]][]);
if(head2 < rear2) dp[i][] = min(dp[i][], dp[st2[head2]][]); while(head1 < rear1 && dp[i][] < dp[st1[rear1 - ]][]) rear1--;
while(head2 < rear2 && dp[i][] < dp[st2[rear2 - ]][]) rear2--; st1[rear1++] = i;
st2[rear2++] = i;
} for(int j = ; j < ; j++) {
mn[n] = min(mn[n], dp[n][j]);
} for(int i = ; i <= n; i++) {
printf("%d\n", mn[i] - mn[i - ]);
}
return ;
}
/*
*/

Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition) D - Travel Card的更多相关文章

  1. Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition) E - Nikita and stack 线段树好题

    http://codeforces.com/contest/760/problem/E 题目大意:现在对栈有m个操作,但是顺序是乱的,现在每输入一个操作要求你输出当前的栈顶, 注意,已有操作要按它们的 ...

  2. Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition)A 水 B 二分 C并查集

    A. Petr and a calendar time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  3. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) D. Factory Repairs 树状数组

    D. Factory Repairs 题目连接: http://www.codeforces.com/contest/635/problem/D Description A factory produ ...

  4. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) C. XOR Equation 数学

    C. XOR Equation 题目连接: http://www.codeforces.com/contest/635/problem/C Description Two positive integ ...

  5. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition)B. sland Puzzle 水题

    B. sland Puzzle 题目连接: http://www.codeforces.com/contest/635/problem/B Description A remote island ch ...

  6. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) A. Orchestra 水题

    A. Orchestra 题目连接: http://www.codeforces.com/contest/635/problem/A Description Paul is at the orches ...

  7. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition)

    暴力 A - Orchestra import java.io.*; import java.util.*; public class Main { public static void main(S ...

  8. 8VC Venture Cup 2016 - Final Round (Div. 1 Edition) E - Preorder Test 树形dp

    E - Preorder Test 思路:想到二分答案了之后就不难啦, 对于每个答案用树形dp取check, 如果二分的值是val, dp[ i ]表示 i 这棵子树答案不低于val的可以访问的 最多 ...

  9. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) A

    A. Orchestra time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

随机推荐

  1. HDU4003 树形DP

    题意 :给一棵n个节点的树, 节点编号为1~n, 每条边都有一个花费值.        有k个机器人从S点出发, 问让机器人遍历所有边,最少花费值多少? 这题最难的地方应该就是如何定义状态了 定义dp ...

  2. linux 内存计算

    原文: http://www.open-open.com/lib/view/open1424325362577.html Linux中的Cache Memory 什么是Cache Memory(缓存内 ...

  3. 获取Session和request方法

    action中的几种写法 //第一种很少用public class LoginAction1 extends ActionSupport {        private Map request;   ...

  4. $.extend()与$.fn.extend()

    jQuery.extend(object) 扩展jQuery对象本身.用来在jQuery命名空间上增加新函数.jQuery.fn.extend(object) 扩展 jQuery 元素集来提供新的方法 ...

  5. SpringCloud(二)注册服务提供者搭建

    上文已经写了如何去搭建注册中心,仅有注册中心是远远不够的,所以我们需要注册到注册中心并提供服务的节点,这里称为注册服务提供者 前提 阅读上文,并成功搭建注册中心,环境无需改变 项目搭建 这里我们需要新 ...

  6. git使用(1)----推送代码到远程

    git使用(1) 首先要明白git上有三个区域 1.工作区 2.暂存区 3.历史记录区 步骤: 1.git  init 2.配置环境(如果配置一次了以后就不用再继续配置) git  config  - ...

  7. Oracle Number类型超长小数位为0问题

    碰到了一个非常奇怪的问题,从Excel拷贝出来的数据,位数很长,通过Pl Sql 导出到Oracle后为0了,而且设置查询条件为0时,无法查询出来,条件大于0居然能查询出来,通过to_number也是 ...

  8. wepy 使用组件时一个注意事项。。。

    组件传值prop 必须使用指定引用地址的数据 如果像下面这样 取为空shop 中的phone ,后续获取数据或就算 shop中有phone元素 子组件也无法获取到修改后的值 <template& ...

  9. 【洛谷 P3191】 [HNOI2007]紧急疏散EVACUATE(二分答案,最大流)

    题目链接 sb错误调了3hour+.. bfs预处理出每个\(.\)到每个\(D\)的最短距离. 二分时间\(t\),把每个\(D\)拆成\(t\)个点,这\(t\)个点两两连边,流量\(INF\)表 ...

  10. Metasploit 使用基础

    本文将简单介绍一下Kali2 上Metasploit的一些基本使用,包括启动.更新及一个小问题"Module database cache not built yet, using slow ...