乐逍遥xwl

导航

Educational Codeforces Round 71 (Rated for Div. 2) C - Gas Pipeline

原文链接:https://www.cnblogs.com/xwl3109377858/p/11404321.html

Educational Codeforces Round 71 (Rated for Div. 2)

C - Gas Pipeline

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

4

8 2 5

00110010

8 1 1

00110010

9 100000000 100000000

010101010

2 5 1

00

output

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:

 

 


题意:题目意思是给你一串二进制数字,让你根据这串数字铺管道,开头结尾桩的高度都为1,

中间如果 i 位置串的数字为1,那么 i 和 i+1 位置的桩应该高度要到2,否则高度1和2都行,

转折处需要两个0.5单位的水平管道和1单位竖直管道连接。每单位管道和桩的价格已知,

问你建起管道的最小花费。

思路:我们可以根据原数字串算出基础花费,一共n个单位的管道,还有每个转折处多一个单位管道,

再计算总的桩的单位,总价格为基础花费。然后我们可以贪心的思想,把中间高度1的连续低木桩建成2高度,

使转折费用减掉,增加木桩费用,看费用能否减少,最后计算出最小花费。

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<map>
 7 #include<set>
 8 #include<vector>
 9 #include<queue>
10 #include<stack>
11 #include<list>
12 //#include<unordered_map>
13 using namespace std;
14 #define ll long long
15 const int mod=998244353;
16 const long long int inf=1e18+7;
17  
18 const int maxn=2e5+10;
19  
20 int h[maxn];
21  
22 int main()
23 {
24     ios::sync_with_stdio(false);
25     int T;
26     cin>>T;
27     int n,a,b;
28     string str;
29     while(T--)
30     {
31         cin>>n>>a>>b; 
32         memset(h,0,sizeof(h));
33         cin>>str;
34         int st=99999999,ed=-1;//贪心开始的起点终点 
35         h[0]=1;//标记 
36         for(int i=1;i<=n-1;i++)
37         {
38             if(str[i]=='1')
39             {
40                 st=min(st,i);//由第一个1开始 
41                 ed=max(ed,i+1);
42                 h[i]=2;//标记 
43                 h[i+1]=2;
44             }
45             else if(h[i]==0)//未被标记过 
46             {
47                 h[i]=1;//标记 
48             }
49         }
50         h[n]=1;//标记 
51     
52         ll int sum=0;//先计算基础费用 
53         ll int cnt=0;//计算转折数 
54         for(int i=0;i<=n;i++)
55         {
56             sum+=h[i];
57             if(i>0&&h[i]!=h[i-1])//有转折 
58                 cnt++;
59         }
60         
61         sum=sum*b;//木桩总数 
62         sum+=(n+cnt)*a;//管道总数 
63         ll int minn=sum;//记录 
64         
65         cnt=0;//记录可以贪心的低高度连续木桩数 
66         for(int i=st;i<=ed;i++)
67         {
68             if(h[i]==1)
69                 cnt++;
70             else
71             {
72                 if(cnt==0)
73                     continue; 
74                 
75                 ll int temp1=2*a;//转折费用 
76                 ll int temp2=cnt*b;//木桩费用 
77                 
78                 if(temp2<temp1)//建木桩费用更少 
79                     minn-=(temp1-temp2);//减掉 
80                 cnt=0;
81             }
82         }
83             
84         cout<<minn<<endl;    
85     }
86     return 0;
87 }

 

posted on 2019-08-24 13:06  乐逍遥xwl  阅读(328)  评论(0编辑  收藏  举报