NYOJ-14 会场安排问题(贪心 区间覆盖)

    xiaoxiao2021-03-25  58

    #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #define MAX 10000+1 #define IN 100000 using namespace std; struct node{ int left,right; }; bool cmp_right(struct node a, struct node b){ return a.right<b.right; } int main(){ int T,n; int now_len,count,min,flag; struct node a[MAX]; cin>>T; while(T--){ cin>>n; for(int i=0;i<n;i++){ cin>>a[i].left>>a[i].right; } sort(a,a+n,cmp_right); now_len=0; count=0; flag=1; int k=0; while(1){ min=IN; for(int j=0;j<n;j++){ if(a[j].left>now_len){ if((a[j].right-now_len)< min) { min=a[j].right-now_len; } } } if(min==IN){ break; } else{ count++; now_len+=min; } } cout<<count<<endl; } return 0; }千万不要写成for(int j=0;j<n&&a[j].left>now_len;j++) 因为for循环的第二部分不满足的话就直接退出循环而不是j自增再循环一遍,被坑了好久
    转载请注明原文地址: https://ju.6miu.com/read-37559.html

    最新回复(0)