zoukankan      html  css  js  c++  java
  • hdu_1031_结构体排序

    题目很好理解,将列求和,取前k大的

    我的代码思路:对列求和,后取出前k大的id加入结果数组,对比后面和第k大相同的评分id也加入到结果数组,最后对结果数组排序

    代码:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 const int Max = 200009;
     6 struct Node{
     7     double score;
     8     int id;
     9     bool operator < (const Node o) const{
    10         return score>o.score;
    11     }
    12 }node[Max];
    13 int ans[Max];
    14 int main()
    15 {
    16     int n,m,k;
    17     while(~scanf("%d%d%d",&n,&m,&k))
    18     {
    19         double tm;
    20         for(int i = 0; i < m; i++){
    21             node[i].id = i;
    22             node[i].score = 0;
    23         }
    24         for(int i = 0; i < n; i++){
    25             for(int j = 0; j < m; j++){
    26                 scanf("%lf",&tm);
    27                 node[j].score+=tm;
    28             }
    29         }
    30         sort(node,node+m);
    31         int cnt = 0;
    32         for(int i = 0; i < k; i++){
    33             ans[cnt++] = node[i].id+1;
    34         }
    35         int top = k;
    36         while(node[top].score==node[top-1].score){
    37             ans[cnt++] = node[top].id;
    38             top++;
    39         }
    40         sort(ans,ans+cnt);
    41         for(int i = k-1; i > 0; i--){
    42             printf("%d ",ans[i]);
    43         }
    44         printf("%d
    ",ans[0]);
    45     }
    46     return 0;
    47 }

    (有一种wa的痛叫忘记数组初始化)

    下面是看了大神的代码,原来自己想复杂了,直接对结构体进行两次排序即可

    get一种新方法,记录代码如下:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 const int Max = 200007;
     6 struct Node{
     7     double score;
     8     int id;
     9 }node[Max];
    10 bool cmpByScore(Node a, Node b){
    11     return a.score>b.score;
    12 }
    13 bool cmpById(Node a, Node b){
    14     return a.id>b.id;
    15 }
    16 int main()
    17 {
    18     int n,m,k;
    19     while(~scanf("%d%d%d",&n,&m,&k))
    20     {
    21         double tm;
    22         for(int i = 0; i < m; i++){
    23             node[i].id = i+1;
    24             node[i].score = 0;
    25         }
    26         for(int i = 0; i < n; i++){
    27             for(int j = 0; j < m; j++){
    28                 scanf("%lf",&tm);
    29                 node[j].score+=tm;
    30             }
    31         }
    32         sort(node,node+m,cmpByScore);
    33         sort(node,node+k,cmpById);
    34         for(int i = 0; i < k-1; i++){
    35             printf("%d ",node[i].id);
    36         }
    37         printf("%d
    ",node[k-1].id);
    38     }
    39     return 0;
    40 }
  • 相关阅读:
    使用SVG symbols建立图标系统完整指南
    ural 1874 Football Goal
    ural 1572 Yekaterinozavodsk Great Well
    ural 1084 Goat in the Garden
    ural 1192 Ball in a Dream
    ural 1020 Rope
    ural 1494 Monobilliards
    ural 1671 Anansi's Cobweb
    ural 1613 For Fans of Statistics
    ural 1126 Magnetic Storms
  • 原文地址:https://www.cnblogs.com/shanyr/p/7216343.html
Copyright © 2011-2022 走看看