1107
来源:互联网 发布:自由贸易的利弊 知乎 编辑:程序博客网 时间:2024/06/08 20:04
DESCRIPTION
Once upon a time a little girl named Niro found an ancient book in a forest. Written on it was an ancient story named Nyu Wa Repairs the Sky. This story only consisted of characters ‘0’s and ‘1’s. Like the sky, this book was broken and some of the characters was replaced by ‘?’s. Niro wanted to repair it by replacing the ‘?’s with ‘0’s and ‘1’s. However, Using a ‘0’ cost C0
yuan and using a ‘1’ cost C1 yuan. Additionally, there shouldn’t be N0 consecutive ‘0’s or N1
consecutive ‘1’s. What was the minimum cost to repair this book?
INPUT
The first line contains a positive integer T
, the number of test cases. Each test case contains two lines. In each test case, the first line contains five positive integers N, C0, C1, N0, N1.The second line contains N
characters. Each character can only be ‘0’, ‘1’ or ‘?’.
It is guaranteed that there is at least one valid way to repair the book.
OUTPUT
T
line(s). Each line contains one positive integer, the minimum cost to repair the book for the corresponding test case.
SAMPLE INPUT
2
10 2 3 4 5
1000?0001?
9 1 100 3 3
0?0?0?0?0
SAMPLE OUTPUT
5
400
对于‘?’扫描它的左右,尽量使用便宜的。
#include <iostream>#include <cstring>#include <cstdio>using namespace std;char s[100005];int n,c0,c1,n0,n1;struct node { char num; int sum;}; struct tg{ int co; int li;}; struct tg ex[2];int main(){ int t;scanf("%d",&t); while(t--){ scanf("%d%d%d%d%d",&n,&ex[0].co,&ex[1].co,&ex[0].li,&ex[1].li); scanf("%s",s); int mini = ex[0].co < ex[1].co? 0:1; int maxi = ex[0].co < ex[1].co? 1:0; long long cost = 0; struct node l,r; l.num = '2', l.sum = 0; r.num = '2', r.sum = 0; for(int i=0;i<n;i++){ if(s[i] == '?'){ int flag = 1, j; struct node x; x.num = '2', x.sum = 0; for(j=i+1;j<n;j++){ if(s[j]=='?') { j--;break; } if(s[j] != x.num){ if(!flag){ j--;break; } else{ x.num = s[j]; x.sum=1; flag = 0; } } else{ x.sum++; } } if(i==0){ if(x.sum+1 < ex[mini].li && x.num == mini+'0'){ cost += ex[mini].co; x.sum++; } else if(x.num != mini + '0'){ cost += ex[mini].co; if(x.num=='2') x.num = mini+'0',x.sum = 1; } else if(x.num == maxi + '0'){ cost += ex[maxi].co;x.sum++; } else { cost += ex[maxi].co; if(x.num=='2') x.num = maxi+'0',x.sum =1; } } else if(i==n-1){ if(l.num != mini+'0' || l.sum+1 < ex[mini].li){ cost += ex[mini].co; } else{ cost += ex[maxi].co; } } else{ int test = true; if(l.num == mini+'0' && l.sum +1 >= ex[mini].li || x.num == mini+'0' && x.sum+1 >= ex[mini].li){ test = false; } if(l.num == x.num && l.num == mini + '0' && l.sum + x.sum + 1 >= ex[mini].li){ test = false; } //cout<<test<<endl; if(test){ if(x.num == '2') x.num =mini + '0' , x.sum = 0; cost += ex[mini].co; if(x.num == mini + '0'){ x.sum+=1; if(l.num == mini + '0') x.sum+=l.sum; } } else{ if(x.num == '2') x.num=maxi+'0', x.sum = 0; cost += ex[maxi].co; if(x.num == '2') x.num = maxi+'0'; if(x.num == maxi +'0'){ x.sum+=1; if(l.num == maxi+'0') x.sum+=l.sum; } } } //cout<<l.num <<" "<<l.sum<<endl; //cout<<x.num<<" "<<x.sum<<endl<<endl; l = x; i = j; } else{ if(s[i] != l.num) { l.num = s[i], l.sum = 1; } else l.sum++; } } printf("%lld\n",cost); } return 0;}
- 1107
- 1107
- 1107
- 1107
- 1107
- 1107
- 1107
- 1107
- problem 1107
- 1107周五
- 煩煩煩。。。1107
- zju 1107
- HDU 1107
- ts 1107
- 1107 -- 闰年
- Lightoj 1107
- PAT 1107
- XDOJ 1107
- iOS中runtime运行机制解析
- hibernate之集合映射的属性详细解释
- 【ES6】set和map
- Android修改状态栏的背景颜色
- [联表] -- YII2 GridView联表:文本框搜索、下拉框选择搜索和排序
- 1107
- KVO改变textfield的placeholderLabel的属性
- LINUX下部署NFS
- Vim
- Prime Ring Problem UVA
- 2017-3月 java面试题总览
- 安卓开发:角标显示未读消息数
- CSS相对于屏幕上下居中定位写法
- ajxa异步传输文件