2019牛客多校第一場 I Points Division(動態規划+線段樹)


2019牛客多校第一場 I Points Division(動態規划+線段樹)

傳送門:https://ac.nowcoder.com/acm/contest/881/I

題意:

給你n個點,每個點有兩個屬性a,b

需要將點划分為兩堆,划分依據是對於在A划分中的任意點a和在B划分中的任意點b滿足

不存在當a.x>b.x時,a.y<b.y 的情況

在A划分中的點可以給出其a屬性的貢獻,在B划分中的點可以給出其b屬性的貢獻

求最大貢獻和

題解:

根據題意,我們可以得出結論,我們需要找的是一根折線,這根折線將點集分為A、B兩部分、

我們需要求這兩個部分的最大權值和

我們考慮dp狀態

dp[i]表示到第i個點在折線上時和的最大值,如果增加了這個點,他對答案產生的貢獻就是,對於之前比這個點高的點,對答案的貢獻是ai,對於之前比這個點低的點,對答案的貢獻是bi

於是\(d p[j]=\left\{\begin{array}{ll}{d p[j]+b_{i}} & {j<i, y_{j}>y_{i}} \\ {d p[j]+a_{i}} & {j<i, y_{j}<y_{i}}\end{array}\right.\)

\(d p[i]=b_{i}+\max _{1 \leq j<i, y_{j}<y_{i}} d p[j]\)

顯然這個式子是可以用線段樹維護區間最值的

因為值域范圍為1e9,我們將y值離散化后建樹,維護的區間最大值就是我們最后的答案

因為dp的值是從0開始的,所以我們建樹也是從0開始

排序是為了能夠有A,B的合法划分

感謝邱神的博客學習:https://blog.csdn.net/u013534123/article/details/96465704

代碼:

#include <set>
#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
LL quick_pow(LL x, LL y) {
    LL ans = 1;
    while(y) {
        if(y & 1) {
            ans = ans * x % mod;
        } x = x * x % mod;
        y >>= 1;
    } return ans;
}
LL Max[maxn << 2];
LL lazy[maxn << 2];
void push_up(int rt) {
    Max[rt] = max(Max[ls], Max[rs]);
}
void build(int l, int r, int rt) {
    Max[rt] = lazy[rt] = 0;
    if(l == r) return;
    int mid = (l + r) >> 1;
    build(lson);
    build(rson);
}
void push_down(int rt) {
    if(lazy[rt]) {
        lazy[ls] += lazy[rt];
        lazy[rs] += lazy[rt];
        Max[ls] += lazy[rt];
        Max[rs] += lazy[rt];
        lazy[rt] = 0;
    }
}
void update(int L, int R, LL val, int l, int r, int rt) {
    if(L <= l && r <= R) {
        Max[rt] += val;
        lazy[rt] += val;
        return;
    }
    push_down(rt);
    int mid = (l + r) >> 1;
    if(L <= mid) update(L, R, val, lson);
    if(R > mid) update(L, R, val, rson);
    push_up(rt);
}
void change(int pos, LL val, int l, int r, int rt) {
    if(l == r) {
        Max[rt] =  val;
        return;
    }
    push_down(rt);
    int mid = (l + r) >> 1;
    if(pos <= mid) change(pos, val, lson);
    else change(pos, val, rson);
    push_up(rt);
}
LL query(int L, int R, int l, int r, int rt) {
    if(L <= l && r <= R) {
        return Max[rt];
    }
    push_down(rt);
    int mid = (l + r) >> 1;
    LL ans = 0;
    if(L <= mid) ans = max(ans, query(L, R, lson));
    if(R > mid) ans = max(ans, query(L, R, rson));
    return ans;
}
struct node {
    int x, y, a, b;
} p[maxn];
bool cmp(node a, node b) {
    if(a.x != b.x) return a.x < b.x;
    return a.y > b.y;
}
int main() {
#ifndef ONLINE_JUDGE
    FIN
#endif
    int n;
    while(~scanf("%d", &n)) {
        vector<int> vec;
        for(int i = 1; i <= n; i++) {
            scanf("%d%d%d%d", &p[i].x, &p[i].y, &p[i].a, &p[i].b);
            vec.push_back(p[i].y);
        }
        sort(vec.begin(), vec.end());
        vec.erase(unique(vec.begin(), vec.end()), vec.end());
        for(int i = 1; i <= n; i++) {
            p[i].y = lower_bound(vec.begin(), vec.end(), p[i].y) - vec.begin() + 1;
        }
        int tot = vec.size();
        sort(p + 1, p + n + 1, cmp);
        build(0, tot, 1);
        for(int i = 1; i <= n; i++) {
            change(p[i].y, query(0, p[i].y, 0, tot, 1) + p[i].b, 0, tot, 1);
            if(p[i].y - 1 >= 0) update(0, p[i].y - 1, p[i].a, 0, tot, 1);
            if(p[i].y + 1 <= tot) update(p[i].y + 1, tot, p[i].b, 0, tot, 1);
        }
        printf("%lld\n", Max[1]);
    }


    return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM