#include<bits/stdc++.h> usingnamespace std; #define ll long long #define endl '\n' #define int ll constint N = 6e5 + 10;
int n; intlowbit(int x) { return x & (-x); } structnode { int val, id; booloperator<(const node &t) const { if (val == t.val) return id > t.id; return val > t.val; } } a[N]; int tr[N]; voidadd(int x, int y) { for (; x <= n; x += lowbit(x)) tr[x] += y; } intgetsum(int x) { int ans = 0; for (; x; x -= lowbit(x)) ans += tr[x]; return ans; } int p[N]; voidsolve() { int m; cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> a[i].val; a[i].id = i; p[i - 1] = a[i].val; } ll ans = 0; sort(a + 1, a + 1 + n); for (int i = 1; i <= n; i++) { add(a[i].id, 1); ans += getsum(a[i].id - 1); } cout << ans << endl;
int op = 1; int res = (n * (n - 1)) / 2; int id = 0; while (m--) { char c; cin >> c; if (c == 'S') { int now = p[id]; ans = ans - (now - 1) + (n - now); id = (id + op + n) % n; } else { ans = res - ans; op = -op; id = (id + op + n) % n; } cout << ans % 10; } }
#include<bits/stdc++.h> usingnamespace std; #define ll long long #define endl '\n' #define int ll constint N = 1e6 + 10;
voidsolve() { int n, m; cin >> n >> m; int mm = m; vector<int> ans(m + 100, 1); for (int i = 1; i <= n; i += 2) ans[i] = ans[i + 1] = max(1ll, m--); for (int i = 1; i <= n; i++) { for (int j = 1; j <= mm; j++) { if (ans[i] == j) cout << 1 << " "; else cout << 0 << " "; } cout << endl; } }
voidsolve() { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> w[i]; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; e[u].push_back(v), e[v].push_back(u); } if (n == 1) { cout << 0 << endl; return; } dfs(1, 0); dff(1, 0, 0); cout << max(ans, dp[1]) << endl; }
J. Permutation Puzzle
#include<bits/stdc++.h> usingnamespace std; #define ll long long #define endl '\n'
//区间段按照右端点从小到大排序 structnode { int l, r, id; booloperator<(const node &x) { return l < x.l; } };
voidsolve() { int n, m, k; cin >> n >> m; vector<int> e[n + 1], g[n + 1]; vector<int> in1(n + 1, 0), in2(n + 1, 0), w(n + 1, 0); vector<node> a(n + 1);
for (int i = 1; i <= n; i++) { cin >> w[i]; if (w[i] != 0) a[i] = {w[i], w[i], i}; //初始化能选区间 else a[i] = {1, n, i}; } for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; e[x].push_back(y); in1[y]++; //正向建有向图 g[y].push_back(x); in2[x]++; //反向建有向图 }
auto top1 = [&]() { queue<int> q; for (int i = 1; i <= n; i++) if (in1[i] == 0) q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); for (auto v : e[u]) { a[v].l = max(a[v].l, a[u].l + 1); //求最小,保留符合的中的最大值 if (--in1[v] == 0) q.push(v); } } }; auto top2 = [&]() { queue<int> q; for (int i = 1; i <= n; i++) if (in2[i] == 0) q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); for (auto v : g[u]) { a[v].r = min(a[v].r, a[u].r - 1); //求最大,保留符合的中的最小值 if (--in2[v] == 0) q.push(v); } } };
top1(); //正向拓扑找出约束最小值 top2(); //逆序拓扑找出约束最大值 for (int i = 1; i <= n; i++) { if (a[i].l > a[i].r) { cout << -1 << endl; return; } }
//对于多个位置能选答案的区间,贪心选择右端点最小的,因为备选数从小到大枚举,右端点更大的一定比小的选择多 sort(a.begin() + 1, a.begin() + 1 + n); priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q; vector<int> ans(n + 1, 0); for (int i = 1, j = 1; i <= n; i++) { //先把可能选择当前数的区间塞进去 while (j <= n && a[j].l <= i) q.push({a[j].r, a[j].id}), j++; if (q.empty() || q.top().first < i) { cout << -1 << endl; //右端点最小的也无法满足,那么不存在答案 return; } int x = q.top().second; q.pop(); ans[x] = i; } for (int i = 1; i <= n; i++) cout << ans[i] << " "; cout << endl; }