zoukankan      html  css  js  c++  java
  • poj1089 Intervals 排序+贪心

      

    这提做的郁闷,开始马虎大意,wrong了n次,最后来了一个tle

    郁闷的是,将我自己写的快排换成qsort后,竟然AC了

    #include <stdio.h>
    #include<stdlib.h>
    struct s
    {
        int a;
        int b;
    }qj[100000];
    int cmp(const void *a,const void *b)
    {
     return (*(struct s *)a).a-(*(struct s *)b).a;
    }
    int main(int argc, char** argv) {
    
        int n,i,x,y,k;
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d %d",&qj[i].a,&qj[i].b);
        }
        qsort(qj,n,sizeof(struct s),cmp);
        i=0;
        x=qj[0].a;
        y=qj[0].b;
        while(i<n)
        {
            if(x==qj[i].a)
            {
                if(y<qj[i].b)
                    y=qj[i].b;
                
    
            }
            else if(y>=qj[i].a)
            {
                if(qj[i].b>y)
                y=qj[i].b;
                
            }
            else
            {
                printf("%d %d\n",x,y);
                
                x=qj[i].a;
                y=qj[i].b;
                
            }
            
            
                
            
            i++;
        }
    printf("%d %d\n",x,y);
    
        return 0;
    }
    

      以下是自己写的tle的快排,排序还是能排的,但不知为什么这么慢,今天休息下,有时间在慢慢研究

    int one(struct s a[],int s,int e)
    {
        struct s t,x=a[e];
        int i=s-1;
        int j;
        for(j=s;j<e;j++)
        {
            if(a[j].a<=x.a)
            {
                i++;
                t=a[j];
                a[j]=a[i];
                a[i]=t;
            }
        }
        t=a[i+1];
        a[i+1]=a[e];
        a[e]=t;
        return i+1;
    }
    void qsort(struct s a[],int s,int e)
    {
        int q;
        if(s<e)
        {
            q=one(a,s,e);
            qsort(a,q+1,e);
            qsort(a,s,q-1);
    
        }
    }
    

      

  • 相关阅读:
    hdu 1005(找循环节)
    hdu 1452(因子和+逆元)
    hdu 1215(因子和)
    hdu 1492(约数的个数)
    hdu 2136(质数筛选+整数分解)
    HDU 1286 找新朋友
    HDU 2136 Largest prime factor
    HDU 1722 Cake
    HDU 1713 相遇周期
    HDU 2138 How many prime numbers
  • 原文地址:https://www.cnblogs.com/fengyuehan/p/poj1089.html
Copyright © 2011-2022 走看看