QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#690783#4925. Adjacent PairsMher7770 4ms15900kbC++206.3kb2024-10-31 03:37:022024-10-31 03:37:04

Judging History

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

  • [2024-10-31 03:37:04]
  • 评测
  • 测评结果:0
  • 用时:4ms
  • 内存:15900kb
  • [2024-10-31 03:37:02]
  • 提交

answer

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <array>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <list>
#include <iterator>
#include <numeric>
#include <complex>
#include <utility>
#include <random>
#include <cassert>
#include <fstream>
using namespace std;
mt19937 rnd(time(nullptr));

/* -------------------- Typedefs -------------------- */

typedef int itn;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef float fl;
typedef long double ld;

/* -------------------- Usings -------------------- */

using vi = vector<int>;
using vll = vector<ll>;
using mii = map<int, int>;
using mll = map<ll, ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

/* -------------------- Defines -------------------- */

#define ff first
#define ss second
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mpr make_pair
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
#define all(x) (x).begin(), (x).end()
#define USACO freopen("feast.in", "r", stdin); freopen("feast.out", "w", stdout);

/* -------------------- Constants -------------------- */

const int dx[8] = { -1, 0, 1, 0, -1, -1, 1, 1 };
const int dy[8] = { 0, -1, 0, 1, -1, 1, -1, 1 };
const int MAX = int(1e9 + 5);
const ll MAXL = ll(1e18) + 5ll;
const ll MOD = ll(1000000007);
const ll MOD2 = ll(998244353);

/* -------------------- Functions -------------------- */

void fastio() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}

void precision(int x) {
    cout.setf(ios::fixed | ios::showpoint);
    cout.precision(x);
}

ll gcd(ll a, ll b) {
    if (a == 0 || b == 0) return(max(a, b));
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}

ll lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

ll range_sum(ll a, ll b) {
    if (a > b) return 0ll;
    ll dif = a - 1, cnt = b - a + 1;
    ll ans = ((b - a + 1) * (b - a + 2)) / 2;
    ans += ((b - a + 1) * dif);
    return ans;
}

string dec_to_bin(ll a) {
    string s = "";
    for (ll i = a; i > 0; ) {
        ll k = i % 2;
        i /= 2;
        char c = k + 48;
        s += c;
    }
    if (a == 0) {
        s = "0";
    }
    reverse(all(s));
    return s;
}

ll bin_to_dec(string s) {
    ll num = 0;
    for (int i = 0; i < s.size(); i++) {
        num *= 2ll;
        num += (s[i] - '0');
    }
    return num;
}

ll factorial_by_mod(ll n, ll mod) {
    ll ans = 1;
    ll num;
    for (ll i = 1; i <= n; ++i) {
        num = i % mod;
        ans *= num;
        ans %= mod;
    }
    return ans;
}

bool isPrime(ll a) {
    if (a == 1) return false;
    for (ll i = 2; i * i <= a; i++) {
        if (a % i == 0) return false;
    }
    return true;
}

ll binpow(ll a, ll b) {
    if (!a) return 0;
    ll ans = 1;
    while (b) {
        if (b & 1) {
            ans *= a;
        }
        b >>= 1;
        a *= a;
    }
    return ans;
}

ll binpow_by_mod(ll a, ll b, ll mod) {
    if (!a) return 0;
    ll ans = 1;
    while (b) {
        if (b & 1) {
            ans *= a;
            ans %= mod;
        }
        b >>= 1;
        a *= a;
        a %= mod;
    }
    return ans;
}

/* -------------------- Solution -------------------- */

const int N = 200005;
set<int> adj[N];
int a[N], cnt[N][2], used[N];
int n, ans;

void clearr() {
    for (int i = 1; i <= n; ++i) {
        adj[a[i]].clear();
        cnt[a[i]][0] = cnt[a[i]][1] = used[a[i]] = 0;
    }
}

void slv() {
    cin >> n;
    ans = n;
    set<int> num_set;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        if (i % 2) num_set.insert(a[i]);
        ++cnt[a[i]][i % 2];
        ans = min(ans, n - cnt[a[i]][i % 2]);
    }
    set<pii> st;
    for (int i = 1; i <= n; ++i) {
        if (i % 2) {
            if (i != n) {
                st.insert({ a[i + 1],a[i] });
                adj[a[i]].insert(a[i + 1]);
            }
            if (i != 1) {
                adj[a[i]].insert(a[i - 1]);
            }
        }
        else {
            if (i != n) {
                st.insert({ a[i],a[i + 1] });
            }
        }
    }
    map<pii, int> mp_even, mp_odd;
    for (int i = 1; i < n; ++i) {
        pii opt = { a[i],a[i + 1] };
        int len = 2, ind = i + 2;
        while (ind <= n) {
            if (a[ind] == opt.ff) {
                ++len;
                swap(opt.ff, opt.ss);
                ++ind;
            }
            else {
                break;
            }
        }
        if (i % 2) {
            mp_odd[{a[i], a[i + 1]}] += (len / 2) * 2 + len / 2 + len % 2;
        }
        else {
            mp_even[{a[i], a[i + 1]}] += (len / 2) * 2 + len / 2 + len % 2;
        }
        i = ind - 2;
    }
    for (auto elem : st) {
        int num1 = elem.ff, num2 = elem.ss;
        int answ = n - cnt[num1][0] - cnt[num1][1] - cnt[num2][0] - cnt[num2][1];
        answ += mp_even[elem];
        answ += mp_odd[{num2, num1}];
        ans = min(ans, answ);
    }
    multiset<int> mst, del;
    mst.insert(0);
    for (int i = 2; i <= n; i += 2) {
        if (used[a[i]]) continue;
        used[a[i]] = 1;
        mst.insert(cnt[a[i]][0]);
    }
    for (auto elem : num_set) {
        del.clear();
        if (used[elem]) {
            mst.erase(mst.find(cnt[elem][0]));
            del.insert(cnt[elem][0]);
        }
        for (auto to : adj[elem]) {
            if (st.find({ elem,to }) != st.end()) {
                del.insert(cnt[to][0]);
                mst.erase(mst.find(cnt[to][0]));
            }
        }
        int answ = n - cnt[elem][1] - *mst.rbegin();
        ans = min(ans, answ);
        for (auto u : del) {
            mst.insert(u);
        }
    }
    cout << ans << '\n';
    clearr();
}

void cs() {
    int tstc = 1;
    cin >> tstc;
    while (tstc--) {
        slv();
    }
}

void precalc() {
    return;
}

int main() {
    fastio();
    precalc();
    //precision(0);
    cs();
    return 0;
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 20
Accepted
time: 0ms
memory: 14792kb

input:

2
5
4 5 2 4 5
2
1 2

output:

3
0

result:

ok 2 lines

Test #2:

score: 20
Accepted
time: 3ms
memory: 13828kb

input:

1
9
1 2 1 2 3 1 2 1 2

output:

6

result:

ok single line: '6'

Test #3:

score: 20
Accepted
time: 0ms
memory: 15172kb

input:

1
7
6 5 4 1 2 6 5

output:

5

result:

ok single line: '5'

Test #4:

score: 20
Accepted
time: 3ms
memory: 14100kb

input:

1
16
4 3 4 3 4 3 4 3 1 4 3 4 1 4 3 4

output:

10

result:

ok single line: '10'

Test #5:

score: 20
Accepted
time: 0ms
memory: 14640kb

input:

9
11
1 4 5 7 9 2 10 3 11 6 8
11
10 7 11 4 3 6 9 2 8 1 5
11
11 9 8 10 1 7 5 4 3 6 2
11
9 10 6 11 5 1 8 7 2 4 3
11
11 6 2 1 10 4 3 8 7 9 5
11
5 10 4 9 6 1 2 7 8 11 3
11
7 9 8 11 6 1 4 5 2 3 10
11
10 3 11 2 6 1 9 7 4 5 8
12
10 9 4 7 11 6 8 12 2 1 3 5

output:

9
9
9
9
9
9
9
9
10

result:

ok 9 lines

Test #6:

score: 20
Accepted
time: 0ms
memory: 14356kb

input:

5
19
13 5 17 19 4 9 14 7 15 1 8 10 18 3 6 12 2 16 11
20
2 6 19 12 13 16 8 1 11 7 17 14 4 5 10 18 9 3 15 20
20
10 13 6 17 16 9 8 14 1 5 12 19 20 4 15 11 7 3 2 18
20
13 8 18 16 7 17 10 2 15 1 20 4 19 12 3 14 11 9 5 6
20
4 16 1 19 3 2 6 8 20 7 5 9 10 14 15 13 12 18 11 17

output:

17
18
18
18
18

result:

ok 5 lines

Test #7:

score: 20
Accepted
time: 0ms
memory: 15508kb

input:

2
49
21 4 18 35 34 39 9 48 16 33 31 7 10 12 41 40 8 14 2 22 30 24 44 27 42 29 37 17 23 45 32 46 1 26 28 5 25 49 43 6 38 19 11 36 15 47 13 3 20
50
18 4 7 46 39 37 9 20 48 14 3 35 32 43 17 11 31 8 28 26 1 36 6 21 27 12 24 44 29 15 42 38 22 23 45 25 33 2 13 34 41 19 47 49 5 10 40 16 50 30

output:

47
48

result:

ok 2 lines

Test #8:

score: 20
Accepted
time: 0ms
memory: 15536kb

input:

10
10
1 3 8 5 9 10 6 4 2 7
10
8 2 1 10 9 4 6 7 5 3
10
5 7 6 8 2 10 9 3 4 1
10
6 1 2 7 9 10 5 3 8 4
10
7 5 2 6 4 1 3 10 9 8
10
2 4 3 1 5 8 7 10 6 9
10
1 9 2 4 5 6 7 10 3 8
10
4 10 7 5 1 3 6 8 9 2
10
8 5 7 9 6 4 1 2 3 10
10
10 6 2 3 1 7 5 4 8 9

output:

8
8
8
8
8
8
8
8
8
8

result:

ok 10 lines

Test #9:

score: 20
Accepted
time: 0ms
memory: 14084kb

input:

4
24
8 7 24 21 19 22 12 9 13 11 1 20 3 16 10 6 2 4 15 17 5 14 18 23
25
23 20 1 7 9 22 6 15 25 21 2 14 10 18 11 5 13 4 17 3 24 8 16 19 12
25
17 19 14 8 22 13 10 1 4 20 15 7 25 16 5 23 9 11 12 6 18 24 2 3 21
25
17 2 3 6 13 24 10 25 4 22 1 11 9 14 20 7 15 21 19 16 12 18 8 5 23

output:

22
23
23
23

result:

ok 4 lines

Test #10:

score: 20
Accepted
time: 0ms
memory: 14624kb

input:

1
100
91 92 11 30 28 52 95 76 9 21 70 8 89 84 10 37 83 39 97 16 18 2 27 48 58 78 74 72 15 44 7 4 98 45 63 29 55 49 31 13 17 56 32 14 62 93 24 100 12 6 20 94 79 66 22 19 73 67 25 3 57 42 54 60 5 90 64 53 23 80 40 59 34 88 77 46 47 41 26 96 1 65 43 69 51 99 38 36 33 71 61 75 87 68 50 86 35 82 85 81

output:

98

result:

ok single line: '98'

Test #11:

score: 20
Accepted
time: 0ms
memory: 15900kb

input:

10
9
5 4 1 9 6 7 1 3 6
10
5 10 6 10 6 10 8 7 5 1
10
6 10 7 9 7 4 2 8 2 5
10
9 5 3 9 5 3 7 9 7 3
10
2 9 6 10 6 10 7 3 7 10
10
2 5 1 5 10 8 10 5 8 4
10
5 1 3 4 9 4 10 4 1 6
10
7 6 7 10 6 7 6 7 6 10
10
2 4 2 4 9 2 7 2 7 4
10
8 9 10 6 5 6 7 5 1 9

output:

6
5
7
6
5
5
6
5
5
7

result:

ok 10 lines

Test #12:

score: 20
Accepted
time: 4ms
memory: 15808kb

input:

5
19
15 10 17 1 5 17 6 1 10 18 2 11 16 5 8 5 8 19 12
20
11 10 19 4 17 11 20 16 4 12 17 13 7 16 17 19 10 15 1 18
20
9 20 9 20 9 20 19 9 19 9 2 3 2 3 2 3 2 9 7 9
20
6 14 8 14 19 5 19 5 11 10 19 4 14 1 10 1 10 8 7 8
20
7 18 7 5 13 7 5 8 13 1 8 5 18 7 13 7 6 7 13 6

output:

15
15
12
15
12

result:

ok 5 lines

Test #13:

score: 20
Accepted
time: 3ms
memory: 14116kb

input:

2
49
8 43 31 29 46 29 32 49 32 19 31 12 44 12 9 49 43 49 43 23 8 2 8 2 5 7 13 25 7 25 7 25 4 25 36 28 36 19 40 19 40 19 30 19 47 40 24 31 9
50
15 46 35 30 35 29 27 29 50 43 47 38 40 38 18 27 24 49 7 49 24 44 4 19 30 24 1 25 1 50 7 38 12 15 50 48 1 27 29 21 46 31 46 41 6 41 50 1 50 32

output:

41
43

result:

ok 2 lines

Test #14:

score: 0
Wrong Answer
time: 3ms
memory: 14928kb

input:

9
11
5 9 5 9 6 7 6 10 7 10 6
11
4 8 7 4 10 4 10 5 8 5 8
11
3 11 3 8 3 11 8 11 8 3 11
11
2 6 10 6 2 11 2 11 5 7 11
11
6 4 7 4 7 4 7 4 8 9 6
11
5 11 3 11 4 11 5 8 3 8 4
11
8 3 5 8 5 8 3 5 8 5 8
11
5 3 8 4 1 10 1 6 8 10 3
11
11 5 8 5 4 7 5 7 5 10 3

output:

6
7
6
6
4
6
7
7
7

result:

wrong answer 7th lines differ - expected: '8', found: '7'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

0%