B. 对数组的最小操作次数
Code:
#include<bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int dp[N][8], n, k, a[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= 7; j++) dp[i][j] = INT_MAX; } for (int i = 1; i <= 7; i++) { dp[1][i] = (a[1] != i) ? 1 : 0; } for (int i = 2; i <= n; i++) { for (int j = 1; j <= 7; j++) { for (int p = max(1, j - k); p <= min(7, j + k); p++) { dp[i][j] = min(dp[i][j], dp[i - 1][p] + (a[i] != j)); } } } int ans = INT_MAX; for (int i = 1; i <= 7; i++) { ans = min(ans, dp[n][i]); } cout << ans; return 0; }
标签:int,多校,cin,训练赛,暑假,tie From: https://www.cnblogs.com/youhualiuh/p/18312379