HackerRank - "Algorithmic Crush"

My intuition told me that it is a line-scan process.. and yes it is. First, we sort all (a,b,k) by index\start-end, then we do a line scan. And in C++ you HAVE TO use 'long long'

#include <cmath>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <string>
#include <climits>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
using namespace std;

struct Rec
{
    Rec(long long v, int rx, int b) :val(v), x(rx), bStart(b){}
    long long val;
    int x;
    int bStart;
};

bool comp(const Rec &r1, const Rec &r2)
{
    if (r1.x != r2.x)
        return r1.x < r2.x;
    return r1.bStart > r2.bStart;
}

int main()
{
    int n, m; cin >> n >> m;

    vector<Rec> recs;
    while (m--)
    {
        long long a, b, k;
        cin >> a >> b >> k;
        recs.push_back(Rec(k, a, 1));
        recs.push_back(Rec(k, b, 0));
    }
    std::sort(recs.begin(), recs.end(), comp);

    long long curr = 0, ret = -1;
    for (auto &r : recs)
    {
        if (r.bStart == 1)
        {
            curr += r.val;
        }
        else
        {
            curr -= r.val;
        }
        ret = std::max(ret, curr);
    }
    cout << ret << endl;

    return 0;
}

转载于:https://www.cnblogs.com/tonix/p/4543288.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值