CF 137C History题解

大神勿看

题意

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值