QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#507952#7636. Fair ElectionspandapythonerTL 0ms3844kbC++232.9kb2024-08-07 01:10:332024-08-07 01:10:34

Judging History

你现在查看的是最新测评结果

  • [2024-08-07 01:10:34]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3844kb
  • [2024-08-07 01:10:33]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;


using ll = long long;

#define flt double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define rep(i, n) for(int i = 0; i < n; i += 1)
#define len(a) ((int)(a).size())


const ll inf = 1e18;
mt19937 rnd(234);


int32_t main() {
    if (1) {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
    }
    int n;
    cin >> n;
    vector<vector<int>> p(n);
    rep(i, n) {
        p[i].resize(3);
        rep(j, 3) {
            cin >> p[i][j];
            --p[i][j];
        }
    }
    vector<vector<int>> pos(n);
    rep(i, n) {
        pos[i].resize(3);
        rep(j, 3) {
            pos[i][p[i][j]] = j;
        }
    }
    vector<vector<pair<int, int>>> dp(n + 1);
    for (int x = 0; x <= n; x += 1) {
        if (n - 2 * x > 0) {
            dp[x].push_back(make_pair(0, 2));
        }
        if (max(0, n - 2 * x) <= x) {
            dp[x].push_back(make_pair(max(0, n - 2 * x), 0));
        }
        int val = max({ x + 1, (n - x + 1) / 2 });
        if (val <= n - x) {
            dp[x].push_back(make_pair(val, 1));
        }
        dp[x].push_back(make_pair(10 * n + 10000, -1));
    }
    for (int cnt = n - 1; cnt >= 0; cnt -= 1) {
        vector<vector<pair<int, int>>> ndp(cnt + 1);
        for (int x = 0; x <= cnt; x += 1) {
            auto& a = dp[x + 1];
            auto& b = dp[x];
            auto c = dp[x];
            for (auto& f : c) f.first -= 1;
            int i = 0, j = 0, k = 0;
            // int last_min = -1;
            int cur_pos = 0;
            int vala = -1, valb = -1, valc = -1;
            while (i < len(a) - 1 or j < len(b) - 1 or k < len(c) - 1) {
                cur_pos = min({ a[i].first, b[j].first, c[k].first });
                if (a[i].first == cur_pos) {
                    vala = a[i].second;
                    ++i;
                }
                if (b[j].first == cur_pos) {
                    valb = b[j].second;
                    ++j;
                }
                if (c[k].first == cur_pos) {
                    valc = c[k].second;
                    ++k;
                }
                int mn = vala;
                if (mn == -1 or (valb != -1 and pos[cnt][mn] > pos[cnt][valb])) {
                    mn = valb;
                }
                if (mn == -1 or (valc != -1 and pos[cnt][mn] > pos[cnt][valc])) {
                    mn = valc;
                }
                ndp[x].push_back(make_pair(cur_pos, mn));
            }
            ndp[x].push_back(make_pair(10 * n + 10000, -1));
        }
        dp.swap(ndp);
    }
    assert(!dp.empty() and !dp[0].empty());
    int result = -1;
    for (auto [cur_pos, cur_val] : dp[0]) {
        if (cur_pos <= 0) {
            result = cur_val;
        }
    }
    cout << result + 1 << "\n";
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3792kb

input:

3
3 2 1
1 2 3
2 1 3

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3628kb

input:

5
1 2 3
2 3 1
1 3 2
3 1 2
2 1 3

output:

1

result:

ok 1 number(s): "1"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3560kb

input:

5
1 2 3
3 2 1
1 2 3
1 2 3
3 2 1

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: 0
Accepted
time: 0ms
memory: 3604kb

input:

5
3 2 1
3 1 2
2 3 1
1 2 3
2 1 3

output:

2

result:

ok 1 number(s): "2"

Test #5:

score: 0
Accepted
time: 0ms
memory: 3528kb

input:

5
3 2 1
3 2 1
1 3 2
1 3 2
3 2 1

output:

3

result:

ok 1 number(s): "3"

Test #6:

score: 0
Accepted
time: 0ms
memory: 3540kb

input:

5
3 2 1
3 1 2
1 2 3
1 3 2
1 3 2

output:

1

result:

ok 1 number(s): "1"

Test #7:

score: 0
Accepted
time: 0ms
memory: 3636kb

input:

20
1 2 3
2 3 1
1 3 2
3 1 2
2 1 3
1 2 3
3 2 1
1 2 3
1 2 3
3 2 1
3 2 1
3 1 2
2 3 1
1 2 3
2 1 3
3 2 1
3 2 1
1 3 2
1 3 2
3 2 1

output:

1

result:

ok 1 number(s): "1"

Test #8:

score: 0
Accepted
time: 0ms
memory: 3556kb

input:

20
3 2 1
3 1 2
1 2 3
1 3 2
1 3 2
1 2 3
3 1 2
1 3 2
3 2 1
1 3 2
2 1 3
3 1 2
2 3 1
2 1 3
2 3 1
3 1 2
3 1 2
2 1 3
3 1 2
1 2 3

output:

1

result:

ok 1 number(s): "1"

Test #9:

score: 0
Accepted
time: 0ms
memory: 3548kb

input:

20
1 2 3
2 3 1
2 1 3
2 1 3
1 3 2
3 1 2
2 3 1
1 3 2
3 1 2
1 2 3
1 2 3
3 2 1
3 1 2
1 2 3
3 2 1
1 2 3
2 3 1
3 1 2
3 2 1
3 2 1

output:

3

result:

ok 1 number(s): "3"

Test #10:

score: 0
Accepted
time: 0ms
memory: 3844kb

input:

20
3 1 2
3 2 1
1 2 3
1 3 2
1 3 2
2 3 1
1 3 2
2 3 1
1 2 3
3 2 1
1 2 3
3 2 1
1 2 3
1 2 3
3 2 1
2 3 1
2 3 1
1 2 3
2 1 3
2 1 3

output:

1

result:

ok 1 number(s): "1"

Test #11:

score: 0
Accepted
time: 0ms
memory: 3552kb

input:

20
2 1 3
2 1 3
3 1 2
3 2 1
2 1 3
1 2 3
2 3 1
3 1 2
3 1 2
2 3 1
3 2 1
1 3 2
2 1 3
2 1 3
1 2 3
2 3 1
2 3 1
3 2 1
2 1 3
1 3 2

output:

2

result:

ok 1 number(s): "2"

Test #12:

score: -100
Time Limit Exceeded

input:

10000
1 2 3
2 3 1
1 3 2
3 1 2
2 1 3
1 2 3
3 2 1
1 2 3
1 2 3
3 2 1
3 2 1
3 1 2
2 3 1
1 2 3
2 1 3
3 2 1
3 2 1
1 3 2
1 3 2
3 2 1
3 2 1
3 1 2
1 2 3
1 3 2
1 3 2
1 2 3
3 1 2
1 3 2
3 2 1
1 3 2
2 1 3
3 1 2
2 3 1
2 1 3
2 3 1
3 1 2
3 1 2
2 1 3
3 1 2
1 2 3
1 2 3
2 3 1
2 1 3
2 1 3
1 3 2
3 1 2
2 3 1
1 3 2
3 1 2
...

output:


result: