首页 > 其他分享 >Codeforces Round #821 D2

Codeforces Round #821 D2

时间:2022-09-20 17:55:38浏览次数:93  
标签:get int D2 min Codeforces pos 821 dp define

D2. Zero-One (Hard Version)

我们由D1可知当我们的y小于x/2时 我们可以用2y来减小相邻的cost
那我们考虑要是y比较大的时候 我们也可以用多个x来减小cost
我们可以轻松的写出get函数

 int get(int l,int r) {
     if (l + 1 == r)return min(2 * y, x);
     else return min(x * (r - l), y);
 }

最后我们考虑dp[l][r] 表示为区间[l,r]需要的min_cost
状态转移:
我们每次区间长度+2
我们可以对她前面两个做一次x操作 或者对他后面两个做一次x操作(这样就可以保证dp的完备性
或者是对他两个端点做一次y操作(因为只有这样才是最优的情况
注意这里可能两个端点在a数组里都是0 但是没关系 我们主要的只是要其“路径”的长度(类似于这道题[https://codeforces.com/problemset/problem/1680/E])

 dp[i][j] = 
 min({dp[i + 1][j - 1] + get(pos[i], pos[j]), 
 dp[i + 2][j] + get(pos[i], pos[i + 1]),
 dp[i][j - 2] + get(pos[j - 1], pos[j])});

最后我们加上第一题的特判即可

#include <bits/stdc++.h>
using namespace std;
const int N = 6e5+10;
const int M = 998244353;
const int mod = 998244353;
#define int long long
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define YES cout<<"Yes"<<endl;
#define NO cout<<"No"<<endl;
#define _ 0
#define pi acos(-1)
#define INF 0x3f3f3f3f3f3f3f3f
#define fast ios::sync_with_stdio(false);cin.tie(nullptr);
int n,x,y,dp[5050][5050];
int get(int l,int r) {
    if (l + 1 == r)return min(2 * y, x);
    else return min(x * (r - l), y);
}
void solve() {
    cin >> n >> x >> y;
    vector<char> a(n + 1), b(n + 1);
    vector<int> pos;
    for (int i = 1; i <= n; i++)cin >> a[i];
    for (int i = 1; i <= n; i++)cin >> b[i];
    for (int i = 1; i <= n; i++) {
        if (a[i] != b[i])pos.push_back(i);
    }
    if (pos.size() % 2 == 1) {
        cout << "-1" << endl;
        return;
    }
    if (pos.size() == 0) {
        cout << "0" << endl;
        return;
    }
    if (pos.size() == 2) {
        if (pos[0] + 1 == pos[1])cout << min(2 * y, x) << endl;
        else cout << min(y, x * (pos[1] - pos[0])) << endl;
        return;
    }
    if (y <= x) {
        cout << pos.size() / 2 * y << endl;
        return;
    }
    for (int len = 2; len <= pos.size(); len += 2) {
        for (int i = 0, j = len + i - 1; j < pos.size(); j++, i++) {
            if (len == 2)dp[i][j] = get(pos[i], pos[j]);
            else dp[i][j] = min({dp[i + 1][j - 1] + get(pos[i], pos[j]), dp[i + 2][j] + get(pos[i], pos[i + 1]),
                                 dp[i][j - 2] + get(pos[j - 1], pos[j])});
        }
    }
    cout << dp[0][pos.size() - 1] << endl;
}
signed main(){
    fast
    int T;cin>>T;
    while(T--) {
        solve();
    }
    return ~~(0^_^0);
}

标签:get,int,D2,min,Codeforces,pos,821,dp,define
From: https://www.cnblogs.com/ycllz/p/16711954.html

相关文章

  • Codeforces Round #821 (Div. 2)
    CodeforcesRound#821(Div.2)C.ParityShuffleSorting题目大意每次操作可以选择l,r,如果\(a_l+a_r\)是奇数可以让\(a_l=a_r\),否则可以让\(a_l=a_r\),要求使用不超过n......
  • Codeforces #821 Div2(A~D2)题解
    CF#821Div2AConsecutiveSum题目:​ 选择\(i\)和\(j\),如果\(j=i+xk(x=R)\),可以交换\(i,j\)。任意选择一段长度为k的相加。思路:​ 题目等价于在下标\(mod\)k相同......
  • Codeforces Round #813 (Div. 2)
    CodeforcesRound#813(Div.2)D.EmptyGraph分析我们通过简单的分析,可以得出一个结论,我们的答案一定来自于相邻两个点的位置或是最小值的两倍。我们考虑如何给构造......
  • Codeforces Round #640 (Div. 4) C. K-th Not Divisible by n
    CodeforcesRound#640(Div.4)翻译岛田小雅C.K-thNotDivisiblebyn出题人MikeMirzayanov有两个正整数\(n\)和\(k\),输出第\(k\)个不能被\(n\)整除的正整......
  • Codeforces Round #819 D
    D.EdgeSplitn+2条边并且连通图就证明最多多3条边我们可以想到的是要是连成了环的话不如拆一条边给其他颜色所以我们一定要无环我们先跑一遍最小生成树确定成红色......
  • Codeforces Round #316 (Div. 2) D Tree Requests
    TreeRequests判断\(V_i\)的子树中,深度为\(h_i\)的结点上所带有的字符,能否组成一个回文串启发式合并维护所有深度上不同字符的数量,并且维护其奇数字符出现的次数如......
  • Codeforces Round #221 (Div. 1) D Tree and Queries
    TreeandQueries询问\(V_j\)的子树中,有多少种颜色出现了\(K_j\)次启发式合并最直接的,树上启发式合并的同时维护颜色出现的次数,然后再拿一个数组记录一下出现了\(i......
  • Codeforces Round #151 (Div. 2) E Blood Cousins Return
    BloodCousinsReturn启发式合并在跑启发式合并的同时,对每个深度都维护一个\(set\),就可以自动去重并计算有多少种不同的字符串#include<iostream>#include<cstdio>......
  • Codeforces Round #816 (Div. 2) A-D
    CodeforcesRound#816(Div.2)A-D传送门A题意:长为n,m的一个棋盘,左上的旗子要去右下,左下的棋子要去右上,每次移动花费为1,但当一个棋子在另一个棋子的轨迹上时,可以花费......
  • Codeforces Round #819 (Div. 1 + Div. 2) A-D
    CodeforcesRound#819(Div.1+Div.2)A-D传送门过程本场A小磕一下,浪费了一点时间,随后B的迷惑题面浪费大量时间,心态发生了变化,不过最后还是在强猜后蒙过了,C又浪费了......