Codeforces Round #681 B. Saving the City(贪心)

在这里插入图片描述

题意:
T组样例,点燃一个地雷 i i i时相邻的地雷 ( i − 1 , i + 1 ) (i-1,i+1) (i1,i+1)都会爆炸,点燃一个地雷需要a个点数,放置一个地雷需要b个点数,现在需要炸掉给的s中所有的1,最小化花费的点数
题解:
贪心,注意边界条件
code:

#include<bits/stdc++.h>
//#pragma GCC optimize(2)
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
 
using namespace std;
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0)
#define ll long long
#define ull unsigned long long
#define ld long double
#define PII pair<int,int>
#define pb push_back
#define all(a) a+1,a+n+1
#define ALL(a) a.begin(),a.end()
#define debug(a) cout <<#a << "=" << a << endl;
const int INF = 0x3f3f3f3f;
const ll LINF = 1ll<<60;
const int mod=1e9+7;
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
 //上面是模板,与解题无关-----------------------------------------------
int main(){
    int t;cin>>t;
    while(t--){
        int a,b;
        string s;
        cin>>a>>b>>s;
        int len=s.size(),cnt=50000,pos=len+100;
        rep(i,0,len-1){//找到第一个1所在位置
            if(s[i]=='1') {
                pos=i;break;
            }
        }
        ll ans=0,f=0;//f判断本段是否已经引爆
        rep(i,pos,len-1){
            if(s[i]=='1'){
                if(!f){
                    ans+=min(a,cnt*b);//注意此处的更新条件
                    f=1;
                    cnt=0;
                }
            }
            else{
                f=0;
                cnt++;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值