結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,624 KB
testcase_01 AC 2 ms
5,248 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 13 ms
5,376 KB
testcase_06 AC 104 ms
5,376 KB
testcase_07 AC 509 ms
5,376 KB
testcase_08 AC 506 ms
5,376 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using ll = long long;
#define show(x) std::cerr << #x << " = " << (x) << std::endl
template <typename T, typename A>
std::ostream& operator<<(std::ostream& os, const std::vector<T, A>& v)
{
    os << "[";
    for (const auto& p : v) { os << p << ","; }
    return (os << "]\n");
}
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; i >= 0; i--) {
            if (ind[i] >= ind[i + 1]) { continue; }
            for (int j = N - 1; j > i; j--) {
                if (ind[j] <= ind[i]) { continue; }
                auto index = ind;
                std::swap(index[i], index[j]);
                std::reverse(index.begin() + i + 1, index.end());
                for (int j = i + 1; j < N; j++) {
                    auto inde = index;
                    std::swap(inde[i + 1], inde[j]);
                    std::string s;
                    for (int i = 0; i < N; i++) { s += a[inde[i]]; }
                    if (s < now and max < s) { max = s, maxind = inde; }
                }
                // break;
            }
            // break;
        }
        return ind = maxind, (max != "");
    };
    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