在一行上输入一个整数
,表示志愿者数量。
此后
行,第
行输入两个整数
,表示第
名志愿者浇水的树苗编号区间。
输出一行一个整数,表示被浇水次数最多的树苗被浇水的次数。
4 0 2 2 4 1 4 6 7
3
在该样例中:
区间
覆盖树苗
;
区间
覆盖树苗
;
区间
覆盖树苗
;
区间
覆盖树苗
。
最终树苗
被浇水
次,为最大值,故输出
。
4 1000000 1000000 1000000 1000000 0 1000000 1 1000000
4
在该样例中,编号为
的树苗被四名志愿者同时浇水,故答案为
。
#include <ios> #include <iostream> #include <vector> using namespace std; const int N=1e6+5; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,l,r; cin>>n; int max_num=-1; vector<int> cf(N,0); while(n--){ cin>>l>>r; cf[l]++; cf[r+1 ]--; max_num=max(r,max_num); } int a=cf[0]; int ans=a; for(int i=1;i<=max_num;i++){ a=a+cf[i]; ans=max(ans,a); } cout<<ans; return 0; } // 64 位输出请用 printf("%lld")