Jzoj1795 's villa (greedy)

Source: Internet
Author: User
Tags abs printf
Guru's Villa (standard IO)

Description

"Topic Background"
Lhx as the first millionaire of the universe, has a palatial villa, because the villa is too big, so the leader hired a lot of people to take charge of the villa's health work, we may call these artificial lhxee.

"Title description"
The archbishop employed a total of n Lhxee, these lhxee have male and female.
The master's Grand Villa has a total of M rooms, and now all the lhxee are lined up in front of the Lord. The teaching mainly divides n Lhxee into exactly m parts, each part in the queue is a continuous paragraph, and then to clean the M room respectively.
As the most knowledgeable person in the world, he certainly knows the truth that men and women work well with each other, and that wolves have fewer sheep and more wolves are less harmful. So the Guru wants a distribution that minimizes the difference in the number of men and women in all groups.
The Guru also wants you to output the number of people from left to right for each group.
If there are multiple combinations of people that can achieve the best value, the Guru wants you to tell him separately the smallest and largest dictionary sequence in these scenarios. In other words, you need to find two scenarios, both of which satisfy the minimum number of male and female differences in all groups, the first scenario (the smallest dictionary order) the lower the number of groups, that is, to make the first group of people as small as possible, and the first group of people as small as possible, so that the second group of people to minimize, The second scenario (the largest dictionary order) is to make the number of groups more forward.

Input

The 1th behavior of the input two positive integers n and m, separated by a space.
The 2nd line contains a string of length n, consisting only of characters, and the first character is 0 to indicate that the lhxee in this position is female, and if 1 is a male.

Output

The output file contains two lines, m positive integers per line, separated by a space between positive integers, and no extra space at the end of the line. This m positive integer describes the number of people in each group you divide from left to right.
The 1th behavior is the smallest scheme of the dictionary order, and the 2nd behavior Dictionary is the largest scheme.

Sample Input

8 3
11001100

Sample Output

1 2 5
5 2 1

Data Constraint

Hint

"Sample description"
The Minimum dictionary order scheme is grouped by 1, 10, 01100, and the maximum number of males and females for each group is 1, which is the smallest.
The most dictionary-ordered schemes are grouped by 11001, 10, 0.

"Data Size"
For 40% of the data, there are n≤100;
For 50% of the data, there are n≤1000;
For 65% of the data, there are n≤100000;
For 100% of data, there are n≤5000000,m≤n and m≤100000.

Prompted
About Dictionary Ordering:
Compare S1[n] and S2[n] Dictionary order size, you can find S1[n] and S2[n] in the 1th different number s1[i] and S2[i] (that is, there is 1≤k

Analysis: First find out the difference between male and female students to the average distribution to M rooms.
Use O (n) time to sweep over the answer.
Note: The difference is 0 (i.e. when the number of males and females is the same) special treatment is needed;
This analysis is fine.

Code

#include <cstdio> #include <cstring> #include <string> #define N 6000000 using namespace std;
int S[n],a[n],n,m,ans;

Char C[n];

int abs (int x) {return x>0?x:-x;}
    int main () {int x;
    scanf ("%d%d", &n,&x);
    scanf ("%s", c+1);
            for (int i=1;i<=n;i++) if (c[i]== ' 1 ') s[i]=s[i-1]+1;
    else s[i]=s[i-1]-1;
    int m=x;
    Ans= (ABS (S[N))-1)/m+1;
        if (!s[n]) {int tmp=0;
        for (int i=1;i<=n;i++) if (!s[i]) tmp++;
            if (tmp>=m) ans=0;
    else Ans=1;
    } int k=0;
            for (int i=1;i<=n;i++) {if (S[i]-s[k]<=ans) {int p=abs (s[n]-s[i])/(M-1);
            if (ABS (S[n]-s[i])% (m-1)) p++;
                if (P<=ans) {printf ("%d", i-k);
                K=i;
            m--;
            }} if (M==1) {printf ("%d\n", n-k);
        Break }} for (int i=n;i> =1;i--) if (c[i]== ' 1 ') s[i]=s[i+1]+1;
    else s[i]=s[i+1]-1;
    M=x;
    int tot=0;
    k=n+1;
            for (int i=n;i>0;i--) {if (S[k]-s[i]<=ans) {int p=abs (s[i]-s[1])/(M-1);
            if (ABS (S[i]-s[1])% (m-1)) p++;
                if (P<=ans) {a[++tot]=k-i;
                K=i;
            m--;
            }} if (m==1) {a[++tot]=k-1;
        Break }} for (int i=tot;i>=1;i--) printf ("%d", A[i]);}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.