2018百度之星初赛1003

Problem Description

度度熊有一张纸条和一把剪刀。

纸条上依次写着 N 个数字,数字只可能是 0 或者 1。

度度熊想在纸条上剪 K 刀(每一刀只能剪在数字和数字之间),这样就形成了 K+1 段。

他再把这 K+1 段按一定的顺序重新拼起来。

不同的剪和接的方案,可能会得到不同的结果。

度度熊好奇的是,前缀 1 的数量最多能是多少。

 

 

Input

有多组数据,读到EOF结束。

对于每一组数据,第一行读入两个数 N 和 K 。

第二行有一个长度为 N 的字符串,依次表示初始时纸条上的 N 个数。

0≤K
所有数据 N 的总和不超过100000

 

 

Output

对于每一组数据,输出一个数,表示可能的最大前缀 1 的数量。

 

 

Sample Input

 

5 1 11010 5 2 11010

 

 

Sample Output

 

2 3

 

 

Source

2018 “百度之星”程序设计大赛 - 初赛(A)

思路:

思路就是记录扫一遍,记录每个含有1的小段(需要记录1的数量,和需要砍的刀数,起点坐标,终点坐标),然后依次枚举谁做第一段(只用1刀)+特判就行。

代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
typedef long long ll;
#define INF 0x3f3f3f3f
const int maxn=1e5+10;
const int MAXN=1e3+10;
using namespace std;
int n,k;
char s[maxn];
struct Point
{
    int l,r;
    int num;
    int cut;
}p[maxn];
bool cmp(Point a,Point b)
{
    if(a.num==b.num)
    {
        if(a.cut==b.cut)
        {
            return a.lb.num;
}
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        if(n==0)
        {
            printf("0\n");
            continue;
        }
        scanf("%s",s);
        int n=strlen(s);
        int cnt=0;
        int flag=0;
        for(int i=0;i=g+p[i].cut)
            {
                g+=p[i].cut;
                ans+=p[i].num;
            }
        }
        int ans2=0,g2=0;
        int ans3=0;
        if(p[0].l!=0)
        {
            for(int i=0;i=g2+p[j].cut)
                    {
                        g2+=p[j].cut;
                        ans2+=p[j].num;
                    }
                }
                ans3=max(ans3,ans2);
            }
        }
        printf("%d\n",max(ans,ans3));
    }
    return 0;
}

 

你可能感兴趣的:(贪心)