bool isOk(VI a, VI b) { memset(ok, 0, sizeof(ok)); if(binary_search(a.begin(), a.end(), 6)) { a.push_back(9); } if(a.back()==9) { a.push_back(6); } if(binary_search(b.begin(), b.end(), 6)) { b.push_back(9); } if(b.back()==9) { b.push_back(6); } VI::iterator i, j; for(i=a.begin(); i!=a.end(); ++i) { for(j=b.begin(); j!=b.end(); ++j) { ok[(*i)*10+(*j)]=true; ok[(*j)*10+(*i)]=true; } } bool ret=true; for(int in=1; in<10; ++in) { if(!ok[in*in]) { ret=false; } } return ret; }
bool solve(int n, int k) { if (k == 1) return true; if (k > MAXK) return false; if (fact[k] > n) return false; VI v; int N = n; for (VI::iterator it = primes.begin(); it != primes.end() && (*it) * (*it) <= N; ++it) if (N % *it == 0) { v.push_back(0); while (N % *it == 0) { ++v.back(); N /= *it; } } if (N > 1) v.push_back(1); sort(v.begin(), v.end()); reverse(v.begin(), v.end()); k -= (int) v.size() + 1; for (VI::iterator it = v.begin(); it != v.end(); ++it) *it -= 1; while (!v.empty() && v.back() == 0) { v.pop_back(); } if (k <= 0) return true; if (DBG + 0) { cerr << "BEGIN!!!!!\n"; for (VI::iterator it = v.begin(); it != v.end(); ++it) cerr << *it << " -- "; cerr << endl; cerr << k << endl; } VI w(v.size(), 1); bool found = walk1(v, w, 0, k/*,set<VI>()*/); if (!found) { if (v.size() >= 3 && v[2] >= 3) { v[0] -= 1; v[1] -= 1; v[2] -= 1; found = walk1(v, w, 0, k - 1/*,set<VI>()*/); } } return found; }
void stableMatching (int n, VVI& maleRank, VVI& femaleRank, VI& wife) { // a male m prefers w to w' if maleRank[m][w] < maleRank[m][w'] // returns male-optimal matching VI freeMen; VVPI fq(n); VI husband(n, -1); for (int m = 0; m < n; ++m) { for (int w = 0; w < n; ++w) { fq[m].push_back(make_pair(maleRank[m][w], w)); } sort(all(fq[m]), greater<PI>()); freeMen.push_back(m); } while (!freeMen.empty()) { int m = freeMen.back(), w = fq[m].back().y; fq[m].pop_back(); if (husband[w] == -1) { husband[w] = m; freeMen.pop_back(); } else if (femaleRank[w][m] < femaleRank[w][husband[w]]) { freeMen.pop_back(); freeMen.push_back(husband[w]); husband[w] = m; } } wife = VI(n); for (int w = 0; w < n; ++w) { wife[husband[w]] = w; } }
void DFS (int v) { idx[v] = index; low[v] = index; index += 1; st.push_back(v); inStack[v] = true; for (auto w : adj[v]) { if (idx[w] == -1) { DFS(w); low[v] = min(low[v], low[w]); } else if (inStack[w]) { low[v] = min(low[v], low[w]); } } if (low[v] == idx[v]) { int w; components.push_back(VI()); do { w = st.back(); st.pop_back(); inStack[w] = false; componentOf[w] = totalComponents; components[totalComponents].push_back(w); } while (w != v); totalComponents++; } }
//lastrow = row for the last column void getPath(const VVI &parent, int lastrow, VI &path) { int nc = parent.size(); path.clear(); path.push_back(lastrow); if(nc > 1){ for(int c = nc - 2; c >= 0; c--){ //decide row for column c int r = parent[c+1][path.back()]; path.push_back(r); } } }
pii minimum_odd() { pii ret(-1,-1); if(!ones.empty()){ ret = pii(1,ones.back()); ones.pop_back(); } else if(!odd.empty()) { ret = odd.top(); odd.pop(); } return ret; }
int main() { int n, k; cin >> n >> k; if (k == 1) { for (int i = 1; i <= n; ++i) cout << i << ' '; return 0; } VI V; V.push_back(1); set<int> used; used.insert(1); int tmp = k; for (int i = 2; i <= n; ++i) { int prev = V.back(); if (prev > k) { if (used.count(prev - k) == 0) { V.push_back(prev - k); used.insert(prev - k); } else { V.push_back(prev + k); used.insert(prev + k); } } else { if (used.count(prev + k) == 0) { V.push_back(prev + k); used.insert(prev + k); } else { V.push_back(prev - k); used.insert(prev - k); } } k--; if (k < 1) break; } set<int> left; for(int i = 1; i <= n; ++i) left.insert(i); for(int i = 0; i < V.size(); ++i) left.erase(V[i]); for(set<int>::iterator it = left.begin(); it != left.end(); ++it) V.push_back(*it); for (int i = 0; i < V.size(); ++i) { cout << V[i] << ' '; } return 0; }
void DFS(){ visited.resize(k,0); REP(v,k) if (!visited[v]){ visited[v] = 1; printf("%d 0\n",v+1); stack.PB(v); while (!(stack.empty())){ int u = stack.back(); stack.pop_back(); for(auto ngb : sol[u]) if (!visited[ngb]){ visited[ngb] = 1; printf("%d %d\n",ngb+1,u+1); stack.PB(ngb); } } } }
int f(VI Q, VI R) { int rem=0; while(SZ(Q)<SZ(R)) Q.push_back(0); while(SZ(Q)>SZ(R)) rem+=Q.back(), Q.pop_back(); assert(SZ(Q)==SZ(R)); int debt=0; REP(i,SZ(R)) { if(R[i]>Q[i]) { debt+=R[i]-Q[i]; continue; } if(R[i]<Q[i]) { rem+=Q[i]-R[i]; } } return debt>rem?Inf:debt; }
int DFS (PI v, int index) { idx[v.x] = index; low[v.x] = index; index += 1; int children = 0; bool ap = false; for (auto w : adj[v.x]) if (w.y != v.y) { if (idx[w.x] == -1) { st.push_back(w.y); index = DFS(w, index); low[v.x] = min(low[v.x], low[w.x]); if (low[w.x] > idx[v.x]) { bridges.push_back(w.y); } children++; if (low[w.x] >= idx[v.x]) { if (v.y != -1 || children >= 2) { ap = true; } components.push_back(VI()); totalComponents++; int u; do { u = st.back(); st.pop_back(); components.back().push_back(u); } while (u != w.y); } } else if (idx[w.x] < idx[v.x]) { st.push_back(w.y); low[v.x] = min(low[v.x], idx[w.x]); } } if (ap) { cutVertices.push_back(v.x); } return index; }
int minValue(int N, int K, vector <int> s) { int m = SZ(s); int sum = 0; REP(i, m) sum += s[i]; if (sum <= (K - 1) * N) return 0; else { int tot = N; int ans = 0; VI q; REP(i, m) if (s[i]) q.push_back(s[i]); sort(ALL(q)); reverse(ALL(q)); while (sum > (K - 1) * N && N > 0) { sum -= SZ(q); REP(i, SZ(q)) q[i]--; while (SZ(q) && q.back() == 0) q.pop_back(); N--; ans++; } return ans; } }
int howLong(vector <string> connects, vector <string> costs) { vector<VI> path = parseVector(connects); vector<VI> cost = parseVector(costs); map<VI, int> visitedPath; stack<VI> S; int max = 0; for (int i = 0; i < path.size(); i++) { S.push({i}); visitedPath[{i}] = 0; while (!S.empty()) { // grab the nextPath to check VI curPath = S.top(); S.pop(); // grab the last node that was added int start = curPath.back(); // grab the path that correspond to the last node VI startPath = path[start]; for(int j = 0; j < startPath.size(); j++) { VI newPath = curPath; newPath.push_back(startPath[j]); S.push(newPath); visitedPath[newPath] = visitedPath[curPath] + cost[start][j]; if(visitedPath[newPath] > max) { max = visitedPath[newPath]; } } } } return max; }