CF 12D. Ball

 

 

D - Ball

Crawling in process...Crawling failedTime Limit:3000MS Memory Limit:262144KB64bit IO Format:%I64d & %I64u

Description

N ladies attend the ball in the King's palace. Every lady can be described with three values: beauty, intellect and richness. King's Master of Ceremonies knows that ladies are very special creatures. If some lady understands that there is other lady at the ball which is more beautiful, smarter and more rich, she can jump out of the window. He knows values of all ladies and wants to find out how many probable self-murderers will be on the ball. Lets denote beauty of thei-th lady by Bi, her intellect byIi and her richness byRi. Theni-th lady is a probable self-murderer if there is somej-th lady that Bi < Bj, Ii < Ij, Ri < Rj. Find the number of probable self-murderers.

Input

The first line contains one integer N (1 ≤ N ≤ 500000). The second line containsN integer numbersBi, separated by single spaces. The third and the fourth lines contain sequencesIi andRi in the same format. It is guaranteed that0 ≤ Bi, Ii, Ri ≤ 109.

Output

Output the answer to the problem.

Sample Input

Input
3
1 4 2
4 3 2
2 5 3
Output
1

 

 

这算是一道好题了,记得去年亚洲网络赛的时候也出了一道类似的题,只不过那是二维的。

刚看到这题的时候,忘了二维具体是怎么解的,找自己写的解题报告,发现除了代码什么思路都没写,泪~~~

还是决定简单记录一下,以免以后又忘了怎么解了。

首先关键是排序,按第一个关键字从小到大排序,如果第一个关键字相等,则按第二个关键字从大到小排序,如果第二个关键字相等则按第三个关键字从小到大排序。之所以这样排序,是因为我们要用map来维护第二个和第三个关键字,从尾查找和插入。

#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<map>
#include<algorithm>
using namespace std;
const int maxn=500005;
const int minn=1000000000;
struct Node
{
    int b,l,r;
    bool operator <(const Node &node) const
    {
        return b!=node.b?b<node.b:l!=node.l?l>node.l:r<node.r;
    }
}q[maxn];
int main()
{
    int n;
    map<int,int>qiqi;
    map<int,int>::iterator it;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++) scanf("%d",&q[i].b);
        for(int i=0;i<n;i++) scanf("%d",&q[i].l);
        for(int i=0;i<n;i++) scanf("%d",&q[i].r);
        sort(q,q+n);
        int count=0;
        qiqi.clear();
        qiqi[minn]=-minn;qiqi[-minn]=minn;
        for(int i=n-1;i>=0;i--)
        {
            it=qiqi.upper_bound(q[i].l);//从map里找比当前lady的第二个关键字大的,it指向它
            if(it->second>q[i].r) count++;//如果map里it指向的lady的第三个关键字大于当前lady的第三个关键字,那么当前lady
            //肯定会自杀。因为先插入到map里的lady的第一关键字肯定大于当前lady的第一关键字(想想我们是怎么排序和查找的)
            //然后it指向的第二个关键字又大于当前lady(upper_bound),再加上if里表示map里的lady的第三个关键字也大于当前lady
            //所以当前lady 自杀,好啰嗦~~~~~
            else if(qiqi[q[i].l]<q[i].r)//不然我们就要更新map里的值,把map里与当前lady的第二个关键字相同的lady的第三个关键字
            //进行比较,大于则把当前lady插入到map里,同时把map里的其他lady的第二关键字比当前lady小并且第三关键字也比当前lady
            //小的那些人去掉,因为以后在比较和查找过程中遇到那些人要自杀的人遇到当前这个lady就更要自杀了。
            {
                qiqi[q[i].l]=q[i].r;
                for(it=--qiqi.lower_bound(q[i].l);it->second<q[i].r;)
                {
                    qiqi.erase(it--);
                }
            }
        }
        cout<<count<<endl;

    }
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值