HDU 1031.Design T-Shirt【结构体二次排序】【8月21】

Design T-Shirt

Problem Description

Soon after he decided to design a T-shirt for our Algorithm Board on Free-City BBS, XKA found that he was trapped by all kinds of suggestions from everyone on the board. It is indeed a mission-impossible to have everybody perfectly satisfied. So he took a poll
to collect people‘s opinions. Here are what he obtained: N people voted for M design elements (such as the ACM-ICPC logo, big names in computer science, well-known graphs, etc.). Everyone assigned each element a number of satisfaction. However, XKA can only
put K (<=M) elements into his design. He needs you to pick for him the K elements such that the total number of satisfaction is maximized.

Input

The input consists of multiple test cases. For each case, the first line contains three positive integers N, M and K where N is the number of people, M is the number of design elements, and K is the number of elements XKA will put into his design. Then N lines
follow, each contains M numbers. The j-th number in the i-th line represents the i-th person‘s satisfaction on the j-th element.

Output

For each test case, print in one line the indices of the K elements you would suggest XKA to take into consideration so that the total number of satisfaction is maximized. If there are more than one solutions, you must output the one with minimal indices. The
indices start from 1 and must be printed in non-increasing order. There must be exactly one space between two adjacent indices, and no extra space at the end of the line.

Sample Input

3 6 4
2 2.5 5 1 3 4
5 1 3.5 2 2 2
1 1 1 1 1 10
3 3 2
1 2 3
2 3 1
3 1 2

Sample Output

6 5 3 1
2 1

n个人,m件衣服,输出评价最高的前k件,输出时,评价相同的标号大的先输出。一定要二次排,因为可能全部的评价一样高,只排一次输出就会有错误。

代码如下:

#include<cstdio>
#include<algorithm>
using namespace std;
struct ss{
    double v;
    int x;
};
bool cmp1(ss x,ss y){
    if(x.v>y.v) return true;
    else return false;
}
bool cmp2(ss x,ss y){
    if(x.x>y.x) return true;
    else return false;
}
int main(){
    int n,m,k;
    double t;
    ss f[1010];
    while(scanf("%d%d%d",&n,&m,&k)==3){
        for(int i=0;i<m;i++)
            f[i].v=0;
        while(n--)
        for(int i=0;i<m;i++){
            scanf("%lf",&t);
            f[i].v+=t;
            f[i].x=i+1;
        }
        sort(f,f+m,cmp1);
        sort(f,f+k,cmp2);//二次排,只排前k项
        for(int i=0;i<k;i++){
            if(i!=0) printf(" ");
            printf("%d",f[i].x);
        }
        printf("\n");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-24 13:50:55

HDU 1031.Design T-Shirt【结构体二次排序】【8月21】的相关文章

HDU 1031 Design T-Shirt 选前k大

相当于给出一组数列,然后选择前K大的数的算法. 本题没有给出详细的数据,故此就使用动态分配空间的方法了. 而这种题最好的算法就是使用快排思想,期望时间效率就是O(n)了. 最基本入门解决这种题的算法是直接排序了.那就成了水代码了.用上快排的思想才能体现出水平. 不过这种快排实在考的太多了,建议一定要掌握. 每次做这个算法的题目总会要调试一定时间的,每次都出现奇葩的错误.看来还是不够细心. 做题的时候一定要排除杂念,有干扰,后果很严重,会花长很多时间. 靖空间做题,一定要静,达到一种禅的境界.说禅

sort+结构体实现二级排序

之前介绍的sort函数由于其效率较高,使用较为简单让我用起来那叫一个爽,今天再写一篇使用sort+结构体实现二级排序的方法. 还是先想个问题吧,比如我想输入5个同学的名字和身高,然后得到他们身高的降序,但是如果出现相同身高的情况,名字的拼音靠前的排在前面. 好,现在这个问题已经涉及到了二级排序,要按照身高的降序和姓名的升序排列,那么就要先定义一个结构体,将姓名和身高都包含进去,然后用sort对结构体排序,而实现二级排序,关键在于自己写的cmp函数(sort的比较方法) 1 #include<io

4.19 使用qsort对结构体数组进行排序,实现对结构的体一级排序和二级排序,进一步了解qsort的原理

qsort对结构体数组进行排序时,可以根据结构体元素中的任意某个成员进行比较之后,如果要交换则会连带结构体中其他成员的一起进行整体的结构体元素交换所以感觉真是万能排序接口 只进行对结构体的一级排序 #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct _stu { char name[10]; float score; }Stu; int callBackCompare(con

HDU 2409 Team Arrangement (结构体排序)

题目链接:HDU 2409 Team Arrangement 题意:给出22个人(编号,名字,踢的位置,在队里的时间),让我们选DD-MM-SS的阵型+一个守门员.在选出队长(时间在最久的就是队长,时间相同编号大为队长),选人的顺序是从编号小的开始. 结构体排序就好了,注意出输出是按队长,D,M,S的顺序,选队长记录队长的编号(而不是下标,我的代码之后还要排序). AC代码: #include<stdio.h> #include<string.h> #include<algo

hdu 5702 Solving Order(结构体排序 水题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5702 Solving Order Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 184    Accepted Submission(s): 135 Problem Description Welcome to HDU to take p

c动态分配结构体二维数组

这个问题我纠结了蛮久了,因为前面一直忙(自己也懒了点),所以没有能好好研究这个.希望这篇文章能够帮助你们. 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <stddef.h> 4 5 typedef struct LNode { 6 int F; 7 struct LNode* next; 8 }LNode, *LinkList; 9 int main() 10 { 11 LNode** map = (LNo

HDU 1031 Design T-Shirt

题意:讲的是XKA要设计T-Shirt,征集大众对各元素的满意度.分别输入3个整数,分别给N.M.K,其中N代表参与打分的人数,M代表元素总数量,K代表XKA所要选用的元素数量.XKA将选用分值较高的前K个元素,若分值相同则选择索引小的的元素.思路:记录每个M的总分数和索引,用结构体存起来,两次排序就够了 另外要注意的输出格式,不能有多余的空格. 1 #include<iostream> 2 #include<algorithm> 3 #include<cmath> 4

hdu 1069 Monkey and Banana (结构体排序,也属于简单的dp)

Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7770    Accepted Submission(s): 4003 Problem Description A group of researchers are designing an experiment to test the IQ of a

结构体、枚举类型——8月3日

一.结构体.美剧类型都是属于值类型 (一)结构体 就是一个自定义集合,里面可以放各种类型的元素,用法大体跟集合一样. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections;//下面要用到ArrayList集合,就要先引用这个 namespace 结构体_枚举类型 { cl