結果

問題 No.775 tatyamと素数大富豪(hard)
ユーザー PachicobuePachicobue
提出日時 2018-12-22 17:51:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,795 bytes
コンパイル時間 1,136 ms
コンパイル使用メモリ 91,944 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-22 02:56:51
合計ジャッジ時間 6,971 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 17 ms
5,376 KB
testcase_05 AC 11 ms
5,376 KB
testcase_06 AC 104 ms
5,376 KB
testcase_07 AC 493 ms
5,376 KB
testcase_08 AC 484 ms
5,376 KB
testcase_09 TLE -
testcase_10 TLE -
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 WA -
testcase_14 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <vector>
#include <iostream>
#include <numeric>
using ll = long long;
int main()
{
    int N, K;
    std::cin >> N >> K;
    std::vector<std::string> a(N);
    for (int i = 0; i < N; i++) { std::cin >> a[i]; }
    auto comp = [](const std::string& s1, const std::string& s2) { return s1 + s2 == s2 + s1 ? s1.size() > s2.size() : s1 + s2 > s2 + s1; };
    std::sort(a.begin(), a.end(), comp);
    std::vector<int> ind(N);
    std::iota(ind.begin(), ind.end(), 0);
    auto next = [&]() -> bool {
        std::string now;
        for (int i = 0; i < N; i++) { now += a[ind[i]]; }
        std::string max;
        std::vector<int> maxind;
        for (int i = N - 2, icnt = 0; i >= 0; i--) {
            if (ind[i] >= ind[i + 1]) { continue; }
            icnt++;
            for (int j = N - 1, jcnt = 0; j > i; j--) {
                if (ind[j] <= ind[i]) { continue; }
                jcnt++;
                std::swap(ind[i], ind[j]);
                std::reverse(ind.begin() + i + 1, ind.end());
                for (int j = N - 1; j > i; j--) {
                    std::swap(ind[i + 1], ind[j]);
                    std::string s;
                    for (int i = 0; i < N; i++) { s += a[ind[i]]; }
                    if (s < now and max < s) { max = s, maxind = ind; }
                    std::swap(ind[i + 1], ind[j]);
                }
                std::reverse(ind.begin() + i + 1, ind.end());
                std::swap(ind[i], ind[j]);
            }
            if (icnt > (N > 80 ? 14 : N)) { break; }
        }
        return ind = maxind, not max.empty();
    };
    for (int i = 0; i < K; i++) {
        for (int i = 0; i < N; i++) { std::cout << a[ind[i]]; }
        std::cout << std::endl;
        if (not next()) { break; }
    }
    return 0;
}
0