#include <bits/stdc++.h>
using namespace std;
const long long INFll = (long long) 1e18 + 10;
const int INFii = (int) 1e9 + 10;
typedef long long ll;
typedef int ii;
typedef long double dbl;
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define maxn 2002
ii n, m, a;
pair<ii,ii> p[maxn];
int main() {
ios::sync_with_stdio(false); cin.tie(0);
//freopen("in.in", "r", stdin);
//freopen("out.out", "w", stdout);
cin >> n >> m;
ii mn = INFii;
ii mx = 0;
priority_queue<pair<ii,pair<ii,ii>>> val;
for(ii i = 1; i <= n; i++) {
for(ii j = 1; j <= m; j++) {
cin >> a;
val.push(mp(a,mp(i,j)));
mn = min(mn,a);
mx = max(mx,a);
}
}
if(mn == mx) {
cout << 0 << endl;
return 0;
}
vector<pair<pair<ii,ii>,vector<pair<ii,ii>>>> pq;
vector<pair<ii,ii>> vazio;
ii ant = -1;
while(val.size()) {
if(val.top().fr != ant) {
ant = val.top().fr;
pq.pb(mp(mp(val.top().fr-mn,0),vazio));
pq.pb(mp(mp(mx-val.top().fr,1),vazio));
}
else {
pq[pq.size()-1].sc.pb(val.top().sc);
pq[pq.size()-2].sc.pb(val.top().sc);
val.pop();
}
}
sort(all(pq), greater<pair<pair<ii,ii>,vector<pair<ii,ii>>>>());
// com minimo na esquerda:
ii l = 0;
ii r = 1e9+1;
ii ans1;
while(l <= r) {
for(ii i = 1; i <= n; i++) {
p[i].fr = 0;
p[i].sc = m+1;
}
for(ii i = 0; i < pq.size() && pq[i].fr.fr >= (l+r)/2; i++) {
for(ii j = 0; j < pq[i].sc.size(); j++) {
if(pq[i].fr.sc == 1) { // tem que ta junto com o mn
p[pq[i].sc[j].fr].fr = max(p[pq[i].sc[j].fr].fr,pq[i].sc[j].sc);
}
else {
p[pq[i].sc[j].fr].sc = min(p[pq[i].sc[j].fr].sc,pq[i].sc[j].sc-1);
}
}
}
//ver se ta crescente/decrescente
ii actc = 0; bool okc = 1;
ii actd = m+1; bool okd = 1;
for(ii i = 1; i <= n; i++) {
actc = max(actc,p[i].fr);
if(actc > p[i].sc || p[i].fr > p[i].sc) okc = 0;
actd = min(actd,p[i].sc);
if(actd < p[i].fr || p[i].fr > p[i].sc) okd = 0;
}
if(okc || okd) {
//se deu bom com esse (l+r)/2 quero abaixar o (l+r)/2
ans1 = (l+r)/2-1;
r = (l+r)/2-1;
}
else {
l = (l+r)/2+1;
}
}
//com mx na esquerda
for(ii i = 0; i < pq.size(); i++) {
pq[i].fr.sc^= 1;
}
l = 0;
r = 1e9+1;
ii ans2;
while(l <= r) {
for(ii i = 1; i <= n; i++) {
p[i].fr = 0;
p[i].sc = m+1;
}
for(ii i = 0; i < pq.size() && pq[i].fr.fr >= (l+r)/2; i++) {
for(ii j = 0; j < pq[i].sc.size(); j++) {
if(pq[i].fr.sc == 1) { // tem que ta junto com o mn
p[pq[i].sc[j].fr].fr = max(p[pq[i].sc[j].fr].fr,pq[i].sc[j].sc);
}
else {
p[pq[i].sc[j].fr].sc = min(p[pq[i].sc[j].fr].sc,pq[i].sc[j].sc-1);
}
}
}
//ver se ta crescente/decrescente
ii actc = 0; bool okc = 1;
ii actd = m+1; bool okd = 1;
for(ii i = 1; i <= n; i++) {
actc = max(actc,p[i].fr);
if(actc > p[i].sc || p[i].fr > p[i].sc) okc = 0;
actd = min(actd,p[i].sc);
if(actd < p[i].fr || p[i].fr > p[i].sc) okd = 0;
}
if(okc || okd) {
//se deu bom com esse (l+r)/2 quero abaixar o (l+r)/2
ans2 = (l+r)/2-1;
r = (l+r)/2-1;
}
else {
l = (l+r)/2+1;
}
}
cout << min(ans1,ans2) << endl;
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:80:25: warning: comparison of integer expressions of different signedness: 'ii' {aka 'int'} and 'std::vector<std::pair<std::pair<int, int>, std::vector<std::pair<int, int> > > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for(ii i = 0; i < pq.size() && pq[i].fr.fr >= (l+r)/2; i++) {
| ~~^~~~~~~~~~~
joioi.cpp:81:29: warning: comparison of integer expressions of different signedness: 'ii' {aka 'int'} and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for(ii j = 0; j < pq[i].sc.size(); j++) {
| ~~^~~~~~~~~~~~~~~~~
joioi.cpp:115:21: warning: comparison of integer expressions of different signedness: 'ii' {aka 'int'} and 'std::vector<std::pair<std::pair<int, int>, std::vector<std::pair<int, int> > > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
115 | for(ii i = 0; i < pq.size(); i++) {
| ~~^~~~~~~~~~~
joioi.cpp:129:25: warning: comparison of integer expressions of different signedness: 'ii' {aka 'int'} and 'std::vector<std::pair<std::pair<int, int>, std::vector<std::pair<int, int> > > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
129 | for(ii i = 0; i < pq.size() && pq[i].fr.fr >= (l+r)/2; i++) {
| ~~^~~~~~~~~~~
joioi.cpp:130:29: warning: comparison of integer expressions of different signedness: 'ii' {aka 'int'} and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
130 | for(ii j = 0; j < pq[i].sc.size(); j++) {
| ~~^~~~~~~~~~~~~~~~~
joioi.cpp:121:8: warning: 'ans2' may be used uninitialized in this function [-Wmaybe-uninitialized]
121 | ii ans2;
| ^~~~
joioi.cpp:72:8: warning: 'ans1' may be used uninitialized in this function [-Wmaybe-uninitialized]
72 | ii ans1;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
11 ms |
1448 KB |
Output is correct |
17 |
Correct |
23 ms |
6880 KB |
Output is correct |
18 |
Correct |
30 ms |
6904 KB |
Output is correct |
19 |
Correct |
28 ms |
6972 KB |
Output is correct |
20 |
Correct |
23 ms |
6804 KB |
Output is correct |
21 |
Correct |
25 ms |
6972 KB |
Output is correct |
22 |
Correct |
36 ms |
6844 KB |
Output is correct |
23 |
Correct |
34 ms |
6952 KB |
Output is correct |
24 |
Correct |
32 ms |
6844 KB |
Output is correct |
25 |
Correct |
30 ms |
6904 KB |
Output is correct |
26 |
Correct |
32 ms |
6976 KB |
Output is correct |
27 |
Correct |
27 ms |
6924 KB |
Output is correct |
28 |
Correct |
25 ms |
6860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
11 ms |
1448 KB |
Output is correct |
17 |
Correct |
23 ms |
6880 KB |
Output is correct |
18 |
Correct |
30 ms |
6904 KB |
Output is correct |
19 |
Correct |
28 ms |
6972 KB |
Output is correct |
20 |
Correct |
23 ms |
6804 KB |
Output is correct |
21 |
Correct |
25 ms |
6972 KB |
Output is correct |
22 |
Correct |
36 ms |
6844 KB |
Output is correct |
23 |
Correct |
34 ms |
6952 KB |
Output is correct |
24 |
Correct |
32 ms |
6844 KB |
Output is correct |
25 |
Correct |
30 ms |
6904 KB |
Output is correct |
26 |
Correct |
32 ms |
6976 KB |
Output is correct |
27 |
Correct |
27 ms |
6924 KB |
Output is correct |
28 |
Correct |
25 ms |
6860 KB |
Output is correct |
29 |
Correct |
2037 ms |
141456 KB |
Output is correct |
30 |
Correct |
1998 ms |
133140 KB |
Output is correct |
31 |
Correct |
2186 ms |
140008 KB |
Output is correct |
32 |
Correct |
2161 ms |
150860 KB |
Output is correct |
33 |
Correct |
2152 ms |
131512 KB |
Output is correct |
34 |
Correct |
2171 ms |
138484 KB |
Output is correct |
35 |
Runtime error |
1615 ms |
262148 KB |
Execution killed with signal 9 |
36 |
Halted |
0 ms |
0 KB |
- |