QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#757420#9558. The Devilucup-team3099#TL 1ms5736kbC++2314.0kb2024-11-17 06:59:432024-11-17 06:59:43

Judging History

你现在查看的是最新测评结果

  • [2024-11-17 06:59:43]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:5736kb
  • [2024-11-17 06:59:43]
  • 提交

answer

#include <iostream>
#include <vector>
#include <chrono>
#include <random>
#include <cassert>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());

template <class T = int>
class MCMF {
public:
    struct Edge {
        Edge(int a, T b, T c) : to(a), cap(b), cost(c) {}
        int to;
        T cap, cost;
    };

    MCMF(int size) {
        n = size;
        edges.resize(n);
        pot.assign(n, 0);
        dist.resize(n);
        visit.assign(n, false);
    }

    std::pair<T, T> mcmf(int src, int sink) {
        std::pair<T, T> ans(0, 0);
        if(!SPFA(src, sink)) return ans;
        fixPot();
        // can use dijkstra to speed up depending on the graph
        while(dij(src, sink)) {
            auto flow = augment(src, sink);
            ans.first += flow.first;
            ans.second += flow.first * flow.second;
            fixPot();
        }
        return ans;
    }

    void addEdge(int frm, int to, T cap, T cost) {
        edges[frm].push_back((int) list.size());
        list.push_back(Edge(to, cap, cost));
        edges[to].push_back((int) list.size());
        list.push_back(Edge(frm, 0, -cost));
    }
// private:
    int n;
    std::vector<std::vector<int>> edges;
    std::vector<Edge> list;
    std::vector<int> from;
    std::vector<T> dist, pot;
    std::vector<bool> visit;

    bool dij(int src, int sink) {
        T INF = std::numeric_limits<T>::max();
        dist.assign(n, INF);
        from.assign(n, -1);
        visit.assign(n, false);
        dist[src] = 0;
        for(int i = 0; i < n; i++) {
            int best = -1;
            for(int j = 0; j < n; j++) {
                if(visit[j]) continue;
                if(best == -1 || dist[best] > dist[j]) best = j;
            }
            if(dist[best] >= INF) break;
            visit[best] = true;
            for(auto e : edges[best]) {
                auto ed = list[e];
                if(ed.cap == 0) continue;
                T toDist = dist[best] + ed.cost + pot[best] - pot[ed.to];
                assert(toDist >= dist[best]);
                if(toDist < dist[ed.to]) {
                    dist[ed.to] = toDist;
                    from[ed.to] = e;
                }
            }
        }
        return dist[sink] < INF;
    }

    std::pair<T, T> augment(int src, int sink) {
        std::pair<T, T> flow = {list[from[sink]].cap, 0};
        for(int v = sink; v != src; v = list[from[v]^1].to) {
            flow.first = std::min(flow.first, list[from[v]].cap);
            flow.second += list[from[v]].cost;
        }
        for(int v = sink; v != src; v = list[from[v]^1].to) {
            list[from[v]].cap -= flow.first;
            list[from[v]^1].cap += flow.first;
        }
        return flow;
    }

    std::queue<int> q;
    bool SPFA(int src, int sink) {
        T INF = std::numeric_limits<T>::max();
        dist.assign(n, INF);
        from.assign(n, -1);
        q.push(src);
        dist[src] = 0;
        while(!q.empty()) {
            int on = q.front();
            q.pop();
            visit[on] = false;
            for(auto e : edges[on]) {
                auto ed = list[e];
                if(ed.cap == 0) continue;
                T toDist = dist[on] + ed.cost + pot[on] - pot[ed.to];
                if(toDist < dist[ed.to]) {
                    dist[ed.to] = toDist;
                    from[ed.to] = e;
                    if(!visit[ed.to]) {
                        visit[ed.to] = true;
                        q.push(ed.to);
                    }
                }
            }
        }
        return dist[sink] < INF;
    }

    void fixPot() {
        T INF = std::numeric_limits<T>::max();
        for(int i = 0; i < n; i++) {
            if(dist[i] < INF) pot[i] += dist[i];
        }
    }
};

std::map<std::string, int> id;
std::vector<std::string> wtf;

int getId(std::string str) {
    auto it = id.find(str);
    if(it == id.end()) {
        int got = (int) id.size();
        id[str] = got;
        wtf.push_back(str);
        //std::cout << "string with id " << got << " is " << str << '\n';
    }
    return id[str];
}

struct State {
    struct Position {
        int pos, pref, cost;
    };
    std::vector<Position> positions;
    int getCost() const {
        int ans = 1e9;
        for(auto st : positions) {
            ans = std::min(st.cost, ans);
        }
        return ans;
    }
    void reduce() {
        std::sort(positions.begin(), positions.end(), [](auto v0, auto v1) { return v0.pos * 300 + v0.pref < v1.pos * 300 + v1.pref; });
        int sz = 0;
        for(int l = 0, r = 0; l < (int) positions.size(); l = r) {
            int best = 1e9;
            while(r < (int) positions.size() && positions[r].pos == positions[l].pos && positions[r].pref == positions[l].pref) {
                best = std::min(best, positions[r++].cost);
            }
            positions[sz++] = {positions[l].pos, positions[l].pref, best};
        }
        positions.resize(sz);
    }
    int timeTilTerminal() const {
        int ans = -1e9;
        auto cost = getCost();
        for(auto st : positions) {
            if(st.cost == cost) {
                ans = std::max(ans, st.pos * 300 + st.pref);
            }
        }
        return ans;
    }
    int pointer;
};


const int ms = 70070000;
int prv[ms];
char cha[ms];


int main() {
    std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
    int n;
    std::cin >> n;
    MCMF<int> graph(n + n * n + 2);
    int src = n + n * n;
    int sink = src + 1;
    int bonus = 0;
    {
        std::string line;
        std::getline(std::cin, line);
    }
    for(int i = 0; i < n; i++) {
        graph.addEdge(src, i, 1, 0);
        std::string line;
        std::getline(std::cin, line);
        std::vector<std::string> vec;
        for(int l = 0, r = 0; l < (int) line.size(); l = r) {
            while(r < (int) line.size() && line[r] != ' ') r++;
            vec.push_back(line.substr(l, r-l));
            r++;
        }
        int sz = (int) vec.size();
        //std::cout << "read line " << line << " vec has size " << sz << '\n';
        bonus += sz;
        std::vector<State> stacks[150];
        std::vector<std::string> buffer[150];
        stacks[0].push_back({State({{0, 0, 0}}, 0)});
        int need = std::min(50, n);
        int ptr = 1;
        int limit = 150;
        int rest = 0;
        for(int cost = 0; cost < 150; ) {
            if(need <= 0) {
                break;
            }
            while(limit-1 > cost && rest - (int) stacks[limit-1].size() >= need + 2) {
                rest -= (int) stacks[--limit].size();
            }
            if(!buffer[cost].empty()) {
                auto str = buffer[cost].back();
                buffer[cost].pop_back();
                //std::cout << "adding edge from " << i << " to " << str << " with cost " << cost << std::endl;
                need--;
                graph.addEdge(i, n + getId(str), 1, cost);
                continue;
            }
            auto &cur = stacks[cost];
            if(cur.empty()) {
                cost++;
                if(cost == 150) break;
                std::sort(stacks[cost].begin(), stacks[cost].end(), [](auto v1, auto v2) { return v1.timeTilTerminal() < v2.timeTilTerminal(); });
                continue;
            }
            auto curState = cur.back();
            cur.pop_back();
            rest--;
            // std::cout << "curState has string " << curState.str << " with cost " << curState.getCost() << " should be " << cost << '\n';
            // for(auto st : curState.positions) {
            //     std::cout << "(" << st.pos << ", " << st.pref << ", " << st.cost << ")\n";
            // }
            // check if it's terminal
            {
                int terminalCost = 1e9;
                for(auto st : curState.positions) {
                    if(st.pos == sz-1 && st.pref > 0) {
                        terminalCost = std::min(terminalCost, st.cost);
                    }
                }
                if(terminalCost < 150) {
                    std::string str;
                    for(int on = curState.pointer; on; on = prv[on]) {
                        str += cha[on];
                    }
                    std::reverse(str.begin(), str.end());
                    buffer[terminalCost].push_back(str);
                }
            }
            static State nxt[256];
            //std::set<int> lol;
            for(auto st : curState.positions) {
                if(rng() % 2) {
                    if(st.pref < (int) vec[st.pos].size()) {
                    int toCost = st.cost + (st.pref == 0 ? 0 : 1);
                    if(toCost < limit) nxt[(int) vec[st.pos][st.pref]].positions.push_back({st.pos, st.pref+1, toCost});
                    //lol.insert((int) vec[st.pos][st.pref]);
                    }
                    if(st.pos+1 < sz && st.pref > 0 && st.cost < limit) {
                        nxt[(int) vec[st.pos+1][0]].positions.push_back({st.pos+1, 1, st.cost});
                        //lol.insert((int) vec[st.pos+1][0]);
                    }
                } else {
                    if(st.pos+1 < sz && st.pref > 0 && st.cost < limit) {
                        nxt[(int) vec[st.pos+1][0]].positions.push_back({st.pos+1, 1, st.cost});
                        //lol.insert((int) vec[st.pos+1][0]);
                    }
                    if(st.pref < (int) vec[st.pos].size()) {
                    int toCost = st.cost + (st.pref == 0 ? 0 : 1);
                    if(toCost < limit) nxt[(int) vec[st.pos][st.pref]].positions.push_back({st.pos, st.pref+1, toCost});
                    //lol.insert((int) vec[st.pos][st.pref]);
                    }
                }
                
            }
            // for(auto ch : lol) {
            //     auto st = nxt[(int) ch];
            //     st.reduce();
            //     auto toCost = st.getCost();
            //     cha[ptr] = (char) ch;
            //     prv[ptr] = curState.pointer;
            //     st.pointer = ptr++;
            //     stacks[toCost].push_back(st);
            //     nxt[(int) ch].positions.clear();
            // }
            for(auto ch = 'a'; ch <= 'z'; ch++) if(!nxt[(int) ch].positions.empty()) {
                auto st = nxt[(int) ch];
                st.reduce();
                auto toCost = st.getCost();
                cha[ptr] = ch;
                prv[ptr] = curState.pointer;
                st.pointer = ptr++;
                stacks[toCost].push_back(st);
                rest++;
                nxt[(int) ch].positions.clear();
            }
            for(auto ch = 'A'; ch <= 'Z'; ch++) if(!nxt[(int) ch].positions.empty()) {
                auto st = nxt[(int) ch];
                st.reduce();
                cha[ptr] = ch;
                prv[ptr] = curState.pointer;
                st.pointer = ptr++;
                auto toCost = st.getCost();
                stacks[toCost].push_back(st);
                rest++;
                nxt[(int) ch].positions.clear();
            }
        }
    }
    for(int i = 0; i < (int) id.size(); i++) {
        graph.addEdge(n + i, sink, 1, 0);
    }
    //assert(n < 100);
    auto [flow, cost] = graph.mcmf(src, sink);
    if(flow < n) {
        std::cout << "no solution\n";
        return 0;
    }
    //std::cout << cost + bonus << '\n';
    for(int i = 0; i < n; i++) {
        bool got = false;
        for(auto ed : graph.edges[i]) if(graph.list[ed].cap == 0) {
            std::cout << wtf[graph.list[ed].to - n] << '\n';
            assert(!got);
            got = true;
        }
    }
}

/*
NEVER FORGET TO:
    Look at the problem's constraints before coding.
How to cheese cf:
    Find a lower bound or upper bound for the problem. Have faith that it is the answer of the problem.
    If it isn't the answer, have more faith or change to another bound god by looking for a better bound.

    Trust guesses. Who has time to think? If people in div2 AC the problem it requires no proof since people don't prove things.

    You must draw cases. Thinking gets you nowhere, so draw cases and reach illogical conclusions from them.
    Sometimes drawing cases is bad because it takes too much time. Faster is to not think at all and just code a bruteforce solution.
    This is called "law of small numbers". If something works for small numbers, surely it works for big numbers.
    https://en.wikipedia.org/wiki/Faulty_generalization#Hasty_generalization don't mind the "faulty" part of it, in competitive programming mistakes are lightly punished
    Don't think about them being right or not, cf is a battle of intuition only.

    Be as stupid as possible in implementation. Trying to be smart is an easy way to get WA.

    Think about 2x2 cases for matrix problems and hope that everything works for the general case.

    Find a necessary condition and trust it to be sufficient. They're basically the same thing.

    Heuristics might speed up your code. Forget about complexity, it's only about ACing and not proving that your solution is good.

    For paths in a grid starting at (1, i) or something like that, assume that they never cross and do D&C

    Consider doing problems in reverse order of queries/updates

    For combinatorics problems, consider symmetry

    For problems that are similar to past problems, think about the differences betweem it and the current problem.
    Sometimes the difference makes no difference. Sometimes it does.

General strategy (MUST DO):
    Try to solve the problem with more restricted constraints.

About testing:
    Test n=1, a[i]=1, a[i]=n, etc. Basically, test low values. No need to test if pretests are strong, but if you get WA it's good.

This isn't a joke. Do it if you get stuck. It's shit practice in my opinion, but do it if you want AC.
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 5736kb

input:

5
automated teller machine
active teller machine
active trouble maker
always telling misinformation
American Teller Machinery

output:

autm
atma
atm
atem
ATM

result:

ok len=18

Test #2:

score: 0
Accepted
time: 0ms
memory: 5672kb

input:

5
Forest Conservation Committee Forum
Fuming Corruption Collusion Federation
Fulsome Cash Concealment Foundation
Funky Crony Capitalism Facilitator
Funny Cocky Cocky Funny

output:

FoCCF
FCCoF
FCCFo
FCCF
FCoCF

result:

ok len=24

Test #3:

score: 0
Accepted
time: 0ms
memory: 5712kb

input:

3
A AA
AA A
A A A

output:

no solution

result:

ok len=-1

Test #4:

score: 0
Accepted
time: 1ms
memory: 5720kb

input:

2
QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnmertyuiop
Q W E R T Y U I O P A S D F G H J K L Z X C V B N M q w e r t y u i o p a s d f g h j k l z x c v b n m j k l z x c v b n m

output:

Q
QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnmjklzxcvbnm

result:

ok len=63

Test #5:

score: 0
Accepted
time: 1ms
memory: 5728kb

input:

10
aaa aaa aaa aaa aaa aaa
aab aaa aaa aaa aaa aaa
aaa aab aaa aaa aaa aaa
aab aab aaa aaa aaa aaa
a a a a a a
ab ab a a a a a a
ab ab b a a a a a a
aw a a a a a
az az a a a a
az a a a a a

output:

aaaaaaaaa
aaaaaaa
aaabaaaa
aabaaaaa
aaaaaa
aaaaaaaa
aabaaaaaa
awaaaaa
aazaaaa
azaaaaa

result:

ok len=76

Test #6:

score: -100
Time Limit Exceeded

input:

128
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz...

output:


result: