CSES - Datatähti 2019 alku - Results
Submission details
Task:Taulukko
Sender:DevBonBon
Submission time:2018-10-10 22:52:36 +0300
Language:Java
Status:READY
Result:12
Feedback
groupverdictscore
#1ACCEPTED12
#20
#30
#40
Test results
testverdicttimegroup
#1ACCEPTED0.24 s1details
#2ACCEPTED0.22 s1details
#3ACCEPTED0.22 s1details
#4ACCEPTED0.24 s1details
#5ACCEPTED0.24 s1details
#6ACCEPTED0.22 s1details
#7ACCEPTED0.22 s1details
#8ACCEPTED0.25 s1details
#9ACCEPTED0.25 s1details
#10ACCEPTED0.22 s1details
#11ACCEPTED0.22 s1details
#12ACCEPTED0.24 s1details
#13ACCEPTED0.24 s1details
#14ACCEPTED0.22 s1details
#15--2details
#16ACCEPTED0.24 s2details
#17ACCEPTED0.30 s2details
#18--2details
#19--2details
#20ACCEPTED0.23 s2details
#21ACCEPTED0.43 s2details
#22--2details
#23--2details
#24--2details
#25--2details
#26--2details
#27--2details
#28--2details
#29--3details
#30--3details
#31--3details
#32--3details
#33ACCEPTED0.52 s3details
#34--3details
#35--3details
#36--3details
#37--4details
#38--4details
#39--4details
#40--4details
#41ACCEPTED0.52 s4details
#42--4details
#43--4details
#44--4details
#45--4details
#46--4details
#47--4details
#48--4details
#49--4details
#50--4details
#51--4details
#52--4details

Code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

/**
 *
 * @author DevBonBon
 */
public class Datatahti_taulukko_brute {

    public static class SetFinder implements Runnable {

        private final int[] ints;
        private final int startIndex;
        private final int endIndex;
        private final int maxDifferent;
        //
        private final AtomicInteger result;
        private final AtomicBoolean done;

        public SetFinder(int[] ints, int startIndex, int endIndex, int maxDifferent) {
            this.ints = ints;
            this.startIndex = startIndex;
            this.endIndex = endIndex;
            this.maxDifferent = maxDifferent;

            this.result = new AtomicInteger(0);
            this.done = new AtomicBoolean(false);
        }

        public int getResult() {
            return this.result.get();
        }

        public boolean isDone() {
            return this.done.get();
        }

        @Override
        public void run() {
            for (int i = startIndex; i < endIndex; i++) {
                //Every i spot is a new number
                this.result.incrementAndGet();
                for (int j = i + 1; j < ints.length; j++) {
                    if (!tooManyDifferent(Arrays.copyOfRange(ints, i, j + 1), maxDifferent)) {
                        this.result.incrementAndGet();
                    } else {
                        //No need to continue checking, as there can't be longer sublists
                        break;
                    }
                }
            }

            this.done.lazySet(true);
        }
        private static final BitSet intsSeen = new BitSet(1000000000);
        public static final boolean tooManyDifferent(int[] ints, int maxDifferent) {
            //This is propably faster than any homebrewn solution I could make[/STRIKE] dare me
//            int count[] = {0};
//            Arrays.stream(ints).parallel().distinct().limit(maxDifferent + 1).forEach(t -> count[0]++);
//            return count[0] > maxDifferent;
            //welp, this is slightly slower, because of the stupid size the underlying set gets
            intsSeen.clear();
            for (int i : ints) {
                intsSeen.set(i);
                if (intsSeen.cardinality() > maxDifferent) {
                    return true;
                }
            }
            return false;
//            Set<Integer> set = new HashSet<>();
//            for (int i : ints) {
//                set.add(i);
//                if (set.size() > maxDifferent) {
//                    return true;
//                }
//            }
//            return false;
        }
    }

    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws IOException {
//        Scanner input = new Scanner(System.in);
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        //I read that split is actually "slow" (only hundreds of milliseconds slower, when iterating
        //hundreds of thousands of times) so lets try something different
        //First inputline
        String fl = input.readLine();
        //Get the size of the table and max different ints in any subset
        int size = Integer.parseInt(fl.substring(0, fl.indexOf(" ", 1)));
        int maxDifferent = Integer.parseInt(fl.substring(fl.indexOf(" ", 1) + 1, fl.length()));
        //NOTE: Came up with something better
        String tableString = input.readLine();

        //Convert tableString into an int array
        int pow[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
        int[] tableInts = new int[size];
        int length = tableString.length();
        int index = length - 1;
        int spot = tableInts.length - 1;
        int number = 0;
        //Iterates through all characters from the back and adds them together
        while (index >= 0) {
            char c = tableString.charAt(index);
            if (c != ' ') {
                switch (c) {
                    case '0':
                        break;
                    default:
                        number += (c - '0') * pow[length - index - 1];
                        break;
                }
            } else {
                tableInts[spot] = number;
                number = 0;
                //Get ready for next iteration
                length = index;
                spot--;
            }
            index--;
        }
        //Also get the last number
        tableInts[spot] = number;

        //So let's be greedy, who needs efficiency anyway...
        //I've got some ideas, but I'm currious to see how this goes
        //NOTE: could move the creation and such inside the string conversion above
        long result = 0;
        //The amount of threads we want to create and the size of the chunks we're dividing the array into
        int threads;
        //I figured this is more readable than nested if's
        switch (size) {
            case 1:
            case 2:
            case 3:
                threads = 1;
                break;
            case 4:
            case 5:
            case 6:
            case 7:
                threads = 4;
                break;
            default:
                threads = 8;
                break;
        }
        int chunkSize = ((size - 1) + threads - 1) / threads; // divide by threads rounded up.

        SetFinder finders[] = new SetFinder[threads];
        boolean findersDone[] = new boolean[threads];

        for (int i = 0; i < threads; i++) {
            int start = i * chunkSize;
            int end = Math.min(start + chunkSize, size - 1);
            //Create and start the threads and mark their task as incomplete
            finders[i] = new SetFinder(tableInts, start, end, maxDifferent);
            finders[i].run();

            findersDone[i] = false;
        }

        //Check all threads and get their results
        int allDone = 0;
        while (allDone < threads) {
            for (int i = 0; i < threads; i++) {
                if (!findersDone[i] && finders[i].isDone()) {
                    findersDone[i] = true;
                    result += finders[i].getResult();
                    allDone++;
                }
            }
        }

        //The logic above always skips the veeeeery last digit, so we add it here
        System.out.println(result + 1);
    }
}

Test details

Test 1

Group: 1

Verdict: ACCEPTED

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

correct output
5050

user output
5050

Test 2

Group: 1

Verdict: ACCEPTED

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

correct output
190

user output
190

Test 3

Group: 1

Verdict: ACCEPTED

input
100 1
5 9 9 6 9 8 1 4 7 7 8 9 5 5 6 ...

correct output
110

user output
110

Test 4

Group: 1

Verdict: ACCEPTED

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

correct output
5050

user output
5050

Test 5

Group: 1

Verdict: ACCEPTED

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

correct output
5050

user output
5050

Test 6

Group: 1

Verdict: ACCEPTED

input
100 2
2 3 1 1 2 2 3 1 2 1 1 1 3 3 1 ...

correct output
379

user output
379

Test 7

Group: 1

Verdict: ACCEPTED

input
100 2
4 6 10 8 6 8 10 8 4 7 8 9 6 2 ...

correct output
245

user output
245

Test 8

Group: 1

Verdict: ACCEPTED

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

correct output
5050

user output
5050

Test 9

Group: 1

Verdict: ACCEPTED

input
100 5
4 4 2 2 4 3 3 2 3 4 3 5 3 1 5 ...

correct output
5050

user output
5050

Test 10

Group: 1

Verdict: ACCEPTED

input
100 5
5 3 4 1 1 1 1 4 5 5 4 6 6 3 3 ...

correct output
1488

user output
1488

Test 11

Group: 1

Verdict: ACCEPTED

input
100 5
10 1 1 9 1 6 9 4 3 10 9 2 4 2 ...

correct output
743

user output
743

Test 12

Group: 1

Verdict: ACCEPTED

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

correct output
5050

user output
5050

Test 13

Group: 1

Verdict: ACCEPTED

input
100 10
9 4 9 1 9 1 5 2 10 10 9 3 9 6 ...

correct output
5050

user output
5050

Test 14

Group: 1

Verdict: ACCEPTED

input
100 10
80 59 58 87 28 83 83 93 96 24 ...

correct output
994

user output
994

Test 15

Group: 2

Verdict:

input
5000 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
12502500

user output
(empty)

Test 16

Group: 2

Verdict: ACCEPTED

input
5000 1
2 2 2 1 2 1 1 1 2 2 1 2 1 1 1 ...

correct output
10148

user output
10148

Test 17

Group: 2

Verdict: ACCEPTED

input
5000 1
434568 634119 102509 107238 72...

correct output
5000

user output
5000

Test 18

Group: 2

Verdict:

input
5000 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
12502500

user output
(empty)

Test 19

Group: 2

Verdict:

input
5000 2
2 1 1 1 2 2 2 2 1 1 1 1 2 1 2 ...

correct output
12502500

user output
(empty)

Test 20

Group: 2

Verdict: ACCEPTED

input
5000 2
1 3 2 2 3 3 1 2 1 2 1 2 3 1 2 ...

correct output
22451

user output
22451

Test 21

Group: 2

Verdict: ACCEPTED

input
5000 2
132968 414421 468358 432744 43...

correct output
9999

user output
9999

Test 22

Group: 2

Verdict:

input
5000 100
93 76 87 71 93 74 69 35 92 96 ...

correct output
12502500

user output
(empty)

Test 23

Group: 2

Verdict:

input
5000 100
2 57 51 4 35 76 5 40 51 55 20 ...

correct output
2669737

user output
(empty)

Test 24

Group: 2

Verdict:

input
5000 100
657 823 1773 1801 2785 4107 25...

correct output
498978

user output
(empty)

Test 25

Group: 2

Verdict:

input
5000 4999
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
12502500

user output
(empty)

Test 26

Group: 2

Verdict:

input
5000 4999
865706 910619 695192 183574 92...

correct output
12502500

user output
(empty)

Test 27

Group: 2

Verdict:

input
5000 5000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
12502500

user output
(empty)

Test 28

Group: 2

Verdict:

input
5000 5000
774752 159472 183796 654476 69...

correct output
12502500

user output
(empty)

Test 29

Group: 3

Verdict:

input
500000 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
125000250000

user output
(empty)

Test 30

Group: 3

Verdict:

input
500000 1
28107328 613212742 298033960 7...

correct output
500000

user output
(empty)

Test 31

Group: 3

Verdict:

input
500000 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
125000250000

user output
(empty)

Test 32

Group: 3

Verdict:

input
500000 2
1 1 2 1 1 2 2 1 2 1 2 1 1 2 1 ...

correct output
125000250000

user output
(empty)

Test 33

Group: 3

Verdict: ACCEPTED

input
500000 2
3 2 2 3 2 2 2 1 1 2 2 3 2 3 2 ...

correct output
2245355

user output
2245355

Test 34

Group: 3

Verdict:

input
500000 2
1 1 1 2 2 2 1 1 2 1 1 2 1 2 1 ...

correct output
1570486882

user output
(empty)

Test 35

Group: 3

Verdict:

input
500000 2
318961563 84011941 882177798 1...

correct output
999999

user output
(empty)

Test 36

Group: 3

Verdict:

input
500000 2
1 1 2 1 2 1 2 1 2 2 2 2 1 2 1 ...

correct output
483793169

user output
(empty)

Test 37

Group: 4

Verdict:

input
500000 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
125000250000

user output
(empty)

Test 38

Group: 4

Verdict:

input
500000 1
709486749 350496125 796065873 ...

correct output
500000

user output
(empty)

Test 39

Group: 4

Verdict:

input
500000 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
125000250000

user output
(empty)

Test 40

Group: 4

Verdict:

input
500000 2
1 2 2 2 2 2 1 1 2 2 2 2 2 2 1 ...

correct output
125000250000

user output
(empty)

Test 41

Group: 4

Verdict: ACCEPTED

input
500000 2
3 3 3 3 2 1 3 2 1 3 1 2 1 1 1 ...

correct output
2255047

user output
2255047

Test 42

Group: 4

Verdict:

input
500000 2
414942284 362802746 108881396 ...

correct output
999999

user output
(empty)

Test 43

Group: 4

Verdict:

input
500000 100
59 14 100 74 49 43 91 84 31 16...

correct output
260458849

user output
(empty)

Test 44

Group: 4

Verdict:

input
500000 1000
505 511 86 321 780 495 106 330...

correct output
3694803834

user output
(empty)

Test 45

Group: 4

Verdict:

input
500000 10000
4956 8463 8582 980 9278 1747 2...

correct output
41655235436

user output
(empty)

Test 46

Group: 4

Verdict:

input
500000 100000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
125000250000

user output
(empty)

Test 47

Group: 4

Verdict:

input
500000 100000
40198 78364 3724 51802 378 130...

correct output
125000250000

user output
(empty)

Test 48

Group: 4

Verdict:

input
500000 100000
320513086 811766509 339605137 ...

correct output
45001856854

user output
(empty)

Test 49

Group: 4

Verdict:

input
500000 499999
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
125000250000

user output
(empty)

Test 50

Group: 4

Verdict:

input
500000 499999
995132060 587162982 59723733 8...

correct output
125000250000

user output
(empty)

Test 51

Group: 4

Verdict:

input
500000 500000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
125000250000

user output
(empty)

Test 52

Group: 4

Verdict:

input
500000 500000
72083718 753463162 730560260 6...

correct output
125000250000

user output
(empty)