結果

問題 No.775 tatyamと素数大富豪(hard)
ユーザー ei1333333ei1333333
提出日時 2018-12-22 00:52:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,183 bytes
コンパイル時間 3,424 ms
コンパイル使用メモリ 221,260 KB
実行使用メモリ 189,032 KB
最終ジャッジ日時 2024-04-22 02:44:22
合計ジャッジ時間 5,998 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

int N, K;

int main() {
  cin >> N >> K;
  int Z = K;
  vector< string > P(N);
  for(int i = 0; i < N; i++) {
    cin >> P[i];
  }

  vector< int > ord(N);
  iota(begin(ord), end(ord), 0);
  sort(begin(ord), end(ord), [&](const int &a, const int &b) {
    if(P[a] + P[b] == P[b] + P[a]) return P[a].size() > P[b].size();
    return P[a] + P[b] > P[b] + P[a];
  });

  string concat;
  for(auto &p : ord) concat += P[p];
  priority_queue< pair< string, vector< int > > > que;
  que.emplace(concat, ord);
  set< string > memo;
  memo.emplace(concat);
  K *= 2;
  while(que.size() && K > 0) {
    auto p = que.top().second;

    cout << que.top().first << endl;
    --K;

    que.pop();

    for(int i = 0; i < ord.size(); i++) {
      for(int j = i - 1; j >= 0; j--) {
        if(P[p[i]] + P[p[j]] >= P[p[j]] + P[p[i]]) continue;
        swap(p[i], p[j]);
        string con;
        for(auto &q : p) con += P[q];
        if(memo.count(con)) continue;
        memo.emplace(con);
        que.emplace(con, p);
        swap(p[i], p[j]);
        break;
      }
    }
  }
  auto it = prev(memo.end());
  while(Z--) cout << *it-- << endl;
}


0