10分求助

P1803 凌乱的yyy / 线段覆盖

``` for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ if(comp[i].end<=comp[j].beg){ ans++; break; } } } ``` 哥们你这一段是在干嘛?你用sort排序之后,从头开始遍历即可, ,这一段改成 ``` for(int i=0; i<n; i++) { if(comp[i].beg<last) continue; ans++; last=comp[i].end; } ``` last为我们每次比完一场赛后,该赛的结束时间,如果下一场比赛的开始时间比我们这场比赛的结束时间晚,也就是比他大,说明可以参加,如果小,我们就考虑下一场能不能参加就行了。 还有cmp函数改一下, ``` bool cmp(com a,com b) { return a.end<b.end; } ``` 不需要if 完整AC代码``` #include<bits/stdc++.h> #define N 1000000 using namespace std; struct com { int beg,end; } comp[N]; bool cmp(com a,com b) { return a.end<b.end; } int main() { int n,ans,last; cin>>n; for(int i=0; i<n; i++) { cin>>comp[i].beg>>comp[i].end; } sort(comp,comp+n,cmp); for(int i=0; i<n; i++) { if(comp[i].beg<last) continue; ans++; last=comp[i].end; } cout<<ans; return 0; } ```
by Lazy_Durant @ 2023-11-05 10:19:56


``` #include<bits/stdc++.h> #define N 1000000 using namespace std; struct com { int beg,end; } comp[N]; bool cmp(com a,com b) { return a.end<b.end; } int main() { int n,ans,last; cin>>n; for(int i=0; i<n; i++) { cin>>comp[i].beg>>comp[i].end; } sort(comp,comp+n,cmp); for(int i=0; i<n; i++) { if(comp[i].beg<last) continue; ans++; last=comp[i].end; } cout<<ans; return 0; } ``` 完整AC @[Lazy_Durant](/user/1138579)
by Lazy_Durant @ 2023-11-05 10:21:33


|