题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1031
题目大意:设计衣服。询问N个人对M个配饰的满意度,选择其中K个满意度最高的,降序输出序号。(若有满意度相同而名额不够,选择序号小的)
关键思想:在节省空间的前提下排好序。经典的序号排序思想
代码如下:
//给序号排序典例
#include <iostream>
#include <algorithm>
#include <map>
#include <memory.h>
#include <vector>
using namespace std;
int N,M,K;
double t,temp[10010];//temp[i]为第i件物品的欢迎度
int ans[10010];
bool cmp1(int a,int b){
return temp[a]>temp[b];//ans里为备选的编号,这个cmp配合sort可以让ans里的序号按权重排序。
}
bool cmp2(int a,int b){
return a>b;
}
int main(){
while(~scanf("%d%d%d",&N,&M,&K)){
memset(temp,0,sizeof(temp));
for(int i=0;i<=M;i++)ans[i]=i;
for(int i=0;i<N;i++){
for(int j=1;j<=M;j++){
scanf("%lf",&t);
temp[j]+=t;
}
}
sort(ans+1,ans+M+1,cmp1);//M件的序号按欢迎程度排序
sort(ans+1,ans+K+1,cmp2);//将前K件受欢迎的序号大小排序
for(int i=1;i<=K;i++)
i==1?printf("%d",ans[i]):printf(" %d",ans[i]);//按格式输出
printf("\n");
}
return 0;
}