Codeforces 997A Convert to Ones(思维)

https://codeforces.com/problemset/problem/997/A

 

 

 

 

 

 

 

 

 

 

Subject to the effect:

Given a string sequences 0-1, defines two operations:

An operation: Select a continuous string inverted.

Operation II: Select a continuous string 01 to be interchanged (inverted).

A given operation and the cost of two operations, namely x and y.

Finally, the operation to take the string into a string containing only 1, Q minimal operating costs.

 

Assuming that the number of segments of consecutive 0s num, then you will get, once for each operation one, can reduce the number of two primary operations.

Therefore, we must consider the use of operational priority one and two:

  If x <y on priority inversion, all sections 0 0 a synthesis section, and then negated, the cost (num-1) * x + y

  If x> y 0 directly to each well section negated, the cost num * y

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <math.h>
13 const int INF=0x3f3f3f3f;
14 typedef long long LL;
15 const int mod=1e9+7;
16 #define Bug cout<<"---------------------"<<endl
17 const int maxn=3e5+10;
18 using namespace std;
19 
20 char str[maxn];
21 
22 int main()
23 {
24     int n,x,y;
25     scanf("%d %d %d",&n,&x,&y);
26     int op = x<=y? 0:1;
27     scanf("%s",str);
28     int pre = 1;//上一个字符 
29     int num = 0;//连续0段数 
30     for(int i = 0;i < n;i++)
31     {
32         if(str[i] == '0' && pre == 1)
33             num++;
34         pre = str[i]-'0';
35     }
36     LL ans = 0;
37     if(op==0&&num!=0)//优先操作一 
38         ans=(LL)(num-1)*x+y;//注意结果要类型转换为long long
39     else
40         ans=(LL)num*y;//注意结果要类型转换为long long 
41     printf("%lld\n",ans);
42     return 0;
43 }

 

Guess you like

Origin www.cnblogs.com/jiamian/p/11620343.html