luogu1803 凌乱的yyy / 线段覆盖

https://www.luogu.com.cn/problem/P1803

调试出现了re 

while(cur>z[cnt].a&&cnt<n);

错写成了

while(cur>z[cnt].a);

导致了先越界了,外层的while还没有判断

#include<bits/stdc++.h>
using namespace std;

struct work
{
    int a;
    int b;
}z[2000010];


bool cmp(work x,work y)
{
    if(x.b!=y.b)
    return x.b<y.b;
    else return x.a > y.a ;
}
bool cmp1(work x,work y)
{
    return x.a>y.a;
}

int n;

int main()
{
    
    cin>>n;
    int ans=0;//参加总数 
    int cur=0;//现在 的时刻 
    int cnt=0;//次数 
    int temp=0;//结果总时间 
    for(int i=0;i<n;i++)
    {
        scanf("%d",&z[i].a);
        scanf("%d",&z[i].b);
    }
    sort(z,z+n,cmp1);
    temp=z[0].a;
    sort(z,z+n,cmp);
    while(temp>=cur)
    {
        cur=z[cnt].b;
        do
        {
            cnt++;
        }
        while(cur>z[cnt].a&&cnt<n);
        ans++;
    }
    printf("%d",ans);
    
    return 0;
    
 } 

猜你喜欢

转载自www.cnblogs.com/cyfe67373/p/12422506.html
yyy