Code Submission Evaluation System Login

CSES - HIIT Open 2017

HIIT Open 2017

Contest start:2017-05-27 11:00:00
Contest end:2017-05-27 16:00:00

Task list | Submit code | Submissions | Messages | Scoreboard


History
2017-05-27 12:04:36
Task:Jonnesort
Sender:Kanadan virallinen maajoukkue
Submission time:2017-05-27 12:04:36
Status:READY
Result:ACCEPTED

Show test data

Code

#include<iostream>
#include<queue>
#include<algorithm>

using namespace std;

bool sorted(int* arr, int k){
    for(int i=1; i<k; i++)
        if(arr[i-1]>arr[i])
            return false;
    return true;
}

int a[1000];
int main() {
    int n,k;
    cin>>n>>k;
    for(int i=0; i<n; i++)
        cin>>a[i];
        
    int R=0;
    bool _=1;
    for(;_;){
        R++;
        bool cont=0;
        if(!sorted(a,k))
            cont=1;
        sort(a,a+k);
        
        for(int i=k; i<n; i++){
            for(int j=0; j<k-1; j++){
                if(a[i-j]<a[i-j-1]){
                    cont=1;
                    swap(a[i-j],a[i-j-1]);
                }
            }
        }
        
        if(!cont)
            break;
    }
    cout<<R<<endl;
    return 0;
}