四个超时,为啥呀

P1803 凌乱的yyy / 线段覆盖

$n^2$ 肯定过不了,只要比较左边与最右边就行了。 ```cpp #include<iostream> #include<cstring> #include<string> #include<cstdio> #include<algorithm> #include<cmath> using namespace std; const int MAX = 1e6 + 10 ; struct node{ int left ; int right ; }no[MAX]; bool cmp ( node a , node b ){ return a.right < b.right ; } int main() { int n; int ans = 0 , the_rightest = -1 ; scanf( "%d" , &n ); for (int i = 1 ; i <= n ; i ++){ scanf( "%d %d" , &no[i].left , &no[i].right ); } sort (no + 1 , no+ n + 1 , cmp); for (int i = 1 ; i <= n ; i++){ if( no[i].left < the_rightest ) continue; ans++ ; the_rightest = no[i].right ; } cout << ans ; return 0; } ```
by lhz123bc @ 2024-03-24 17:32:29


|