P3028 汽水机(差分)

题目

P3028 [USACO10OCT]汽水机Soda Machine

解析

差分,看到\(a[i]\leq 1e9\),离散化一下,在\(l\)\(+1\)\(r+1\)\(-1\),这样就只有\(2n\)个点了,再按位置排一下序,扫一遍记录答案就可以了。
需要注意的是,如果在某个位置既有\(+1\)操作又有\(-1\)操作,要先\(-1\)\(+1\),否则在统计答案的时候会多算

代码

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;

int sum, ans, n;

struct node {
    int pos, val;
    bool operator<(const node & oth) const {
        return pos == oth.pos ? val < oth.val : pos < oth.pos;
    }
} e[N];

int main() {
    ios::sync_with_stdio(false);
    cin >> n;
    for (int i = 1, x, y; i <= n; ++i) {
        cin >> x >> y;
        e[i] = (node) {x, 1}, e[i + n] = (node) {y + 1, -1};
    }
    sort(e + 1, e + 1 + n + n);
    for (int i = 1; i <= n + n; ++i) {
        sum += e[i].val;
        ans = max(ans, sum);
    }
    cout << ans;
}

全部评论

相关推荐

专业嗎喽:个人信息名字太大,合到电话邮箱那一栏就行,有党员写过党,剩下其他全删,站空太大了 把实习经历丰富,放最前面,然后是个人评价,技能之类的,然后是学校信息。项目经历最后面,可以就选一个自己擅长的。 现在是学校不是92就扣分的,没必要放前面。 然后现在看重实习经历>竞赛经历(校园经历)>课程项目经历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务