牛客练习赛4 A Laptop【二维偏序_树状数组】

https://www.nowcoder.com/acm/contest/16/A

这是一道二维偏序的问题,在一维偏序中我们可以直接排序,然而在二维偏序中,当然可以用n^2的复杂度解决问题。然而如果需要优化的话,我们必须要防止后效性,也就是固定一维考虑另外一维并且防止错解。

不妨设对于每一个物品的两个参数为se与ve,那么我需要知道在ve大于当前物品的物品中,是否存在se也大于当前物品se的,这里就可以使用树状数组统计了。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
#include<vector>
using namespace std;
#define ll long long int
#define INF 0x3f3f3f3f
#define lowbit(x) x&(-x)
const int maxn = 1e5 + 10;
int c[maxn];
int n;
void add(int x) {
    for (; x <= n; x += lowbit(x)) c[x]++;
}
int query(int x) {
    int ans = 0;
    for (; x ; x -= lowbit(x)) ans += c[x];
    return ans;
}
pair<int, int> pii[maxn];
vector<int> ve;
int main()
{
    while (~scanf("%d", &n)) {
        ve.clear();
        for (int i = 1; i <= n; i++) {
            scanf("%d%d", &pii[i].first, &pii[i].second);
            ve.push_back(pii[i].second);
        }
        for (int i = 0; i <= n; i++) c[i] = 0;
        sort(pii + 1, pii + 1 + n);
        sort(ve.begin(), ve.end());
        int ans = 0;
        for (int i = n; i >= 1; i--) {
            int pos = lower_bound(ve.begin(), ve.end(), pii[i].second) - ve.begin() + 1;
            if (query(n) - query(pos)) ans++;
            add(pos);
        }
        printf("%d\n", ans);
    }
    return 0;
}
//_CRT_SECURE_NO_WARNINGS
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值