【贪心】Doki Doki Literature Club

把权值高的单词放在前面

AC code

#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <cstring>
#include <string>
using namespace std;
typedef long long LL;

struct Str
{
  string str;
  int value;
}arr[120];

bool cmp(const Str &a, const Str &b) 
{
  if (a.value == b.value)
  {
    return a.str < b.str;
  } 
  else
  {
    return a.value > b.value;
  }
}

int main()
{
  int t = 0;
  cin >> t;
  while (t--)
  {
    LL n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
      cin >> arr[i].str >> arr[i].value;
    }
    sort(arr, arr + n, cmp);
    LL sum = 0;
    for (LL i = 0; i < m; i++)
    {
      sum += (m - i)*arr[i].value;
    }
    cout << sum<<" ";
    string out;
    for (int i = 0; i < m; i++)
    {
      out += arr[i].str+" ";
    }
    cout << out.substr(0,out.length()-1) << endl;
  }
  return 0;
}

注意最后不要多输出了空格

发表评论

电子邮件地址不会被公开。 必填项已用*标注

18 + 13 =