D题有没有大佬看看我这个代码,为什么过不了

#include<bits/stdc++.h> using namespace std; using ll=long long; const int N=2e5+10; pair<ll,ll> A[N]; int main(){     int T;     cin>>T;     while(T--){         int n;         cin>>n;         for(int i=1;i<=n;i++){             cin>>A[i].first>>A[i].second;         }         sort(A+1,A+1+n);         ll maxn=A[1].second;         int flag=1;         for(int i=2;i<=n;i++){             ll l=A[i].first,r=A[i].second;             if(maxn<l) flag=0;             if(maxn>=r) flag=0;             else maxn=r;         }         if(flag==1) cout<<"Yes"<<endl;         else cout<<"No"<<endl;     }     return 0; }

全部评论
首先说明我是菜鸡,这是我的O(n)方法做 #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> PLL; const int N = 2e5 + 10;  void solve() {     int n;     cin >> n;          int p, f = 1;     for (int i = 1; i <= n; i ++ ) {         int l, r;         cin >> l >> r;         if (i == 1) p = r;         if (p < l || p > r) f = 0;     }     if (f) cout << "Yes\n";     else cout << "No\n"; } int main() {     ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);     int _ = 1;     cin >> _;     while (_ -- ) solve();     return 0; } 思路,从第一个区间到第n - 1个区间任取一个右端点,然后这个点必须在其他区间范围内,就A了。 反之,从第n个区间到第二个区间任取一个左端点,然后这个点必须在其他区间范围内。 两种方法都行
点赞 回复 分享
发布于 11-10 12:59 湖南

相关推荐

10-14 12:20
门头沟学院 Java
迷茫的大四🐶:摊牌了,我是25届的,你们也不招我
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务