5260 | 2023-04-24 12:52:39 | ZsofiaKeresztely | Mágikus táblázat | cpp14 | Time limit exceeded 0/100 | 600ms | 15544 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++){
int di = (mid - 1) / i + 1;
map<int, int> ma;
int amax = -1e9, bmin = 1e9;
if (i <= n && di <= 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<di; j++){
ma[b[j]]++;
}
bmin = min(bmin, ma.rbegin()->first);
for (int j=di; 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 (di <= n && i <= m){
for (int j=0; j<di; j++){
ma[a[j]]++;
}
amax = max(amax, ma.begin()->first);
for (int j=di; 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()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
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 | 1980 KiB | ||||
2 | Time limit exceeded | 600ms | 1896 KiB | ||||
subtask2 | 0/14 | ||||||
3 | Accepted | 13ms | 2404 KiB | ||||
4 | Wrong answer | 21ms | 2372 KiB | ||||
5 | Accepted | 3ms | 2496 KiB | ||||
6 | Wrong answer | 7ms | 2708 KiB | ||||
7 | Wrong answer | 4ms | 2716 KiB | ||||
8 | Accepted | 4ms | 2848 KiB | ||||
subtask3 | 0/27 | ||||||
9 | Time limit exceeded | 574ms | 2472 KiB | ||||
10 | Time limit exceeded | 575ms | 3388 KiB | ||||
11 | Time limit exceeded | 555ms | 2560 KiB | ||||
12 | Time limit exceeded | 563ms | 3620 KiB | ||||
13 | Time limit exceeded | 564ms | 2972 KiB | ||||
14 | Wrong answer | 211ms | 3800 KiB | ||||
15 | Time limit exceeded | 563ms | 4064 KiB | ||||
16 | Time limit exceeded | 564ms | 3540 KiB | ||||
17 | Time limit exceeded | 560ms | 4372 KiB | ||||
subtask4 | 0/21 | ||||||
18 | Time limit exceeded | 547ms | 4560 KiB | ||||
19 | Time limit exceeded | 573ms | 4232 KiB | ||||
20 | Time limit exceeded | 600ms | 4216 KiB | ||||
21 | Time limit exceeded | 552ms | 4280 KiB | ||||
22 | Wrong answer | 17ms | 5452 KiB | ||||
23 | Wrong answer | 17ms | 5456 KiB | ||||
24 | Time limit exceeded | 600ms | 4236 KiB | ||||
subtask5 | 0/38 | ||||||
25 | Time limit exceeded | 564ms | 9104 KiB | ||||
26 | Time limit exceeded | 561ms | 9112 KiB | ||||
27 | Time limit exceeded | 572ms | 9120 KiB | ||||
28 | Time limit exceeded | 564ms | 9092 KiB | ||||
29 | Time limit exceeded | 505ms | 9244 KiB | ||||
30 | Time limit exceeded | 569ms | 9200 KiB | ||||
31 | Time limit exceeded | 541ms | 9228 KiB | ||||
32 | Time limit exceeded | 558ms | 7432 KiB | ||||
33 | Time limit exceeded | 550ms | 7384 KiB | ||||
34 | Time limit exceeded | 577ms | 4596 KiB | ||||
35 | Time limit exceeded | 565ms | 6576 KiB | ||||
36 | Time limit exceeded | 600ms | 9628 KiB | ||||
37 | Time limit exceeded | 568ms | 5224 KiB | ||||
38 | Wrong answer | 398ms | 15544 KiB | ||||
39 | Time limit exceeded | 583ms | 9740 KiB | ||||
40 | Time limit exceeded | 558ms | 9652 KiB | ||||
41 | Time limit exceeded | 556ms | 9196 KiB | ||||
42 | Time limit exceeded | 560ms | 5068 KiB | ||||
43 | Time limit exceeded | 568ms | 7264 KiB | ||||
44 | Time limit exceeded | 568ms | 7376 KiB |