E.Elevator
把原条件转化为求剩余距离,然后转二维数点树状数组维护一下即可。
#include <bits/stdc++.h>
#define lowbit(x) ((x) & (-x))
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e6 + 10, MOD = 998244353;
struct node {
int x, id;
const bool operator< (const node &b) const { return x < b.x || (x == b.x && id < b.id); }
}a[N];
int ans[N], sum[N];
namespace Fenwick{
int a[N], len;
void add(int x, int v) {
for(; x <= len; x += lowbit(x)) a[x] += v;
}
int query(int x, int ans = 0) {
for(; x >= 1; x -= lowbit(x)) ans += a[x];
return ans;
}
}
inline void solve() {
int n, m; cin >> n >> m;
Fenwick::len = n;
for(int i = 1; i <= n; i++) cin >> a[i].x, a[i].id = i;
sort(a + 1, a + 1 + n);
for(int i = 2; i <= n; i++) {
sum[a[i].id] = sum[a[i - 1].id] + (i - 1) * (a[i].x - a[i - 1].x);
}
for(int i = 1; i <= n; i++) {
sum[a[i].id] += Fenwick::query(a[i].id);
Fenwick::add(a[i].id, 1);
}
for(int i = 1; i <= n; i++) {
if(sum[i] > m - 2) cout << -1 << endl;
else cout << sum[i] << endl;
}
}
signed main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int t = 1; // cin >> t;
while(t--) solve();
return 0;
}
H.GameX
稍微玩一下发现每次的最优一定是选择最小的奇数/选择最小的偶数。那么暴力筛个数出来统计奇数偶数的个数即可,相等时特判一下下个数字。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 10;
map<int, bool> mp;
vector<int> p;
inline void solve() {
mp.clear(); p.clear();
int n = 0, k = 0; cin >> n >> k;
for(int i = 1; i <= n; i++) {
int x; cin >> x;
mp[x] = 1;
}
int num = 0;
for(int i = 0; i <= 2 * k; i++) {
while(mp.count(num)) num++;
p.emplace_back(num); num++;
}
int cnt_odd = 0, cnt_even = 0;
for(auto x : p) {
if(x & 1) cnt_odd++;
else cnt_even++;
}
// cout << cnt_odd << " " << cnt_even << endl;
if(cnt_odd == cnt_even) {
if(num & 1) cout << "Bob\n";
else cout << "Alice\n";
}
else if(cnt_odd > cnt_even) cout << "Bob\n";
else cout << "Alice\n";
}
signed main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int t = 1; cin >> t;
while(t--) solve();
return 0;
}
K.Middle Point Graph
贴个题解的分析:
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 2e5 + 10, MOD = 1e9 + 7;
vector<int> g[N], key[N];
int kth[N], rk[N], deg[N], cnt[N], n, m, mod2;
int binpow(int x, int y, int mod, int res = 1){
for (; y; y >>= 1, (x *= x) %= mod) if (y & 1) (res *= x) %= mod;
return res;
}
vector<pair<int, int>> edges;
int cycle3() {
int ans = 0;
for(int u = 1; u <= n; u++) cnt[u] = 0, key[u].clear();
for(auto &[u, v] : edges) {
if(deg[u] < deg[v] || deg[u] == deg[v] && u < v) key[u].emplace_back(v);
else key[v].emplace_back(u);
}
for(int x = 1; x <= n; x++) {
for(auto y : key[x]) cnt[y] = x;
for(auto y : key[x]) for(auto z : key[y]) if(cnt[z] == x) ans++;
}
return ans;
}
int cycle4() {
int ans = 0;
iota(kth, kth + 1 + n, 0);
sort(kth + 1, kth + n + 1, [&](int x, int y){ return deg[x] < deg[y] || deg[x] == deg[y] && x < y; });
for(int i = 1; i <= n; i++) cnt[i] = 0, rk[kth[i]] = i, key[i].clear();
for(int u = 1; u <= n; u++) {
for(auto v : g[u]) if(rk[v] > rk[u]) key[u].emplace_back(v);
}
for(int u = 1; u <= n; u++) {
for(auto v : g[u])
for(auto w : key[v]) if(rk[w] > rk[u]) ans += cnt[w]++;
for(auto v : g[u])
for(auto w : key[v]) if(rk[w] > rk[u]) --cnt[w];
}
return ans;
}
inline void solve() {
cin >> n >> m;
edges.clear();
for(int i = 1; i <= n; i++) deg[i] = 0, g[i].clear();
for(int i = 1; i <= m; i++) {
int u, v; cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
deg[u]++, deg[v]++;
edges.emplace_back(make_pair(u, v));
}
// cout << cycle3() << "@@@@" << cycle4() << endl;
int ans = 0;
for(int i = 1; i <= n; i++) {
ans += (deg[i] * (deg[i] - 1) / 2);
ans %= MOD;
}
(ans += m * ((n + m - 3 + MOD) % MOD) % MOD) %= MOD;
(ans += (cycle3() * 3 % MOD)) %= MOD;
(ans += cycle4()) %= MOD;
cout << ans << endl;
}
signed main() {
mod2 = binpow(2, MOD - 2, MOD);
ios_base::sync_with_stdio(false), cin.tie(0);
int t = 1; cin >> t;
while(t--) solve();
return 0;
}
L.Station of Fate
组合数学,不同球入不同盒,全排列*C[空数][插板数]即可。
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e5 + 10, MOD = 998244353;
int fac[N];
void init() {
fac[0] = 1;
for(int i = 1; i <= 1e5; i++) {
fac[i] = fac[i - 1] * i;
fac[i] %= MOD;
}
}
long long binpow(long long a, long long b) {
long long res = 1;
while (b > 0) {
if (b & 1) res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
inline void solve() {
int n, m; cin >> n >> m;
int ans = fac[n - 1] * fac[n] % MOD;
ans *= binpow(fac[m - 1] * fac[n - m] % MOD, MOD - 2);
cout << ans % MOD << endl;
}
signed main() {
init();
ios_base::sync_with_stdio(false), cin.tie(0);
int t = 1; cin >> t;
while(t--) solve();
return 0;
}