首页 > 编程语言 > 详细

结构体排序

时间:2017-11-06 13:34:42      阅读:379      评论:0      收藏:0      [点我收藏+]

重写排序方法

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;

const int N=1e6+5;
const int INF=0x7fffffff;

int dp[N];
int n;

struct Road
{
    int B;
    int E;
}road[N];

int cmp( const void *a , const void *b )
{
    struct Road *c = (Road *)a;
    struct Road *d = (Road *)b;
    return c->B > d->B;
}

 

int solve()
{
    fill(dp,dp+n,INF);
    /*
    Road t;
    int Min;
    for(int i=0;i<=n-2;i++)
    {
        Min=i;
        for(int j=i+1;j<n;j++)
        {
            if(road[Min].B>road[j].B)
            {
                Min=j;
            }
        }
        if(Min!=i)
        {
            t=road[i];
            road[i]=road[Min];
            road[Min]=t;
        }
    }
    */
    qsort(road,n,sizeof(road[0]),cmp);
    /*
    for(int i=0;i<n;i++)
        printf("%d ",road[i].B);
        */
    for(int i=0;i<n;i++)
    {
        *lower_bound(dp,dp+n,road[i].E)=road[i].E;
    }
    int ans=lower_bound(dp,dp+n,INF)-dp;
    return ans;
}

int main()
{
    int cas=0;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
        {
            cin>>road[i].B>>road[i].E;
        }
        int res=solve();
        if(res==1)
            printf("Case %d:\nMy king, at most %d road can be built.\n",++cas,res);
        else
            printf("Case %d:\nMy king, at most %d roads can be built.\n",++cas,res);
    }
    return 0;
}

结构体排序

原文:http://www.cnblogs.com/unknownname/p/7792642.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!