杭电 HDU 1031 Design T-Shirt

Design T-Shirt

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 6527    Accepted Submission(s): 3061

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

Author

CHEN, Yue

Source

CYJJ‘s Funny Contest #1, Killing in Seconds

气的我头疼,丫的蛋,虽然是传说中的简单题,题目一边看懂,解法明确,悲剧在好不容易写完后,超时,栈溢出……超时……溢出……wa将近三十次;最后用了结构,跟可悲的是也他妈超时,更可笑的是结构排序解法程序改为 c写法后 立马通过,虽说笨死 也不能这样欺负人 时间太多浪费了,但还是感觉学到不少东西;

看看这几种解法:

第一种:开三个数组:一个保存n个人的m个数据,一个保存m个求和 数据,另一个记录前k个支持度高的方案序号,并排完后降序输出;

第二种:和第一种基本一样 ,只是第三个数组不是用值来记录序号,而是用数组下标;

第三种:前两种,写着写着就发现序号和每个方案支持度总和是关联的,结构更方便。

一:

#include<iostream>
#include<algorithm>
#include<string.h>
const int M=100;
using namespace std;
bool cmp(int a,int b)
{
	return a>b;
}
int main()
{
	double ls[M][M];
	double gq[M];
	int flag[M];
	int n,m,k,i,j,T;
	while(scanf("%d%d%d",&n,&m,&k))
	{
		int x=0;
		memset(gq,0,sizeof(gq));
		memset(ls,0,sizeof(ls));
		memset(flag,0,sizeof(flag));
		for( i=0;i<n;i++)
			for(j=0;j<m;j++)
			{
				scanf("%lf",&ls[i][j]);
				gq[j]+=ls[i][j];
			}
			while(k--)
			{
				int max=-1;
				for(int t=0;t<m;t++)
				{
					if(gq[t]>max)
					{
						max=gq[t];
					    T =t;
					}
				}
					gq[T]=-1;
				flag[x++]=T+1;
			}
			sort(flag,flag+x,cmp);

			for(int p=0;p<x-1;p++)
				printf("%d ",flag[p]);
			printf("%d\n",flag[x-1]);
	}
	return 0;
}

第二:

#include<iostream>
#include<algorithm>
#include<string.h>

const int M=10000;
using namespace std;

int main()
{
	double ls[M][M];
	double gq[M];
	int flag[M];
	int n,m,k,i,j,T;
	while(scanf("%d%d%d",&n,&m,&k))
	{
		int x=0,I=k;
		memset(gq,0,sizeof(gq));
		memset(ls,0,sizeof(ls));
		memset(flag,0,sizeof(flag));
		for( i=0;i<n;i++)
			for(j=0;j<m;j++)
			{
				scanf("%lf",&ls[i][j]);
				gq[j]+=ls[i][j];
			}
			while(k--)
			{
				int max=-1;
				for(int t=0;t<m;t++)
				{
					if(gq[t]>max)
					{
						max=gq[t];
					    T =t;
					}
				}
					gq[T]=-1;
				flag[T]=1;
			}

			for(int p=m-1;p>=0;p--)

			{
				if(flag[p]==1)
				{
					x++;

				if(x==I)
                {
					printf("%d\n",p+1);

					break;
				}

				else

				printf("%d ",p+1);
				}
			}
	}
	return 0;
}

第三:AC代码:

#include<cmath>
#include<iostream>
using namespace std;
#include<algorithm>
#include<string.h>
const int N=10005;
struct ls
{
    int k;
    double sum;
}gq[N];

bool cmp1(ls a,ls b)
{

    return a.sum>b.sum;

}
bool cmp2(ls a,ls b)
{
    return a.k>b.k;
}

int main()
{
    int n,m,k,i;
    double re;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        for(i=0;i<m;i++)
        {
            gq[i].k=i;
            gq[i].sum=0.0;
        }
        for(int j=0;j<n;j++)
            for(int t=0;t<m;t++)
            {
                scanf("%lf",&re);
                gq[t].sum+=re;

            }

            sort(gq,gq+m,cmp1);
            sort(gq,gq+k,cmp2);
            for(int w=0;w<k-1;w++)
                printf("%d ",gq[w].k+1);
        printf("%d\n",gq[k-1].k+1);
    }
    return 0;
}

时间: 2024-10-12 23:36:53

杭电 HDU 1031 Design T-Shirt的相关文章

杭电 HDU 1164 Eddy&#39;s research I

Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7117    Accepted Submission(s): 4268 Problem Description Eddy's interest is very extensive, recently  he is interested in prime

杭电 HDU 1038 Biker&#39;s Trip Odometer

Biker's Trip Odometer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4745    Accepted Submission(s): 3144 Problem Description Most bicycle speedometers work by using a Hall Effect sensor faste

HDU 1031 Design T-Shirt 选前k大

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

杭电 HDU 1098 Ignatius&#39;s puzzle

Ignatius's puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7068    Accepted Submission(s): 4883 Problem Description Ignatius is poor at math,he falls across a puzzle problem,so he has no

杭电 HDU 1163 Eddy&#39;s digital Roots

Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4904    Accepted Submission(s): 2743 Problem Description The digital root of a positive integer is found by summing the digi

杭电hdu 4861 Couple doubi

杭电 2014多校联训第一场   1001   Couple doubi   逗比夫妇 这标题我就不多说什么了. 题意:有K个球在桌上,每个球都有价值,第i个球的价值是1^i+2^i+...+(p-1)^i (mod p).其中p是一个素数,之后逗比男先选球,最后所有球总分高的获胜.如果逗比男获胜,那么输出“YES”否则输出“NO”.(逗比男和逗比女都采取最有策略). 当然这也p是奇素数的一个重要公式.曾有题是这个公式求和.当然如果你知道就很简单了.如果不知道,就打表找规律吧. 根据这一重要的公

杭电 HDU 1037 Keep on Truckin&#39;

Keep on Truckin' Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9881    Accepted Submission(s): 6859 Problem Description Boudreaux and Thibodeaux are on the road again . . . "Boudreaux, we hav

杭电 HDU ACM 1397 Goldbach&#39;s Conjecture

Goldbach's Conjecture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4976    Accepted Submission(s): 1901 Problem Description Goldbach's Conjecture: For any even number n greater than or equal

杭电 HDU ACM 5186 zhx&#39;s submissions

zhx's submissions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1892    Accepted Submission(s): 507 Problem Description As one of the most powerful brushes, zhx submits a lot of code on many