CSES - Datatähti 2025 alku - Results
Submission details
Task:Niitty
Sender:Pikaksi
Submission time:2024-11-03 12:10:17 +0200
Language:C++ (C++20)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
#40
#50
#60
Test results
testverdicttimegroup
#1--1, 2, 3, 4, 5, 6details
#2ACCEPTED0.00 s1, 2, 3, 4, 5, 6details
#3--1, 2, 3, 4, 5, 6details
#4ACCEPTED0.00 s1, 2, 3, 4, 5, 6details
#5ACCEPTED0.00 s1, 2, 3, 4, 5, 6details
#6--2, 3, 4, 5, 6details
#7--2, 3, 4, 5, 6details
#8--2, 3, 4, 5, 6details
#9ACCEPTED0.00 s2, 3, 4, 5, 6details
#10--3, 4, 5, 6details
#11--3, 4, 5, 6details
#12--3, 4, 5, 6details
#13ACCEPTED0.00 s3, 4, 5, 6details
#14--4, 5, 6details
#15--4, 5, 6details
#16--4, 5, 6details
#17ACCEPTED0.00 s4, 5, 6details
#18--5, 6details
#19--5, 6details
#20--5, 6details
#21--5, 6details
#22--6details
#23--6details
#24--6details
#25--6details

Compiler report

input/code.cpp: In function 'bool closeLeftEdge(Fence, std::array<int, 26>&)':
input/code.cpp:33:51: warning: array subscript has type 'char' [-Wchar-subscripts]
   33 |         removedFlowers[field[fence.y + k][fence.x]] += 1;
      |                                                   ^
input/code.cpp: In function 'bool closeRightEdge(Fence, std::array<int, 26>&)':
input/code.cpp:55:69: warning: array subscript has type 'char' [-Wchar-subscripts]
   55 |         removedFlowers[field[fence.y + k][fence.x + fence.sizeX - 1]] += 1;
      |                                                                     ^
input/code.cpp: In function 'bool closeTopEdge(Fence, std::array<int, 26>&)':
input/code.cpp:76:51: warning: array subscript has type 'char' [-Wchar-subscripts]
   76 |         removedFlowers[field[fence.y][fence.x + k]] += 1;
      |                                                   ^
input/code.cpp: In function 'bool closeBottomEdge(Fence, std::array<int, 26>&)':
input/code.cpp:100:...

Code

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

union Fence
{
    struct {
        uint16_t x;
        uint16_t y;
        uint16_t sizeX;
        uint16_t sizeY;
    };
    ll bytes;
};

bool containedNumbers[26] = {0};
vector<vector<char>> field;
unordered_set<ll> elementaryFences;
vector<Fence> eFencesFinal;
int n = 0;

void printFence(Fence fence)
{
    cout << fence.x << " " << fence.y << " " << fence.sizeX << " " << fence.sizeY << "\n";
}

bool closeLeftEdge(Fence fence, array<int, 26>& flowerCount)
{
    int removedFlowers[26] = {0};

    for (int k = 0; k < fence.sizeY; k++) {
        removedFlowers[field[fence.y + k][fence.x]] += 1;
    }
    
    for (int k = 0; k < 26; k++) {
        if (containedNumbers[k] && flowerCount[k] - removedFlowers[k] <= 0) {
            return false;
        }
    }
        
    for (int i = 0; i < 26; i++) {
        if (containedNumbers[i]) {
            flowerCount[i] -= removedFlowers[i];
        }
    }
    return true;
}

bool closeRightEdge(Fence fence, array<int, 26>& flowerCount)
{
    int removedFlowers[26] = {0};

    for (int k = 0; k < fence.sizeY; k++) {
        removedFlowers[field[fence.y + k][fence.x + fence.sizeX - 1]] += 1;
    }
    
    for (int k = 0; k < 26; k++) {
        if (containedNumbers[k] && flowerCount[k] - removedFlowers[k] <= 0) {
            return false;
        }
    }

    for (int i = 0; i < 26; i++) {
        if (containedNumbers[i]) {
            flowerCount[i] -= removedFlowers[i];
        }
    }
    return true;
}
bool closeTopEdge(Fence fence, array<int, 26>& flowerCount)
{
    int removedFlowers[26] = {0};

    for (int k = 0; k < fence.sizeX; k++) {
        removedFlowers[field[fence.y][fence.x + k]] += 1;
    }
    
    for (int k = 0; k < 26; k++) {
        if (containedNumbers[k] && flowerCount[k] - removedFlowers[k] <= 0) {
            return false;
        }
    }
    
    for (int i = 0; i < 26; i++) {
        if (containedNumbers[i]) {
            flowerCount[i] -= removedFlowers[i];
        }
    }
    return true;
}

bool closeBottomEdge(Fence fence, array<int, 26>& flowerCount)
{
    int removedFlowers[26] = {0};
    
    int y = fence.y + fence.sizeY - 1;
    
    for (int k = 0; k < fence.sizeX; k++) {
        removedFlowers[field[y][fence.x + k]] += 1;
    }
    
    for (int k = 0; k < 26; k++) {
        if (containedNumbers[k] && flowerCount[k] - removedFlowers[k] <= 0) {
            return false;
        }
    }
    
    for (int i = 0; i < 26; i++) {
        if (containedNumbers[i]) {
            flowerCount[i] -= removedFlowers[i];
        }
    }
    return true;
}

Fence findElementaryFence(Fence fence, array<int, 26>& flowerCount)
{
    //printFence(fence);
    while (closeLeftEdge(fence, flowerCount)) {
        fence.x += 1;
        fence.sizeX -= 1;
    }
    //printFence(fence);

    while (closeRightEdge(fence, flowerCount)) {
        fence.sizeX -= 1;
    }
    //printFence(fence);

    while (closeTopEdge(fence, flowerCount)) {
        fence.y += 1;
        fence.sizeY -= 1;
    }
    //printFence(fence);

    while (closeBottomEdge(fence, flowerCount)) {
        fence.sizeY -= 1;
    }
    //printFence(fence);

    return fence;
}

array<int, 26> getFlowerCount(Fence fence)
{
    //printFence(fence);
    array<int, 26> flowerCount = {0};

    for (int y = 0; y < fence.sizeY; y++) {
        for (int x = 0; x < fence.sizeX; x++) {
            flowerCount[field[fence.y + y][fence.x + x]] += 1;
        }
    }
    return flowerCount;
}

void splitFence(Fence fence, Fence elementaryFence, vector<Fence>& outFence)
{
    uint16_t r = elementaryFence.x + elementaryFence.sizeX - 1;
    uint16_t l = elementaryFence.x;
    uint16_t d = elementaryFence.y + elementaryFence.sizeY - 1;
    uint16_t u = elementaryFence.y;

    if (fence.x != r) {
        Fence fenceAdd = {fence.x, fence.y, r - fence.x, fence.sizeY};
        outFence.push_back(fenceAdd);
    }
    if (fence.y != d) {
        Fence fenceAdd = {fence.x, fence.y, fence.sizeX, d - fence.y};
        outFence.push_back(fenceAdd);
    }
    if (fence.x + fence.sizeX - 1 != l) {
        Fence fenceAdd = {elementaryFence.x + 1, fence.y, fence.x + fence.sizeX - l - 1, fence.sizeY};
        outFence.push_back(fenceAdd);
    }
    if (fence.y + fence.sizeY - 1 != u) {
        Fence fenceAdd = {fence.x, elementaryFence.y + 1, fence.sizeX, fence.y + fence.sizeY - u - 1};
        outFence.push_back(fenceAdd);
    }

    /*cout << "splittin fence = ";
    printFence(fence);
    cout << "with elementaryFence = ";
    printFence(elementaryFence);
    for (Fence fence : outFence) {
        cout << "  ";
        printFence(fence);
    }
    cout << "\n";*/
}

void recursiveSplit(Fence fence, array<int, 26>& flowerCount)
{
    Fence eFence = findElementaryFence(fence, flowerCount);
    elementaryFences.insert(eFence.bytes);

    vector<Fence> splitFences;
    splitFence(fence, eFence, splitFences);

    //cout << "eFence = ";
    //printFence(eFence);
    //cout << "split fences = " << splitFences.size() << "\n";

    for (int i = 0; i < splitFences.size(); i++) {
        array<int, 26> splitFlowerCount = getFlowerCount(splitFences[i]);

        bool isValid = true;
        for (int k = 0; k < 26; k++) {
            if (containedNumbers[k] && splitFlowerCount[k] <= 0) {
                isValid = false;
                break;
            }
        }
        if (isValid) {
            recursiveSplit(splitFences[i], splitFlowerCount);
        }
    }   
}

ll eFenceArea(vector<Fence>& eFences)
{
    uint16_t u = 600, d = 0, l = 600, r = 0;

    for (int i = 0; i < eFences.size(); i++) {
        Fence fence = eFences[i];
        u = min(u, fence.y);
        d = max(d, (uint16_t)(fence.y + fence.sizeY - (uint16_t)1));
        l = min(l, fence.x);
        r = max(r, (uint16_t)(fence.x + fence.sizeX - (uint16_t)1));
    }
    ll area = l + 1LL;
    area *= n - r;
    area *= u + 1LL;
    area *= n - d;
    return area;
}

ll areaRecursive(int calls, int startIndex, vector<Fence>& fences)
{
    ll area = 0;
    if (calls == 0) {
        area += eFenceArea(fences);
    }

    for (int i = startIndex; i < eFencesFinal.size(); i++) {
        //cout << "recursiveLoop\n";
        fences.push_back(eFencesFinal[i]);
        area += areaRecursive(calls - 1, i + 1, fences);
        fences.pop_back();
    }

    return area;
}

void calculateCombinations()
{
    for (ll fenceBytes : elementaryFences) {
        Fence fence;
        fence.bytes = fenceBytes;
        eFencesFinal.push_back(fence);
    }

    ll area = 0;
    for (int i = 0; i < eFencesFinal.size(); i++) {
        //cout << "startLoop\n";
        vector<Fence> fences;
        ll areaLoop = areaRecursive(i + 1, 0, fences);
        if (i & 1) {
            area -= areaLoop;
        }
        else {
            area += areaLoop;
        }
    }
    cout << area;
}

int main()
{
    //vector<string> testField = {"AAAAA", "AAAAA", "AABAA", "AAACA", "AAADA"};
    //vector<string> testField = {"BAA", "AAB", "AAA"};
    //vector<string> testField = {"AAAK", "ABAA", "ABAA", "AAAA"};
    //n = testField.size();
    cin >> n;

    field = vector<vector<char>>(n, vector<char>(n));

    for (int i = 0; i < n; i++) {
        string row = "";
        cin >> row;
        //string row = testField[i];

        for (int k = 0; k < n; k++) {
            char value = row[k] - 'A';
            field[i][k] = value;
            containedNumbers[value] = true;
        }
    }

    array<int, 26> flowerCount = {0};
    for (vector<char> fieldRow : field) {
        for (char character : fieldRow) {
            flowerCount[character]++;
            //cout << (int)character;
        }
        //cout << "\n";
    }

    /*for (int i = 0; i < 26; i++) {
        cout << (int)containedNumbers[i];
    }*/

    Fence startFence = {0, 0, (uint16_t)n, (uint16_t)n};
    recursiveSplit(startFence, flowerCount);

    /*cout << "efences:\n";
    for (ll fenceBytes : elementaryFences) {
        Fence eFence;
        eFence.bytes = fenceBytes;
        printFence(eFence);
    }*/

    calculateCombinations();
}

Test details

Test 1

Group: 1, 2, 3, 4, 5, 6

Verdict:

input
10
TNCTNPNTPC
NPPNTNTPTP
NTNTTCNTCT
NPCPNPPNTT
...

correct output
2035

user output
(empty)

Test 2

Group: 1, 2, 3, 4, 5, 6

Verdict: ACCEPTED

input
10
NFWQLWNWYS
DZOQJVXFPJ
CNHXPXMCQD
QRTBVNLTQC
...

correct output
9

user output
9

Test 3

Group: 1, 2, 3, 4, 5, 6

Verdict:

input
10
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
...

correct output
3025

user output
(empty)

Test 4

Group: 1, 2, 3, 4, 5, 6

Verdict: ACCEPTED

input
10
FFFFFFFFFF
FFFFFCFFFF
FFFFFFJFFF
FFFFFFFFFF
...

correct output
12

user output
12

Test 5

Group: 1, 2, 3, 4, 5, 6

Verdict: ACCEPTED

input
1
X

correct output
1

user output
1

Test 6

Group: 2, 3, 4, 5, 6

Verdict:

input
20
BBCBUBOUOBBCUUBBCOUO
BOUCOOCUBCOOOCOBOCUO
UCCUUUOBCOCBCBUBUCOO
BUOBUCUCUOOBCOOUBUOO
...

correct output
38724

user output
(empty)

Test 7

Group: 2, 3, 4, 5, 6

Verdict:

input
20
CBGLSHGZHYZDWBNDBJUG
SMUXOJQYPXZDTMJUIWOJ
XIDSTNBGHKRKOVUVMINB
MTQGCFRUHQKALXRNCQGS
...

correct output
8334

user output
(empty)

Test 8

Group: 2, 3, 4, 5, 6

Verdict:

input
20
KKKKKKKKKKKKKKKKKKKK
KKKKKKKKKKKKKKKKKKKK
KKKKKKKKKKKKKKKKKKKK
KKKKKKKKKKKKKKKKKKKK
...

correct output
44100

user output
(empty)

Test 9

Group: 2, 3, 4, 5, 6

Verdict: ACCEPTED

input
20
AAAAAAAAXAAAAAAAAAAA
AAAWAAAAAAAAAAAAAOAA
AAAAAAAAAAAAAAAAAPAA
AAAAAAAAKAAAAAAAAAAZ
...

correct output
18

user output
18

Test 10

Group: 3, 4, 5, 6

Verdict:

input
50
GRGREEEGREGXRXXEGXXREXGRRRGRRR...

correct output
1584665

user output
(empty)

Test 11

Group: 3, 4, 5, 6

Verdict:

input
50
AITIISJUHCCRZNKSDCNQKYSQRINFWJ...

correct output
1077746

user output
(empty)

Test 12

Group: 3, 4, 5, 6

Verdict:

input
50
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOO...

correct output
1625625

user output
(empty)

Test 13

Group: 3, 4, 5, 6

Verdict: ACCEPTED

input
50
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF...

correct output
1680

user output
1680

Test 14

Group: 4, 5, 6

Verdict:

input
100
NNCMDCDDCCNNNDNCMMNCDCDCCDCDNM...

correct output
25325366

user output
(empty)

Test 15

Group: 4, 5, 6

Verdict:

input
100
LIMQQIHASECROEVILNVULGWZJPPKOG...

correct output
22342463

user output
(empty)

Test 16

Group: 4, 5, 6

Verdict:

input
100
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTT...

correct output
25502500

user output
(empty)

Test 17

Group: 4, 5, 6

Verdict: ACCEPTED

input
100
QXQQQQQQQQQQQQQQQQQQQQQQQQQQQQ...

correct output
25650

user output
25650

Test 18

Group: 5, 6

Verdict:

input
200
NAANANMMKNKKAKMKMAKNKMNKMMNNAA...

correct output
403292767

user output
(empty)

Test 19

Group: 5, 6

Verdict:

input
200
OMYWATTLURKQPTKEFMGGYAOONXWVSC...

correct output
388111321

user output
(empty)

Test 20

Group: 5, 6

Verdict:

input
200
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC...

correct output
404010000

user output
(empty)

Test 21

Group: 5, 6

Verdict:

input
200
LLLLLLLLLLLLLLLLLHLLLLLLLLLLLL...

correct output
14159445

user output
(empty)

Test 22

Group: 6

Verdict:

input
500
VVHWVUHVHUWWWVUUUWVUUHUUWHWUVW...

correct output
15683003812

user output
(empty)

Test 23

Group: 6

Verdict:

input
500
OIMZGEQSBMBDSDXSWRFNKSGFEBBTJE...

correct output
15575906951

user output
(empty)

Test 24

Group: 6

Verdict:

input
500
IIIIIIIIIIIIIIIIIIIIIIIIIIIIII...

correct output
15687562500

user output
(empty)

Test 25

Group: 6

Verdict:

input
500
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW...

correct output
3058970930

user output
(empty)