K-periodic Garland

给定一个长度位\(n\)的\(01\)串,每次操作可以将\(1\)变为\(0\)或者将\(0\)变为\(1\),现在你需要通过操作使得所有\(1\)之间的距离为\(k\),求最少的操作次数,注意全为\(0\)也算

\(1<=n<=1e6,1<=k<=n\)

题解:\(dp\) / 贪心 : 最大子段和思想

方法一:\(dp\) \(O(n)\)

状态表示:\(f[i][0/1]\):代表将区间\([1,i]\)变为合法串的最小操作次数,且第\(i\)位为\(0/1\)

状态转移:

  1. 贪心考虑只有两种情况的时候我们可以将第\(i\)位变成\(1\):
  • 若第\(i-k\)位也是\(1\),我们可以考虑将第\(i\)位变为\(1\),那么我们需要将\([i-k+1,i-1]\)中的所有1变为0
  • 我们同样可以考虑使第\(i\)位前面所有的1变为0,从第\(i\)位的\(1\)重新当作起始位置,那么我们需要将前面所有的1变为0
  • 同时如果该位本身不是\(1\),我们需要将其变为\(1\)

\(f[i][1]=min(f[i][1]+pre[i-1]-pre[i-k],pre[i-1]) + (s[i]=='0')\)

  1. 同样只有两种情况我们可以将第\(i\)位变为\(0\):
  • 第\(i-1\)位是1的情况
  • 第\(i-1\)位是0的情况
  • 如果第\(i\)位不是0,我们需要将其变为0

\(f[i][0]=min(f[i-1][1],f[i-1][0])+(s[i]=='1')\)

状态初始:\(f[i]=0\)

方法二:贪心 + 枚举对\(k\)余数 \(复杂度:调和级数O(nlogn)\)

由题意可知这些\(1\)一定是连续的且距离间隔\(k\),这说明1所处的位置对\(k\)取模的模数是相同的,且这些1连续,所以我们可以考虑根据对\(k\)的余数来枚举\(1\)的起始位置,所以我们不妨先将所有的1变为0

我们设\(cnt\):可以减免的答案贡献,或者说从起始位置到现在1的前缀和数量与0的前缀和数量之差;

  1. 对于某一位来说,我们需要其为1,并且其本身已经为1,说明我们原本不用将其变为0,所以这部分答案可以减免,\(cnt\)++,
  2. 对于某一位来说,我们需要其为1,但是其本身为0,说明我们这部分答案不能减免,\(cnt\)--,
  3. 如果\(cnt<0\),说明从起始位置开始0的数量已经比1的数量多了,也就是说我们将这些前面位置都变成1就比全部变为0吃亏,倒不如全部变为0,借用最大子段和的思想我们直接舍弃前面的部分,将该位置重新当成起始位置,\(cnt=0\)
  4. 我们在枚举过程中始终维护\(cnt\)的最大值即可

时间复杂度为调和级数:\(O(nlogn)\)

#include <bits/stdc++.h>
#define Zeoy std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define debug(x) cerr << #x << '=' << x << endl
#define all(x) (x).begin(), (x).end()
#define rson id << 1 | 1
#define lson id << 1
#define int long long
#define mpk make_pair
#define endl '\n'
using namespace std;
typedef unsigned long long ULL;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int N = 1e6 + 10, M = 4e5 + 10; int n, k;
int f[N][2];
int pre[N]; void solve()
{
cin >> n >> k;
string s;
cin >> s;
s = " " + s;
for (int i = 1; i <= n; ++i)
pre[i] = pre[i - 1] + (s[i] == '1');
for (int i = 1; i <= n; ++i)
{
f[i][1] = min(f[max(0ll, i - k)][1] + pre[i - 1] - pre[max(0ll, i - k)], pre[i - 1]) + (s[i] == '0');
f[i][0] = min(f[i - 1][1], f[i - 1][0]) + (s[i] == '1');
}
cout << min(f[n][0], f[n][1]) << endl;
}
signed main(void)
{
Zeoy;
int T = 1;
cin >> T;
while (T--)
{
solve();
}
return 0;
}
#include <bits/stdc++.h>
#define Zeoy std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define debug(x) cerr << #x << '=' << x << endl
#define all(x) (x).begin(), (x).end()
#define rson id << 1 | 1
#define lson id << 1
#define int long long
#define mpk make_pair
#define endl '\n'
using namespace std;
typedef unsigned long long ULL;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int N = 1e6 + 10, M = 4e5 + 10; int n, k; void solve()
{
cin >> n >> k;
string s;
cin >> s;
s = " " + s;
int ans = 0;
for (int i = 1; i <= n; ++i)
if (s[i] == '1')
ans++;
int maxx = -INF;
for (int i = 1; i <= k; ++i)
{
int cnt = 0;
for (int j = i; j <= n; j += k)
{
if (s[j] == '1')
cnt++;
else
cnt--;
if (cnt < 0)
cnt = 0;
maxx = max(cnt, maxx);
}
}
cout << ans - maxx << endl;
}
signed main(void)
{
Zeoy;
int T = 1;
cin >> T;
while (T--)
{
solve();
}
return 0;
}

Decreasing Heights

给出一个 \(n \times m\) 的矩阵,每个矩阵的权值代表该点的初始高度。

现在需要从点 \(( 1 , 1 )\) 走到点 \(( n , m )\) ,每一步需要满足以下条件:

  • 只能向右或向下
  • 设当前格子的高度为 \(x\) ,只能移动到高度为 \(x + 1\) 的格子上去

初始时可以进行操作,使得某个格子的高度减少一个单位。

问最少需要进行多少次操作,可以存在至少一条从点 \(( 1 , 1 )\) 到点 \(( n , m )\) 的路线

\(1≤n,m≤100\)

题解:\(dp\) + 枚举

我们发现只要确定起点\((1,1)\)的高度,那么对于任意位置\((i,j)\)的高度我们都是确定的:\(i+j-2\),所以我们只需要枚举起点的高度即可,但是我们怎么枚举?

我们贪心的进行枚举,每次枚举的起点高度能够保证使得位置\((i,j)\)上的高度不需要降低,即起码保证有一个位置我们不需要改变其高度,如果这个位置原有的高度\(h\),且\(h<a[1][1]+i+j-2\),说明我们不能通过降低起点的高度使得到达\((i,j)\)时不需要降低\((i,j)\)的高度,否则我们可以利用\(dp\)求出从起点到终点的最小操作次数

设起点高度为\(h\)

状态表示:\(f[i][j]\):从起点\((1,1)\)到达\((i,j)\)所需的最小操作次数

状态转移:

  1. 该位置是从上面过来的,即\((i-1,j)->(i,j)\)

\(f[i][j] = min(f[i][j],f[i-1][j]+a[i][j]-(h+i+j-2)),a[i][j]>=h+i+j-2\)

  1. 该位置是从右边过来的,即\((i,j-1)->(i,j)\)

\(f[i][j] = min(f[i][j],f[i][j-1]+a[i][j]-(h+i+j-2)),a[i][j]>=h+i+j-2\)

  1. 如果$ a[i][j]<h+i+j-2 \(,说明位置\)(i,j)$无法到达,我们直接跳过即可
#include <bits/stdc++.h>
#define Zeoy std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define debug(x) cerr << #x << '=' << x << endl
#define all(x) (x).begin(), (x).end()
#define rson id << 1 | 1
#define lson id << 1
#define int long long
#define mpk make_pair
#define endl '\n'
using namespace std;
typedef unsigned long long ULL;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int N = 1e2 + 10, M = 4e5 + 10; int n, m;
int a[N][N]; int calc(int x, int y)
{
vector<vector<int>> f(n + 10, vector<int>(m + 10, INF));
int h = a[x][y] - x - y + 2;
f[1][1] = a[1][1] + x + y - 2 - a[x][y];
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
int cur = h + i + j - 2;
if (cur > a[i][j])
continue;
f[i][j] = min(f[i][j], f[i - 1][j] + a[i][j] - cur);
f[i][j] = min(f[i][j], f[i][j - 1] + a[i][j] - cur);
}
}
return f[n][m];
} void solve()
{
cin >> n >> m;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
cin >> a[i][j];
int ans = INF;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
if (a[1][1] + i + j - 2 < a[i][j])
continue;
ans = min(ans, calc(i, j));
}
}
cout << ans << endl;
}
signed main(void)
{
Zeoy;
int T = 1;
cin >> T;
while (T--)
{
solve();
}
return 0;
}

Codeforces Round 642 (Div3)的更多相关文章

  1. 【赛时总结】◇赛时·V◇ Codeforces Round #486 Div3

    ◇赛时·V◇ Codeforces Round #486 Div3 又是一场历史悠久的比赛,老师拉着我回来考古了……为了不抢了后面一些同学的排名,我没有做A题 ◆ 题目&解析 [B题]Subs ...

  2. CodeForces Round #527 (Div3) B. Teams Forming

    http://codeforces.com/contest/1092/problem/B There are nn students in a university. The number of st ...

  3. CodeForces Round#480 div3 第2场

    这次div3比上次多一道, 也加了半小时, 说区分不出1600以上的水平.(我也不清楚). A. Remove Duplicates 题意:给你一个数组,删除这个数组中相同的元素, 并且保留右边的元素 ...

  4. CODEFORCES ROUND#624 DIV3

    这次比赛从名字就可以看出非常水,然鹅因为第一次打codeforces不太熟悉操作只来的及做签到题(还错了一次) A,B,C都是签到题考点思维就不写了 D题 https://codeforces.ml/ ...

  5. A. Launch of Collider Codeforces Round #363 (Div2)

    A. Launch of Collider time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  6. CodeForces Round #527 (Div3) D2. Great Vova Wall (Version 2)

    http://codeforces.com/contest/1092/problem/D2 Vova's family is building the Great Vova Wall (named b ...

  7. CodeForces Round #527 (Div3) D1. Great Vova Wall (Version 1)

    http://codeforces.com/contest/1092/problem/D1 Vova's family is building the Great Vova Wall (named b ...

  8. CodeForces Round #527 (Div3) C. Prefixes and Suffixes

    http://codeforces.com/contest/1092/problem/C Ivan wants to play a game with you. He picked some stri ...

  9. CodeForces Round #527 (Div3) A. Uniform String

    http://codeforces.com/contest/1092/problem/A You are given two integers nn and kk. Your task is to c ...

  10. 「暑期训练」「Brute Force」 Optimal Point on a Line (Educational Codeforces Round 16, B)

    题意 You are given n points on a line with their coordinates $x_i$. Find the point x so the sum of dis ...

随机推荐

  1. nRF24L01芯片驱动记录

    nRF24L01芯片驱动记录 ​ 学习完了usb,了解了部分元器件的功能以及用途后,打算在端午假期用一天的时间完成一个小目标,不过实际上是花了一天半才成功实现,现将驱动nRF24L01芯片的整个过程记 ...

  2. Dart 2.14 版现已发布

    支持 Apple Silicon,增加了默认的 lint.更好的工具和新的语言功能提高生产力. 本月,我们发布了 Dart SDK 2.14 的正式版,新的版本旨在通过独特的可移植性.生产力和稳健性组 ...

  3. [OI] 容斥原理拓展

    10.容斥原理拓展 10.1 二项式反演 \[P.10.1(1) \] 设 \(U=\{S_1,S_2,S_3...S_n\}\),且任意 \(i\) 个元素的交集都相等 定义 \(g(x)\) 为 ...

  4. Windows右下角时间显示具体星期

    事件起因: 有时候脑子不清楚,过着过着就会忘记今天是星期几,错过一些重要事情,于是乎就想看看Windows右下角能不能显示到具体星期,果然在查了资料之后这个需求可以达成 解决办法: 控制面板 - 日期 ...

  5. plt.gca()坐标轴移动

    我们可以看到绘制出来的图有四个边框,我们通过gca()对坐标轴进行一些简单处理,代码如下. import torch import torch.nn.functional as F import ma ...

  6. C# 中的数组使用

    · // 数组 /// 数组是一组相同类型的数据(ps:js中的数组可以不同类型) 访问通过索引访问数组元素 /// 数组的声明 要使用 new 使用 {} 来初始化数组元素 还需要指定数组的大小 / ...

  7. vant2 自动检查表单验证 -validate

    ref 给 <van-form @submit="onSubmit" ref="form"> 标签 : // 检验手机号是否合格 await thi ...

  8. Kubernetes的RBAC权限控制

    role和roleBinding Role资源定义了哪些操作可以在哪些资源上执行.也可以直接控制访问的url的权限,下面的cluster也是这样. 查询所有service的demo: apiVersi ...

  9. 异常处理、逻辑与(&)在条件结束判定的应用

    例子:求1+2+-+n的和,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C)(注 题目来自力扣) (1)boolean和逻辑与(&am ...

  10. Nuxt.js 应用中的 build:manifest 事件钩子详解

    title: Nuxt.js 应用中的 build:manifest 事件钩子详解 date: 2024/10/22 updated: 2024/10/22 author: cmdragon exce ...