vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; }
int ans = 0; for (int i = 0; i < n; ) { int j = i + 1; while (j < n and a[j] % 2 == a[i] % 2) { j++; } ans += j - i - 1; i = j; } cout << ans << "\n"; }
constexprint MOD = 1e9 + 7; constexprint N = 1e5 + 10;
intnorm(int x){ if (x < 0) { x += MOD; } if (x >= MOD) { x -= MOD; } return x; } template<class T> T binpow(T a, int b){ T res = 1; for (; b; b /= 2, a *= a) { if (b % 2) { res *= a; } } return res; }
structZ { int x; Z(int x = 0) : x(norm(x)) {} intval()const{ return x; } Z operator-() const { returnZ(norm(MOD - x)); } Z inv()const{ assert(x != 0); returnbinpow(*this, MOD - 2); } Z &operator*=(const Z &rhs) { x = 1LL * x * rhs.x % MOD; return *this; } Z &operator+=(const Z &rhs) { x = norm(x + rhs.x); return *this; } Z &operator-=(const Z &rhs) { x = norm(x - rhs.x); return *this; } Z &operator/=(const Z &rhs) { return *this *= rhs.inv(); } friend Z operator*(const Z &lhs, const Z &rhs) { Z res = lhs; res *= rhs; return res; } friend Z operator+(const Z &lhs, const Z &rhs) { Z res = lhs; res += rhs; return res; } friend Z operator-(const Z &lhs, const Z &rhs) { Z res = lhs; res -= rhs; return res; } friend Z operator/(const Z &lhs, const Z &rhs) { Z res = lhs; res /= rhs; return res; } };
structCombination { vector<Z> fac, inv;
Combination(int n) : fac(n), inv(n) { fac[0] = 1; for (int i = 1; i < n; i++) fac[i] = fac[i - 1] * i; inv[n - 1] = fac[n - 1].inv(); for (int i = n - 2; i >= 0; i--) inv[i] = inv[i + 1] * (i + 1); }
Z C(int n, int m){ if(m < 0or m > n) return0; return fac[n] * inv[m] * inv[n - m]; }
Z A(int n, int m){ if(m < 0or m > n) return0; return fac[n] * inv[n - m]; } };
vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end());
vector<int> cnt(m + 1); int vis = 0; auto op = [&](int x, bool add) { for (auto i : d[x]) { if (i > m) { continue; } if (cnt[i] == 0) { vis++; } cnt[i] += (add ? 1 : -1); if (cnt[i] == 0) { vis--; } } };
int ans = INT_MAX; for (int l = 0, r = 0; l < n; l++) { while (r < n and vis != m) { op(a[r], true); r++; } if (vis == m) { ans = min(ans, a[r - 1] - a[l]); } op(a[l], false); } cout << (ans == INT_MAX ? -1 : ans) << "\n"; }
return0; }
D. Score of a Tree
题解
对于某一结点 u :
u 在第 i 秒的值是以 u 为根节点的子树内与 u 距离为 i 的所有结点初始值的异或
设子树内最大深度为 dmax ,则在 dmax+1 秒后结点 u 的值为 0
在 0∼dmax 秒,结点 u 值的期望都为 21 ( k 个布尔值选奇数项赋 1 ,有一半情况)
intnorm(int x){ if (x < 0) { x += MOD; } if (x >= MOD) { x -= MOD; } return x; } template<class T> T binpow(T a, int b){ T res = 1; for (; b; b /= 2, a *= a) { if (b % 2) { res *= a; } } return res; }
structZ { int x; Z(int x = 0) : x(norm(x)) {} intval()const{ return x; } Z operator-() const { returnZ(norm(MOD - x)); } Z inv()const{ assert(x != 0); returnbinpow(*this, MOD - 2); } Z &operator*=(const Z &rhs) { x = 1LL * x * rhs.x % MOD; return *this; } Z &operator+=(const Z &rhs) { x = norm(x + rhs.x); return *this; } Z &operator-=(const Z &rhs) { x = norm(x - rhs.x); return *this; } Z &operator/=(const Z &rhs) { return *this *= rhs.inv(); } friend Z operator*(const Z &lhs, const Z &rhs) { Z res = lhs; res *= rhs; return res; } friend Z operator+(const Z &lhs, const Z &rhs) { Z res = lhs; res += rhs; return res; } friend Z operator-(const Z &lhs, const Z &rhs) { Z res = lhs; res -= rhs; return res; } friend Z operator/(const Z &lhs, const Z &rhs) { Z res = lhs; res /= rhs; return res; } };