HDU - 6376 - 度度熊剪纸条(贪心)

HDU - 6376 - 度度熊剪纸条

 

所有的连续的一可以分为三个部分。

第一个部分是刚开始的连续的1,然后是末尾的连续的1,再就是中间部分的连续的1 

第一种如果做前缀1的末尾,则其花费为0

第二种要用的话花费为1

第三种做前缀末尾,则其花费为1,否则花费为2

然后就是求出较大的即为答案

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long int LL;
const int N = 1e4 + 10;
int n, k, sz, a[N];
char s[N];
bool cmp(int a,int b) {return a>b;}
int sum(int k){
    int ans = 0;
    for(int i=0;i<k && i<sz;i++) ans += a[i];
    return ans;
}
int main()
{
    while(scanf("%d%d", &n, &k)!=EOF){
        scanf("%s", s);
        int st = 0, ed = n-1;
        int top = 0, last = 0;
        while(st < n && s[st] == '1') st ++; top = st;
        if(k==0){
            printf("%d\n", top);
            continue;
        }
        while(st<=ed && s[ed] == '1') ed --; last = n-1-ed;
        int cnt = 0; sz = 0;
        for(int i=st;i<=ed;i++){
            if(s[i] == '0') {
                if(cnt) a[++sz] = cnt;
                cnt = 0;
            } else cnt ++;
        }
        sort(a+1, a+1+sz);
        int ans = 0;
        while(k > 2 && sz >= 1) ans += a[sz--], k -= 2;
        if(k==1) ans += max(top + last, a[sz]);
        else ans += max(top + a[sz], max(top + last, last + a[sz]));
        printf("%d\n", ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值