nyoj 会场安排问题

Sort函数:
sort函数的头文件
#include <algorithm>
using namespace std;
bool cmp(int a, int b)
{
    //return a<b; 升序排列,(理解为先排小的,后排大的)
    //假设改为return a>b。则为降序(理解为先排大的,后排小的)
    return a > b;
}
一、对int类型数组排序 
 
int num[100]; 
 
Sample: 
 
int cmp ( const void *a , const void *b ) 

return *(int *)a - *(int *)b; 

 
qsort(num,100,sizeof(num[0]),cmp); 
 
二、对char类型数组排序(同int类型) 
 
char word[100]; 
 
Sample: 
 
int cmp( const void *a , const void *b ) 

return *(char *)a - *(int *)b; 

 
qsort(word,100,sizeof(word[0]),cmp); 
 
三、对double类型数组排序(特别要注意) 
 
double in[100]; 
 
int cmp( const void *a , const void *b ) 

return *(double *)a > *(double *)b ?

1 : -1; 

 
qsort(in,100,sizeof(in[0]),cmp); 
 
四、对结构体一级排序 
 
struct In 

double data; 
int other; 
}s[100] 
 
//依照data的值从小到大将结构体排序,关于结构体内的排序重要数据data的类型能够非常多种,參考上面的样例写 
 
int cmp( const void *a ,const void *b) 

return ((In *)a)->data - ((In *)b)->data ; 

 
qsort(s,100,sizeof(s[0]),cmp); 
 
五、对结构体
 
struct In 

int x; 
int y; 
}s[100]; 
 
//依照x从小到大排序。当x相等时依照y从大到小排序 
 
int cmp( const void *a , const void *b ) 

struct In *c = (In *)a; 
struct In *d = (In *)b; 
if(c->x != d->x) return c->x - d->x; 
else return d->y - c->y; 

 
qsort(s,100,sizeof(s[0]),cmp); 
 
六、对字符串进行排序 
 
struct In 

int data; 
char str[100]; 
}s[100]; 
 
//依照结构体中字符串str的字典顺序排序 
 
int cmp ( const void *a , const void *b ) 

return strcmp( ((In *)a)->str , ((In *)b)->str ); 

 
qsort(s,100,sizeof(s[0]),cmp); 
 
七、计算几何中求凸包的cmp 
 
int cmp(const void *a,const void *b) //重点cmp函数,把除了1点外的全部点。旋转角度排序 

struct point *c=(point *)a; 
struct point *d=(point *)b; 
if( calc(*c,*d,p[1]) < 0) return 1; 
else if( !calc(*c,*d,p[1]) && dis(c->x,c->y,p[1].x,p[1].y) < dis(d->x,d->y,p[1].x,p[1].y)) //假设在一条直线上,则把远的放在前面 
return 1; 
else return -1; 
}
 //从大到小排序


/*会场安排问题
时间限制:3000 ms  |  内存限制:65535 KB
难度:4
描写叙述 学校的小礼堂每天都会有很多活动,有时间这些活动的计划时间会发生冲突。
须要选择出一些活动进行举办。小刘的工作就是安排学校小礼堂的活动。
每一个时间最多安排一个活动。如今小刘有一些活动计划的时间表,他想尽可能的安排很多其它的活动,
请问他该怎样安排。

输入第一行是一个整型数m(m<100)表示共同拥有m组測试数据。
每组測试数据的第一行是一个整数n(1<n<10000)表示该測试数据共同拥有n个活动。
随后的n行,每行有两个正整数Bi,Ei(0<=Bi,Ei<10000),
分别表示第i个活动的起始与结束时间(Bi<=Ei)
输出对于每一组输入。输出最多可以安排的活动数量。
每组的输出占一行例子输入2
2
1 10
10 11
3
1 10
10 11
11 20
例子输出1
2*/

<span style="font-size:18px;">#include<stdio.h>  
#include<algorithm>  
const int MAX=10001;  
using namespace std; //用SORT函数所要的头文件 
struct time  
{  
    int start;  
    int end;  
}; //结构体 
bool order_by_end(struct time a,struct time b)  
{  
    if(a.end!=b.end)  
        return a.end<b.end;  
    else  
        return a.start<b.start;  
}//sort函数的定义。  
int main()  
{  
    int i,x,m,end,count;  
    struct time a[MAX];  
    scanf("%d",&x);  
    while(x--){  
        count=1;  
        scanf("%d",&m);  
        for(i=0;i<m;i++)  
            scanf("%d%d",&a[i].start,&a[i].end);  
        sort(a,a+m,order_by_end);  
        end=a[0].end;  
        for(i=1;i<m;i++)  
            if(a[i].start>end){  
                end=a[i].end;  
                count++;  
            }  
        printf("%d\n",count);  
    }  
}  </span>





posted on 2017-07-03 13:10  yjbjingcha  阅读(125)  评论(0编辑  收藏  举报

导航