题意:给定你n个哑铃,每个哑铃都有质量和价格两个属性,要你把它分为k个一组;分组要求为,每组哑铃数相同,同一组内哑铃质量不同,不同组内哑铃质量必须一一相同,问你最多能分成多少组(如果有 组数 相同的组合,输出总花费最大的那个方案)

解题思路:用结构体数组 list 存储哑铃,然后在哑铃质量的基础上对花费进行从大到小的排序(把哑铃按照质量在数组中分为不同的堆),然后用  因为哑铃的质量比较小,所以我们用   hash 数组(结构体   包含这个值第一次在lish出现的位置   和 它的个数   )对不同质量的进行统计, 然后对 hash 中的 个数 进行排序,  就能知道可以得出几组,然后在  通过  hash 对list 数组进行计算,,就能得出最大的花费是多少

解题代码:

  1 // File Name: h.c
  2 // Author: darkdream
  3 // Created Time: 2013年07月18日 星期四 16时51分06秒
  4 
  5 #include<stdio.h>
  6 #include<string.h>
  7 #include<stdlib.h>
  8 #include<time.h>
  9 #include<math.h>
 10 #include<ctype.h>
 11 struct node
 12 {
 13     int m,c;
 14 }list[4005];
 15 struct Hash
 16 {
 17     int sit,num;
 18 }hash[4005];
 19 int ans[4005];
 20 int nodecmp(const void *b ,const void * a)
 21 {
 22     if( (*(node*)a).m != (*(node *)b).m)
 23         return (*(node *)a).m - (*(node *) b).m;
 24     else return (*(node*)a).c - (*(node *)b).c;
 25 }
 26 int intcmp(const void *a,const void *b)
 27 {
 28     return *(int *)b -*(int *)a;
 29 }
 30 int hashcmp(const void *a ,const void *b)
 31 {
 32     return (*(Hash *)b).num - (*(Hash *)a).num;
 33 }
 34 int main(){
 35 
 36     //freopen("/home/plac/problem/input.txt","r",stdin);
 37     //freopen("/home/plac/problem/output.txt","w",stdout);
 38     int n ,k ; 
 39     while(scanf("%d %d",&n,&k)!= EOF)
 40     {
 41         memset(list,0,sizeof(list));
 42         memset(hash,0,sizeof(hash));
 43         memset(ans,0,sizeof(ans));
 44         for(int i = 1;i <= n;i ++)
 45         {
 46             scanf("%d %d",&list[i].m,&list[i].c);
 47         }
 48 
 49 
 50         qsort(list+1,n,sizeof(node),nodecmp);
 51         for(int i= 1;i <= n; i ++)
 52         {
 53             if(list[i].m != list[i-1].m)
 54             {
 55                 hash[list[i].m].sit = i ; 
 56             }
 57             hash[list[i].m].num ++;
 58         }
 59         /* for(int i = 1;i <= n;i ++)
 60            {
 61            printf("****%d %d\n",list[i].m,list[i].c);
 62            }*/
 63 
 64         qsort(hash+1,4000,sizeof(Hash),hashcmp);
 65         int up = 0 ;
 66         int ok = 1 ;
 67         /*for(int i= 1;i <= 7 ;i ++)
 68           printf("%d ",hash[i].num);
 69           printf("\n"); */
 70 
 71         if(hash[k].num == 0)
 72             ok = 0 ;
 73 
 74         if(ok)
 75         {
 76             int pre = 0 ;
 77             for(up = k; up <= 4000 ;  )
 78             {
 79                 if(hash[up+1].num == hash[up].num)
 80                 {
 81                     up++;
 82                 }
 83                 else break;
 84             }
 85               
 86             for(int i = 1; i <= up;i ++)
 87             {
 88                 pre ++;
 89                 ans[pre] = 0 ; 
 90                 for(int j = hash[i].sit; j <= hash[i].sit + hash[k].num -1 ; j++)
 91                 {
 92                     ans[pre] += list[j].c;
 93                 }
 94                 //printf("????????%d\n",ans[pre]);
 95             }
 96             qsort(ans+1,pre,sizeof(int),intcmp);
 97             int sum = 0 ;
 98             for(int i= 1;i <= k ;i ++)
 99                 sum += ans[i];
100             printf("%d %d\n",hash[k].num,sum);
101 
102         }
103         else 
104             printf("0 0\n");
105     }
106 
107     return 0 ;
108 }
View Code

 

posted on 2013-07-18 19:17  dark_dream  阅读(269)  评论(0编辑  收藏  举报