Submission #841757

# Submission time Handle Problem Language Result Execution time Memory
841757 2023-09-02T03:27:16 Z olnyfcxwps Soccer Stadium (IOI23_soccer) C++17
36 / 100
4500 ms 65052 KB
#include <iostream>
#include <vector>
using namespace std;

const int N = 30;
int n;
int tree[2000][2000];
bool seen[2000][2000];
int prefix[2000][2000];
int dp1[N][N][N];
int dp2[N][N][N];
int max_area;
vector<int> startP;
vector<int> endP;

bool are_empty(int row, int col1, int col2) {
    if (col1 == 0) {
        return prefix[row][col2] == 0;
    }
    return prefix[row][col2] == prefix[row][col1 - 1];
}

int solve_1() {
    int x = 0;
    int y = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (tree[i][j] == 1) {
                x = i;
                y = j;
            }
        }
    }
    x += 1;
    y += 1;
    int m = x * y;
    int t = x * (n - y + 1);
    if (t < m) {
        m = t;
    }
    t = (n - x + 1) * y;
    if (t < m) {
        m = t;
    }
    t = (n - x + 1) * (n - y + 1);
    if (t < m) {
        m = t;
    }
    return n * n - m;
}

void dfs(int r, int c) {
    seen[r][c] = true;
    if (r - 1 >= 0 && tree[r - 1][c] == 0 && !seen[r - 1][c]) {
        dfs(r - 1, c);
    }
    if (r + 1 < n && tree[r + 1][c] == 0 && !seen[r + 1][c]) {
        dfs(r + 1, c);
    }
    if (c - 1 >= 0 && tree[r][c - 1] == 0 && !seen[r][c - 1]) {
        dfs(r, c - 1);
    }
    if (c + 1 < n && tree[r][c + 1] == 0 && !seen[r][c + 1]) {
        dfs(r, c + 1);
    }
}

bool check() {
    int c = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            seen[i][j] = false;
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (tree[i][j] == 0) {
                if (!seen[i][j]) {
                    dfs(i, j);
                    c += 1;
                }
            }
        }
    }
    if (c != 1) {
        return false;
    }

    vector<int> s;
    vector<int> t;
    int longest = 0;
    int where = 0;
    for (int i = 0; i < n; i++) {
        bool seen = false;
        for (int j = 0; j < n;) {
            if (tree[i][j] == 0) {
                if (seen) {
                    return false;
                }
                seen = true;
                int k = j + 1;
                while (k < n && tree[i][k] == 0) {
                    k++;
                }
                s.push_back(j);
                t.push_back(k - 1);
                if (longest < k - j) {
                    longest = k - j;
                    where = s.size() - 1;
                }
                j = k;
            } else {
                j++;
            }
        }
    }

    /*for (int i = 0; i < s.size(); i++) {
        cout << s[i] << " " << t[i] << "\n";
    }
    cout << where << "\n";*/

    for (int i = where - 1; i >= 0; i--) {
        if (s[i] < s[i + 1] || t[i] > t[i + 1]) {
            return false;
        }
    }

    for (int i = where; i + 1 < s.size(); i++) {
        // cout << i << "\n";
        if (s[i + 1] < s[i] || t[i + 1] > t[i]) {
            return false;
        }
    }
    // cout << "here\n";

    for (int i = 0; i < s.size(); i++) {
        for (int j = i + 1; j < s.size(); j++) {
            if (!(s[i] <= s[j] && t[j] <= t[i]
                || s[j] <= s[i] && t[i] <= t[j]
            )) {
                return false;
            }
        }
    }

    return true;
}

bool is_possible(int row) {
    bool start_shrink = false;
    int t = endP[0] - startP[0] + 1;
    int l = t;
    for (int i = 1; i < startP.size(); i++) {
        int l1 = startP[i - 1];
        int r1 = endP[i - 1];
        int l2 = startP[i];
        int r2 = endP[i];
        t += r2 - l2 + 1;
        l = r2 - l2 + 1;
        // check if include
        if (!(l1 <= l2 && r2 <= r1 || l2 <= l1 && r1 <= r2)) {
            return false;
        }
        // check if shrink
        if (r2 - l2 < r1 - l1) {
            start_shrink = true;
        } else if (r2 - l2 > r1 - l1) {
            if (start_shrink) {
                return false;
            }
        }
    }

    if (start_shrink) {
        if (t + l * (n - row - 1) <= max_area) {
            return false;
        }
    }

    for (int i = 0; i < startP.size(); i++) {
        for (int j = i; j < startP.size(); j++) {
            int l1 = startP[i];
            int r1 = endP[i];
            int l2 = startP[j];
            int r2 = endP[j];
            // check if include
            if (!(l1 <= l2 && r2 <= r1 || l2 <= l1 && r1 <= r2)) {
                return false;
            }
        }
    }

    return true;
}

void brute_force(int row, int a) {
    if (row == n) {
        return;
    }
    // skip row if possible
    if (startP.empty()) {
        brute_force(row + 1, a);
    }
    // brute force
    for (int i = 0; i < n; i++) {
        for (int j = n - 1; j >= i; j--) {
            if (!are_empty(row, i, j)) {
                continue;
            }
            startP.push_back(i);
            endP.push_back(j);
            if (is_possible(row)) {
                if (a + j - i + 1 > max_area) {
                    max_area = a + j - i + 1;
                }
                brute_force(row + 1, a + j - i + 1);
            }
            startP.pop_back();
            endP.pop_back();
        }
    }
}

// int biggest_stadium(int N, int[][] F)
int biggest_stadium(int X, vector<vector<int>> F) {
    n = X;
    int total = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            tree[i][j] = F[i][j];
            total += tree[i][j];
            prefix[i][j] = tree[i][j];
            if (j - 1 >= 0) {
                prefix[i][j] += prefix[i][j - 1];
            }
        }
    }
    if (total == 1) {
        return solve_1();
    }
    if (total == 0) {
        return n * n;
    }
    /*if (n > 30) {
        return 123;
    }*/

    if (check()) {
        return n * n - total;
    }

    if (n <= 30) {
        brute_force(0, 0);
        return max_area;
    }

    return 0;
}

Compilation message

soccer.cpp: In function 'bool check()':
soccer.cpp:129:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |     for (int i = where; i + 1 < s.size(); i++) {
      |                         ~~~~~~^~~~~~~~~~
soccer.cpp:137:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  137 |     for (int i = 0; i < s.size(); i++) {
      |                     ~~^~~~~~~~~~
soccer.cpp:138:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  138 |         for (int j = i + 1; j < s.size(); j++) {
      |                             ~~^~~~~~~~~~
soccer.cpp:139:32: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  139 |             if (!(s[i] <= s[j] && t[j] <= t[i]
soccer.cpp: In function 'bool is_possible(int)':
soccer.cpp:154:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  154 |     for (int i = 1; i < startP.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
soccer.cpp:162:24: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  162 |         if (!(l1 <= l2 && r2 <= r1 || l2 <= l1 && r1 <= r2)) {
      |               ~~~~~~~~~^~~~~~~~~~~
soccer.cpp:181:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  181 |     for (int i = 0; i < startP.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
soccer.cpp:182:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  182 |         for (int j = i; j < startP.size(); j++) {
      |                         ~~^~~~~~~~~~~~~~~
soccer.cpp:188:28: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  188 |             if (!(l1 <= l2 && r2 <= r1 || l2 <= l1 && r1 <= r2)) {
      |                   ~~~~~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB ok
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB ok
2 Correct 1 ms 4444 KB ok
3 Correct 1 ms 4444 KB ok
4 Correct 1 ms 4444 KB ok
5 Correct 1 ms 4444 KB ok
6 Correct 1 ms 4444 KB ok
7 Correct 1 ms 4524 KB ok
8 Correct 17 ms 12636 KB ok
9 Correct 243 ms 65052 KB ok
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB ok
2 Correct 1 ms 4444 KB ok
3 Correct 1 ms 6492 KB ok
4 Correct 1 ms 6492 KB ok
5 Correct 1 ms 6488 KB ok
6 Correct 1 ms 6492 KB ok
7 Correct 1 ms 6492 KB ok
8 Correct 1 ms 6492 KB ok
9 Correct 1 ms 4444 KB ok
10 Correct 1 ms 6492 KB ok
11 Correct 1 ms 6492 KB ok
12 Correct 1 ms 6492 KB ok
13 Correct 1 ms 6492 KB ok
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB ok
2 Correct 1 ms 4444 KB ok
3 Correct 1 ms 4444 KB ok
4 Correct 1 ms 6492 KB ok
5 Correct 1 ms 6492 KB ok
6 Correct 1 ms 6488 KB ok
7 Correct 1 ms 6492 KB ok
8 Correct 1 ms 6492 KB ok
9 Correct 1 ms 6492 KB ok
10 Correct 1 ms 4444 KB ok
11 Correct 1 ms 6492 KB ok
12 Correct 1 ms 6492 KB ok
13 Correct 1 ms 6492 KB ok
14 Correct 1 ms 6492 KB ok
15 Correct 9 ms 6488 KB ok
16 Correct 3 ms 6596 KB ok
17 Correct 1 ms 6492 KB ok
18 Correct 1 ms 6492 KB ok
19 Correct 1 ms 6492 KB ok
20 Correct 1 ms 6492 KB ok
21 Correct 1 ms 6492 KB ok
22 Correct 1 ms 6492 KB ok
23 Correct 1 ms 6492 KB ok
24 Correct 1 ms 6492 KB ok
25 Correct 1 ms 6492 KB ok
26 Correct 2 ms 6500 KB ok
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB ok
2 Correct 1 ms 4444 KB ok
3 Correct 1 ms 4444 KB ok
4 Correct 1 ms 4444 KB ok
5 Correct 1 ms 4444 KB ok
6 Correct 1 ms 6492 KB ok
7 Correct 1 ms 6492 KB ok
8 Correct 1 ms 6488 KB ok
9 Correct 1 ms 6492 KB ok
10 Correct 1 ms 6492 KB ok
11 Correct 1 ms 6492 KB ok
12 Correct 1 ms 4444 KB ok
13 Correct 1 ms 6492 KB ok
14 Correct 1 ms 6492 KB ok
15 Correct 1 ms 6492 KB ok
16 Correct 1 ms 6492 KB ok
17 Correct 9 ms 6488 KB ok
18 Correct 3 ms 6596 KB ok
19 Correct 1 ms 6492 KB ok
20 Correct 1 ms 6492 KB ok
21 Correct 1 ms 6492 KB ok
22 Correct 1 ms 6492 KB ok
23 Correct 1 ms 6492 KB ok
24 Correct 1 ms 6492 KB ok
25 Correct 1 ms 6492 KB ok
26 Correct 1 ms 6492 KB ok
27 Correct 1 ms 6492 KB ok
28 Correct 2 ms 6500 KB ok
29 Correct 1 ms 6488 KB ok
30 Execution timed out 4589 ms 6492 KB Time limit exceeded
31 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB ok
2 Correct 1 ms 4444 KB ok
3 Correct 1 ms 4444 KB ok
4 Correct 1 ms 4444 KB ok
5 Correct 1 ms 4444 KB ok
6 Correct 1 ms 6492 KB ok
7 Correct 1 ms 6492 KB ok
8 Correct 1 ms 6488 KB ok
9 Correct 1 ms 6492 KB ok
10 Correct 1 ms 6492 KB ok
11 Correct 1 ms 6492 KB ok
12 Correct 1 ms 4444 KB ok
13 Correct 1 ms 6492 KB ok
14 Correct 1 ms 6492 KB ok
15 Correct 1 ms 6492 KB ok
16 Correct 1 ms 6492 KB ok
17 Correct 9 ms 6488 KB ok
18 Correct 3 ms 6596 KB ok
19 Correct 1 ms 6492 KB ok
20 Correct 1 ms 6492 KB ok
21 Correct 1 ms 6492 KB ok
22 Correct 1 ms 6492 KB ok
23 Correct 1 ms 6492 KB ok
24 Correct 1 ms 6492 KB ok
25 Correct 1 ms 6492 KB ok
26 Correct 1 ms 6492 KB ok
27 Correct 1 ms 6492 KB ok
28 Correct 2 ms 6500 KB ok
29 Correct 1 ms 6488 KB ok
30 Execution timed out 4589 ms 6492 KB Time limit exceeded
31 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB ok
2 Correct 1 ms 4444 KB ok
3 Correct 1 ms 4444 KB ok
4 Correct 1 ms 4444 KB ok
5 Correct 1 ms 4444 KB ok
6 Correct 1 ms 4444 KB ok
7 Correct 1 ms 4444 KB ok
8 Correct 1 ms 4524 KB ok
9 Correct 17 ms 12636 KB ok
10 Correct 243 ms 65052 KB ok
11 Correct 1 ms 6492 KB ok
12 Correct 1 ms 6492 KB ok
13 Correct 1 ms 6488 KB ok
14 Correct 1 ms 6492 KB ok
15 Correct 1 ms 6492 KB ok
16 Correct 1 ms 6492 KB ok
17 Correct 1 ms 4444 KB ok
18 Correct 1 ms 6492 KB ok
19 Correct 1 ms 6492 KB ok
20 Correct 1 ms 6492 KB ok
21 Correct 1 ms 6492 KB ok
22 Correct 9 ms 6488 KB ok
23 Correct 3 ms 6596 KB ok
24 Correct 1 ms 6492 KB ok
25 Correct 1 ms 6492 KB ok
26 Correct 1 ms 6492 KB ok
27 Correct 1 ms 6492 KB ok
28 Correct 1 ms 6492 KB ok
29 Correct 1 ms 6492 KB ok
30 Correct 1 ms 6492 KB ok
31 Correct 1 ms 6492 KB ok
32 Correct 1 ms 6492 KB ok
33 Correct 2 ms 6500 KB ok
34 Correct 1 ms 6488 KB ok
35 Execution timed out 4589 ms 6492 KB Time limit exceeded
36 Halted 0 ms 0 KB -