QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#50434 | #4835. Mode | YaoBIG# | WA | 246ms | 8092kb | C++ | 3.2kb | 2022-09-26 02:57:44 | 2022-09-26 02:57:47 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = (int)1e9 + 7;
const int TRESHOLD = 450;
// returns number of elements strictly smaller than v in vec
template<class T>
int bins(const vector<T>& vec, T v) {
int low = 0;
int high = vec.size();
while(low != high) {
int mid = (low + high) >> 1;
if (vec[mid] < v) low = mid + 1;
else high = mid;
}
return low;
}
void offer(int v, int off, pair<int, vector<int>>& res) {
if (off == res.first) res.second.push_back(v);
else if (off > res.first) {
res.first = off;
res.second = {v};
}
}
void solve() {
int n;
cin >> n;
vector<int> as(n);
for (int& a : as) cin >> a;
vector<int> cmp = as;
sort(cmp.begin(), cmp.end());
cmp.erase(unique(cmp.begin(), cmp.end()), cmp.end());
for (int i = 0; i < n; ++i) as[i] = bins(cmp, as[i]);
int k = cmp.size();
vector<int> tot(k, 0), suff_max(n + 1, 0);
for (int i = n-1; i >= 0; --i) {
++tot[as[i]];
suff_max[i] = max(suff_max[i + 1], tot[as[i]]);
}
int h = 0;
for (int v : tot) h = max(h, v);
vector<int> pre(n), lst(k, -1);
for (int i = 0; i < n; ++i) {
pre[i] = lst[as[i]];
lst[as[i]] = i;
}
pair<int, vector<int>> res;
res.first = 0;
if (h >= TRESHOLD) {
// O(n^2 / h)
for (int v = 0; v < k; ++v) {
if (tot[v] < h/2) continue;
vector<int> cou(n + 1, 0);
for (int i = 0; i < n; ++i) cou[i + 1] = (cou[i] + (as[i] == v));
for (int v2 = 0; v2 < k; ++v2) {
if (v2 == v) continue;
vector<int> apps = {lst[v2]};
for (int j = pre[lst[v2]]; j >= 0; j = pre[j]) apps.push_back(j);
reverse(apps.begin(), apps.end());
// Have v2 on the inside
int bst = -INF;
for (int j = (int)apps.size() - 1; j >= 0; --j) {
bst = max(bst, j + 1 - cou[apps[j]]);
offer(v, tot[v] + bst - j + cou[apps[j]], res);
// cerr << v << ' ' << v2 << ' ' << j << ' ' << apps[j] << ": " << tot[v] << ' ' << bst << ' ' << j << ' ' << cou[apps[j]] << '\n';
}
// Have v2 on the outside
bst = -INF;
for (int j = (int)apps.size() - 1; j >= 0; --j) {
bst = max(bst, cou[apps[j]] - j);
offer(v2, tot[v2] + bst + j - cou[apps[j]], res);
// cerr << "alt: " << v << ' ' << v2 << ' ' << j << ' ' << apps[j] << ": " << tot[v2] + bst + j - cou[apps[j]] << '\n';
}
}
}
} else {
// O(nh)
vector<int> counts = {n};
for (int i = 0; i < n; ++i) {
for (int a = 1, b = 0, j = pre[i]; a < counts.size(); ++a) {
while(counts[a] <= j) {
++b;
j = pre[j];
}
offer(as[i], tot[as[i]] + a - b, res);
}
int cur = 0;
for (int j = i, cou = 1; j >= 0; j = pre[j], ++cou, ++cur) {
if (counts.size() <= cou) counts.emplace_back(j);
else counts[cou] = max(counts[cou], j);
}
offer(as[i], cur + suff_max[i + 1], res);
}
}
cout << res.first << '\n';
sort(res.second.begin(), res.second.end());
res.second.erase(unique(res.second.begin(), res.second.end()), res.second.end());
for (int v : res.second) cout << cmp[v] << ' '; cout << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
for (int ti = 0; ti < t; ++ti) solve();
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 3572kb
input:
4 5 1 2 3 2 1 5 1 1 3 1 1 6 2 4 2 4 8 8 5 1 2 3 4 5
output:
4 1 5 1 4 2 4 8 2 1 2 3 4 5
result:
ok 14 numbers
Test #2:
score: 0
Accepted
time: 3ms
memory: 3660kb
input:
10 300 336470888 634074578 642802746 167959139 642802746 481199252 481199252 481199252 167959139 634074578 634074578 336470888 336470888 481199252 642802746 481199252 481199252 167959139 642802746 634074578 167959139 336470888 634074578 642802746 167959139 481199252 167959139 167959139 167959139 481...
output:
80 481199252 634074578 46 153774342 39 846318354 30 937534594 27 698063951 27 419330425 20 603780410 706588687 801036056 20 541308492 19 352404708 16 187061768 428773075
result:
ok 24 numbers
Test #3:
score: 0
Accepted
time: 3ms
memory: 3644kb
input:
10 300 641009859 804928248 804928248 804928248 804928248 641009859 476927808 641009859 641009859 641009859 75475634 804928248 804928248 641009859 804928248 54748096 75475634 75475634 54748096 75475634 54748096 54748096 476927808 476927808 75475634 476927808 641009859 75475634 476927808 476927808 754...
output:
84 75475634 47 173884819 253838924 593535580 37 119584259 29 66715052 671541499 706982083 25 683509776 23 145885283 637691905 26 968506132 18 277852473 891198181 954696748 18 967045702 19 493331319
result:
ok 27 numbers
Test #4:
score: 0
Accepted
time: 3ms
memory: 3668kb
input:
10 300 923264237 524125987 524125987 524125987 923264237 751244358 374288891 923264237 923264237 923264237 535590429 524125987 374288891 751244358 524125987 923264237 751244358 751244358 923264237 751244358 535590429 535590429 751244358 923264237 751244358 524125987 751244358 923264237 524125987 923...
output:
85 524125987 50 475906689 38 802613215 28 824887911 28 506836893 754648411 23 708438632 731263599 20 210467639 284624362 746100908 806519500 980100704 21 797383894 21 156438550 977566828 17 111777754
result:
ok 27 numbers
Test #5:
score: 0
Accepted
time: 3ms
memory: 3580kb
input:
10 300 702209411 496813081 561219907 702209411 702209411 561219907 730593611 496813081 702209411 561219907 673102149 702209411 496813081 702209411 673102149 496813081 730593611 496813081 673102149 702209411 673102149 673102149 496813081 496813081 702209411 673102149 561219907 702209411 561219907 561...
output:
81 496813081 53 675266630 38 767363622 32 404396525 27 118275344 195136790 21 422498140 522949042 869477976 887728896 998214353 22 611458649 20 298642883 618165181 853396846 18 510545542 18 41119063 42804963 383659701
result:
ok 29 numbers
Test #6:
score: 0
Accepted
time: 66ms
memory: 7924kb
input:
6 200000 564718673 564718673 291882089 291882089 412106895 291882089 291882089 412106895 564718673 564718673 412106895 412106895 412106895 564718673 291882089 564718673 412106895 291882089 564718673 291882089 564718673 291882089 291882089 564718673 291882089 412106895 564718673 291882089 564718673 5...
output:
72021 564718673 40464 764450551 14267 485362070 14627 904735088 4168 647351267 2097 876938652
result:
ok 12 numbers
Test #7:
score: 0
Accepted
time: 82ms
memory: 8092kb
input:
6 200000 757703054 544067926 5887448 544067926 757703054 757703054 544067926 757703054 544067926 544067926 643910770 544067926 5887448 544067926 757703054 544067926 5887448 544067926 5887448 643910770 5887448 757703054 544067926 544067926 544067926 544067926 544067926 544067926 757703054 757703054 5...
output:
72009 544067926 40553 532894160 14507 280401558 14661 652613244 4149 954112179 2118 387281831
result:
ok 12 numbers
Test #8:
score: 0
Accepted
time: 81ms
memory: 7784kb
input:
6 200000 941492387 378192988 783332532 378192988 378192988 72235422 378192988 378192988 378192988 783332532 783332532 783332532 378192988 378192988 783332532 378192988 783332532 72235422 378192988 449924898 378192988 783332532 941492387 449924898 72235422 72235422 72235422 378192988 378192988 378192...
output:
72125 378192988 40566 204258647 14420 679764421 14458 621315231 4176 283127255 2125 379004468
result:
ok 12 numbers
Test #9:
score: 0
Accepted
time: 82ms
memory: 8020kb
input:
6 200000 652509537 513994713 513994713 513994713 940751563 652509537 652509537 43705451 513994713 652509537 43705451 513994713 513994713 652509537 652509537 43705451 513994713 940751563 513994713 652509537 43705451 940751563 43705451 43705451 652509537 513994713 513994713 652509537 513994713 5139947...
output:
72040 652509537 40565 33034714 14364 848061415 14689 972643361 4192 106061785 2110 861238489
result:
ok 12 numbers
Test #10:
score: -100
Wrong Answer
time: 246ms
memory: 6760kb
input:
9 50000 264178469 144383292 725622388 29492121 605488152 580675905 55883389 390555088 441004197 831609975 438791943 987615719 859267729 743196568 55883389 39388076 959436397 876153780 45621610 363531956 977960026 782710197 106874917 807199457 562810007 715666999 743196568 538628510 824046493 1893675...
output:
432 752268030 91 254698774 3 12093 60583 90748 127514 236374 353911 368891 378263 400779 470972 478812 494946 510827 581713 599252 615790 629984 637246 701304 786939 829043 829312 845188 889918 916550 959344 965105 1017725 1049787 1062654 1070749 1080286 1081671 1166002 1169723 1181573 1183208 122...
result:
wrong answer 25070th numbers differ - expected: '1020', found: '1019'