codeforces 98 div2 C.History 水题

C. History
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarpus likes studying at school a lot and he is always diligent about his homework. Polycarpus has never had any problems with natural sciences as his great-great-grandfather was the great physicist Seinstein. On the other hand though, Polycarpus has never had an easy time with history.

Everybody knows that the World history encompasses exactly n events: the i-th event had continued from the year ai to the year biinclusive (ai < bi). Polycarpus easily learned the dates when each of n events started and ended (Polycarpus inherited excellent memory from his great-great-granddad). But the teacher gave him a more complicated task: Polycaprus should know when all events began and ended and he should also find out for each event whether it includes another event. Polycarpus' teacher thinks that an event jincludes an event i if aj < ai and bi < bj. Your task is simpler: find the number of events that are included in some other event.

Input

The first input line contains integer n (1 ≤ n ≤ 105) which represents the number of events. Next n lines contain descriptions of the historical events, one event per line. The i + 1 line contains two integers ai and bi (1 ≤ ai < bi ≤ 109) — the beginning and the end of the i-th event. No two events start or finish in the same year, that is, ai ≠ aj, ai ≠ bj, bi ≠ aj, bi ≠ bj for all ij (where i ≠ j). Events are given in arbitrary order.

Output

Print the only integer — the answer to the problem.

Examples
input
5
1 10
2 9
3 8
4 7
5 6
output
4
input
5
1 100
2 50
51 99
52 98
10 60
output
4
input
1
1 1000000000
output
0
题意:给你n歌时间段,开始和结束,找出有多少个时间段在另外任意的时间段内;
思路:所以我们先按结束时间排序,每次更新最小值;
复制代码
#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#define mod 1000000007
#define inf 100000000000005
#define MAXN 10000010
//#pragma comment(linker, "/STACK:102400000,102400000")
struct is
{
    int st,en;
    bool operator <(const is &x)const
    {
        if(en!=x.en)
        return en>x.en;
        return st<x.st;
    }
 }a[100010];
int main()
{
    int x,y,z,i,t;
    scanf("%d",&x);
    for(i=0;i<x;i++)
    scanf("%d%d",&a[i].st,&a[i].en);
    sort(a,a+x);
    int minn=a[0].st,maxx=a[0].en;
    int minnn=a[0].st,maxxx=a[0].en;
    int ans=0;
    for(i=1;i<x;i++)
    {
        if((maxx>a[i].en&&minn<a[i].st)||(maxxx>a[i].en&&minnn<a[i].st))
        ans++;
        if(a[i].st<minnn)
        {
            maxx=maxxx;
            minn=minnn;
            minnn=a[i].st;
            maxxx=a[i].en;
        }
    }
    cout<<ans<<endl;
    return 0;
}
复制代码

 

posted @   jhz033  阅读(612)  评论(0编辑  收藏  举报
编辑推荐:
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
阅读排行:
· [翻译] 为什么 Tracebit 用 C# 开发
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· DeepSeek崛起:程序员“饭碗”被抢,还是职业进化新起点?
· 2分钟学会 DeepSeek API,竟然比官方更好用!
· .NET 使用 DeepSeek R1 开发智能 AI 客户端
点击右上角即可分享
微信分享提示