当前位置:编程学习 > C/C++ >>

(STL自带的排序功能7.1.3)POJ 2379 ACM Rank Table(结构体的排序、初始化)

 
#include <iostream>  
#include <cstdio>  
#include <algorithm>  
  
  
using namespace std;  
  
const int maxn = 1010;  
  
struct judge{  
    int c;  
    int p;  
    int r;  
    int t;  
}a[maxn];  
  
struct team{  
    int id;  
    int ac;  
    int t;  
    int p[25];  
    int sol[25];  
}t[maxn];  
  
bool com_t(const  judge& a,const  judge& b){  
    return a.t < b.t;  
}  
  
bool com_ac(const team& a , const team& b){  
    if(a.ac != b.ac){  
        return a.ac > b.ac;  
    }  
  
    if(a.t != b.t){  
        return a.t < b.t;  
    }  
  
    return a.id < b.id;  
}  
  
  
int main(){  
    int n,m;  
    while(scanf("%d%d",&n,&m)!=EOF){  
        memset(a,0,sizeof(a));//注意,结构体的初始化  
        memset(t,0,sizeof(t));  
  
        int i;  
        for(i = 1 ; i <= n ; ++i){  
            t[i].id = i;  
        }  
  
        for(i = 1 ; i <= m ; ++i){  
            scanf("%d%d%d%d",&a[i].c,&a[i].p,&a[i].t,&a[i].r);  
        }  
  
        sort(a+1,a+1+m,com_t);  
  
        for(i = 1 ; i <= m ; ++i){  
            int x = a[i].c;  
            int y = a[i].p;  
  
            if(t[x].sol[y]){//如果这道题已经解决了,则进行下一次处理  
                continue;  
            }  
  
            if(a[i].r){  
                t[x].ac++;  
                t[x].t += t[x].p[y]*1200 + a[i].t;  
                t[x].sol[y] = true;  
            }else{  
                t[x].p[y]++;  
            }  
        }  
        sort(t+1,t+1+n,com_ac);//注意这种写法,还有一种写法就是sort(t+1,t+1+n),这是需要在结构体里面重载<运算符  
  
        for(i = 1 ; i < n ; ++i){  
            printf("%d ",t[i].id);  
        }  
        printf("%d\n",t[n].id);  
//      for(i = 1 ; i <= n ; ++i){ 这种写法比较耗时  
//          printf((i<n)?"%d ":"%d\n",t[i].id);  
//      }  
    }  
  
    return 0;  
}  

 

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,