【AtCoder Beginner Contest 128】 B - Guidebook の備忘録

解法のポイント

・構造体を用いて、[名前、点数、index(出力用)]のデータを保持
・sort()を使い、名前をアルファベット順かつ点数を降順に並び替え
・ソート後、その順で構造体内の index データを出力

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Shop {
 string name;
 int point;
 int index;
};

bool cmp(const Shop &a, const Shop &b)
{
   // 名前が一緒ならば point の大きい方を返す
   if (a.name == b.name)
   {
       return a.point > b.point;
   }
   
   return a.name < b.name;
}

int main()
{
   int n;
   cin >> n;

   vector<Shop> shop;

   for (int i = 1; i <= n; i++)
   {
       Shop s;

       cin >> s.name >> s.point;

       // 出力に使うための index を保存
       s.index = i;

       shop.push_back(s);
   }

   // 並び替え
   sort(shop.begin(), shop.end(), cmp);

   for (int i = 0; i < n; i++)
   {
       cout << shop[i].index << endl;
   }

   return 0;
}

閲覧ありがとうございます。 コンテンツをいいねと思ってくださった方にサポートいただけると大変嬉しいです!