QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#405004#3005. MonotonyLspeed#TL 1263ms3808kbC++143.6kb2024-05-05 06:19:092024-05-05 06:19:11

Judging History

This is the latest submission verdict.

  • [2024-05-05 06:19:11]
  • Judged
  • Verdict: TL
  • Time: 1263ms
  • Memory: 3808kb
  • [2024-05-05 06:19:09]
  • Submitted

answer

#include <iostream>     // Input/output stream objects
#include <fstream>      // File stream objects
#include <sstream>      // String stream objects
#include <iomanip>      // Input/output manipulators
#include <string>       // String class and functions
#include <vector>       // Dynamic array
#include <list>         // Doubly linked list
#include <set>          // Set container
#include <map>          // Map container
#include <queue>        // Queue container
#include <stack>        // Stack container
#include <algorithm>    // Algorithms on sequences (e.g., sort, find)
#include <cmath>        // Mathematical functions
#include <ctime>        // Date and time functions
#include <cstdlib>      // General purpose functions (e.g., memory management)
#include <cstring>      // C-style string functions
#include <cctype>       // Character classification functions
#include <cassert>      // Assert function for debugging
#include <exception>    // Standard exceptions
#include <functional>   // Function objects
#include <iterator>     // Iterator classes
#include <limits>       // Numeric limits
#include <locale>       // Localization and internationalization
#include <numeric>      // Numeric operations (e.g., accumulate)
#include <random>       // Random number generators
#include <stdexcept>    // Standard exception classes
#include <typeinfo>     // Runtime type information
#include <utility>      // Utility components (e.g., std::pair)
#include <bitset>

using namespace std;

#define FOR(i, a, b) for(int i = a; i < (b); i++)
#define FORE(i, a, b) for(int i = a; i <= (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
#define x first 
#define y second
#define mp make_pair
#define PI 3.141592653
const double eps = 1e-9;

int r, c;
ll ans;
vector<vector<int>> grid(22, vector<int>(22, 0));

bool check_mono(int col, int rows) {
    vector<int> num;
    FOR (i, 0, r) if ((1 << i) & rows) num.push_back(grid[i][col]);
    if (sz(num) == 1) return true;
    bool inc = (num[0] < num[1]);
    FOR (i, 1, sz(num) - 1) if (inc != (num[i] < num[i+1])) return false;
    return true;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
    cin >> r >> c;
    
    FOR (i, 0, r) FOR (j, 0, c) cin >> grid[i][j];

    vector<vector<ll>> dp(c + 5, vector<ll>(405, 0LL));
    FOR (rows, 1, 1 << r) {

        // preprocess
        // parity[i][j] from j to i (1 = increase, 0 = decrease)
        vector<vector<int>> parity(22, vector<int>(22, 0));
        vector<int> coord;
        FOR (i, 0, c) FOR (j, i+1, c) {
            FOR (k, 0, r) if ((rows & (1 << k)) != 0 && grid[k][j] > grid[k][i]) parity[j][i] += (1 << k);
        }

        FOR (i, 1, c) FORE (j, 0, i-1) coord.push_back(parity[i][j]);
        sort(all(coord));
        coord.resize(unique(all(coord)) - coord.begin());
        auto get_id = [&](int x) { return lower_bound(all(coord), x) - coord.begin(); };
        FOR (i, 1, c) FORE (j, 0, i-1) parity[i][j] = get_id(parity[i][j]);

        vector<bool> is_mono(c + 5, true);
        FOR (i, 0, c) is_mono[i] = check_mono(i, rows);
        FOR (i, 0, c) {
            if (!is_mono[i]) continue;
            ans += 1;
            for (int j = 0; j <= i-1; j++) if (is_mono[j]) {
                ans += dp[j][parity[i][j]] + 1;
                dp[i][parity[i][j]] += dp[j][parity[i][j]] + 1;
            }
        }

        FOR (i, 0, c) {
            for (int j = 0; j <= i-1; j++) {
                dp[i][parity[i][j]] = 0;
            }
        }
    }
    cout << ans << endl;
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 117ms
memory: 3808kb

input:

13 17
155 221 29 114 165 175 120 107 27 134 125 116 11 56 100 20 15
88 128 38 111 198 212 173 136 44 164 64 150 83 19 93 197 194
219 25 16 50 2 55 151 34 163 76 217 209 141 174 80 144 159
60 156 54 149 97 28 102 145 26 35 188 160 65 152 135 6 21
8 62 195 70 127 30 171 205 147 177 210 117 10 103 161 ...

output:

40490

result:

ok answer is '40490'

Test #2:

score: 0
Accepted
time: 1263ms
memory: 3604kb

input:

17 13
41 205 162 42 35 147 143 185 89 176 4 103 181
85 115 59 2 182 190 20 142 97 172 31 211 101
154 62 119 82 16 117 1 157 72 3 149 216 21
188 49 202 77 161 160 169 46 79 133 50 113 91
30 71 156 193 92 29 54 187 111 199 80 22 17
209 122 138 127 135 81 53 86 158 145 109 56 6
150 40 15 212 10 186 32 ...

output:

35799

result:

ok answer is '35799'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3588kb

input:

1 1
1

output:

1

result:

ok answer is '1'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3544kb

input:

1 2
1 2

output:

3

result:

ok answer is '3'

Test #5:

score: 0
Accepted
time: 0ms
memory: 3600kb

input:

1 2
2 1

output:

3

result:

ok answer is '3'

Test #6:

score: 0
Accepted
time: 0ms
memory: 3584kb

input:

1 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

output:

1048575

result:

ok answer is '1048575'

Test #7:

score: 0
Accepted
time: 0ms
memory: 3512kb

input:

2 1
1
2

output:

3

result:

ok answer is '3'

Test #8:

score: 0
Accepted
time: 0ms
memory: 3772kb

input:

2 1
2
1

output:

3

result:

ok answer is '3'

Test #9:

score: 0
Accepted
time: 0ms
memory: 3612kb

input:

2 2
1 2
3 4

output:

9

result:

ok answer is '9'

Test #10:

score: 0
Accepted
time: 1ms
memory: 3620kb

input:

2 2
1 3
4 2

output:

9

result:

ok answer is '9'

Test #11:

score: 0
Accepted
time: 0ms
memory: 3528kb

input:

2 2
2 1
4 3

output:

9

result:

ok answer is '9'

Test #12:

score: 0
Accepted
time: 0ms
memory: 3620kb

input:

2 2
3 2
4 1

output:

9

result:

ok answer is '9'

Test #13:

score: 0
Accepted
time: 0ms
memory: 3584kb

input:

2 2
4 3
2 1

output:

9

result:

ok answer is '9'

Test #14:

score: 0
Accepted
time: 0ms
memory: 3520kb

input:

4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

output:

225

result:

ok answer is '225'

Test #15:

score: 0
Accepted
time: 0ms
memory: 3800kb

input:

4 4
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13

output:

225

result:

ok answer is '225'

Test #16:

score: 0
Accepted
time: 1ms
memory: 3532kb

input:

4 4
4 5 1 3
11 6 13 10
16 2 9 8
14 15 12 7

output:

110

result:

ok answer is '110'

Test #17:

score: 0
Accepted
time: 609ms
memory: 3516kb

input:

20 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

output:

1048575

result:

ok answer is '1048575'

Test #18:

score: -100
Time Limit Exceeded

input:

20 19
180 179 178 177 176 175 174 173 172 190 189 188 187 186 185 184 183 182 181
161 160 159 158 157 156 155 154 153 171 170 169 168 167 166 165 164 163 162
142 141 140 139 138 137 136 135 134 152 151 150 149 148 147 146 145 144 143
123 122 121 120 119 118 117 116 115 133 132 131 130 129 128 127 12...

output:


result: