大神勿看
题意
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 bi inclusive (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 j includes 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.
翻译
有N个历史事件,告诉你每个历史事件的开始和结束时间,问有多少个历史事件包含在别的历史事件里?
思路
只需要将这N个历史事件按开始时间排序,然后看每个历史事件的右端点是否<前面的历史事件中右端点最大的历史事件的右端点,如果是,那么这个历史事件就被包含,最后统计一下被包含的个数,输出。
实现
上代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int read(){
int f=1,k=0;
char c;
c=getchar();
while(c<'0'||c>'9'){
if(c=='-'){
f=-1;
}
c=getchar();
}
while(c>='0'&&c<='9'){
k=(k<<1)+(k<<3)+(c^48);
c=getchar();
}
return f*k;
}
void write(int x){
if(x<0){
putchar('-');
x=-x;
}
if(x>9){
write(x/10);
}
putchar(x%10+'0');
}
void pl(){putchar('\n');}
vector<pair<int,int> > a;
bool cmp(const pair<int,int> A,const pair<int,int> B){return A.first<B.first;}
int main(){
const int n=read();
for(int i=0;i<n;i++){
pair<int,int>p;
p.first=read();
p.second=read();
a.push_back(p);
}
sort(a.begin(),a.end(),cmp);
int big;
big=0;
int ans=0;
for(int i=0;i<n;i++){
if(big>a[i].second){
ans++;
}
big=max(big,a[i].second);
}
write(ans);
return 0;
}