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

最新文章

  1. javascript实现简单的轮播图片
  2. [Xamarin] 從Xamarin中呼叫 *.jar 的 library -建立.jar篇 (转帖)
  3. Sublime Text 3汉化中文版
  4. xib连线出错,模型保存cell状态(最后个Cell隐藏分割线),
  5. End Routine
  6. 请问用Inno_Setup打包文件夹时怎么排除其中一个文件?
  7. 清理sql2000查询分析器登录名记录
  8. 转载:Ubuntu下deb包的安装方法
  9. Dice Possibility
  10. esri-leaflet部分瓦片缺失问题及解决办法
  11. Springboot security cas整合方案-原理篇
  12. ACM Let the Balloon Rise
  13. spring mvc mybatis shiro构建cms系统ios android
  14. NPM 上传自己的包
  15. 喵哈哈村的魔法考试 Round #13 (Div.2) 题解
  16. uglifyjs-webpack-plugin 插件,drop_console 默认为 false(不清除 console 语句),drop_debugger 默认为 true(清除 debugger 语句)
  17. seajs 使用文档
  18. 解决安装Egit时Egit Mylyn和org.eclipse.team.core报错
  19. IOS statusBarStyle 设置
  20. rocketmq 记

热门文章

  1. Java多线程(四)isAlive
  2. ACM_蛇形矩阵
  3. HTML基础2——综合案例2——复杂的嵌套列表
  4. Axis通过wsdd部署Web Service
  5. 每天学点linux命令之nc
  6. duilib入门问题集
  7. Sql生成不重复的数字
  8. Statement和PreparedStatement深入学习总结
  9. Spring+Spring MVC+Hibernate增查(使用注解)
  10. C#压缩文件夹至zip,不包含所选文件夹【转+修改】