C. Gas Pipeline
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment [0,n]

on OX axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval (x,x+1) with integer x. So we can represent the road as a binary string consisting of n

characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.

Usually, we can install the pipeline along the road on height of 1

unit with supporting pillars in each integer point (so, if we are responsible for [0,n] road, we must install n+1 pillars). But on crossroads we should lift the pipeline up to the height 2

, so the pipeline won't obstruct the way for cars.

We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment [x,x+1]

with integer x consisting of three parts: 0.5 units of horizontal pipe + 1 unit of vertical pipe + 0.5 of horizontal. Note that if pipeline is currently on height 2, the pillars that support it should also have length equal to 2

units.

Each unit of gas pipeline costs us a

bourles, and each unit of pillar — b bourles. So, it's not always optimal to make the whole pipeline on the height 2

. Find the shape of the pipeline with minimum possible cost and calculate that cost.

Note that you must start and finish the pipeline on height 1

and, also, it's guaranteed that the first and last characters of the input string are equal to 0.

Input

The fist line contains one integer T

(1≤T≤100) — the number of queries. Next 2⋅T

lines contain independent queries — one query per two lines.

The first line contains three integers n

, a, b (2≤n≤2⋅105, 1≤a≤108, 1≤b≤108

) — the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively.

The second line contains binary string s

(|s|=n, si∈{0,1}, s1=sn=0

) — the description of the road.

It's guaranteed that the total length of all strings s

doesn't exceed 2⋅105

.

Output

Print T

integers — one per query. For each query print the minimum possible cost of the constructed pipeline.

Example
Input

Copy
4
8 2 5
00110010
8 1 1
00110010
9 100000000 100000000
010101010
2 5 1
00
Output

Copy
94
25
2900000000
13
Note

The optimal pipeline for the first query is shown at the picture above.

The optimal pipeline for the second query is pictured below:

The optimal (and the only possible) pipeline for the third query is shown below:

The optimal pipeline for the fourth query is shown below:

题意:

给一个长度为n的01串,和建单位长度的管道的代价a和单位长度的柱子b的代价,有4种柱子,低管道代价为a+b,高管道代价为a+2*b,上升管道代价为2*a+b,下降管道代价为2*a+2*b,01串中1代表要建高管道,一段高管道前要建上升管道,后要建下降管道,如果一个地方既要上升管道有要下降管道,则那个地方必须为高管道,问建这个区域管道和柱子的最小代价

思路:

读入01串后给管道分类,低管道为0,高管道为1,上升管道为2,下降管道为3,如果这个管道既要上升又要下降,则必须建高管道,再提取出上升管道和下降管道(这里设i从0开始),注意到只能是中间的某些下降管道到下一个上升管道这段看要不要换成全部都是高管道来比较代价,注意题中规定第1个管道必定是低管道或上升管道,最后一个必定是下降管道或低管道,下面的每个格子代价计算的是格子左边柱子的代价和格子管道的代价,在第一次访问时i为偶数是上升管道,但前面没有下降管道,故要算建低管道的代价和上升管道的代价,在最后一次访问时i为奇数时下降管道,但后面没有上升管道了,故要算建下降管道和低管道的代价,如果在中间,由于管道有升就有降且必定先升再降,而且才0开始存上升下降位置,所以偶数位存上升管道,奇数位置存下降管道,如果现在这个管道是下降管道,have计算从下降管道到上升管道之间有多少个高度低管道注意这里要开long long,不然会溢出,在可以先下降在上升的管道区域要判断是否这样建还是不下降而是继续全建高管道,选一个代价小的建,用2*a-b>=have*b这个公式判断,它是这样推出来的,设1类为这一段建下降管道,低管道,上升管道,2类为这一段全建高管道,1类的代价为(4*a+3*b+have*(a+b)),2类代价为(have+2)*(a+2*b),可见1类和2类在have不同时代价不同,所以可以做差来比较它们的大小,即推出此公式如果现在这个是上升管道则只算高管道有多少,因为第一个上升管道代价第一次时已经算过了,而中间碰到下降管道时计算代价是下降管道+低管道+上升管道.还要判断如果没有上升和下降的管道则全建低管道,输出答案时最后一个管道的右边柱子的代价要加上

 #include<bits/stdc++.h>
using namespace std;
const int amn=2e5+;
long long m[amn],jg[amn];
int main(){
long long T,n,a,b,st,ed,tp=;long long ans;char in;
ios::sync_with_stdio();
cin>>T;
while(T--){
tp=;
cin>>n>>a>>b;
st=-;ans=ed=;
for(int i=;i<n;i++){cin>>in;if(in=='')m[i]=;else m[i]=;}m[n]=;
for(int i=;i<n;i++){
if(m[i]==){
if(st==-)st=i-;
if(i>){
if(m[i-]==)m[i-]=;
else m[i-]=; ///如果这个管道既要上升又要下降,则必须建高管道
}
if(i+<n){
if(m[i+]==){m[i+]=;ed=i+;}
else m[i+]=; ///如果这个管道既要上升又要下降,则必须建高管道(其实这里不用判断,因为前面还没判断过,不可能有2,只可能时1或0
}
}
}
for(int i=;i<n;i++)
if(m[i]==||m[i]==)jg[tp++]=i;
for(int i=;i<tp;i++){ ///注意位置i从0开始哦! 注意题中规定第1个管道必定是低管道或上升管道,最后一个必定是下降管道或低管道,下面的每个格子代价计算的是格子左边柱子的代价和格子管道的代价,低管道a+b,高管道a+2*b,上升管道2*a+b,下降管道2*a+2*b
if(i==) ///在第一次访问时i为偶数是上升管道,但前面没有下降管道,故要算建低管道的代价和上升管道的代价
ans+=jg[i]*(a+b)+*a+b;
else if(i==tp-) ///在最后一次访问时i为奇数时下降管道,但后面没有上升管道了,故要算建下降管道和低管道的代价
ans+=(n--jg[i])*(a+b)+*a+*b;
if(i!=tp-){ ///如果在中间
if(i&){ ///由于管道有升就有降且必定先升再降,而且才0开始存上升下降位置,所以偶数位存上升管道,奇数位置存下降管道
long long have=jg[i+]-jg[i]-; ///have计算从下降管道到上升管道之间有多少个高度低管道注意这里要开long long,不然会溢出
if(*a-b>=have*b) ///在可以先下降在上升的管道区域要判断是否这样建还是不下降而是继续全建高管道,选一个代价小的建,用2*a-b>=have*b这个公式判断,它是这样推出来的,设1类为这一段建下降管道,低管道,上升管道,2类为这一段全建高管道,1类的代价为(4*a+3*b+have*(a+b)),2类代价为(have+2)*(a+2*b),可见1类和2类在have不同时代价不同,所以可以做差来比较它们的大小,即推出此公式
ans+=(have+)*(a+*b);
else
ans+=(*a+*b+have*(a+b));
}
else ///如果现在这个是上升管道则只算高管道有多少,因为第一个上升管道代价第一次时已经算过了,而中间碰到下降管道时计算代价是下降管道+低管道+上升管道
ans+=(jg[i+]-jg[i]-)*(a+*b);
}
}
if(tp==) ///如果没有上升和下降的管道则全建低管道
ans+=n*(a+b);
printf("%lld\n",ans+b); ///最后一个管道的右边柱子的代价要加上
}
}
/**
给一个长度为n的01串,和建单位长度的管道的代价a和单位长度的柱子b的代价,有4种柱子,低管道代价为a+b,高管道代价为a+2*b,上升管道代价为2*a+b,下降管道代价为2*a+2*b,01串中1代表要建高管道,一段高管道前要建上升管道,后要建下降管道,如果一个地方既要上升管道有要下降管道,则那个地方必须为高管道,问建这个区域管道和柱子的最小代价
读入01串后给管道分类,低管道为0,高管道为1,上升管道为2,下降管道为3,如果这个管道既要上升又要下降,则必须建高管道,再提取出上升管道和下降管道(这里设i从0开始),注意到只能是中间的某些下降管道到下一个上升管道这段看要不要换成全部都是高管道来比较代价,
注意题中规定第1个管道必定是低管道或上升管道,最后一个必定是下降管道或低管道,下面的每个格子代价计算的是格子左边柱子的代价和格子管道的代价,
在第一次访问时i为偶数是上升管道,但前面没有下降管道,故要算建低管道的代价和上升管道的代价,在最后一次访问时i为奇数时下降管道,但后面没有上升管道了,故要算建下降管道和低管道的代价,
如果在中间,由于管道有升就有降且必定先升再降,而且才0开始存上升下降位置,所以偶数位存上升管道,奇数位置存下降管道,
如果现在这个管道是下降管道,have计算从下降管道到上升管道之间有多少个高度低管道注意这里要开long long,不然会溢出,在可以先下降在上升的管道区域要判断是否这样建还是不下降而是继续全建高管道,选一个代价小的建,用2*a-b>=have*b这个公式判断,它是这样推出来的,设1类为这一段建下降管道,低管道,上升管道,2类为这一段全建高管道,1类的代价为(4*a+3*b+have*(a+b)),2类代价为(have+2)*(a+2*b),可见1类和2类在have不同时代价不同,所以可以做差来比较它们的大小,即推出此公式
如果现在这个是上升管道则只算高管道有多少,因为第一个上升管道代价第一次时已经算过了,而中间碰到下降管道时计算代价是下降管道+低管道+上升管道.
还要判断如果没有上升和下降的管道则全建低管道,
输出答案时最后一个管道的右边柱子的代价要加上
**/

[贪心,dp] Educational Codeforces Round 71 (Rated for Div. 2) C. Gas Pipeline (1207C)的更多相关文章

  1. Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块

    Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块 [Problem Description] ​ ...

  2. Educational Codeforces Round 71 (Rated for Div. 2)-E. XOR Guessing-交互题

    Educational Codeforces Round 71 (Rated for Div. 2)-E. XOR Guessing-交互题 [Problem Description] ​ 总共两次询 ...

  3. Educational Codeforces Round 71 (Rated for Div. 2) Solution

    A. There Are Two Types Of Burgers 题意: 给一些面包,鸡肉,牛肉,你可以做成鸡肉汉堡或者牛肉汉堡并卖掉 一个鸡肉汉堡需要两个面包和一个鸡肉,牛肉汉堡需要两个面包和一个 ...

  4. Educational Codeforces Round 71 (Rated for Div. 2)

    传送门 A.There Are Two Types Of Burgers 签到. B.Square Filling 签到 C.Gas Pipeline 每个位置只有"高.低"两种状 ...

  5. Educational Codeforces Round 71 (Rated for Div. 2)E. XOR Guessing

    一道容斥题 如果直接做就是找到所有出现过递减的不同排列,当时硬钢到自闭,然后在凯妹毁人不倦的教导下想到可以容斥做,就是:所有的排列设为a,只考虑第一个非递减设为b,第二个非递减设为c+两个都非递减的情 ...

  6. Educational Codeforces Round 71 (Rated for Div. 2) E XOR Guessing (二进制分组,交互)

    E. XOR Guessing time limit per test1 second memory limit per test256 megabytes inputstandard input o ...

  7. [暴力] Educational Codeforces Round 71 (Rated for Div. 2) B. Square Filling (1207B)

    题目:http://codeforces.com/contest/1207/problem/B   B. Square Filling time limit per test 1 second mem ...

  8. Remainder Problem(分块) Educational Codeforces Round 71 (Rated for Div. 2)

    引用:https://blog.csdn.net/qq_41879343/article/details/100565031 下面代码写错了,注意要上面这种.查:2  800  0,下面代码就错了. ...

  9. XOR Guessing(交互题+思维)Educational Codeforces Round 71 (Rated for Div. 2)

    题意:https://codeforc.es/contest/1207/problem/E 答案guessing(0~2^14-1) 有两次机会,内次必须输出不同的100个数,每次系统会随机挑一个你给 ...

随机推荐

  1. The difference between applicationContext.xml in Spring and xxx-servlet.xml in SpringMVC

    一直搞不明白两者的区别.如果使用了SpringMVC,事实上,bean的配置完全可以在xxx-servlet.xml中进行配置.为什么需要applicationContext.xml?一定必须? 因为 ...

  2. 用新架构适配MI3中遇到的各种坑

    用新架构适配MI3中遇到的各种坑 首先不得不说hendy架构的强大之处, mi3也直接开机但是遇到各种坑,不能怪架构不够强大,只有说miui定制化太高.下面详细说一下mi3适配中的各种坑.有些坑会附带 ...

  3. string类应用举例

    * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhello ...

  4. 自定义Toast的出现样式

    使用下面的方法来获取一个Toast对象: private Toast showShortToast() { if (toast == null) { toast = new Toast(this); ...

  5. linux svn 安装(支持http访问)

    1.安装svn yum install -y subversion 2.查看svn版本 svn --version 3.创建仓库 mkdir -p /opt/java/repos cd /opt/ja ...

  6. VMware安装CentOS6.X 系统

    1.虚拟机中的"CD/DVD(IDE)"配置好Linux映像文件后,打开虚拟机,点击"开启此虚拟机" 2.进入光盘启动界面,选择第一项,表示安装升级Linux系 ...

  7. 挖SRC逻辑漏洞心得分享

    文章来源i春秋 白帽子挖洞的道路还漫长的很,老司机岂非一日一年能炼成的. 本文多处引用了 YSRC 的 公(qi)开(yin)漏(ji)洞(qiao).挖SRC思路一定要广!!!!漏洞不会仅限于SQL ...

  8. JSR310-新日期APIJSR310新日期API(完结篇)-生产实战

    前提 前面通过五篇文章基本介绍完JSR-310常用的日期时间API以及一些工具类,这篇博文主要说说笔者在生产实战中使用JSR-310日期时间API的一些经验. 系列文章: JSR310新日期API(一 ...

  9. NSURLSession的前世今生

    系统网络框架架构图 前世-NSURLConnection NSURLConnection是苹果提供的原生网络访问类,已经有10多年的历史了,它从 iOS 2.0 开始,一直到iOS9被废弃.异步方法在 ...

  10. tomcat服务器的应用总结

    tomcat的安装和部署: >> Web的基本入门: |-- C/S架构:客户端和服务器: |-- B/S架构:浏览器和服务器: >> 服务器当中可以放入的资源: |-- 静态 ...