• Codeforces Round #821 (Div. 2)


    D2. Zero-One (Hard Version)

    time limit per test

    3 seconds

    memory limit per test

    512 megabytes

    input

    standard input

    output

    standard output

    This is the hard version of this problem. In this version, n≤5000 holds, and this version has no restriction between x and y. You can make hacks only if both versions of the problem are solved.

    You are given two binary strings aa and bb, both of length nn. You can do the following operation any number of times (possibly zero).

    • Select two indices ll and rr (l
    • Change alal to (1−al) and arar to (1−ar).
    • If l+1=r, the cost of the operation is x. Otherwise, the cost is y.

    You have to find the minimum cost needed to make aa equal to bb or say there is no way to do so.

    Input

    The first line contains one integer tt (1≤t≤1000) — the number of test cases.

    Each test case consists of three lines. The first line of each test case contains three integers nn, xx, and yy (5≤n≤5000, 1≤x,y≤10^9) — the length of the strings, and the costs per operation.

    The second line of each test case contains the string aa of length nn. The string only consists of digits 0 and 1.

    The third line of each test case contains the string bb of length nn. The string only consists of digits 0 and 1.

    It is guaranteed that the sum of nn over all test cases doesn't exceed 5000.

    Output

    For each test case, if there is no way to make a equal to b, print −1. Otherwise, print the minimum cost needed to make a equal to b.

    Example

    input

    Copy

     
     

    6

    5 8 9

    01001

    00101

    6 2 11

    000001

    100000

    5 7 2

    01000

    11011

    7 8 3

    0111001

    0100001

    6 3 4

    010001

    101000

    5 10 1

    01100

    01100

    output

    Copy

    8
    10
    -1
    6
    7
    0
    

    Note

    In the first test case, selecting indices 2 and 3 costs 8, which is the minimum.

    In the second test case, we can perform the following operations.

    • Select indices 1 and 2. It costs 2, and aa is 110001 now.
    • Select indices 2 and 3. It costs 2, and aa is 101001 now.
    • Select indices 3 and 4. It costs 2, and aa is 100101 now.
    • Select indices 4 and 5. It costs 2, and aa is 100011 now.
    • Select indices 5 and 6. It costs 2, and aa is 100000 now.

    The total cost is 10

    In the third test case, we cannot make aa equal to bb using any number of operations.

    In the fourth test case, we can perform the following operations.

    • Select indices 3 and 6. It costs 3, and aa is 0101011 now.
    • Select indices 4 and 6. It costs 3, and aa is 0100001 now.

    The total cost is 6.

    In the fifth test case, we can perform the following operations.

    • Select indices 1 and 6. It costs 4, and aa is 110000 now.
    • Select indices 2 and 3. It costs 3, and aa is 101000 now.

    The total cost is 7.

    In the sixth test case, we don't have to perform any operation.

     dp:要么和我前一个没匹配的通过若干个x或一次y消
    要么是前i-1个中剩下的直接用y消

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. #define int long long
    4. #define PII pair<int,int>
    5. typedef double db;
    6. const int N=5010;
    7. string s,t;
    8. int dp[N];
    9. int a[N];
    10. int n,x,y,cnt,ans;
    11. void solve()
    12. {
    13. cin>>n>>x>>y>>s>>t;
    14. s=" "+s;
    15. t=" "+t;
    16. ans=cnt=0;
    17. int fg=0,st=0;
    18. for(int i=1; i<=n; i++)
    19. {
    20. if(s[i]!=t[i])
    21. {
    22. cnt++;
    23. st=i;
    24. a[cnt]=i;
    25. }
    26. }
    27. if(cnt%2)
    28. {
    29. cout<<-1<<"\n";
    30. return;
    31. }
    32. if(cnt==0)
    33. {
    34. cout<<0<<"\n";
    35. return;
    36. }
    37. if(x>=y) //D1
    38. {
    39. if(cnt==2&&s[st-1]!=t[st-1])cout<<min(x,y*2)<<"\n";
    40. else cout<<cnt*y/2<<"\n";
    41. return;
    42. }
    43. for(int i=2; i<=cnt; i++)
    44. {
    45. if(i%2)
    46. {
    47. dp[i]=min(dp[i-2]+min((a[i]-a[i-1])*x,y),dp[i-1]);
    48. }
    49. else
    50. {
    51. dp[i]=min(dp[i-2]+min((a[i]-a[i-1])*x,y),dp[i-1]+y);
    52. }
    53. }
    54. cout<<dp[cnt]<<"\n";
    55. }
    56. signed main()
    57. {
    58. ios::sync_with_stdio(false);
    59. cin.tie(0);
    60. cout.tie(0);
    61. int T;
    62. cin>>T;
    63. while(T--)
    64. {
    65. solve();
    66. }
    67. return 0;
    68. }

  • 相关阅读:
    编写一款2D CAD/CAM软件(十五)封装交互操作类
    永恒之蓝漏洞原理及复现
    MU editor IDE编辑器 二次开发记录与踩坑
    Base64编码知识详解
    小米屡次违反GPL协议,疑成“惯犯”
    031-JAVA窗体图形图像处理(Toolkit工具类及Font类及事件处理综合应用)
    海量物理刚体 高性能物理引擎Unity Physics和Havok Physics的简单性能对比
    网络授时服务器(NTP授时系统)售后与安装步骤
    【vue】vue2.x解决兼容IE8以上问题:
    http模块中----------res响应对象 与服务器相关的数据和属性
  • 原文地址:https://blog.csdn.net/m0_61949623/article/details/126949253