zoukankan      html  css  js  c++  java
  • 干物妹小埋 (离散化 + 线段树 + DP)

    链接:https://ac.nowcoder.com/acm/contest/992/B
    来源:牛客网

    题目描述

    在之前很火的一个动漫《干物妹小埋》中,大家对小埋打游戏喝可乐的印象十分的深刻。
    现在欧尼酱将小埋的快乐水全部分开藏在了家具的顶端。
    小埋使出空中1080°转身接战术翻滚跳到任一家具上,她相信,只要她翻滚的足够快,欧尼酱就跟不上她。
     
    1.为获取梦幻开局,小埋一套技能可以使她一开始掉落在任一家具上。
    2.小埋家的家具按顺序给出,每个家具可跳可不跳,为避开欧尼酱的追击,小埋翻滚到某个家具上面后,只能向前继续翻滚。
    3.启动超重力感应系统的小埋不会从较高的家具翻滚到较低的家具上。
    4.由于每个家具上的快乐水都有对应的happy值,IQ==250的小埋会选择一条happy值总和最大的路线。
    那么,最终小埋将获得的happy值总和是多少呢?

    输入描述:

    第一行一个整数n(0<n<=200000),表示小埋家的家具数。

    第二行n个整数,对于每个整数ai, 0<=ai<=10^9,表示第i个家具的高度。

    第三行n个整数,对于每个整数vi, 0<=vi<=10^9,表示第i个家具上的快乐水的happy值。

    输出描述:

    一个整数,表示小埋获得的happy值总和。
    示例1

    输入

    复制
    6
    2 1 1 3 3 4
    3 1 1 1 1 1

    输出

    复制
    6

    说明

    路线:2->3->3->4

    答案:3+1+1+1

    析:根据题意可以知道,对于跳到第 i 个位置的得到最高的方法就是从前面的某个小于等于它的高度的位置转移过来,也就是求 Max{dp[j]},其中 j 的高度是小于等于 i 的,这样的话,就可以对前面的所有小于等于高度 i 的所有位置求个最大值,使用离散化+线段树很容易就解决了,当然也可以使用树状数组,但我比较习惯使用线段树

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #include <list>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lowbit(x) -x&x
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define all 1,n,1
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 2e5 + 7;
    const int mod = 1000007;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c){
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    
    int h[maxn], v[maxn];
    int a[maxn];
    
    LL dp[maxn<<2];
    
    
    void update(int M, LL val, int l, int r, int rt){
        if(l == r){
            dp[rt] = val;
            return ;
        }
        int m = l + r >> 1;
        if(m >= M)  update(M, val, lson);
        else  update(M, val, rson);
        dp[rt] = max(dp[rt<<1], dp[rt<<1|1]);
    }
    
    
    LL query(int L, int R, int l, int r, int rt){
        if(L <= l && r <= R)  return dp[rt];
        int m = l + r >> 1;
        LL ans = 0;
        if(m >= L)  ans = query(L, R, lson);
        if(R > m)  ans = max(ans, query(L, R, rson));
        return ans;
    }
    
    int getPos(int x){
        return lower_bound(a+1, a+1+n, x) - a;
    }
    
    
    int main(){
        scanf("%d", &n);
        for(int i = 1; i <= n; ++i)  scanf("%d", h + i), a[i] = h[i];
        for(int i = 1; i <= n; ++i)  scanf("%d", v + i);
        sort(a+1, a+1+n);
        for(int i = 1; i <= n; ++i){
            int pos = getPos(h[i]);
            LL ans = query(1, pos, all);
            update(pos, ans + v[i], all);
        }
        cout << query(1, n, all) << endl;
        return 0;
    }
    

      

  • 相关阅读:
    <Ajax> 四. get请求(验证用户名是否存在)
    <Ajax> 三. 前端和后端通过表单数据交互
    <Ajax> 一. PHP基本使用和基本数据类型
    <Ajax> 二. PHP选择语句和循环语句
    <Bootstrap> 学习笔记八. 导航栏和颁
    <Bootstrap> 学习笔记七. 下拉菜单和标签页
    <Bootstrap> 学习笔记六. 栅格系统使用案例
    <Bootstrap> 学习笔记五. 按钮组的使用
    <Bootstrap> 学习笔记三. 浮动的使用
    <Bootstrap> 学习笔记四. 表单组和输入框组的使用
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/11226933.html
Copyright © 2011-2022 走看看