码迷,mamicode.com
首页 > 其他好文 > 详细

2019杭电多校第三场 1004 Distribution of books

时间:2019-07-30 09:26:05      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:情况   时间   max   air   tin   stdin   typename   date   void   

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6606

考虑二分答案,我们二分一个值\(x\),那么要怎么来验证这个答案是否可行,考虑dp求解,设\(dp[i]\)为前i个在答案为\(x\)的情况下划分最最多组数,那么若\(dp[n] \geq k\) 则这个x可行, 很显然可以看出\(x\)是单调的,所以二分。
\[dp[i] = max(dp[j]) + 1 (sum[i] - sum[j-1] \leq x)\]
如果直接采用暴力枚举的话复杂度\(O(n^2)\),不妨考虑离散化之后建权值线段树。我们将所有的\(sum[i]\)离散化给定标号后, 我们从\(i=1\)开始遍历到\(n\)的过程中,对于每个i,只需要找到第一个\(sum[t] \geq sum[i] - x\)也就是\(sum[i]-x\)的后继。 然后在权值线段树上取query求\([t, m]\)的最大值即可,\(m\)为权值线段树的节点个数。 时间复杂度\(O(nlog^2n)\)

#include <bits/stdc++.h>
#define pii pair<ll, ll>
#define pil pair<ll, long long>
#define pll pair<long long, long long>
#define lowbit(x) ((x)&(-x))
#define mem(i, a) memset(i, a, sizeof(i))
#define sqr(x) ((x)*(x))
#define all(x) x.begin(),x.end()
#define ls (k << 1)
#define rs (k << 1 | 1)
using namespace std;
typedef long long ll;
template <typename T>
inline void read(T &X) {
    X = 0; char ch = 0; T op = 1;
    for(; ch > '9' || ch < '0'; ch = getchar())
        if(ch == '-') op = -1;
    for(; ch >= '0' && ch <= '9'; ch = getchar())
        X = (X << 3) + (X << 1) + ch - 48;
    X *= op;
}

const ll INF = 0x3f3f3f3f;
const ll N = 2e5 + 5;
vector<ll> v;
struct node {
    ll l,r,w = 0;
}tr[N * 4];
ll a[N],sum[N];
ll n,k,m;
void build(ll k, ll l, ll r) {
    tr[k].l = l; tr[k].r = r;
    if(l == r) {
        tr[k].w = 0; return;
    }
    ll mid = l + r >> 1;
    build(ls, l, mid);
    build(rs, mid + 1, r);
    tr[k].w = 0;
}
void update(ll k, ll p, ll x) {
    ll l = tr[k].l, r = tr[k].r;
    ll mid = l + r >> 1;
    if(l == r) {tr[k].w = max(tr[k].w, x); return;}
    if(p <= mid) update(ls, p, x);
    else update(rs, p, x);
    tr[k].w = max(tr[ls].w, tr[rs].w);
}
ll query(ll k, ll s, ll t) {
    ll l = tr[k].l, r = tr[k].r;
    if(s <= l && t >= r) {
        return tr[k].w;
    }
    ll ans = 0;
    ll mid = l + r >> 1;
    if(s <= mid) ans = max(ans, query(ls, s, t));
    if(t > mid) ans = max(ans, query(rs, s, t));
    return ans;
}
bool check(ll x) {
    build(1, 1, m);
    for(ll i = 1; i <= n; i++) {
        ll l = lower_bound(all(v), sum[i] - x) - v.begin() + 1;
        ll r = lower_bound(all(v), sum[i]) - v.begin() + 1;
        //cout << l << " " << r << " " << i << "\n";
        if(l > m) {
            if(sum[i] <= x) update(1, r, 1);
            continue;
        }
        ll w = query(1, l, m);
        if(w) {
            update(1, r, w + 1);
        } else if(sum[i] <= x) {
            update(1, r, 1);
        }
    }
  // cout << query(1, 1, m) << " " << x << "\n";
    return query(1, 1, m) >= k;
}
int main() {
#ifdef INCTRY
    freopen("input.txt", "rt", stdin);
#endif
    ll t;
    cin >> t;
    while(t--) {
        read(n);  read(k);
        ll mi = -INF, mx = INF;
        v.clear();
        for(ll i = 1; i <= n; i++) {
            read(a[i]); sum[i] = sum[i - 1] + a[i];
            v.push_back(sum[i]);
        }
        
        sort(all(v)); v.erase(unique(all(v)), v.end());
        m = v.size();
        /* for(ll i = 1; i <= n; i++) {
            id[i] = lower_bound(all(v), sum[i]) - v.begin() + 1;
        }*/
        ll l = -1e14-10, r = 1e14+10;
        ll ans = 0;
         while(l <= r) {
            ll mid = l + r >> 1;
            //cout << mid << "\n";
            if(check(mid)) ans = mid, r = mid - 1;
            else l = mid + 1;
        }
        
        //cout << query(1, 1, m) << " " << k << "\n";
        cout << ans << "\n";
    }



#ifdef INCTRY
    cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}

2019杭电多校第三场 1004 Distribution of books

标签:情况   时间   max   air   tin   stdin   typename   date   void   

原文地址:https://www.cnblogs.com/inctry/p/11267516.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!