6223 Infinite Fraction Path——剪枝
阿新 • • 發佈:2019-01-10
對於兩個位置i,j,他們的下一個位置分別為pi = (i*i+1)%n, pj = (j*j+1)%n, pi==pj的概率很高,因此只要把這種情況減掉就能過了
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <queue> #include <vector> using namespace std; const int maxn = 2e5 + 10; struct Node { int dep; char c; int p; bool operator < (const Node &rhs) const { if (dep != rhs.dep) return dep > rhs.dep; if (c != rhs.c) return c < rhs.c; return p > rhs.p; } }; char s[maxn]; char ans[maxn]; int main() { int t, ks = 0; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); scanf("%s", s); priority_queue<Node> que; for (int i = 0; i < n; i++) que.push(Node{0, s[i], i}); char maxv = 0; int predep = -1, prep = -1; while (!que.empty()) { Node node = que.top(); que.pop(); if (node.dep >= n) break; if (node.dep != predep) { maxv = node.c; } else maxv = max(maxv, node.c); if (node.c < maxv) continue; if (node.dep == predep && node.p == prep) continue; predep = node.dep, prep = node.p; ans[node.dep] = node.c; int nex = (1LL*node.p*node.p+1)%n; que.push(Node{node.dep+1, s[nex], nex}); } printf("Case #%d: ", ++ks); for (int i = 0; i < n; i++) printf("%c", ans[i]); puts(""); } return 0; }