CUIT ACM Personal Training 11.27(FM) H - Slightly Decreasing Permutations

H - Slightly Decreasing Permutations

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.

The decreasing coefficient of permutation p1, p2, ..., pn is the number of such i (1 ≤ i < n), that pi > pi + 1.

You have numbers n and k. Your task is to print the permutation of length n with decreasing coefficient k.

Input

The single line contains two space-separated integers: n, k (1 ≤ n ≤ 105, 0 ≤ k < n) — the permutation length and the decreasing coefficient.

Output

In a single line print n space-separated integers: p1, p2, ..., pn — the permutation of length n with decreasing coefficient k.

If there are several permutations that meet this condition, print any of them. It is guaranteed that the permutation with the sought parameters exists.

Sample Input

Input
5 2
Output
1 5 2 4 3
Input
3 0
Output
1 2 3
Input
3 2
Output
3 2 1

題解:這道題其實可以理解成在n數字中,有k次發生前一項大於後一項的情況。

那麼這個其實也是一道簡單的貪心算法,如果較大的數字隨機排布在n個數字中,就如樣例1,有可能會符合題目,但是這只是一個特例。只有當我們從前倒序輸出,輸出k個數字之後再正序輸出時,我們的結果才能保證正確。比如在5個數字中,找到2次發生前一項大於後一項的數列,那我們只要讓5 4這樣倒序輸出,緊接着1 2 3輸出,就能找到一組5 4 1 2 3符合題意

同理給出下面兩組數據:

7 4

7 6 5 4 1 2 3

6 5

6 5 4 3 2 1

這些都是符合題意的結果。


AC代碼如下:

#include<bits/stdc++.h>
using namespace std;

int n,m;

int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        if(i<=m) cout<<n-i+1<<" ";
        else cout<<i-m<<" ";
    cout<<endl;
    return 0;
}


發佈了36 篇原創文章 · 獲贊 14 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章