zoukankan      html  css  js  c++  java
  • 构造逆序数

    Description

    The world famous scientist Innokentiy almost finished the creation of perpetuum mobile. Its main part is the energy generator which allows the other mobile's parts work. The generator consists of two long parallel plates with n lasers on one of them and n receivers on another. The generator is considered to be working if each laser emits the beam to some receiver such that exactly one beam is emitted to each receiver.

    It is obvious that some beams emitted by distinct lasers can intersect. If two beams intersect each other, one joule of energy is released per second because of the interaction of the beams. So the more beams intersect, the more energy is released. Innokentiy noticed that if the energy generator releases exactly k joules per second, the perpetuum mobile will work up to 10 times longer. The scientist can direct any laser at any receiver, but he hasn't thought of such a construction that will give exactly the required amount of energy yet. You should help the scientist to tune up the generator.

    Input

    The only line contains two integers n and k (1 ≤ n ≤ 200000, ) separated by space — the number of lasers in the energy generator and the power of the generator Innokentiy wants to reach.

    Output

    Output n integers separated by spaces. i-th number should be equal to the number of receiver which the i-th laser should be directed at. Both lasers and receivers are numbered from 1 to n. It is guaranteed that the solution exists. If there are several solutions, you can output any of them.

    Sample Input

    Input
    4 5
    Output
    4 2 3 1

    Input
    5 7
    Output
    4 2 5 3 1

    Input
    6 0
    Output
    1 2 3 4 5 6



    思路
     第i个位置的数最大可以构造i-1个逆序数,当目前这个数构造的逆序数小于你的目标逆序数就可以把这个数放进去。

     1 #include<iostream>
     2 using namespace std;
     3 #define maxn 200000+5
     4 long long xu[maxn];
     5 int main()
     6 {
     7     long long n,k;
     8     cin >> n >> k;
     9     int num = 1,ko = n - 1;
    10     while (ko >= 0)
    11     { 
    12         if (k >= ko)   //目前构造的逆序数小于目标逆序数
    13         {
    14             k -= ko;
    15             xu[ko] = num++; //从后面开始放数
    16         }
    17         ko--;
    18     }
    19     for (int  i = 0; i < n; i++)
    20     {
    21         if (xu[i])   //放了数
    22             cout << xu[i] << " ";//就输出那个数
    23         else
    24             cout<<num++<<" ";//不然就输出原来的数
    25     }
    26     return 0;
    27 }


  • 相关阅读:
    sql中的group by 和 having 用法解析
    关于js的this上下文环境绑定
    windows Service循环任务.服务启动后无法停止重启的解决办法
    js 数组对象的操作方法
    SVN Client API的.net 接口 SharpSvn介紹 Checkout操作实例
    JQuery实现表格的相同单元格合并的三种方法
    SharpSVN出错信息:Can't determine the user's config path,从此证明了百度是个垃圾
    显示js对象所有属性和方法的函数
    软件开发实践的24条军规
    C#中的泛型
  • 原文地址:https://www.cnblogs.com/Lynn0814/p/4726599.html
Copyright © 2011-2022 走看看