Clockwork

Rabbits are small mammals in the family Lep or idae of the or der Lagom or pha. So says Wikipedia.
C or rectly. The c or ollary is that they are not b or ing, f or they are all or iginal and well or ganized.
The rabbits on our farm live in guarded corrals whose borders are adorned with elaborate floral ornaments. Scores of adorable orange carrot clumps grow in the corrals. Rabbits reproduce quickly (it is a norm, hordes of rabbits are born each year) and our mentors are keen to locate them in the corrals effortlessly.
The corrals are well-ordered, they form one straight row of corrals. At the beginning of the first breeding season, some corrals may be unoccupied by rabbits. At the end of each breeding season, a well orchestrated relocation of rabbits is performed. The relocation is guided by a simple formula which depends on one positive integer parameter K , which may be chosen arbitrarily for each season. The relocation works on all corrals in parallel. In each corral,approximately one half of rabbits are removed from the corral and moved K corrals down the row of the corrals. It does not matter whether the target corral is already occupied by some rabbits or not.
If a corral is too close to the end of the row (there are less than K corrals down the row of corrals) then all the rabbits stay in the corral and are not moved anywhere.
Any corral can accommodate unlimited number of rabbits and there are always enough rabbits to breed successfully in any nonempty corral.
You are given a specification of which corrals are occupied and which are empty at the beginning of the first breeding season. Determine the minimum number of relocations needed to populate all corrals by rabbits.

输入

Input consists of a single line with a string of b characters (1 ≤ b ≤ 40), each representing one corral and being either 0 (empty corral) or 1 (inhabited corral). The first character corresponds to the first corral in the row.

输出

Output the minimum number of relocations needed to populate all corrals. If it is not possible to populate all corrals by any number of relocations, print −1.

样例输入

复制样例数据

1010

样例输出

1

ps:贪心找跳几步。

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
string s;
int check(int n){
    for(int i=0;i<n;i++){
        if(s[i]=='0') return i;
    }
    return 0;
}
void fun(int n){
    int ma=-1111;
    int t=0;
    for(int i=1;i<=n;i++){
        int ans=0;
        for(int j=0;j<s.length()-i;j++){
            if(s[j+i]=='0'&&s[j]=='1') ans++;
        }
        if(ans>ma) {
            t=i;
            ma=ans;
        }
    }
    for(int i=s.length()-t-1;i>=0;i--){
        if(s[i+t]=='0'&&s[i]=='1') s[i+t]='1';
    }
}
int main()
{
    cin>>s;
    int ans=0;
    if(s[0]=='0') {
        printf("-1
");
        return 0;
    }
    else {
        while(1){
            int x=check(s.length());
            if(x==0) break;
            fun(x);
            ans++;
        }
    }
    cout<<ans<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/skyleafcoder/p/12319505.html