CSES - Datatähti 2023 alku - Results
Submission details
Task:Ruudukko
Sender:jmarttinen
Submission time:2022-11-02 11:19:11 +0200
Language:C++ (C++11)
Status:READY
Result:61
Feedback
groupverdictscore
#1ACCEPTED28
#2ACCEPTED33
#30
Test results
testverdicttimegroup
#1ACCEPTED0.00 s1, 2, 3details
#2ACCEPTED0.00 s1, 2, 3details
#3ACCEPTED0.00 s1, 2, 3details
#4ACCEPTED0.18 s2, 3details
#5ACCEPTED0.35 s2, 3details
#6ACCEPTED0.44 s2, 3details
#7--3details
#8--3details
#9--3details

Code

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;
int n;
vector<int> table;
set<int> numbers;
map<int, set<int>> numberLocations;
map<int, long long> hashTable;
int counted = 0;
bool areConnected(int a, int b) {
    return ((a - b) % n == 0) || (a/n == b/n);
}
// O(n)
set<int> allConnectedCells(int a) {
    set<int> connectedCells;
    for (int i=0; i<n; i++) {
        connectedCells.insert(n*i + (a % n));
        connectedCells.insert(a - (a % n) + i);
    }

    return connectedCells;
}

long long countPaths(int i) {
    long long total = 0;
    // counted++;
    if (hashTable.count(i) != 0) {
        total = hashTable[i];
    }
    else {
        set<int> connectedCells = allConnectedCells(i);
        for (int cell: connectedCells) {
            if (table[cell] < table[i]) // connected per the rule
                total += (countPaths(cell) + 1) % 1000000007; 
        }
        hashTable[i] = total;
    }
    return total;
}

int main() {
    int k;
    cin >> n;
    
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            cin >> k;
            numbers.insert(k);
            table.push_back(k);
            if (numberLocations.count(k) == 0) {
                numberLocations[k] = set<int>();
            }
            numberLocations[k].insert(n*i+j);
        }
    }
    long long total = n*n;
    // O(n^2)
    for (auto rit = numbers.rbegin(); rit != numbers.rend(); rit++) {
        for (auto i: numberLocations[*rit]) {

            if (hashTable.count(i) == 0)
                k = countPaths(i);
        }
    }
    for (auto x: hashTable) {
        total += x.second;
        total = total % 1000000007;
    }
    cout << total << "\n";
}

Test details

Test 1

Group: 1, 2, 3

Verdict: ACCEPTED

input
3
1 1 1
1 1 1
1 1 1

correct output
9

user output
9

Test 2

Group: 1, 2, 3

Verdict: ACCEPTED

input
3
1 2 3
6 5 4
7 8 9

correct output
135

user output
135

Test 3

Group: 1, 2, 3

Verdict: ACCEPTED

input
3
7 8 1
4 5 4
3 9 6

correct output
57

user output
57

Test 4

Group: 2, 3

Verdict: ACCEPTED

input
100
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
10000

user output
10000

Test 5

Group: 2, 3

Verdict: ACCEPTED

input
100
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
187458477

user output
187458477

Test 6

Group: 2, 3

Verdict: ACCEPTED

input
100
2995 8734 1018 2513 7971 5063 ...

correct output
964692694

user output
964692694

Test 7

Group: 3

Verdict:

input
1000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
1000000

user output
(empty)

Test 8

Group: 3

Verdict:

input
1000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
229147081

user output
(empty)

Test 9

Group: 3

Verdict:

input
1000
520283 805991 492643 75254 527...

correct output
951147313

user output
(empty)