1015 - 模拟赛改题
T389391 修改 01 序列 (mo)
思路
由于题目说的是两个相邻的 1 的距离为 1 的位置统计起来,那么会有一种思路是直接统计,但是这样子不好求,不如反过来思考,思考有多少个位置是余数相同的位置而且有 1 的位置,然后将所有的 1 的数量减去这些统计好的数量,就可以的到我们上述的答案了。
code
#include <iostream>
using namespace std;
const int MaxN = 1e5 + 10;
int a[MaxN], cnt[MaxN], n, d, ans = 1e9, sum;
int main() {
cin >> n >> d;
for (int i = 1; i <= n; i++) {
cin >> a[i], cnt[i % d] += a[i], sum += a[i];
}
for (int i = 0; i < d; i++) {
ans = min(ans, sum - cnt[i]);
}
cout << ans << endl;
return 0;
}
时空复杂度
时间:
T389365 集合 (set)
思路
可以考虑每个子集但来的贡献,对于每个子集的贡献应等于其和,我们会发现其和其实会有很多相同的子集,可以一起计算,那么于是可以将和做为下标,然后统计方案数,这样就可以求了,求法就是方案数的和次方,求方案数可以用
不过我们要取模,但这个方案数是指数,我们发现模数是质数,那么可以取余
code
#include <iostream>
using namespace std;
using ll = long long;
const int MaxN = 210, mod = 998244353;
int n;
ll dp[MaxN * MaxN / 2], ans = 1;
ll qpow(ll a, ll b) {
ll res = 1;
for (ll i = 1; i <= b; i <<= 1) {
(b & i) && (res = res * a % mod);
a = a * a % mod;
}
return res;
}
int main() {
cin >> n, dp[0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = (n + 1) * n / 2; j >= 0; j--) {
if (j + i <= (n + 1) * n / 2) {
dp[i + j] = (dp[i + j] + dp[j]) % (mod - 1);
}
}
}
for (int i = 1; i <= (n + 1) * n / 2; i++) {
ans = (ans * qpow(i, dp[i])) % mod;
}
cout << ans << endl;
return 0;
}
时空复杂度
时间:
T389377 情景剧 (tv)
思路
这道题的唯一的难点是如果区间越大那么最小值可能会变小,所以可以枚举最小值,这样可以保证区间越大答案也会越大,不过要保证区间最小值是和枚举的一样的才行,那么也就是找到往左右寻找比当前的还要小的位置,这个情况可以用单调栈来做,那么最大值的维护,可以用
code
#include <iostream>
using namespace std;
const int MaxN = 2e6 + 10, MaxK = 22;
int f[MaxN][MaxK], s[MaxN], mpr[MaxN], mpl[MaxN], tot, n;
unsigned long long ans;
void init() {
for (int j = 1; j < MaxK; j++) {
for (int i = 1; i + (1 << j) - 1 <= n; i++) {
f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
}
}
}
int Q(int l, int g) {
int ans = 0;
for (int i = 0; i < MaxK; i++) {
if (g >> i & 1) {
ans = max(ans, f[l][i]);
l += (1 << i);
}
}
return ans;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> f[i][0];
}
init();
for (int i = 1; i <= n; i++) {
while (tot > 0 && f[s[tot]][0] > f[i][0]) {
mpr[s[tot]] = i - 1;
tot--;
}
s[++tot] = i;
}
while (tot > 0) {
mpr[s[tot]] = n;
tot--;
}
for (int i = n; i >= 1; i--) {
while (tot > 0 && f[s[tot]][0] > f[i][0]) {
mpl[s[tot]] = i + 1;
tot--;
}
s[++tot] = i;
}
while (tot > 0) {
mpl[s[tot]] = 1;
tot--;
}
for (int i = 1; i <= n; i++) {
ans = max(ans, (unsigned long long)(mpr[i] - mpl[i] + 1) * Q(mpl[i], mpr[i] - mpl[i] + 1) * f[i][0]);
}
cout << ans << endl;
return 0;
}
时空复杂度
时间: