Design T-Shirt
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.
#include<iostream> using namespace std; struct element{ int number; double satisfaction; }s[100], a[100], b; int adjust(element s[], int low, int high) { b = s[low]; double x = s[low].satisfaction; while(low < high){ while(low < high && s[high].satisfaction > x) high--; if(low<high) { s[low] = s[high]; low++; } while(low < high && s[low].satisfaction < x) low++; if(low<high) { s[high] = s[low]; high--; } } s[low] = b; return low; } void qsort(element s[], int l, int r) { if(l < r){ int i = adjust(s, l, r); qsort(s, l, i-1); qsort(s, i+1, r); } } int Adjust(element a[], int low, int high) { b = a[low]; int x = a[low].number; while(low < high){ while(low < high && a[high].number >= x) high--; if(low<high) { a[low] = a[high]; low++; } while(low < high && a[low].number <= x) low++; if(low<high) { a[high] = a[low]; high--; } } a[low] = b; return low; } void qqsort(element a[], int l, int r) { if(l < r){ int i = Adjust(a, l, r); qqsort(a, l, i-1); qqsort(a, i+1, r); } } int main(){ int n, m, k, i; while(cin>>n>>m>>k){ for(i=0; i<m; i++){ s[i].number = i+1; s[i].satisfaction = 0; } double y; for(i=0; i<n; i++) for(int j=0; j<m; j++) { cin>>y; s[j].satisfaction += y; } qsort(s, 0, m-1); int p=0; for(i=m-k; i<m; i++) a[p++] = s[i]; qqsort(a, 0, k-1); for(i=k-1; i>=0; i--) i==k-1? cout<<a[i].number : cout<<" "<<a[i].number; cout<<endl; } return 0; }