会场安排问题(贪心算法)

#include <iostream>
#include <string>
#include <malloc.h>
using namespace std;
typedef struct plan
{
    int startime;
    int endtime;
    struct plan *next;
}sqlist;
void insqlist(sqlist **top,int s,int e)
{
    sqlist *p,*tmp=*top,*q;
    p=(sqlist *)malloc(sizeof(sqlist));
    p->startime=s;
    p->endtime=e;
    if(tmp==NULL)
    {
        *top=p;//修改头指针所指地址
        p->next=NULL;
    }
    else
    {
        for(q=tmp;q!=NULL;q=q->next)
        {
            if(q->endtime>= e)
            {
                p->next=q;
                *top=p;
                break;
            }
            if(q->endtime<e&&q->next!=NULL)
            {
                if(q->endtime<e&&q->next->endtime>=e)
                {
                    p->next=q->next;
                    q->next=p;
                    break;
                }
            }
            else
            {
                q->next=p;
                p->next=NULL;
                break;
            }
        }
    }
}
//销毁链表
int  destroysqlist(sqlist **p)
{   sqlist *tmp=*p,*tmptmp=NULL;
    if(tmp==NULL)
        return 0;
    while(tmp!=NULL)
    {
        tmptmp=tmp->next;
        free(tmp);
        tmp=tmptmp;
    }
    return 0;
}
int main()
{
    int num=0,onetotall=0,begin=0,end=0,total;
    cin>>num;
    while(num--)
    {   sqlist *top=NULL,*tmp=NULL;
        cin>>onetotall;
        for(int i=0;i<onetotall;i++)
        {
            cin>>begin;cin>>end;
            insqlist(&top,begin,end);
        }
//         tmp=top;
//        while(tmp!=NULL)
//        {
//            cout<<tmp->endtime<<endl;
//            tmp=tmp->next;
//        }
        tmp=top;
        total=1;
        int tmpb=tmp->startime,tmpe=tmp->endtime;
        tmp=tmp->next;
        while(tmp!=NULL)
        {
           if(tmp->startime>=(tmpe+1))
           {
               total++;
               tmpb=tmp->startime;
               tmpe=tmp->endtime;
           }
           tmp=tmp->next;
        }
        cout<<total<<endl;
        destroysqlist(&top);
        tmpb=0;tmpe=0;
    }
    return 0;
}

优化后的代码:

#include <iostream>
#include <string>
#include <malloc.h>
using namespace std;
typedef struct plan
{
    int startime;
    int endtime;
    struct plan *next;
}sqlist;
void insqlist(sqlist **top,int s,int e)
{
    sqlist *p,*tmp=*top,*q;
    p=(sqlist *)malloc(sizeof(sqlist));
    p->startime=s;
    p->endtime=e;
    if(tmp==NULL)
    {
        *top=p;
        p->next=NULL;
    }
    else
    {
        for(q=tmp;q!=NULL;q=q->next)
        {
            if(q->endtime>= e)
            {
                p->next=q;
                *top=p;
                break;
            }
            else
            {
                if(q->next!=NULL)
                {
                   if(q->endtime<=e&&q->next->endtime>=e)
                   {
                       p->next=q->next;
                       q->next=p;
                       break;
                   }
                }
                else{
                    q->next=p;
                    p->next=NULL;
                    break;
                }
            }
        }
    }
}

int main()
{
    int num=0,onetotall=0,begin=0,end=0,total;
    cin>>num;
    while(num--)
    {
        sqlist *top=NULL,*tmp=NULL;
        cin>>onetotall;
        for(int i=0;i<onetotall;i++)
        {
            cin>>begin;
            cin>>end;
            insqlist(&top,begin,end);
        }
        tmp=top;
        total=1;
        int tmpb=tmp->startime,tmpe=tmp->endtime;
        tmp=tmp->next;
        while(tmp!=NULL)
        {
           if(tmp->startime>=(tmpe+1))
           {
               total++;
               tmpb=tmp->startime;
               tmpe=tmp->endtime;
           }
           free(tmp);
           tmp=tmp->next;
        }
        cout<<total<<endl;
    }
    return 0;
}

原题信息

    原文作者:贪心算法
    原文地址: https://blog.csdn.net/somestill/article/details/8648256
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞