#include<bits/stdc++.h> intii(){ int x; scanf("%d", &x); return x; } intmain(){ int n = ii(); int k = ii(); std::priority_queue<int> vec; for (int i = 0; i < k; ++i) vec.push(0); int a[n]; for (int i = 0; i < n; ++i) a[i] = ii(); std::sort(a, a + n); longlong ans{}; for (int i = 0; i < n; ++i) { int x = -vec.top(); vec.pop(); int y = -x - a[i]; ans += x + a[i]; vec.push(y); } std::cout << ans << std::endl; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 207 #define mod 998244353
inlineintfpow(int x, int t){ int res = 1; for (; t; t >>= 1, x = 1ll * x * x % mod) if (t & 1) res = 1ll * res * x % mod; return res; }
ll pos[N], p[N][N], c[N][N], f[N][N];
intmain(){ int n = rd(), m = rd(), s = rd(); int invn = fpow(n, mod - 2); for (int i = 0; i <= m; ++i) { c[i][0] = 1; for (int j = 1; j <= i; ++j) c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod; } for (int i = 1; i <= m; ++i) { pos[i] = rd(); if (pos[i] > s) pos[i] -= n; } pos[0] = s - n; sort(pos + 1, pos + 1 + m); for (int i = 1; i <= m; ++i) { p[i][0] = 1; p[i][1] = 1ll * (pos[m - i + 1] - pos[m - i]) * invn % mod; for (int j = 2; j <= m; ++j) p[i][j] = p[i][j - 1] * p[i][1] % mod; } f[0][0] = 1; for (int i = 1; i <= m; ++i) for (int j = 0; j < i; ++j) for (int k = 0; k <= j; ++k) f[i][j] = (f[i][j] + c[j][k] * f[i - 1][j - k] % mod * p[i][k]) % mod; ll ans = 0; for (int j = 0; j < m; ++j) ans = (ans + f[m][j]) % mod; printf("%lld\n", ans); return0; }
K - K-Shaped Figures
给定平面上 \(n\le 10^3\)
个线段,问有多少个 \(K\)
,定义三条线段构成了一个 K :
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 1007 #define letp const P
structP { ll x, y; P(ll x = 0, ll y = 0) : x(x), y(y) {} P operator + (letp &p) const {return {x + p.x, y + p.y};} P operator - (letp &p) const {return {x - p.x, y - p.y};} ll operator | (letp &p) const {return x * p.x + y * p.y;} // dot ll operator ^ (letp &p) const {return x * p.y - y * p.x;} // cross booloperator < (letp &p) const {return x < p.x || (x == p.x && y < p.y);} booloperator == (letp &p) const {return x == p.x && y == p.y;} intori(letp &p)const{ll t = (*this) ^ p; return (t > 0) - (t < 0);} };
vector<P> pt;
structargcmp { booloperator()(letp &a, letp &b)const{ constauto quad = [](letp &a) { if (a.y < 0) return1; if (a.y > 0) return4; if (a.x < 0) return5; if (a.x > 0) return3; return2; }; constint qa = quad(a), qb = quad(b); if (qa != qb) return qa < qb; constauto t = (a ^ b); return t > 0; } };
structS { P a, b; intis_on(letp &p)const{ if (p == a || p == b) return-1; return (p - a).ori(p - b) == 0 && ((p - a) | (p - b)) < 0; } intori(letp &p)const{return (b - a).ori(p - a);} } s[N];
vector<pair<P, S>> Cr;
vector<P> out;
vector<pair<P, int>> Out;
inline ll c2(int x){return1ll * x * (x - 1) / 2;}
inlinevoidwork(){ int n = rd(); pt.clear(); for (int i = 1; i <= n; ++i) { P a, b; a.x = rd(); a.y = rd(); b.x = rd(); b.y = rd(); pt.push_back(a); pt.push_back(b); s[i] = {a, b}; } sort(pt.begin(), pt.end()); pt.erase(unique(pt.begin(), pt.end()), pt.end());
ll ans = 0; for (auto p : pt) { Cr.clear(); out.clear(); Out.clear(); for (int i = 1; i <= n; ++i) { int fl = s[i].is_on(p); if (fl == 1) { Cr.push_back(make_pair(s[i].a, S{s[i].a, s[i].b})); Cr.push_back(make_pair(s[i].b, S{s[i].b, s[i].a})); } elseif (fl == -1) { out.push_back(p == s[i].a ? s[i].b : s[i].a); } } if (Cr.empty()) continue; if (out.empty()) continue; Cr.push_back(make_pair(P{p.x - 100000000, p.y}, S{P{p.x - 100000000, p.y}, P{p.x + 1, p.y}})); Cr.push_back(make_pair(P{p.x + 100000000, p.y}, S{P{p.x + 100000000, p.y}, P{p.x - 1, p.y}})); Cr.push_back(make_pair(P{p.x, p.y - 100000000}, S{P{p.x, p.y - 100000000}, P{p.x, p.y + 1}})); Cr.push_back(make_pair(P{p.x, p.y + 100000000}, S{P{p.x, p.y + 100000000}, P{p.x, p.y - 1}})); auto cmp = [&](P &a, P &b) {return argcmp()(a - p, b - p);}; auto cmpCr = [&](pair<P, S> &a, pair<P, S> &b) {return cmp(a.first, b.first);}; auto cmpOut = [&](pair<P, int> &a, pair<P, int> &b) {return cmp(a.first, b.first);};
sort(Cr.begin(), Cr.end(), cmpCr); sort(out.begin(), out.end(), cmp); auto para = [&](P &a, P &b) {return ((a - p) ^ (b - p)) == 0 && ((a - p) | (b - p)) > 0;}; for (auto x : out) if (Out.empty() || !para(Out.back().first, x)) Out.push_back(make_pair(x, 1)); else ++Out[Out.size() - 1].second; Out.push_back(make_pair(P{p.x + 1, p.y}, 0)); Out.push_back(make_pair(P{p.x - 1, p.y}, 0)); Out.push_back(make_pair(P{p.x, p.y + 1}, 0)); Out.push_back(make_pair(P{p.x, p.y - 1}, 0)); sort(Out.begin(), Out.end(), cmpOut); int sz = Out.size(); auto nxt = [&](int x) {return x == sz - 1 ? 0 : x + 1;}; auto pre = [&](int x) {return x == 0 ? sz - 1 : x - 1;}; int l = 0, r = 0; auto pos = [&](S &l, P &x) {return l.ori(x) > 0;}; ll sum = 0, del = 0; while (!pos(Cr[0].second, Out[r].first)) r = nxt(r); while (pos(Cr[0].second, Out[r].first)) r = nxt(r); r = pre(r); l = r; while (pos(Cr[0].second, Out[l].first)) l = pre(l); l = nxt(l); for (int i = l; i != nxt(r); i = nxt(i)) { sum += Out[i].second; del += c2(Out[i].second); } for (auto [tmp, s] : Cr) { while (pos(s, Out[nxt(r)].first)) { r = nxt(r); sum += Out[r].second; del += c2(Out[r].second); } while (!pos(s, Out[l].first)) { sum -= Out[l].second; del -= c2(Out[l].second); l = nxt(l); } if (max(abs(tmp.x), abs(tmp.y)) <= 10000000) ans += c2(sum) - del; } } printf("%lld\n", ans); }
intmain(){ int t; scanf("%d", &t); for (int i = 1; i <= t; ++i) work(); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
intgcd(int a, int b){return b ? gcd(b, a % b) : a;}
#define N 1007 #define letp const P
structP { ll x, y; P(ll x = 0, ll y = 0) : x(x), y(y) {} P operator + (letp &p) const {return {x + p.x, y + p.y};} P operator - (letp &p) const {return {x - p.x, y - p.y};} P operator / (int t) const {return {x / t, y / t};} ll operator | (letp &p) const {return x * p.x + y * p.y;} // dot ll operator ^ (letp &p) const {return x * p.y - y * p.x;} // cross booloperator < (letp &p) const {return x < p.x || (x == p.x && y < p.y);} booloperator == (letp &p) const {return x == p.x && y == p.y;} intori(letp &p)const{ll t = (*this) ^ p; return (t > 0) - (t < 0);} };
vector<P> pt;
structargcmp { booloperator()(letp &a, letp &b)const{ constauto quad = [](letp &a) { if (a.y < 0) return1; if (a.y > 0) return4; if (a.x < 0) return5; if (a.x > 0) return3; return2; }; constint qa = quad(a), qb = quad(b); if (qa != qb) return qa < qb; constauto t = (a ^ b); return t > 0; } };
structS { P a, b; intis_on(letp &p)const{ if (p == a || p == b) return-1; return (p - a).ori(p - b) == 0 && ((p - a) | (p - b)) < 0; } intori(letp &p)const{return (b - a).ori(p - a);} } s[N];
#include<bits/stdc++.h> intii(){ int x; scanf("%d", &x); return x; } intmain(){ int n = ii(); int a[n]; for (int i = 0; i < n; ++i) a[i] = ii(); int b[n]; for (int i = 0; i < n; ++i) b[i] = ii(); std::vector<int> ans; for (int i = 0; i < n; ++i) { int k = 0; for (int j = i; j < n; ++j) if (a[j] == b[i]) k = j; for (; k > i; --k) if (abs(a[k - 1] - a[k]) < 2) { puts("-1"); return0; } else { ans.push_back(k); std::swap(a[k - 1], a[k]); } } int m = ans.size(); std::cout << ans.size() << std::endl; for (int i = 0; i < m; ++i) std::cout << ans[i] << " \n"[i == m - 1]; }