Submission #33299

# Submission time Handle Problem Language Result Execution time Memory
33299 2017-10-23T09:38:14 Z UncleGrandpa925 The Kingdom of JOIOI (JOI17_joioi) C++14
100 / 100
3249 ms 37348 KB
/*input
4 4
1 12 6 11
11 10 2 14
10 1 9 20
4 17 19 10

8 6
23 23 10 11 16 21 15 26
19 28 19 20 25 26 28 16
15 11 11 8 19 11 15 24
14 19 15 14 24 11 10 8
11 7 6 14 23 5 19 23
17 17 18 11 21 14 20 16
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
#define sp ' '
#define endl '\n'
#define fi first
#define se second
#define mp make_pair
#define N 2005
#define bit(x,y) ((x>>y)&1LL)
#define show(x) cout << (#x) << ": " << x << endl;
#define ii pair<int,int>
ostream& operator << (ostream &os, vector<int>&x) {
	for (int i = 0; i < x.size(); i++) os << x[i] << sp;
	return os;
}
ostream& operator << (ostream &os, pair<int, int> x) {
	cout << x.fi << sp << x.se << sp;
	return os;
}
ostream& operator << (ostream &os, vector<pair<int, int> >&x) {
	for (int i = 0; i < x.size(); i++) os << x[i] << endl;
	return os;
}

int n, m;
int a[N][N];
int dx[] = { -1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int mn = 1e9, mx = 0;
int color[N][N];
bool taken[N][N];

bool check(int lim, int piv) {
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			color[i][j] = 0;
			if (mn + lim >= a[i][j]) color[i][j]++;
			if (mx - lim <= a[i][j]) color[i][j] += 2;
			taken[i][j] = false;
		}
	}
	int preLen = 0;
	for (int i = 1; i <= n; i++) {
		int nxlen = 0;
		if (i == 1) {
			if (bit(color[1][1], piv) == 0) return false;
			taken[1][1] = true;
			for (int j = 2; j <= m; j++) {
				if (bit(color[i][j], piv) == 0)
					break;
				nxlen = j;
				taken[i][j] = true;
			}
		}
		else {
			if (bit(color[i][1], piv) == 1) {
				taken[i][1] = true;
				for (int j = 2; j <= preLen; j++) {
					if (bit(color[i][j], piv) == 0)
						break;
					nxlen = j; taken[i][j] = true;
				}
			}
		}
		preLen = nxlen;
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (!taken[i][j] && bit(color[i][j], (1 - piv)) == 0) return false;
		}
	}
	return true;
}

bool fullCheck(int lim) {
	if (check(lim, 0) || check(lim, 1)) return true;
	for (int i = 1; i <= n; i++) {
		reverse(a[i] + 1, a[i] + 1 + m);
	}
	if (check(lim, 0) || check(lim, 1)) return true;
	return false;
}

signed main() {
#ifdef UncleGrandpa
	freopen("1test.inp", "r", stdin);
#endif
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			scanf("%d", &a[i][j]);
			mn = min(mn, a[i][j]); mx = max(mx, a[i][j]);
		}
	}
	int l = 0, r = 1e9;
	while (l != r) {
		int mid = (l + r) / 2;
		if (fullCheck(mid)) r = mid;
		else l = mid + 1;
	}
	cout << l << endl;
}

Compilation message

joioi.cpp: In function 'std::ostream& operator<<(std::ostream&, std::vector<int>&)':
joioi.cpp:48:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < x.size(); i++) os << x[i] << sp;
                    ^
joioi.cpp: In function 'std::ostream& operator<<(std::ostream&, std::vector<std::pair<int, int> >&)':
joioi.cpp:56:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < x.size(); i++) os << x[i] << endl;
                    ^
joioi.cpp: In function 'int main()':
joioi.cpp:123:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &m);
                        ^
joioi.cpp:126:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d", &a[i][j]);
                         ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 37348 KB Output is correct
2 Correct 0 ms 37348 KB Output is correct
3 Correct 0 ms 37348 KB Output is correct
4 Correct 0 ms 37348 KB Output is correct
5 Correct 0 ms 37348 KB Output is correct
6 Correct 0 ms 37348 KB Output is correct
7 Correct 0 ms 37348 KB Output is correct
8 Correct 0 ms 37348 KB Output is correct
9 Correct 0 ms 37348 KB Output is correct
10 Correct 0 ms 37348 KB Output is correct
11 Correct 0 ms 37348 KB Output is correct
12 Correct 0 ms 37348 KB Output is correct
13 Correct 0 ms 37348 KB Output is correct
14 Correct 0 ms 37348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 37348 KB Output is correct
2 Correct 0 ms 37348 KB Output is correct
3 Correct 0 ms 37348 KB Output is correct
4 Correct 0 ms 37348 KB Output is correct
5 Correct 0 ms 37348 KB Output is correct
6 Correct 0 ms 37348 KB Output is correct
7 Correct 0 ms 37348 KB Output is correct
8 Correct 0 ms 37348 KB Output is correct
9 Correct 0 ms 37348 KB Output is correct
10 Correct 0 ms 37348 KB Output is correct
11 Correct 0 ms 37348 KB Output is correct
12 Correct 0 ms 37348 KB Output is correct
13 Correct 0 ms 37348 KB Output is correct
14 Correct 0 ms 37348 KB Output is correct
15 Correct 0 ms 37348 KB Output is correct
16 Correct 9 ms 37348 KB Output is correct
17 Correct 19 ms 37348 KB Output is correct
18 Correct 16 ms 37348 KB Output is correct
19 Correct 23 ms 37348 KB Output is correct
20 Correct 23 ms 37348 KB Output is correct
21 Correct 23 ms 37348 KB Output is correct
22 Correct 19 ms 37348 KB Output is correct
23 Correct 23 ms 37348 KB Output is correct
24 Correct 19 ms 37348 KB Output is correct
25 Correct 29 ms 37348 KB Output is correct
26 Correct 26 ms 37348 KB Output is correct
27 Correct 23 ms 37348 KB Output is correct
28 Correct 23 ms 37348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 37348 KB Output is correct
2 Correct 0 ms 37348 KB Output is correct
3 Correct 0 ms 37348 KB Output is correct
4 Correct 0 ms 37348 KB Output is correct
5 Correct 0 ms 37348 KB Output is correct
6 Correct 0 ms 37348 KB Output is correct
7 Correct 0 ms 37348 KB Output is correct
8 Correct 0 ms 37348 KB Output is correct
9 Correct 0 ms 37348 KB Output is correct
10 Correct 0 ms 37348 KB Output is correct
11 Correct 0 ms 37348 KB Output is correct
12 Correct 0 ms 37348 KB Output is correct
13 Correct 0 ms 37348 KB Output is correct
14 Correct 0 ms 37348 KB Output is correct
15 Correct 0 ms 37348 KB Output is correct
16 Correct 9 ms 37348 KB Output is correct
17 Correct 19 ms 37348 KB Output is correct
18 Correct 16 ms 37348 KB Output is correct
19 Correct 23 ms 37348 KB Output is correct
20 Correct 23 ms 37348 KB Output is correct
21 Correct 23 ms 37348 KB Output is correct
22 Correct 19 ms 37348 KB Output is correct
23 Correct 23 ms 37348 KB Output is correct
24 Correct 19 ms 37348 KB Output is correct
25 Correct 29 ms 37348 KB Output is correct
26 Correct 26 ms 37348 KB Output is correct
27 Correct 23 ms 37348 KB Output is correct
28 Correct 23 ms 37348 KB Output is correct
29 Correct 2009 ms 37348 KB Output is correct
30 Correct 2186 ms 37348 KB Output is correct
31 Correct 2699 ms 37348 KB Output is correct
32 Correct 1709 ms 37348 KB Output is correct
33 Correct 1476 ms 37348 KB Output is correct
34 Correct 2386 ms 37348 KB Output is correct
35 Correct 2456 ms 37348 KB Output is correct
36 Correct 1859 ms 37348 KB Output is correct
37 Correct 3249 ms 37348 KB Output is correct