標籤:
A:就根據題意計算比較一下即可
B:從每個起點往後走一遍走到底,輸出即可,字串直接map映射掉
C:類似拓撲排序,從臨接個數為1的入隊,那麼臨接Xor和,其實就是他的上一個結點,因為他只臨接了一個結點,這樣利用拓撲排序,當一個結點的度數為1的時候入隊即可,注意要判斷一下度數0的情況,直接continue
D:利用樹狀數組去求這種大的全排列數,其實一個全排列 ,可以看成a1 * (n - 1)! + a2 * (n - 2)!....,那麼其實只要處理出每一項的係數,然後在由係數就可以求出變換後的全排列了,那麼求係數這一步,只需要把兩個序列的係數加起來,然後進行進位操作,最後在轉化回去即可,這個過程要利用樹狀數組來維護,因為每個位置都要查詢,當前剩下數字中,比該數字小的數字個數
代碼:
A:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;double a, b, c, d;int main() { scanf("%lf%lf%lf%lf", &a, &b, &c, &d); double a1 = max(3 * a / 10, a - a / 250 * c); double a2 = max(3 * b / 10, b - b / 250 * d); if (a1 < a2) printf("Vasya\n"); else if (a1 > a2) printf("Misha\n"); else printf("Tie\n"); return 0;}
B:
#include <cstdio>#include <cstring>#include <string>#include <map>using namespace std;int n, hn, vis[2005], to[2005];map<string, int> hash;char a[25], b[25];char out[2005][25];int get(char *str) { if (hash.count(str))return hash[str]; strcpy(out[hn], str); hash[str] = hn++; return hash[str];}int dfs(int u) { while (1) {if (to[u] == -1) break;u = to[u]; } return u;}int main() { scanf("%d", &n); hn = 0; memset(to, -1, sizeof(to)); while (n--) {scanf("%s%s", a, b);int u = get(a), v = get(b);to[u] = v;vis[v] = 1; } int tot = 0; for (int i = 0; i < hn; i++)if (!vis[i]) { tot++;} printf("%d\n", tot); for (int i = 0; i < hn ;i++)if (!vis[i]) { printf("%s %s\n", out[i], out[dfs(i)]);} return 0;}
C:
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <vector>using namespace std;typedef pair<int, int> pii;const int N = (1<<16) + 5;int n, du[N], s[N];vector<pii> ans;int main() { scanf("%d", &n); queue<int> Q; for (int i = 0; i < n; i++) {scanf("%d%d", &du[i], &s[i]);if (du[i] == 1) Q.push(i); } while (!Q.empty()) {int u = Q.front();Q.pop();if (du[u] != 1) continue;ans.push_back(make_pair(u, s[u]));s[s[u]] ^= u;du[s[u]]--;if (du[s[u]] == 1) Q.push(s[u]); } int tot = ans.size(); printf("%d\n", tot); for (int i = 0; i < tot; i++)printf("%d %d\n", ans[i].first, ans[i].second); return 0;}
D:
#include <cstdio>#include <cstring>#define lowbit(x) (x&(-x))const int N = 200005;int n, a[N];int bit[N];void add(int x, int v) { while (x <= n) {bit[x] += v;x += lowbit(x); }}int query(int x) { int ans = 0; while (x) {ans += bit[x];x -= lowbit(x); } return ans;}int find(int x) { int l = 1, r = n; while (l < r) {int mid = (l + r) / 2;int tmp = query(mid);if (tmp < x) l = mid + 1;else r = mid; } return l;}int main() { scanf("%d", &n); memset(bit, 0, sizeof(bit)); for (int i = 1; i <= n; i++) add(i, 1); int x; for (int i = 1; i <= n; i++) {scanf("%d", &x); x++;int tmp = query(x) - 1;add(x, -1);a[i] += tmp; } for (int i = 1; i <= n; i++) add(i, 1); for (int i = 1; i <= n; i++) {scanf("%d", &x); x++;int tmp = query(x) - 1;add(x, -1);a[i] += tmp; } for (int i = n; i >= 1; i--) {a[i - 1] += a[i] / (n - i + 1);a[i] %= (n - i + 1); } for (int i = 1; i <= n; i++) add(i, 1); for (int i = 1; i <= n; i++) {int tmp = find(a[i] + 1);printf("%d ", tmp - 1);add(tmp, -1); } printf("\n"); return 0;}
Codeforces Round #285 (Div. 2) (A、B、C、D)