码迷,mamicode.com
首页 > 其他好文 > 详细

【盗版动归】Codeforces998C——Convert to Ones 归一操作

时间:2020-04-11 20:24:04      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:row   etc   cond   stream   标题   int   ini   模拟   ORC   

嘤嘤嘤,因为最近文化课老师追的紧了+班主任开班会,所以这博客是赶制的赝品

题目:

You‘ve got a string a1,a2,,ana1,a2,…,an, consisting of zeros and ones.

Let‘s call a sequence of consecutive elements ai,ai+1,,ajai,ai + 1,…, aj (1ijn1≤ i≤ j≤ n) a substring of string aa.

You can apply the following operations any number of times:

  • Choose some substring of string aa (for example, you can choose entire string) and reverse it, paying xx coins for it (for example, «0101101» → «0111001»);
  • Choose some substring of string aa (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying yy coins for it (for example, «0101101» → «0110001»).

You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring.

What is the minimum number of coins you need to spend to get a string consisting only of ones?

Input

The first line of input contains integers nn, xx and yy (1n300000,0x,y1091 ≤ n ≤ 300000,0≤x,y≤109) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring).

The second line contains the string aa of length nn, consisting of zeros and ones.

Output

Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print 00, if you do not need to perform any operations.

Examples

Input
5 1 10
01000
Output
11
简译:

给定一串0-1序列,定义两种操作:

  • 操作一:选取一连续子串倒置。
  • 操作二:选取一连续子串把进行01互换(取反)。
  • 并给出操作一和操作二的代价,分别为x和y。
    操作到最后要把串变成只含1的串,问最小的操作代价。

 


 

思路:

一看到这道题,脑子里想的是区间DP建立二维数组f,f[i][j]表示把i~j都转化为1的最小代价,后来因为转移方程过于复杂,且时间开销大了,所以直接放弃,然后搜了一下题解,一看标题说是假动归,我立即转向普通暴力。

读题可以了解到,1100和10是在翻转和反转下是等价的,连续的0或1可以直接转化成一个0或一个1,如11100110可以直接变为1010,这样他们的代价是一样的。然后蒟蒻提出了第一个思路:统计压缩后的0和1的数目,然后根据0和1的数目进行选择,代码如下:

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
const int maxn=300000+5,INF=0x3f3f3f3f;
int n,m,c1,a[maxn],c2,low[maxn],f[maxn],len1,len0,len;
inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<0||ch>9){if(ch==-)w=-1;ch=getchar();}
   while(ch>=0&&ch<=9) s=s*10+ch-0,ch=getchar();
   return s*w;
}
int main(){
    //freopen("a.in","r",stdin);
    n=read(),c1=read(),c2=read();
    for(int i=1;i<=n;i++){
        char x;
        cin>>x;
        x-=0;
        if(i==1||x!=a[len]){
            a[++len]=x;
            if(a[len]==1)len1++;
            else if(a[len]==0)len0++;
        }
    }
    if(len0==0){
        cout<<"0";
        return 0;
    }
    if(len1==0){
        cout<<c2;return 0;
    }
    int ans=0x3f;
    if(len1<len0)cout<<min((len0-1)*c1+c2,c2*len0);
    else if(len1>len0)cout<<min(len0*c1+c2,c2*len0);
    else cout<<min(len0*c1+c2,c2*len0);
}

 

提交了发现有丶问题,这三个判断十分相似,貌似是写错了,其实可以合并,然后就改成了下面的极简代码:

 

 1 int main(){
 2     freopen("a.in","r",stdin);
 3     scanf("%d%d%d %s",&n,&c1,&c2,a+1);
 4     a[0]=1;//你问我为什么这里变成了scanf?因为提交多次发现卡死在了test4,我以为是快读beng了(遇到过卡快读的题)
 5     for(int i=1;i<=n;i++){
 6         if(a[i-1]==1&&a[i]==0)len0++;
 7     }
 8     if(len0==0){
 9         printf("0");
10     }else printf("%lld",(long long)min(c1*(len0-1)+c2,c2*len0);//卡在test4的原因:不开long long
11     return 0;
12 }

 

 


 

解释:

可以通过模拟找规律得到最后的式子

对于这个0和1分隔开的串,就结果来看,我们可以把所有的0放到一边,最后把连续的0变成1,代价为c1*(len0-1)+c2,也可以把分隔的0全部转化为1,代价为c2*len0。

然后我看了眼其他人的想法,比我的要详细一些:第一种方法,我们可以选择第二段 10 ,对其进行倒置操作,所以整个串就变成了 001101010。然后再次合并相邻的1 和相邻的0,原串变成了0101010。然后在进行一次相同的操作,就变成了 01010.以此类推,最后将变成 0 10,再倒置一次,变成 00 1,然后再对00 进行一次操作二即可。这种方法的花费为 (len0-1)*c1+c2;第二种方法,我们直接对每一段0实施操作二,使得其变为全1串,这样的花费是len0*c2(原地址https://blog.csdn.net/qq_44549690/article/details/102498866?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3(怎么这么长))

嘤嘤嘤,没了。晚上完善一下线型DP模板的题解和打地鼠的题解

【盗版动归】Codeforces998C——Convert to Ones 归一操作

标签:row   etc   cond   stream   标题   int   ini   模拟   ORC   

原文地址:https://www.cnblogs.com/614685877--aakennes/p/12681643.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!