A - Cut the sticks SDUT 2017算法训练赛

You are given a number of sticks of varying lengths. You will iteratively cut the sticks into smaller sticks, discarding the shortest pieces until there are none left. At each iteration you will determine the length of the shortest stick remaining, cut that length from each of the longer sticks and then discard all the pieces of that shortest length. When all the remaining sticks are the same length, they cannot be shortened so discard them.

Given the lengths of  sticks, print the number of sticks that are left before each iteration until there are none left.

Note: Before each iteration you must determine the current shortest stick.

Input Format 
The first line contains a single integer 
The next line contains  space-separated integers: a0, a1,...an-1, where represents the length of the  stick in array arr.

Output Format 
For each operation, print the number of sticks that are cut, on separate lines.

Constraints

Sample Input 0

6
5 4 4 2 2 8

Sample Output 0

6
4
2
1

Sample Input 1

8
1 2 3 4 3 3 2 1

Sample Output 1

8
6
4
1

Explanation

Sample Case 0 :

sticks-length        length-of-cut   sticks-cut
5 4 4 2 2 8             2               6
3 2 2 _ _ 6             2               4
1 _ _ _ _ 4             1               2
_ _ _ _ _ 3             3               1
_ _ _ _ _ _           DONE            DONE

Sample Case 1

sticks-length         length-of-cut   sticks-cut
1 2 3 4 3 3 2 1         1               8
_ 1 2 3 2 2 1 _         1               6
_ _ 1 2 1 1 _ _         1               4
_ _ _ 1 _ _ _ _         1               1
_ _ _ _ _ _ _ _       DONE            DONE

#include<iostream>
#include<algorithm>
using namespace std;
int a[10005];
int main()
{
    int n,flag=1;
    cin>>n;
    for(int i=0; i<n; i++)
        cin>>a[i];
    for(;;)
    {
        int sum=0,small;
        sort(a,a+n);
        for(int i=0; i<n; i++)
            if(a[i]!=0)
                sum++;
        for(int i=0; i<n; i++)
            if(a[i]!=0)
            {
                small=a[i];
                break;
            }
        if(sum==0)
            flag=0;
        else
        {
            for(int i=0; i<n; i++)
                if(a[i]!=0)
                    a[i]=a[i]-small;
        }
        if(flag==0)
            break;
        cout<<sum<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/beposit/article/details/80850248
cut