链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4035
题意:给n个字符串,给个字符串对应一个值,要求从中选出m个字符串,使得(m-i+1)*字符串对应的值相加起来最大
思路:存在结构体之后进行值得从大到小排序,如果值相等就按照字符串的字典序进行排序
代码:
#include <bits/stdc++.h> using namespace std; struct node { string s; long long t; }po[110]; bool cmp(node aa,node bb) { if(aa.t!=bb.t) return aa.t>bb.t; return aa.s<bb.s; } int main() { int T; scanf("%d",&T); while(T--) { int n,m; scanf("%d%d",&n,&m); for(int i=0;i<n;i++) cin >> po[i].s >> po[i].t; sort(po,po+n,cmp); long long ans=0; for(int i=0;i<m;i++) ans+=(po[i].t*(m-i)); cout << ans; for(int i=0;i<m;i++) cout << ' ' << po[i].s; cout << endl; } return 0; }