CSES - Datatähti 2024 loppu - Results
Submission details
Task:Sukat
Sender:EmuBird
Submission time:2024-01-20 16:02:17 +0200
Language:Java
Status:READY
Result:65
Feedback
groupverdictscore
#1ACCEPTED28
#2ACCEPTED37
#30
Test results
testverdicttimegroup
#1ACCEPTED0.16 s1, 2, 3details
#2ACCEPTED0.08 s1, 2, 3details
#3ACCEPTED0.16 s1, 2, 3details
#4ACCEPTED0.16 s1, 2, 3details
#5ACCEPTED0.17 s1, 2, 3details
#6ACCEPTED0.16 s1, 2, 3details
#7ACCEPTED0.16 s1, 2, 3details
#8ACCEPTED0.19 s2, 3details
#9ACCEPTED0.08 s2, 3details
#10ACCEPTED0.19 s2, 3details
#11ACCEPTED0.19 s2, 3details
#12ACCEPTED0.19 s2, 3details
#13ACCEPTED0.19 s2, 3details
#14ACCEPTED0.17 s2, 3details
#15ACCEPTED0.97 s3details
#16ACCEPTED0.19 s3details
#17ACCEPTED0.91 s3details
#18ACCEPTED0.99 s3details
#19ACCEPTED1.00 s3details
#20ACCEPTED0.91 s3details
#210.20 s3details

Code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

public class Main {
    public static class Pair {
        public char a;
        public char b;
        public Pair(char a, char b) {
            this.a = a;
            this.b = b;
        }
    }
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String input = reader.readLine();
        int sum = 0;
        HashMap<Character, Integer> map = new HashMap<Character, Integer>();
        
        for (char c : input.toCharArray()) {
            int count = map.getOrDefault(c, 0) + 1;
            sum += count;
            map.put(c, count);
        }

        ArrayList<Pair> pairs = new ArrayList<Pair>();

        ArrayList<Character> sortedKeys = new ArrayList<>(Arrays.asList(map.keySet().toArray(new Character[0])));
        sortedKeys.sort((a, b) -> map.get(b) - map.get(a));

        for (int i = 0; i < sortedKeys.size(); i++) {
            char c = sortedKeys.get(i);
            int selfCount = map.get(c);
            while (selfCount < sum) {
                int otherIndex = -1;
                char other = 'Ö'; // Ö=null

                for (int ib = 0; ib < sortedKeys.size(); ib++) {
                    char otherCandidate = sortedKeys.get(ib);
                    if (otherCandidate == c) continue;
                    else {
                        otherIndex = ib;
                        other = otherCandidate;
                        break;
                    }
                }
                
                if (other == 'Ö') break;

                pairs.add(new Pair(c, other));

                selfCount--;
                int otherCount = map.get(other) - 1;
                sum -= 2;

                if (otherCount > 0) {
                    map.put(other, otherCount);
                } else {
                    map.remove(other);
                    sortedKeys.remove(otherIndex);
                }

                if (selfCount > 0) {
                    map.put(c, selfCount);
                } else {
                    map.remove(c);
                    sortedKeys.remove(i);
                    i--;
                    break;
                }
            }
        }

        // Try to resque the lonely
        assert map.size() < 2;
        if (map.size() > 0) {
            char lastChar = sortedKeys.get(0);
            int count = map.get(lastChar);

            if (count > 1) {
                for (int i = 0; i < pairs.size(); i++) {
                    Pair p = pairs.get(i);

                    if (p.a == lastChar || p.b == lastChar) continue;
                    pairs.set(i, new Pair(lastChar, p.a));
                    pairs.add(i + 1, new Pair(lastChar, p.b));
                    
                    count -= 2;
                    if (count <= 1) break;
                }
            }
        }
        
        
        System.out.println(pairs.size());
        for (int i = 0; i < pairs.size(); i++) {
            Pair pair = pairs.get(i);
            System.out.println(Character.toString(pair.a) + Character.toString(pair.b));
        }
    }
}

Test details

Test 1

Group: 1, 2, 3

Verdict: ACCEPTED

input
QBAXGXJZJS

correct output
5
XJ
ZX
SQ
JG
...

user output
5
XJ
XJ
QA
BS
...

Test 2

Group: 1, 2, 3

Verdict: ACCEPTED

input
SSSSSSSSSS

correct output
0

user output
0

Test 3

Group: 1, 2, 3

Verdict: ACCEPTED

input
SETTSESEEE

correct output
5
ES
ET
ES
ET
...

user output
5
ES
ES
ES
ET
...

Test 4

Group: 1, 2, 3

Verdict: ACCEPTED

input
SSMRSSSSKR

correct output
4
SR
SR
SM
SK

user output
4
SR
SR
SK
SM

Test 5

Group: 1, 2, 3

Verdict: ACCEPTED

input
BIIILIIRRL

correct output
5
IR
IL
IR
IL
...

user output
5
IR
IR
IL
IL
...

Test 6

Group: 1, 2, 3

Verdict: ACCEPTED

input
YYGEYTDSTY

correct output
5
YT
YT
YS
YG
...

user output
5
YT
YT
YS
YD
...

Test 7

Group: 1, 2, 3

Verdict: ACCEPTED

input
MMMMMMMMMN

correct output
1
MN

user output
1
MN

Test 8

Group: 2, 3

Verdict: ACCEPTED

input
XQQZJQDDJOWXDFVQUIODDJIZIQDQIW...

correct output
500
DQ
DQ
DQ
DQ
...

user output
500
DQ
DQ
DQ
DQ
...
Truncated

Test 9

Group: 2, 3

Verdict: ACCEPTED

input
IIIIIIIIIIIIIIIIIIIIIIIIIIIIII...

correct output
0

user output
0

Test 10

Group: 2, 3

Verdict: ACCEPTED

input
KEOHHYHHAIHLLHEKIHKAHHHHOHEHHE...

correct output
493
HA
HA
HA
HA
...

user output
493
HA
HA
HA
HA
...
Truncated

Test 11

Group: 2, 3

Verdict: ACCEPTED

input
WNNFXQNODQFSQVQQPNLQNQNNVVNXHN...

correct output
499
NQ
NQ
NQ
NQ
...

user output
499
NQ
NQ
NQ
NQ
...
Truncated

Test 12

Group: 2, 3

Verdict: ACCEPTED

input
VGAHMMMXMMVXAAMMMOMMMXVMMMYMXT...

correct output
500
MH
MH
MH
MH
...

user output
500
MH
MH
MH
MH
...
Truncated

Test 13

Group: 2, 3

Verdict: ACCEPTED

input
KKKKUUAKKAUKKKKFKKSVKCWKKKCVOJ...

correct output
500
KI
KI
KI
KI
...

user output
500
YK
YI
KI
KI
...
Truncated

Test 14

Group: 2, 3

Verdict: ACCEPTED

input
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVV...

correct output
100
VP
VP
VP
VP
...

user output
100
VP
VP
VP
VP
...
Truncated

Test 15

Group: 3

Verdict: ACCEPTED

input
FKKJJBWTSFCDEKFPOKIPWZSVKHACMC...

correct output
100000
CK
CK
CK
CK
...

user output
100000
XC
XK
XC
XK
...
Truncated

Test 16

Group: 3

Verdict: ACCEPTED

input
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR...

correct output
0

user output
0

Test 17

Group: 3

Verdict: ACCEPTED

input
WVKKVVADNVCVKLVYVVZVKVCTNKVLVV...

correct output
99925
VC
VC
VC
VC
...

user output
99925
VC
VC
VC
VC
...
Truncated

Test 18

Group: 3

Verdict: ACCEPTED

input
BBBBBDNXDZQXBBYBBWBSBTMADXBBVZ...

correct output
99999
BE
BE
BE
BE
...

user output
99999
BE
BE
BE
BE
...
Truncated

Test 19

Group: 3

Verdict: ACCEPTED

input
FBRDPLUMLZMLLLUFUCLEMCMULLZLFL...

correct output
100000
LU
LU
LU
LU
...

user output
100000
LU
LU
LU
LU
...
Truncated

Test 20

Group: 3

Verdict: ACCEPTED

input
UUBUBUTUJSLUVBUXTUUMUXUXTMXUXS...

correct output
100000
UX
UX
UX
UX
...

user output
100000
AU
AX
UX
UX
...
Truncated

Test 21

Group: 3

Verdict:

input
PPPPPPPPPPEPPPPPPPPPPBOPPPPPPP...

correct output
20000
PB
PB
PB
PB
...

user output
0