newcoder F石頭剪刀布(DFS + 思維)題解
阿新 • • 發佈:2019-02-06
type def math r++ ive imp div data- 安排
題意:
wzms 今年舉辦了一場剪刀石頭布大賽,bleaves 被選為負責人。
比賽共有 2n 個人參加, 分為 n 輪,
在每輪中,第 1 位選手和第 2 位選手對戰,勝者作為新的第 1 位選手,
第 3 位和第 4 位對戰,勝者作為新的第 2 位選手,以此類推。
bleaves 調查得知,每個人都有其偏愛決策,每個人在每一次對戰中都會使用他的偏愛決策。
如果一次對戰的雙方的偏愛決策相同,那麽這次對戰就永遠不會結束,所以 bleaves 不希望這種情況發生。
現在 bleaves 知道了每個人的偏愛決策,但她不知道如何安排初始的次序,使得上面的情況不會發生,你能幫幫她嗎?
全部的輸入數據滿足: R+P+S=2n ,1≤n≤20
鏈接:https://ac.nowcoder.com/acm/contest/332/F
來源:牛客網
思路:正向思考好像有點難度,那麽就反向思考。只要確定了最終勝出的人,那麽前面的都能推出來了,那麽就從最後往前遞歸,然後從末尾往前按字典序排序遞歸回來。
代碼:
#include<set> #include<map> #include<stack> #include<cmath> #include<queue> #include<string> #include<cstdio> #include<cstring> #include<sstream> #include<iostream> #include<algorithm> typedef long long ll; using namespace std; const int maxn = (1 << 21) + 10; const int MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; int R, P, S, cnt; int r, p, s; char ans[maxn]; void dfs(char x, intL, int R){ if(L == R){ ans[L] = x; if(x == ‘R‘) r++; else if(x == ‘P‘) p++; else if(x == ‘S‘) s++; return; } int m = (L + R) >> 1; if(x == ‘R‘){ dfs(‘R‘, L, m); dfs(‘S‘, m + 1, R); } else if(x == ‘P‘){ dfs(‘P‘, L, m); dfs(‘R‘, m + 1, R); } else if(x == ‘S‘){ dfs(‘S‘, L, m); dfs(‘P‘, m + 1, R); } bool swapp = false; for(int i = L; i <= m; i++){ if(ans[i] < ans[m + i - L + 1]) break; if(ans[i] > ans[m + i - L + 1]){ swapp = true; break; } } if(swapp){ for(int i = L; i <= m; i++){ swap(ans[i], ans[m + i - L + 1]); } } } int main(){ scanf("%d%d%d", &R, &P, &S); //r p s cnt = R + P + S; bool flag = false; for(int i = 0; i < 3; i++){ r = p = s = 0; if(i == 0){ dfs(‘R‘, 1, cnt); } else if(i == 1){ dfs(‘P‘, 1, cnt); } else if(i == 2){ dfs(‘S‘, 1, cnt); } if(R == r && P == p && S == s){ flag = true; break; } } ans[cnt + 1] = ‘\0‘; if(flag) printf("%s\n", ans + 1); else printf("IMPOSSIBLE\n"); return 0; }
newcoder F石頭剪刀布(DFS + 思維)題解