(一)思路:
由题知:
h是用来交换的1,0所用的钱,c0表示0时所付的钱,c1表示1所付的钱。要使所花钱最少,那么要看:例如:当为0时所花的钱c0,比交换了所用h与交换后变成1所用的c1钱的大小,我们要使钱最少。同理,当为1时也要同样比较大小。
(二)代码:
#include<iostream> #include<cstdio> #include<string> using namespace std; int main() { int t; cin>>t; while(t--) { int n,c0,c1,h; string s; int cost=0; cin>>n>>c0>>c1>>h; cin>>s; for(int i=0;i<n;i++) { if(s[i]=='1') { if(c1<=(c0+h)) { cost+=c1; } else { cost+=(c0+h); } } if(s[i]=='0') { if(c0<=(c1+h)) { cost+=c0; } else { cost+=(c1+h); } } } cout<<cost<<endl; } return 0; }