P1750 greedy Stack Sequence

  

Title Description

Given a sequence of n element constituting, you will need to order the elements of which is pressed into a size of stack and pop c. Elements are arranged in the order of the stack thereof, will get a new sequence. We know that there are many such sequences, please output the smallest of all the new sequence sequence the first element (if the first element of the smallest sequence have more, then make a second as small as possible; if there are more than one, then let the third smallest, and so on).

Input and output formats

Input formats:

 

The first line, the two numbers n, c

Second line number n, the value of n elements in a sequence

 

Output formats:

 

Output number n, to meet the requirements of the sequence.

 

Sample input and output

Input Sample # 1:  Copy
6 3
5 2 3 8 7 4
Output Sample # 1:  Copy
235478 


clearly wrong queue monotonically
as. 3. 4
. 1. 4. 3 2

  • This idea is very simple question, in fact, the so-called greedy simulation, simulation of a sliding window 

 

 

#include<bits/stdc++.h>
using namespace std;
long long a[10005];
bool used[10005];
int main()
{
    long long n,c,i,j;
    scanf("%lld%lld",&n,&c);
    for(i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    long long l=1,r=c,k=n,minn,mi;
    while(k!=0)
    {
        minn=9999999999;
        for(i=l;i<=r;i++)
        {
            if(a[i]<minn&&used[i]==0) 
            {
                minn=a[i];
                mi=i;
            }
        }
        if(k==0) printf("%lld\n",minn);
        else printf("%lld ",minn);
        k--;
        used[mi]=1;
        long long z=0;
        for(j=mi-1;j>=1;j--)
        {
            if(used[j]==0)
            {
                l=j;
                z=1;
                break;
            }
        }
        if(r<n) r++;
        if(z==0) l++;
    }
    return 0;
}
View Code

 








Guess you like

Origin www.cnblogs.com/bxd123/p/10993484.html