5258 | 2023-04-24 12:32:09 | ZsofiaKeresztely | Mágikus táblázat | cpp14 | Time limit exceeded 0/100 | 600ms | 9664 KiB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m;
vector<int> a, b;
bool ok(int mid){
for (int i=1; i * i <= mid; i++){
if (mid % i) continue;
map<int, int> ma;
int amax = -1e9, bmin = 1e9;
if (i <= n && mid / i <= m){
for (int j=0; j<i; j++){
ma[a[j]]++;
}
amax = max(amax, ma.begin()->first);
for (int j=i; j<n; j++){
ma[a[j]]++;
ma[a[j-i]]--;
if (!ma[a[j-i]]) ma.erase(a[j-i]);
amax = max(amax, ma.begin()->first);
}
ma.clear();
for (int j=0; j<mid/i; j++){
ma[b[j]]++;
}
bmin = min(bmin, ma.rbegin()->first);
for (int j=mid/i; j<m; j++){
ma[b[j]]++;
ma[b[j-i]]--;
if (!ma[b[j-i]]) ma.erase(b[j-i]);
bmin = min(bmin, ma.rbegin()->first);
}
}
if (amax >= bmin) return true;
amax = -1e9;
bmin = 1e9;
ma.clear();
if (mid / i <= n && i <= m){
for (int j=0; j<mid/i; j++){
ma[a[j]]++;
}
amax = max(amax, ma.begin()->first);
for (int j=mid/i; j<n; j++){
ma[a[j]]++;
ma[a[j-i]]--;
if (!ma[a[j-i]]) ma.erase(a[j-i]);
amax = max(amax, ma.begin()->first);
}
ma.clear();
for (int j=0; j<i; j++){
ma[b[j]]++;
}
bmin = min(bmin, ma.rbegin()->first);
for (int j=i; j<m; j++){
ma[b[j]]++;
ma[b[j-i]]--;
if (!ma[b[j-i]]) ma.erase(b[j-i]);
bmin = min(bmin, ma.rbegin()->first);
}
}
if (amax >= bmin) return true;
}
return false;
}
int main()
{
cin >> n >> m;
a.resize(n);
b.resize(m);
for (int &x : a){
cin >> x;
}
for (int &x : b){
cin >> x;
}
int l = 0, r = n * m + 1;
while (l + 1 < r){
int mid = (l + r) / 2;
if (ok(mid)){
l = mid;
}
else{
r = mid;
}
}
cout << l;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1812 KiB | ||||
2 | Time limit exceeded | 600ms | 1768 KiB | ||||
subtask2 | 0/14 | ||||||
3 | Wrong answer | 3ms | 2220 KiB | ||||
4 | Wrong answer | 3ms | 2608 KiB | ||||
5 | Wrong answer | 3ms | 2676 KiB | ||||
6 | Wrong answer | 3ms | 2768 KiB | ||||
7 | Wrong answer | 3ms | 2844 KiB | ||||
8 | Accepted | 3ms | 3036 KiB | ||||
subtask3 | 0/27 | ||||||
9 | Wrong answer | 81ms | 3372 KiB | ||||
10 | Wrong answer | 94ms | 3436 KiB | ||||
11 | Wrong answer | 43ms | 3396 KiB | ||||
12 | Wrong answer | 128ms | 3276 KiB | ||||
13 | Wrong answer | 32ms | 3352 KiB | ||||
14 | Wrong answer | 6ms | 3500 KiB | ||||
15 | Wrong answer | 27ms | 3888 KiB | ||||
16 | Wrong answer | 19ms | 4132 KiB | ||||
17 | Accepted | 35ms | 4048 KiB | ||||
subtask4 | 0/21 | ||||||
18 | Time limit exceeded | 550ms | 3992 KiB | ||||
19 | Time limit exceeded | 560ms | 4280 KiB | ||||
20 | Time limit exceeded | 541ms | 5580 KiB | ||||
21 | Time limit exceeded | 577ms | 4528 KiB | ||||
22 | Wrong answer | 46ms | 5780 KiB | ||||
23 | Wrong answer | 45ms | 5536 KiB | ||||
24 | Accepted | 14ms | 4460 KiB | ||||
subtask5 | 0/38 | ||||||
25 | Time limit exceeded | 600ms | 8364 KiB | ||||
26 | Time limit exceeded | 555ms | 8436 KiB | ||||
27 | Time limit exceeded | 541ms | 8304 KiB | ||||
28 | Time limit exceeded | 600ms | 8276 KiB | ||||
29 | Time limit exceeded | 560ms | 8516 KiB | ||||
30 | Time limit exceeded | 561ms | 8536 KiB | ||||
31 | Time limit exceeded | 545ms | 8460 KiB | ||||
32 | Time limit exceeded | 552ms | 7224 KiB | ||||
33 | Time limit exceeded | 560ms | 7256 KiB | ||||
34 | Time limit exceeded | 560ms | 4448 KiB | ||||
35 | Time limit exceeded | 555ms | 6576 KiB | ||||
36 | Time limit exceeded | 577ms | 8708 KiB | ||||
37 | Time limit exceeded | 568ms | 9216 KiB | ||||
38 | Time limit exceeded | 551ms | 8476 KiB | ||||
39 | Time limit exceeded | 552ms | 9116 KiB | ||||
40 | Time limit exceeded | 555ms | 9664 KiB | ||||
41 | Time limit exceeded | 555ms | 8520 KiB | ||||
42 | Time limit exceeded | 560ms | 7308 KiB | ||||
43 | Time limit exceeded | 564ms | 7456 KiB | ||||
44 | Time limit exceeded | 580ms | 7472 KiB |