牛客练习赛4 - A - Laptop(二维偏序)

题目链接:https://ac.nowcoder.com/acm/contest/16/A

思路:二维偏序问题,按照第一维排序,再用树状数组处理第二维即可。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ul;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define bug prllf("*********\n")
#define debug(x) cerr<<#x<<" = "<<(x)<<endl
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int mod = 998244353;
const double eps = 1e-8;
const double pi = acos(-1);
template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}
const int N = 1e5 + 7;
int n, c[N];
vector<int> v;
void add(int x, int y) {while(x <= n) c[x] += y, x += x&-x;}
int sum(int x) {int ret = 0;while(x > 0) ret += c[x], x -= x&-x; return ret;}
int getid(int x){return lower_bound(v.begin(),v.end(),x)-v.begin() + 1;}
struct node
{
    int x, y;
    bool operator <(const node &tmp)const{
        return x < tmp.x;
    }
}a[N];

int main()
{
    IO; cin >> n;
    for(int i = 0; i < n; i++)
    {
        cin >> a[i].x >> a[i].y;
        v.push_back(a[i].y);
    }
    sort(a, a + n);
    sort(all(v)); v.erase(unique(all(v)), v.end());
    int ans = 0;
    for(int i = n - 1; i >= 0; i--)
    {
        int pos = getid(a[i].y);
        if(sum(pos) != n - 1 - i) ans++;
        add(pos, 1);
    }
    cout << ans << endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值