Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 4176   Accepted: 1857

Description

The inversion number of an integer sequence a1, a2, . . . , an is the number of pairs (ai, aj) that satisfy i < j and ai > aj . Given n and the inversion number m, your task is to find the smallest permutation of the set { 1, 2, . . . , n }, whose inversion number is exactly m. 
A permutation a1, a2, . . . , an is smaller than b1, b2, . . . , bn if and only if there exists an integer k such that aj = bj for 1 <= j < k but ak < bk.

Input

The input consists of several test cases. Each line of the input contains two integers n and m. Both of the integers at the last line of the input is −1, which should not be processed. You may assume that 1 <= n <= 50000 and 0 <= m <= n(n − 1)/2.

Output

For each test case, print a line containing the smallest permutation as described above, separates the numbers by single spaces.

Sample Input

5 9
7 3
-1 -1

Sample Output

4 5 3 2 1
1 2 3 4 7 6 5

Source

题目大意:
给出n和m,求1~n组成的序列中逆序对数为m的最小一个序列。
有多组数据,-1 -1表示输入结束。
思路:
首先,ans一定会是个先上升再下降的数列。(如右图)
而且,上升的序列是逐个加一,抛出尾数,即折点数。
而下降的序列就是剩下的数也抛去折点数,依次输出。
因此,只需要求出折点数x和下降序列的长度k即可。
右图中数据的x,k依次为:
9,3;
10,3;
7,4;
10,3;
3,4。
代码实现:
 #include<cstdio>
int n,m,k,x;
bool v;
void write(bool& v,int x){
if(v) v=false;
else putchar(' ');
printf("%d",x);
}
int main(){
while(scanf("%d%d",&n,&m)&&n!=-){
k=;
for(;k*(k+)<*m;k++);
x=m-k*(k-)/+n-k;
v=true;
for(int i=;i<=n-k-;i++) write(v,i);
write(v,x);
for(int i=n;i>=n-k;i--)
if(i!=x) write(v,i);
putchar('\n');
}
return ;
}

题目来源:POJ

Inversion的更多相关文章

  1. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  2. 控制反转Inversion of Control (IoC) 与 依赖注入Dependency Injection (DI)

    控制反转和依赖注入 控制反转和依赖注入是两个密不可分的方法用来分离你应用程序中的依赖性.控制反转Inversion of Control (IoC) 意味着一个对象不会新创建一个对象并依赖着它来完成工 ...

  3. HDU 1394 Minimum Inversion Number(最小逆序数 线段树)

    Minimum Inversion Number [题目链接]Minimum Inversion Number [题目类型]最小逆序数 线段树 &题意: 求一个数列经过n次变换得到的数列其中的 ...

  4. 依赖倒置原则(Dependency Inversion Principle)

    很多软件工程师都多少在处理 "Bad Design"时有一些痛苦的经历.如果发现这些 "Bad Design" 的始作俑者就是我们自己时,那感觉就更糟糕了.那么 ...

  5. HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)

    题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS     Memory Limit: 32768 K Description The inve ...

  6. Inversion Sequence(csu 1555)

    Description For sequence i1, i2, i3, … , iN, we set aj to be the number of members in the sequence w ...

  7. ACM: 强化训练-Inversion Sequence-线段树 or STL·vector

    Inversion Sequence Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%lld & %llu D ...

  8. ACM Minimum Inversion Number 解题报告 -线段树

    C - Minimum Inversion Number Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  9. HDU-Minimum Inversion Number(最小逆序数)

    Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of ...

  10. 【hdu1394】Minimum Inversion Number

    Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of ...

随机推荐

  1. mysql——免安装配置

    1.下载 (1)下载地址:https://dev.mysql.com/downloads/mysql/ (2)选择下载 2.配置环境变量 (1)解压目录:D:\mysql-8.0.16-winx64 ...

  2. ACM_三角形的周长

    三角形的周长 Time Limit: 2000/1000ms (Java/Others) Problem Description: 有n根棍子,棍子i的长度为ai,想要从中选出3根棍子组成周长尽可能长 ...

  3. ToolBar教程:AppCompatActivity下用toolbar当actionbar用

    参考: https://developer.android.com/training/appbar/index.html 1,自定义toolbar主题 2,在布局xml中使用toolbar 3,在代码 ...

  4. pyCharm最新激活码(2018)

    1.修改hosts文件: 添加下面一行到hosts文件,目的是屏蔽掉Pycharm对激活码的验证 0.0.0.0 account.jetbrains.com windwos系统hosts文件路径为:C ...

  5. jQuery学习笔记(2)-选择器的使用

    一.选择器是什么 有了jQuery的选择器,我们几乎可以获取页面上任意一个或一组对象 二.Dom对象和jQuery包装集 1.Dom对象 JavaScript中获取Dom对象的方式 <div i ...

  6. 2017-11-28 Html-浅谈如何正确给table加边框

    一般来说,给表格加边框都会出现不同的问题,以下是给表格加边框后展现比较好的方式 <style> table,table tr th, table tr td { border:1px so ...

  7. Dojo - 操作Dom的函数

    DOM Manipulation You might be seeing a trend here if you have gotten this far in the tutorial, in th ...

  8. linux 怎么用 名字 代替 ip ?

    比如 ssh 1.1.1.1 变成 ssh usr1 在每台机子的/etc/hosts文件中添加ip与名字的对应表

  9. PAT1021Deepset Root

    题意: 连通则输出最深点.第一步找某个点的最深的,然后从这个最深的点查找其他最深点,做并集. 不连通则输出连通图个数. #include<iostream> #include<cst ...

  10. NoSQL与关系数据库

    关系型数据库:完全支持关系代数理论作为基础:有较大的数据规模:固定的数据库模式:查询效率快:强一致性:数据完整性较易实现:扩展性一般:可用性好. NoSQL:部分支持关系代数理论作为基础:有超大数据规 ...