T1
- 首先证明单调性,如果打包 份,则必有小于 份的打包方案。
- 考虑二分打包份数,此时的芒果数量即 ;对于一种芒果,其能打包的最大数量即 因为每份只能选一个。
- 若 则该打包份数合法,往上找,反之往下。
- 时间复杂度 。
主要代码
while (lt <= rt) {
int mid = (lt + rt) >> 1;
if (check(mid) == 1)
{
lt = mid + 1;
ans = mid;
} else {
rt = mid - 1;
}
}bool check(int mid) {
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += min(mid, a[i]);
}
return sum >= mid * k;
}- 期望分数:20;实际分数:20。
T2
- 先使用
ST表维护区间最大值。 - 然后前缀和维护区间差分值。
- 最后二分答案区间长度判断是否满足。
- 其实二分的单调性证明挺繁琐的,
赛时应该都是猜的吧。
主要代码
bool check(int mid) {
for (int i = 1; i <= n - mid + 1; i++) {
int l = i, r = i + mid - 1;
int qu = pre[n] - (pre[r] - pre[l]);
int maxi = Query(l, r);
if (l != 1) {
qu = qu + abs(maxi - a[l - 1]) - abs(a[l] - a[l - 1]);
}
if (r != n) {
qu = qu + abs(a[r + 1] - maxi) - abs(a[r + 1] - a[r]);
}
if (qu <= x)
return 1;
}
return 0;
}- 期望分数:0;实际分数:0。
T3
- 字符串匹配?一眼
hash。 - 对于查询操作,很容易想到二分最远的相等距离。
- 对于修改操作,直接线段树维护
hash/ 树状数组维护hash。 - 时间复杂度 。
主要代码
#include <bits/stdc++.h>
#define lowbit(x) x & -x
using namespace std;
const int base = 27;
const int N = 1e5 + 5;
using u64 = unsigned long long;
int n, m;
string s;
int len;
int a[N], b[N], h[N];
int tree[N];
void pre() {
b[0] = 1;
for (int i = 1; i <= n; i++) {
b[i] = b[i - 1] * base;
}
return ;
}
void update(int x, int k) {
int sum = 0;
while (x <= n) {
tree[x] += k * b[sum];
sum += lowbit(x);
x += lowbit(x);
}
return ;
}
u64 ask(int x) {
u64 res = 0;
int sum = 0;
while (x >= 1) {
res += tree[x] * b[sum];
sum += lowbit(x);
x -= lowbit(x);
}
return res;
}
u64 query(int l, int r) {
return ask(r) - ask(l - 1) * b[r - l + 1];
// h[r] - h[l - 1] * b[r - l + 1]
}
void solve() {
int opt;
cin >> opt;
if (opt == 1) {
int pos;
char c;
cin >> pos >> c;
int d = c - 'a' + 1;
update(pos, d - a[pos]);
a[pos] = d;
} else {
int l1, r1, l2, r2;
cin >> l1 >> r1 >> l2 >> r2;
if (r1 - l1 + 1 != r2 - l2 + 1) {
cout << "NO\n";
return ;
}
int lt = 1, rt = r1 - l1 + 2, ans;
while (lt < rt) {
int mid = (lt + rt) >> 1;
if (query(l1, l1 + mid - 1) == query(l2, l2 + mid - 1)) {
lt = mid + 1;
} else {
rt = mid;
}
}
if (lt == r1 - l1 + 1) {
cout << "YES\n";
} else if (query(l1 + lt, r1) != query(l2 + lt, r2)) {
cout << "NO\n";
} else {
cout << "YES\n";
}
}
return ;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m >> s;
s = " " + s;
pre();
for (int i = 1; i <= n; i++) {
a[i] = s[i] - 'a' + 1;
update(i, a[i]);
}
while (m--) {
solve();
}
return 0;
}- 当然实际上这题我还有另一二分思路,也是我的赛时思路:直接二分查找。因为勉强相等的定义为至多只有一个,所以每次二分的左区间与右区间的
hash值至多只有一个不相同。 - 时间复杂度 。
主要代码(实测能过)
int lt1 = l1, rt1 = r1;
int lt2 = l2, rt2 = r2;
while (lt1 <= rt1) {
int mid1 = (lt1 + rt1) >> 1;
int mid2 = (lt2 + rt2) >> 1;
if (lt1 == rt1) {
cout << "YES\n";
return ;
}
u64 hash1 = get(lt1, mid1), hash2 = get(mid1 + 1, rt1);
u64 hash3 = get(lt2, mid2), hash4 = get(mid2 + 1, rt2);
if (hash1 != hash3 && hash2 != hash4) {
cout << "NO\n";
return ;
} else if (hash1 == hash3) {
lt1 = mid1 + 1;
lt2 = mid2 + 1;
} else {
rt1 = mid1 - 1;
rt2 = mid2 - 1;
}
}- 期望分数:100;实际分数:30。
我不会线段树维护 hash 啊!!!
T4
- 当 时, 进制数在该题允许范围下最多只有三位,此时枚举 进制数,然后二分进制取答案。
- 若二分得到的进制数小于 ,则直接枚举进制数即可。
主要代码
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
i64 n, m;
i64 zhuan(i64 x, i64 y) {
i64 ans = 0, cnt = 1;
while (x != 0) {
if (x % y >= 10)
return -1;
ans += cnt * (x % y);
cnt *= 10;
x /= y;
}
return ans;
}
i64 check(i64 x, i64 y) {
return x % 10 + (x / 10 % 10) * y + (x / 100) * (y * y);
}
int main() {
// ios::sync_with_stdio(0);
// cin.tie(0), cout.tie(0);
cin >> n >> m;
i64 i, ans = 10, mid;
for (i = m; i < 1000; i++) {
i64 lt = 2, rt = 1e18;
if (i >= 100) {
rt = 1e9;
}
while (lt <= rt) {
mid = (lt + rt) / 2;
if (check(i, mid) >= n) {
rt = mid - 1;
} else {
lt = mid + 1;
}
}
if (check(i, rt + 1) == n) {
ans = max(ans, rt + 1);
}
}
for (i = 1e6; i >= ans; i--) {
if (zhuan(n, i) >= m) {
ans = i;
break;
}
}
cout << ans << "\n";
return 0;
}- 期望分数:0;实际分数:0。