#include<bits/stdc++.h> usingnamespace std; #define int long long constint MAXN = 2E5 + 10; int a[MAXN]; int b[MAXN]; int ab[MAXN]; int n; signedmain(){ cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { cin >> b[i]; } for (int i = 1; i <= n; i++) { ab[i] = a[i] - b[i]; } sort(ab + 1, ab + 1 + n); int ans = 0; for (int i = 1; i <= n; i++) { if (ab[i] <= 0) continue; // cout << b[i] - a[i] << endl; int t = lower_bound(ab + 1, ab + 1 + i, -ab[i] + 1) - (ab); // for (int i = 1; i <= t; i++) { // cout << ab[t] << ' '; //} // cout << endl; ans += i - t; } cout << ans << endl; return0; }
#include<bits/stdc++.h> #include<cstring> usingnamespace std; intread(){ int x = 0, f = 1; char c = getchar(); while (c > '9' || c < '0') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + c - '0'; c = getchar(); } return x * f; } constint N = 2E5 + 10; int head[N], to[N << 1], nxt[N << 1], w[N << 1], ecnt = 0; voidadde_(int a, int b){ ecnt++; nxt[ecnt] = head[a]; head[a] = ecnt; to[ecnt] = b; w[ecnt] = 1; } voidadde(int a, int b){ adde_(a, b), adde_(b, a); } int t; int n, k, x, y; int a[N]; int dis[N]; booldfs1(int x, int fa){ bool c = x == y; for (int i = head[x]; i; i = nxt[i]) { int v = to[i]; if (v != fa) { dis[v] = dis[x] + w[i]; bool tmp = dfs1(v, x); if (tmp) { c = 1; w[i] = 0; } } } return c; } int ans = 0; bool has[N]; booldfs2(int x, int fa){ bool in_has = has[x]; for (int i = head[x]; i; i = nxt[i]) { int v = to[i]; if (v != fa) { bool tmp = dfs2(v, x); if (tmp) { in_has = tmp; // clog << x << "=>" << v << endl; ans += 2 * w[i]; w[i] = 0; } } } return in_has; } intmain(){ t = read(); while (t--) { ans = 0; ecnt = 0; memset(head, 0, sizeof head); memset(has, 0, sizeof has); memset(nxt, 0, sizeof nxt); n = read(), k = read(), x = read(), y = read(); for (int i = 1; i <= k; i++) { a[i] = read(); has[a[i]] = 1; } for (int i = 1; i < n; i++) { int a = read(), b = read(); adde(a, b); } memset(dis, 0, sizeof dis); dfs1(x, 0); ans += dis[y]; dfs2(x, 0); cout << ans << endl; // clog << endl; } // TODO: code here return0; }
#include<bits/stdc++.h> #define int unsigned long long usingnamespace std; intread(){ int x = 0, f = 1; char c = getchar(); while (c > '9' || c < '0') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + c - '0'; c = getchar(); } return x * f; } int n, k; signedmain(){ n = read(), k = read(); char maxc = 0; for (auto s : to_string(k)) { maxc = max(s, maxc); } if (maxc == '1' && n > to_string(k).length()) { cout << -1 << endl; return0; } map<int, int> dist; queue<int> q; q.push(k); dist[k] = 0; while (!q.empty()) { auto k = q.front(); q.pop(); string s = to_string(k); if (s.length() == n) { cout << dist[k] << endl; return0; } for (auto c : s) { int i = c - '0'; int nxt = k * i; if (!dist.count(nxt)) { dist.insert(make_pair(nxt, dist[k] + 1)); q.push(nxt); } } } // TODO: code here return0; }