Submission #91332

# Submission time Handle Problem Language Result Execution time Memory
91332 2018-12-27T07:32:49 Z Just_Solve_The_Problem Marriage questions (IZhO14_marriage) C++11
70 / 100
1500 ms 11388 KB
#include <bits/stdc++.h>

using namespace std;

#define ok puts("ok");
#define ll long long

const int N = (int)2e3 + 7;

int n, m, k;

vector < int > gr[N], gr1[(int)3e4 + 7];
pair < int, int > lr[N];
int used[N], mt[(int)3e4 + 7], prr[N], cnt = 1;

bool kuhn(int v) {
	if (used[v] == cnt) return false;
	used[v] = cnt;
	for (int i = lr[v].first; i <= lr[v].second; i++) {
		int to = gr[v][i];
		if (mt[to] == -1 || kuhn(mt[to])) {
			mt[to] = v;
			prr[v] = to;
			return true;
		}
	}
	return false;
}

bool check(int l, int r) {
	for (int i = 1; i <= m; i++) {
		while (lr[i].first < (int)gr[i].size() && gr[i][lr[i].first] < l) lr[i].first++;
		while (lr[i].first > 0 && gr[i][lr[i].first - 1] >= l) lr[i].first--;
		while (lr[i].second + 1 < (int)gr[i].size() && gr[i][lr[i].second + 1] <= r) lr[i].second++;
		while (lr[i].second >= 0 && gr[i][lr[i].second] > r) lr[i].second--;
	}
	memset(mt, -1, sizeof mt);
	memset(prr, -1, sizeof prr);
	int run = 1;
	while (run) {
		run = 0;
		for (int i = 1; i <= m; i++) {
			if (prr[i] == -1 && kuhn(i)) {
				run = 1;
			}
		}
		cnt++;
	}
	for (int i = 1; i <= m; i++) {
		if (prr[i] == -1) {
			return 0;
		}
	}
	return 1;
}

main() {
	scanf("%d %d %d", &n, &m, &k);
	for (int i = 1; i <= k; i++) {
		int a, b;
		scanf("%d %d", &a, &b);
		gr[b].push_back(a);
	}
	ll ans = 0;
	int l, r;
	l = 1;
	r = m;
	for (int i = 1; i <= m; i++) {
		sort(gr[i].begin(), gr[i].end());
		lr[i].second = -1;
		for (int j = 0; j < gr[i].size(); j++) {
			int to = gr[i][j];
			if (l <= to && to <= r) {
				lr[i].second = j;
			}
		}
		lr[i].first = 0;
	}
	for (int i = 1; i <= n - m + 1; i++) {
		int lo = i;
		int hi = n + 1;
		while (hi - lo > 1) {
			int mid = (lo + hi) >> 1;
			if (check(i, mid)) {
				hi = mid;
			} else {
				lo = mid;
			}
		}
		ans += (n - hi + 1);
	}
	cout << ans << endl;
}

Compilation message

marriage.cpp:57:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main() {
      ^
marriage.cpp: In function 'int main()':
marriage.cpp:71:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 0; j < gr[i].size(); j++) {
                   ~~^~~~~~~~~~~~~~
marriage.cpp:58:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %d", &n, &m, &k);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
marriage.cpp:61:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1272 KB Output is correct
2 Correct 4 ms 1408 KB Output is correct
3 Correct 3 ms 1408 KB Output is correct
4 Correct 3 ms 1408 KB Output is correct
5 Correct 4 ms 1408 KB Output is correct
6 Correct 2 ms 1408 KB Output is correct
7 Correct 3 ms 1408 KB Output is correct
8 Incorrect 2 ms 1408 KB Output isn't correct
9 Correct 2 ms 1424 KB Output is correct
10 Correct 2 ms 1424 KB Output is correct
11 Correct 2 ms 1504 KB Output is correct
12 Correct 2 ms 1504 KB Output is correct
13 Incorrect 2 ms 1540 KB Output isn't correct
14 Correct 4 ms 1544 KB Output is correct
15 Correct 3 ms 1576 KB Output is correct
16 Correct 3 ms 1576 KB Output is correct
17 Correct 3 ms 1576 KB Output is correct
18 Correct 3 ms 1584 KB Output is correct
19 Correct 7 ms 1620 KB Output is correct
20 Correct 6 ms 1620 KB Output is correct
21 Correct 5 ms 1620 KB Output is correct
22 Correct 5 ms 1620 KB Output is correct
23 Correct 5 ms 1620 KB Output is correct
24 Correct 5 ms 1620 KB Output is correct
25 Correct 83 ms 1800 KB Output is correct
26 Correct 57 ms 1800 KB Output is correct
27 Correct 29 ms 1800 KB Output is correct
28 Correct 31 ms 1800 KB Output is correct
29 Correct 35 ms 1800 KB Output is correct
30 Correct 32 ms 1856 KB Output is correct
31 Correct 1081 ms 2784 KB Output is correct
32 Correct 246 ms 2784 KB Output is correct
33 Correct 102 ms 2784 KB Output is correct
34 Correct 92 ms 2784 KB Output is correct
35 Correct 1366 ms 3696 KB Output is correct
36 Correct 1130 ms 4224 KB Output is correct
37 Execution timed out 1564 ms 4468 KB Time limit exceeded
38 Correct 767 ms 5432 KB Output is correct
39 Execution timed out 1578 ms 5432 KB Time limit exceeded
40 Execution timed out 1573 ms 5432 KB Time limit exceeded
41 Execution timed out 1574 ms 5432 KB Time limit exceeded
42 Execution timed out 1577 ms 5620 KB Time limit exceeded
43 Execution timed out 1573 ms 6148 KB Time limit exceeded
44 Execution timed out 1576 ms 7228 KB Time limit exceeded
45 Execution timed out 1569 ms 7312 KB Time limit exceeded
46 Execution timed out 1583 ms 8504 KB Time limit exceeded
47 Execution timed out 1572 ms 9392 KB Time limit exceeded
48 Execution timed out 1571 ms 10376 KB Time limit exceeded
49 Execution timed out 1569 ms 11388 KB Time limit exceeded
50 Execution timed out 1555 ms 11388 KB Time limit exceeded