A 小苯的数字染色
简单来说,任何一个大于 的数字都可以表示为
个
和
个
的和(
)
简单证明:
令 ,
,得
可以把他看成, ,显然任何一个整数除以2都有商和余数。
需要注意, 时不满足
。
void solve() {
int n;
cin >> n;
if (n == 1) cout << "NO" << "\n";
else cout << "YES" << "\n";
}
B 小苯的数组重排
化简一下式子,,很显然减掉最小的两个数字即可。
void solve() {
int n, mn1 = inf, mn2 = inf;
ll sum = 0;
cin >> n;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
sum += x;
if (x < mn1) mn2 = mn1, mn1 = x;
else if (mn1 <= x && x < mn2) mn2 = x;
}
cout << 2 * sum - mn1 - mn2 << "\n";
}
C 小苯的麦克斯
首先可以看出:
- 区间越小越好,因为干扰的数字少
- 对于一个长度为
的区间,记较大数字为
,较小数字为
,
。
检查每一个长度为 的区间即可。
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
ll ans = -INF;
for (int i = 1; i < n; ++i) {
ll x = max(a[i], a[i - 1]), y = min(a[i], a[i - 1]);
ans = max(ans, x - (y == 0) * (1 + (x == 1)));
}
cout << ans << "\n";
}
D 小苯的平衡序列
排序一下,删最大或者最小,看看哪个平衡度小即可。
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
sort(all(a));
ll m1 = a[n / 2 - 1], m2 = a[n / 2];
ll s1 = abs(a[0] - m1), s2 = abs(a[n - 1] - m2);
for (int i = 1; i < n - 1; ++i) {
s1 += abs(a[i] - m1);
s2 += abs(a[i] - m2);
}
cout << min(s1, s2) << "\n";
}
E 小苯的数字变换
一个子串的数字和对 取模等于该子串各位数字之和对
取模。
打表发现
记 为长度为
的前缀数位和,
。所以统计 模为
的子串个数 ,对于固定
(右端),所有满足
的
等价于
扫一遍统计一下即可。
注意在统计的时候,我们把 当成
处理了,所以还要减去
串。
void solve() {
string s;
cin >> s;
int n = s.size();
vector<int> cnt(9);
ll ans = 0;
int prev = 0;
cnt[0] = 1;
for (int i = 0; i < n; i++) {
int d = s[i] - '0';
prev = (prev + d) % 9;
for (int m = 0; m < 9; m++) {
int v = (prev - m + 9) % 9;
ans += cnt[m] * (v ? v : 9);
}
cnt[prev]++;
}
ll counts = 0;
ll len = 0;
for (char c : s) {
if (c == '0') {
len++;
counts += len;
} else {
len = 0;
}
}
ans -= 9 * counts;
cout << ans << "\n";
}
F 小苯的序列合并
从高位往低位尝试放 ,判断是否存在一种分割,使得能够在不影响已经放完的位的情况下,把当前位置
。用前缀异或和判断
即可。
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
ll ans = 0;
for (int i = 30; i >= 0; --i) {
int cand = ans | (1 << i), prev = 0;
bool ok = false;
unordered_set<int> vis;
vis.insert(0);
for (int i = 0; i < n; ++i) {
prev ^= a[i];
int t = prev & cand;
if (vis.find(ca***is.end()) {
if (i == n - 1) ok = true;
vis.insert(t);
}
}
if (ok) ans = cand;
}
cout << ans << "\n";
}
头文件
//Another
#include<bits/stdc++.h>
#include<bits/extc++.h>
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef __int128 i128;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef tuple<ll, ll, ll> TLLL;
typedef __gnu_pbds::tree<PLL, __gnu_pbds::null_type, less<PLL>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> Tree;
// typedef __gnu_pbds::tree<ll, __gnu_pbds::null_type, less<ll>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> Tree;
constexpr int inf = (ll)1e9 + 7;
constexpr ll INF = (ll)2e18 + 9;
// constexpr ll INF = (ll)4e18;
// constexpr ll MOD = 1e9 + 7;
constexpr ll MOD = 998244353;
constexpr ld PI = acos(-1.0);
constexpr ld eps = 1e-10;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ull randint(ull l, ull r) {uniform_int_distribution<unsigned long long> dist(l, r); return dist(rng);}
void init() {
}
void solve() {
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
init();
int t = 1;
cin >> t;
for (int _ = 1; _ <= t; ++_) {
solve();
}
return 0;
}