2019 牛客多校 C Governing sand 线段树

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

Governing sand

时间限制:C/C++ 3秒,其他语言6秒
空间限制:C/C++ 65536K,其他语言131072K
64bit IO Format: %lld

题目描述

The Wow village is often hit by wind and sand,the sandstorm seriously hindered the economic development of the Wow village.

There is a forest in front of the Wowo village, this forest can prevent the invasion of wind and sand. But there is a rule that the number of tallest trees in the forest should be more than half of all trees, so that it can prevent the invasion of wind and sand. Cutting down a tree need to cost a certain amount of money. Different kinds of trees cost different amounts of money. Wow village is also poor.

There are n kinds of trees. The number of i-th kind of trees is PiP_iPi​, the height of i-th kind of trees is HiH_iHi​, the cost of cutting down one i-th kind of trees is CiC_iCi​.

 

(Note: "cutting down a tree" means removing the tree from the forest, you can not cut the tree into another height.)

 

输入描述:

The problem is multiple inputs (no more than 30 groups).
For each test case.
The first line contines one positive integers n(1≤n≤105)n (1 \leq n \leq 10^5)n(1≤n≤105),the kinds of trees.
Then followed n lines with each line three integers Hi(1≤Hi≤109)H_i (1 \leq H_i \leq 10^9)Hi​(1≤Hi​≤109)-the height of each tree, Ci(1≤Ci≤200)C_i (1 \leq C_i \leq 200)Ci​(1≤Ci​≤200)-the cost of cutting down each tree, and Pi(1≤Pi≤109)P_i(1 \leq P_i\leq 10^9)Pi​(1≤Pi​≤109)-the number of the tree.

输出描述:

For each test case, you should output the minimum cost.

示例1

输入

复制

2
5 1 1
1 10 1
2
5 1 2
3 2 3

输出

复制

1
2

题解:按照高度从低到高进行操作,对于之前操作的树,建一个权值线段树维护下在每个C下的数量和花费,然后对于当前高度计算当前高度下的树的数量m,然后线段树查询下m-1个权值最大的树的权值和。因为这个C只有200,暴力也能过,如果C很大就只能线段树了,如果有1e9那还需要离散化

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
struct node {
    int l, r;
    ll num, sum;
}tree[210 * 4];
struct node1 {
    int h, c, p;
    bool operator <(const node1 &b) const {
        return h < b.h;
    }
}a[N];
int n;
void build(int l, int r, int cur) {
    tree[cur].l = l;
    tree[cur].r = r;
    tree[cur].num = tree[cur].sum = 0;
    if(l == r) return;
    int mid = (r + l) >> 1;
    build(l, mid , cur << 1);
    build(mid + 1, r, cur << 1 | 1);
}
ll query(int cur, ll k) {
    if(k == 0) return 0;
     
    if(tree[cur].num <= k) {
        return tree[cur].sum;
    }
    if(tree[cur].l == tree[cur].r) {
        return min(k, tree[cur].num) * tree[cur].l;
    }
    ll res = 0;
    if(tree[cur << 1 | 1].num >= k) res = query(cur << 1 | 1, k);
    else res = tree[cur << 1 | 1].sum + query(cur << 1, k - tree[cur << 1 | 1].num);
    return res;
}
void update(int pos, int num, int cur) {
    if(tree[cur].l == tree[cur].r) {
        tree[cur].num += num;
        tree[cur].sum = 1LL * tree[cur].l * tree[cur].num;
        return;
    }
    if(pos <= tree[cur << 1].r) update(pos, num, cur << 1);
    else update(pos, num, cur << 1 | 1);
    tree[cur].num = tree[cur << 1].num + tree[cur << 1 | 1].num;
    tree[cur].sum = tree[cur << 1].sum + tree[cur << 1 | 1].sum;
}
int main() {
    ll ans = 0, cnt, num;
    ll sum = 0;
    while(~scanf("%d", &n)) {
         
        sum = 0;
        for(int i = 1; i <= n; i++)  scanf("%d %d %d", &a[i].h, &a[i].c, &a[i].p), sum += 1LL * a[i].c * a[i].p;
        sort(a + 1, a + 1 + n);
        build(1, 200, 1);
        ans = sum; 
        for(int i = 1, j; i <= n; ) {
            cnt = 0;
            num = 0;
            j = i;
            while(j <= n && a[j].h == a[i].h) {
                cnt += 1LL * a[j].c * a[j].p;
                num += a[j].p;
                j++;
            }
            cnt += query(1, num - 1);
            ans = min(ans, sum - cnt);
            j = i;
            while(j <= n && a[j].h == a[i].h) {
            //  cout << j << endl;
                update(a[j].c, a[j].p, 1);
                j++;
            }
            i = j;
        }
        printf("%lld\n", ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值