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

  1. 5 9
  2. 7 3
  3. -1 -1

Sample Output

  1. 4 5 3 2 1
  2. 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。
代码实现:
  1. #include<cstdio>
  2. int n,m,k,x;
  3. bool v;
  4. void write(bool& v,int x){
  5. if(v) v=false;
  6. else putchar(' ');
  7. printf("%d",x);
  8. }
  9. int main(){
  10. while(scanf("%d%d",&n,&m)&&n!=-){
  11. k=;
  12. for(;k*(k+)<*m;k++);
  13. x=m-k*(k-)/+n-k;
  14. v=true;
  15. for(int i=;i<=n-k-;i++) write(v,i);
  16. write(v,x);
  17. for(int i=n;i>=n-k;i--)
  18. if(i!=x) write(v,i);
  19. putchar('\n');
  20. }
  21. return ;
  22. }

题目来源: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. java 读取word

    读取word文件 import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import org ...

  2. Linux安装FTP文档服务器

    1.检查是否安装 了vsftpd,如果未安装 则安装vsftpd. 1)查看系统中是否安装了vsftpd,可以通过执行命令 :rpm -qa | grep vsftpd 2)如果没有安装 vsftpd ...

  3. Bryce1010 Acm模板

    目录 STL标准模板库 STL简介 STL pair STL set STL vector STL string STL stack STL queue STL map upper_bound和low ...

  4. js截取字符串 区分中英文

    方法如下: //在一个字符串中截取前面部分文字,汉字.全角符号按2个占位,数字英文.半角按一个占位,未显示完的最后加入“……”. //适合多行显示. function suolve(str, sub_ ...

  5. CSV文件转EXCEl(java)

    package main; import java.io.BufferedReader;import java.io.DataInputStream;import java.io.File;impor ...

  6. CSS笔记集合

    CSS CSS 认识 CSS全称为层叠样式表,主要是用于定义HTML内容在浏览器内的显示样式. CSS样式由选择符和声明组成,而声明又由属性和值组成. CSS中注释语句:/*注释语句*/.Html中使 ...

  7. PHP常见问题总结

    1.为什么会出现这种情况?端口什么的都设置正确了. 解决方法: 请将本机的IIS服务关闭,开启Apache服务.IIS服务的关闭方法可参见 https://jingyan.baidu.com/arti ...

  8. struts2之通配符映射

    系统有n多个请求时候,不可能以一个action对应一个映射.可以用通配符映射将成百上千请求简化成一个通用映射. 通配符映射规则:1.若找到多个匹配,没有通配符的将胜出. 2.若指定的动作不存在,str ...

  9. vue-element-admin使用常见问题

    一.vue-element-admin添加快捷导航 这个组件是基于vue-i18n因此,首先在项目中安装i18n npm install --save vue-i18n 然后main.js中引入 im ...

  10. 梦想CAD控件图层COM接口知识点

    梦想CAD控件图层COM接口知识点 一.新建图层 主要用到函数说明: _DMxDrawX::AddLayer 增加新的图层.详细说明如下: 参数 说明 BSTR pszName 图层名 c#中实现代码 ...