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.
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
6 5 3 1 2 1
这题和上题类似,但这题简单多了,不用字符串输入。题意:有n个人选m件衣服(每件衣服都有一个评价),求总评价最高的前k件衣服的编号。 思路是:把每件衣服的评价加起来,并且编号,再用sort对总评价升序,再用sort对总评价前k的编号升序,然后直接输出即可。
#include<iostream> #include<algorithm> #include<cstdio> #include<string> using namespace std; struct node { double sum; int num; }man[1000]; bool cmp1(node x,node y)//对总评价升序 { return x.sum>y.sum; } bool cmp2(node x,node y)//对编号升序 { return x.num>y.num; } int main() { int n,m,k; int i,j; double temp;while(cin>>n>>m>>k) { memset(man,0,sizeof(man));//结构体初始化 for(i=0;i<n;i++) { for(j=0;j<m;j++) { cin>>temp; man[j].sum+=temp; man[j].num=j+1;//编号 } } sort(man,man+m,cmp1); sort(man,man+k,cmp2); for(i=0;i<k;i++) { if(i<k-1) { cout<<man[i].num<<" "; } else { cout<<man[i].num<<endl; } } } }