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

本文介绍了一种通过贪心算法构造具有特定下降系数的排列的方法。对于长度为n的排列,其下降系数定义为满足pi > pi+1的i的数量。文章通过实例解释了如何构造这样的排列,并提供了一个简洁的C++实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}


(venv) PS E:\cuit一步到胃1\Flask_project> flask run Traceback (most recent call last): File "<frozen runpy>", line 198, in _run_module_as_main File "<frozen runpy>", line 88, in _run_code File "D:\python\cuit\venv\Scripts\flask.exe\__main__.py", line 7, in <module> sys.exit(main()) ~~~~^^ File "D:\python\cuit\venv\Lib\site-packages\flask\cli.py", line 1064, in main cli.main() ~~~~~~~~^^ File "D:\python\cuit\venv\Lib\site-packages\click\core.py", line 1363, in main rv = self.invoke(ctx) File "D:\python\cuit\venv\Lib\site-packages\click\core.py", line 1830, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^ File "D:\python\cuit\venv\Lib\site-packages\click\core.py", line 1226, in invoke return ctx.invoke(self.callback, **ctx.params) ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python\cuit\venv\Lib\site-packages\click\core.py", line 794, in invoke return callback(*args, **kwargs) File "D:\python\cuit\venv\Lib\site-packages\click\decorators.py", line 93, in new_func return ctx.invoke(f, obj, *args, **kwargs) ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python\cuit\venv\Lib\site-packages\click\core.py", line 794, in invoke return callback(*args, **kwargs) File "D:\python\cuit\venv\Lib\site-packages\flask\cli.py", line 912, in run_command raise e from None File "D:\python\cuit\venv\Lib\site-packages\flask\cli.py", line 898, in run_command app = info.load_app() File "D:\python\cuit\venv\Lib\site-packages\flask\cli.py", line 313, in load_app app = locate_app(import_name, None, raise_if_not_found=False) File "D:\python\cuit\venv\Lib\site-packages\flask\cli.py", line 236, in locate_app return find_best_app(module) File "D:\python\cuit\venv\Lib\site-packages\flask\cli.py", line 64, in find_best_app app = app_factory() File "E:\cuit一步到胃1\Flask_project\app\__init__.py", line 50, in create_app from app.routes.user import user_bp File "E:\cuit一步到胃1\Flask_project\app\routes\user.py", line 15 db.session.commit() IndentationError: unexpected indent
06-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值