CSES - Datatähti 2025 alku - Results
Submission details
Task:Niitty
Sender:rottis
Submission time:2024-10-31 09:19:39 +0200
Language:C++ (C++17)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
#40
#50
#60
Test results
testverdicttimegroup
#10.00 s1, 2, 3, 4, 5, 6details
#20.00 s1, 2, 3, 4, 5, 6details
#30.00 s1, 2, 3, 4, 5, 6details
#4ACCEPTED0.00 s1, 2, 3, 4, 5, 6details
#5ACCEPTED0.00 s1, 2, 3, 4, 5, 6details
#60.00 s2, 3, 4, 5, 6details
#70.00 s2, 3, 4, 5, 6details
#80.00 s2, 3, 4, 5, 6details
#9ACCEPTED0.01 s2, 3, 4, 5, 6details
#100.00 s3, 4, 5, 6details
#110.00 s3, 4, 5, 6details
#120.00 s3, 4, 5, 6details
#130.45 s3, 4, 5, 6details
#140.00 s4, 5, 6details
#150.00 s4, 5, 6details
#160.01 s4, 5, 6details
#17--4, 5, 6details
#180.01 s5, 6details
#190.01 s5, 6details
#200.00 s5, 6details
#21--5, 6details
#220.01 s6details
#230.03 s6details
#240.01 s6details
#25--6details

Code

// IDEA: use mipmaps to reduce repeated calculations!

#define print_board for (int i = 0; i < n; i++) { std::cout << board[i] << '\n'; } std::cout << std::endl
#define print_flowers(flowers) for (int i = 0; i < 26; i++) { std::cout << ((flowers & (1<<i)) >> i); } std::cout << '\n' << "abcdefghijklmnopqrstuvwxyz" << std::endl
#define print_rect(r) std::cout << r.left << ' ' << r.right << ' ' << r.top << ' ' << r.bottom << std::endl
#define to_bit(c) (1<<(c - 'a'))
#define MAX_RECT ((rect) { .left = 0, .right = (int16_t) (n-1), .top = 0, .bottom = (int16_t) (n-1) })
#define rect_size(r) ((1 + r.right - r.left) * (1 + r.bottom - r.top))
#define LEFT 0
#define RIGHT 1
#define TOP 2
#define BOTTOM 3
#define rect_side(r, s) (&((int16_t*) &r)[s])
#define bitmap uint32_t
 
#include <iostream>
#include <stdlib.h>
#include <stdint.h>
 
// note: int16_t only needed for last part!
// other parts can do with int8_t
typedef struct {
    int16_t left;
    int16_t right;
    int16_t top;
    int16_t bottom;
} rect;
 
bool valid_rect(char **board, uint16_t n, bitmap flowers, rect rectangle) {
    bitmap flowers_in_rect = 0;
    for (uint16_t x = rectangle.left; x <= rectangle.right; x++) {
        for (uint16_t y = rectangle.top; y <= rectangle.bottom; y++) {
            flowers_in_rect = flowers_in_rect | to_bit(board[x][y]);
            if (flowers == flowers_in_rect) { return true; }
        }
    }
    return false;
}

int abs(int v) {
    return (v < 0 ? -v : v);
}

int main() {
    uint16_t n;
    bitmap found_flowers = 0;
    std::cin >> n;
 
    char **board = (char**) malloc((n) * sizeof(char*));
    
    for (unsigned int i = 0; i < n; i++) {
        char *row = (char*) malloc((n+1) * sizeof(char));
        std::cin >> row;
        for (unsigned int j = 0; j < n; j++) {
            found_flowers = found_flowers | to_bit(row[j]);
        }
        board[i] = row;
    }
 
    //print_flowers(found_flowers);
    unsigned int solutions = 0;
    
    rect smallest_rect = MAX_RECT;
    // we can optimize out one side; check which side is best to optimize
    int best_side = -1;
    int best_score = -1;
    rect best_rect;     
    for (int side : (int[]) {LEFT, RIGHT, TOP, BOTTOM}) {
        rect smallest_rect_copy = smallest_rect;
        
        int16_t *ref = rect_side(smallest_rect_copy, side);
        
        if (side % 2) { // right, bottom 
            do { 
                (*ref)--;
            } while (valid_rect(board, n, found_flowers, smallest_rect_copy));
            (*ref)++;
        } else { // left, top
            do {
                (*ref)++;
            } while (valid_rect(board, n, found_flowers, smallest_rect_copy));
            (*ref)--;
        }
        int c_score = abs(*rect_side(smallest_rect, side) - (*ref));
        if (best_score < c_score) {
            best_side = side;
            best_score = c_score;
            best_rect = smallest_rect_copy;
        }
    }

    smallest_rect = best_rect;
    unsigned int solutions_bordering_best_side = 0;

    // scuffed as fuck but i don't care

    bool on_best_side;        
    switch (best_side) {
        case LEFT:
            for (int16_t left = smallest_rect.left; left <= smallest_rect.right; left++) {
                on_best_side = (left == smallest_rect.left);

                for (int16_t right = left; right <= smallest_rect.right; right++) {
                    for (int16_t top = smallest_rect.top; top <= smallest_rect.bottom; top++) {
                        for (int16_t bottom = top; bottom <= smallest_rect.bottom; bottom++) {
                            if (valid_rect(board, n, found_flowers, (rect) {.left=left, .right=right, .top=top, .bottom=bottom})) {
                                solutions++;
                                if (on_best_side) {
                                    solutions_bordering_best_side++;
                                }
                            }
                        }
                    }
                }
            }
            break;

        case RIGHT:
            for (int16_t right = smallest_rect.right; right >= smallest_rect.left; right--) {
                on_best_side = (right == smallest_rect.right);
                
                for (int16_t left = right; left >= smallest_rect.left; left--) {
                    for (int16_t top = smallest_rect.top; top <= smallest_rect.bottom; top++) {
                        for (int16_t bottom = top; bottom <= smallest_rect.bottom; bottom++) {
                            if (valid_rect(board, n, found_flowers, (rect) {.left=left, .right=right, .top=top, .bottom=bottom})) {
                                solutions++;
                                if (on_best_side) {
                                    solutions_bordering_best_side++;
                                }
                            }
                        }
                    }
                }
            }
            break;

        case TOP:
            for (int16_t top = smallest_rect.top; top <= smallest_rect.bottom; top++) {
                on_best_side = (top == smallest_rect.top);

                for (int16_t bottom = top; bottom <= smallest_rect.bottom; bottom++) {
                    for (int16_t left = smallest_rect.left; left <= smallest_rect.right; left++) {
                        for (int16_t right = left; right <= smallest_rect.right; right++) {
                            if (valid_rect(board, n, found_flowers, (rect) {.left=left, .right=right, .top=top, .bottom=bottom})) {
                                solutions++;
                                if (on_best_side) {
                                    solutions_bordering_best_side++;
                                }
                            }
                        }
                    }
                }
            }
            break;

        case BOTTOM:
            for (int16_t bottom = smallest_rect.bottom; bottom >= smallest_rect.top; bottom--) {
                on_best_side = (bottom == smallest_rect.bottom);

                for (int16_t top = bottom; top >= smallest_rect.top; top--) {
                    for (int16_t left = smallest_rect.left; left <= smallest_rect.right; left++) {
                        for (int16_t right = left; right <= smallest_rect.right; right++) {
                            if (valid_rect(board, n, found_flowers, (rect) {.left=left, .right=right, .top=top, .bottom=bottom})) {
                                solutions++;
                                if (on_best_side) {
                                    solutions_bordering_best_side++;
                                }
                            }
                        }
                    }
                }
            }
            break;
    }
    
    solutions += solutions_bordering_best_side * best_score;
 
    std::cout << solutions << std::endl;
 
    /*
    for (unsigned int i = 0; i < n; i++) {
        free(board[i]);
    }
    free(board);
    */
 
    return 0;
}

Test details

Test 1

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

Verdict:

input
10
TNCTNPNTPC
NPPNTNTPTP
NTNTTCNTCT
NPCPNPPNTT
...

correct output
2035

user output
40

Test 2

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

Verdict:

input
10
NFWQLWNWYS
DZOQJVXFPJ
CNHXPXMCQD
QRTBVNLTQC
...

correct output
9

user output
8

Test 3

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

Verdict:

input
10
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
...

correct output
3025

user output
550

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
1900

Test 7

Group: 2, 3, 4, 5, 6

Verdict:

input
20
CBGLSHGZHYZDWBNDBJUG
SMUXOJQYPXZDTMJUIWOJ
XIDSTNBGHKRKOVUVMINB
MTQGCFRUHQKALXRNCQGS
...

correct output
8334

user output
18

Test 8

Group: 2, 3, 4, 5, 6

Verdict:

input
20
KKKKKKKKKKKKKKKKKKKK
KKKKKKKKKKKKKKKKKKKK
KKKKKKKKKKKKKKKKKKKK
KKKKKKKKKKKKKKKKKKKK
...

correct output
44100

user output
4200

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
48450

Test 11

Group: 3, 4, 5, 6

Verdict:

input
50
AITIISJUHCCRZNKSDCNQKYSQRINFWJ...

correct output
1077746

user output
11858

Test 12

Group: 3, 4, 5, 6

Verdict:

input
50
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOO...

correct output
1625625

user output
63750

Test 13

Group: 3, 4, 5, 6

Verdict:

input
50
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF...

correct output
1680

user output
1296

Test 14

Group: 4, 5, 6

Verdict:

input
100
NNCMDCDDCCNNNDNCMMNCDCDCCDCDNM...

correct output
25325366

user output
443800

Test 15

Group: 4, 5, 6

Verdict:

input
100
LIMQQIHASECROEVILNVULGWZJPPKOG...

correct output
22342463

user output
13800

Test 16

Group: 4, 5, 6

Verdict:

input
100
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTT...

correct output
25502500

user output
505000

Test 17

Group: 4, 5, 6

Verdict:

input
100
QXQQQQQQQQQQQQQQQQQQQQQQQQQQQQ...

correct output
25650

user output
(empty)

Test 18

Group: 5, 6

Verdict:

input
200
NAANANMMKNKKAKMKMAKNKMNKMMNNAA...

correct output
403292767

user output
3778600

Test 19

Group: 5, 6

Verdict:

input
200
OMYWATTLURKQPTKEFMGGYAOONXWVSC...

correct output
388111321

user output
1256800

Test 20

Group: 5, 6

Verdict:

input
200
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC...

correct output
404010000

user output
4020000

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
60842500

Test 23

Group: 6

Verdict:

input
500
OIMZGEQSBMBDSDXSWRFNKSGFEBBTJE...

correct output
15575906951

user output
39979000

Test 24

Group: 6

Verdict:

input
500
IIIIIIIIIIIIIIIIIIIIIIIIIIIIII...

correct output
15687562500

user output
62625000

Test 25

Group: 6

Verdict:

input
500
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW...

correct output
3058970930

user output
(empty)