QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#757376 | #9558. The Devil | ucup-team3099# | TL | 1ms | 3832kb | C++23 | 10.4kb | 2024-11-17 06:16:28 | 2024-11-17 06:16:29 |
Judging History
answer
#include <iostream>
#include <vector>
#include <chrono>
#include <random>
#include <cassert>
#include <queue>
#include <map>
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() {
int ans = 1e9;
for(auto st : positions) {
ans = std::min(st.cost, ans);
}
return ans;
}
std::string str;
};
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}}, "")});
int need = n;
for(int cost = 0; cost < 150; ) {
if(need <= 0) {
break;
}
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++;
continue;
}
auto curState = cur.back();
cur.pop_back();
//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) {
buffer[terminalCost].push_back(curState.str);
}
}
std::map<char, State> nxt;
for(auto st : curState.positions) {
if(st.pref < (int) vec[st.pos].size()) {
int toCost = st.cost + (st.pref == 0 ? 0 : 1);
if(toCost < 150) nxt[vec[st.pos][st.pref]].positions.push_back({st.pos, st.pref+1, toCost});
}
if(st.pos+1 < sz && st.pref > 0) {
nxt[vec[st.pos+1][0]].positions.push_back({st.pos+1, 1, st.cost});
}
}
for(auto [ch, st] : nxt) {
st.str = curState.str + ch;
auto toCost = st.getCost();
stacks[toCost].push_back(st);
}
}
}
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.
*/
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3704kb
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: 3832kb
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: 3656kb
input:
3 A AA AA A A A A
output:
no solution
result:
ok len=-1
Test #4:
score: 0
Accepted
time: 0ms
memory: 3796kb
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: 3688kb
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...