CSES - HIIT Open 2018 - Results
Submission details
Task:Find a Word
Sender:HIIT AND RUN
Submission time:2018-05-26 15:51:35 +0300
Language:C++
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.01 sdetails
#2ACCEPTED0.01 sdetails
#3ACCEPTED0.01 sdetails
#4ACCEPTED0.01 sdetails
#5ACCEPTED0.02 sdetails
#6ACCEPTED0.01 sdetails
#70.01 sdetails
#8ACCEPTED0.01 sdetails
#90.01 sdetails
#10ACCEPTED0.01 sdetails
#11ACCEPTED0.01 sdetails
#12ACCEPTED0.01 sdetails
#13ACCEPTED0.01 sdetails
#14ACCEPTED0.02 sdetails
#15--details
#16--details

Compiler report

input/code.cpp: In function 'll has_duplicate(std::__cxx11::string&, ll, ll)':
input/code.cpp:38:49: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if ((next_e.first == s) && (next_e.GETX == x) && (next_e.GETY == y)) {
                                     ~~~~~~~~~~~~^~~~
input/code.cpp:38:71: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if ((next_e.first == s) && (next_e.GETX == x) && (next_e.GETY == y)) {
                                                           ~~~~~~~~~~~~^~~~

Code

#include <bits/stdc++.h>

using namespace std;

typedef long long unsigned int ll;

char C[33][33];

#define GETX second.second.first
#define GETY second.first
#define GETCT second.second.second

set< pair< string, pair<int, pair<int,ll> > > > prevQ;
set< pair< string, pair<int, pair<int,ll> > > > nextQ;

ll n, k;

ll fact(ll x) {
    ll f = 1;
    for (ll i = 2; i <= x; ++i)
        f *= i;
    return f;
}

ll count_from(ll x, ll y) {
    ll nf = fact(n*2-2-x-y);
    ll df1 = fact(n-1-x);
    ll df2 = fact((n*2-2-x-y)-(n-1-x));
    
    return nf / (df1 * df2);
}

ll has_duplicate(string & s, ll x, ll y) {
    auto next_it = nextQ.upper_bound({s, {y ,{x, 0}}});
    
    if (next_it != nextQ.end()) {
        auto next_e = *next_it;
        if ((next_e.first == s) && (next_e.GETX == x) && (next_e.GETY == y)) {
            nextQ.erase(next_it);
            return next_e.GETCT;
        }
        return 0;
    }
    return 0;
}

int main () {
    cin >> n >> k;
    
    for (ll i = 0; i < n; ++i) {
        string line;
        cin >> line;
        
        for (ll j = 0; j < n; ++j) {
            C[i][j] = line[j];
        }
    }
    
    string e = "";
    e += C[0][0];
    prevQ.insert({e, {0 , {0, 1}}});
 
    while (true) {
        if (prevQ.begin()->first.size() == 2*n-1) break;
    
        string prev_prefix = "";
        ll cumulative = 0;
        
        vector < pair< string, pair<int, pair<int,ll> > > > eraselist;
        
        for (auto elem : prevQ) {
            string s = elem.first;
            ll y = elem.GETY;
            ll x = elem.GETX;
            ll ct = elem.GETCT;
            
            ll m = ct * count_from(x,y);
            
            cumulative += m;
            
            //cout << s << " " << m << " " << cumulative << endl;
            
            if (prev_prefix.size() && s > prev_prefix && cumulative > k) {
                //cout << "remove me" << endl;
                eraselist.push_back(elem);
            }
            
            prev_prefix = s;
        }
        
        for (auto elem : eraselist)
            prevQ.erase(elem);
        
        prev_prefix = "";
        eraselist.clear();
        
        for (auto elem_it = prevQ.rbegin(); elem_it != prevQ.rend(); ++elem_it) {
            string s = elem_it->first;
            ll y = elem_it->GETY;
            ll x = elem_it->GETX;
            ll ct = elem_it->GETCT;
            
            ll m = ct * count_from(x,y);
            
            cumulative += m;
            
            //cout << s << " " << m << " " << cumulative << endl;
            
            if (prev_prefix.size() && s > prev_prefix && cumulative > n-k) {
                //cout << "remove me" << endl;
                eraselist.push_back(*elem_it);
            }
            
            prev_prefix = s;
        }
        
        for (auto elem : eraselist)
            prevQ.erase(elem);
        
        while (prevQ.size()) {
            
            auto elem = *prevQ.begin();
            prevQ.erase(prevQ.begin());
            
            string s = elem.first;
            ll y = elem.GETY;
            ll x = elem.GETX;
            ll ct = elem.GETCT;
            
            
            //cout << s << " " << ct << endl;
            
            if (x < n-1) {
                ll newx = x+1;
                ll newy = y;
                
                //ll newct = count_from(newx, newy);
                string new_s = s + C[newy][newx];
                ll lol = has_duplicate(new_s, newx, newy);
                nextQ.insert({new_s, {newy, {newx, ct+lol}}});
            }
            
            if (y < n-1) {
                ll newx = x;
                ll newy = y+1;
                
                //ll newct = count_from(newx, newy);
                string new_s = s + C[newy][newx];
                ll lol = has_duplicate(new_s, newx, newy);
                nextQ.insert({new_s, {newy, {newx, ct+lol}}});
            }
        }
        
        swap(nextQ, prevQ);
    }
    
    /*
    for (auto elem : prevQ) {
        cout << elem.first << " " << elem.GETCT << endl;
    }
    */
    
    cout << prevQ.rbegin()->first << endl;
    
}

Test details

Test 1

Verdict: ACCEPTED

input
4 1
AAAA
AAAA
AAAA
AAAA

correct output
AAAAAAA

user output
AAAAAAA

Test 2

Verdict: ACCEPTED

input
4 2
AAAA
AAAA
AAAA
AAAA

correct output
AAAAAAA

user output
AAAAAAA

Test 3

Verdict: ACCEPTED

input
4 10
AAAA
AAAA
AAAA
AAAA

correct output
AAAAAAA

user output
AAAAAAA

Test 4

Verdict: ACCEPTED

input
4 19
AAAA
AAAA
AAAA
AAAA

correct output
AAAAAAA

user output
AAAAAAA

Test 5

Verdict: ACCEPTED

input
4 20
AAAA
AAAA
AAAA
AAAA

correct output
AAAAAAA

user output
AAAAAAA

Test 6

Verdict: ACCEPTED

input
4 1
QNJP
EVJU
XHZF
RXCV

correct output
QEVHXCV

user output
QEVHXCV

Test 7

Verdict:

input
4 2
QNJP
EVJU
XHZF
RXCV

correct output
QEVHZCV

user output
QEVHXCV

Test 8

Verdict: ACCEPTED

input
4 10
QNJP
EVJU
XHZF
RXCV

correct output
QEXRXCV

user output
QEXRXCV

Test 9

Verdict:

input
4 19
QNJP
EVJU
XHZF
RXCV

correct output
QNVJZCV

user output
QEXRXCV

Test 10

Verdict: ACCEPTED

input
4 20
QNJP
EVJU
XHZF
RXCV

correct output
QNVJZFV

user output
QNVJZFV

Test 11

Verdict: ACCEPTED

input
30 1
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
...

correct output
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

user output
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

Test 12

Verdict: ACCEPTED

input
30 15033633249770520
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
...

correct output
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

user output
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

Test 13

Verdict: ACCEPTED

input
30 30067266499541040
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
...

correct output
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

user output
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

Test 14

Verdict: ACCEPTED

input
30 1
QNJPEVJUXHZFRXCVKBSJKUURVPLYUI
RXLGFBNQPBKQQRQFHLXUIUPLUOUOQW
FZNNUBMTLXUMTSJOOGBDBEVEYVWOLP
WYLTEQJBJRPSEMPOESVKFTQKEMSIAP
...

correct output
QNJLGFBBMJBHCCBOCEFBPLDCFGDJKB...

user output
QNJLGFBBMJBHCCBOCEFBPLDCFGDJKB...

Test 15

Verdict:

input
30 15033633249770520
QNJPEVJUXHZFRXCVKBSJKUURVPLYUI
RXLGFBNQPBKQQRQFHLXUIUPLUOUOQW
FZNNUBMTLXUMTSJOOGBDBEVEYVWOLP
WYLTEQJBJRPSEMPOESVKFTQKEMSIAP
...

correct output
QNXZYLYURTAYRYZQUKZCYOPKYKLQPX...

user output
(empty)

Test 16

Verdict:

input
30 30067266499541040
QNJPEVJUXHZFRXCVKBSJKUURVPLYUI
RXLGFBNQPBKQQRQFHLXUIUPLUOUOQW
FZNNUBMTLXUMTSJOOGBDBEVEYVWOLP
WYLTEQJBJRPSEMPOESVKFTQKEMSIAP
...

correct output
QRXZYLYURTAYRYZQUKZCYOPKYKLQPX...

user output
(empty)