• Clockwork


    问题 C: Clockwork
    时间限制: 1 Sec 内存限制: 128 MB
    提交: 136 解决: 26
    [提交] [状态] [命题人:admin]

    题目描述

    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.

    思路:

        题意很简单,意思就是给一个01的字符串,经过最少此操作打所有的0用1更改(更具体的---选一个k,对于每一个a[i]=1,若a[i+k]=0,令a[i+k]=1)
    意思懂了,现在来看怎么写。首先想到的一定是~~每次更改掉的0越多越好~~ ,但是提交上去后WA掉了。那么来想一下原因,首先看一下这个字符串10000000...,没明显看出,第一次改掉了一个0,然后改掉了2个,第三次4个...,(每次都是翻倍增长)显然如果存在一种反例推翻之前的思路,也就是说当前选取的k更改掉的0可能不是最多的,但在后续的影响中,这一步的效益最大。
    所以正确的思路是找到第一个零,在1-i之间用上述的贪心策略(每次更改掉的0越多越好)。

    #include <bits/stdc++.h>
    using namespace std;
    string s;
    int a[50],ans,b[50],n;
    int judge()
    {
        for(int i=0;i<n;i++)
        {
            if(!a[i])
                return i;
        }
        return 0;
    }
    int main()
    {
        cin>>s;
        n=s.size();
        for(int i=0;i<n;i++)
        {
            if(s[i]=='1') a[i]=1;
        }
        for(int i=0;i<n;i++)
        {
            b[i]=a[i];
        }
        if(a[0]==0)
        {
            printf("-1
    ");
            return 0;
        }
        while(1)
        {
            int cnt=0,pos;
            int p=judge();
            if(p==0) break;
            for(int i=1;i<=p;i++)
            {
                int tmp=0;
                for(int j=0;j<n;j++)
                {
                    if(j+i>=n) break;
                    if(a[j+i]==0&&a[j]==1)
                    {
                        tmp++;
                    }
                }
                if(tmp>cnt)
                {
                    cnt=tmp;
                    pos=i;
                }
            }
            ans++;
            for(int i=0;i<n;i++)
            {
                if(a[i]==1)
                {
                    b[i+pos]=1;
                }
            }
            for(int i=0;i<n;i++)
            {
                a[i]=b[i];
            }
        }
        cout<<ans<<endl;
        return 0;
    }
    View Code

     

  • 相关阅读:
    docker/qemu中是如何对设备管理的
    capacilitys docker中的权限设置 privileged
    比特币的价值
    js连接sqlserver进行查询
    easyUI 添加排序到datagrid
    jquery easyui datagrid 分页 详解
    ASP.net中DateTime获取当前系统时间的大全
    计算数据库中30天以内,30-60天,60-90天,90天以外的数据的个数(用sql实现)
    常用的.net开源项目
    数据库设计三大范式
  • 原文地址:https://www.cnblogs.com/Suiyue-Li/p/10914964.html
Copyright © 2020-2023  润新知