codeforces 98 div2 C.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 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.
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 i, j (where i ≠ j). Events are given in arbitrary order.
Print the only integer — the answer to the problem.
5
1 10
2 9
3 8
4 7
5 6
4
5
1 100
2 50
51 99
52 98
10 60
4
1
1 1000000000
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; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· [翻译] 为什么 Tracebit 用 C# 开发
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· DeepSeek崛起:程序员“饭碗”被抢,还是职业进化新起点?
· 2分钟学会 DeepSeek API,竟然比官方更好用!
· .NET 使用 DeepSeek R1 开发智能 AI 客户端