Inversion

Time Limit: 2000ms
Memory Limit: 65536KB

This problem will be judged on ZJU. Original ID: 2425
64-bit integer IO format: %lld      Java class name: Main

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 <= 1/2*n*(n-1).

Output

For each test case, print a line containing the smallest permutation as described above, separates the numbers by single spaces. Don't output any trailing spaces at the end of each line, or you may get an 'Presentation Error'!

Sample Input

5 9
7 3
-1 -1

Sample Output

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

 

Source

 
解题:贪心法可解。。。
 
逆序最大的时候就是反序 $\frac{n*(n-1)}{2}$
 那么我们尝试尽可能的反序后面的,因为前面的是高位,反序高位的不能保证字典序最小。
 
先求出确定后面的多少位是受影响的,先打印前面不受影响的。
 
后面的第一个就是看前面最后一个数是多少+后面的需要逆序多少+1就是后面受影响的第一个,并且能够保证这个位的数字最小。
 
前面已经处理到 $n - p$ 了,那么受影响的第一个数位的数字应该就是 $n - p + 1$,由于后面还要逆序,所以应该是  $n - p + 1 + (m - \frac{(p-2)*(p-1)}{2})$
 
 #include <bits/stdc++.h>
using namespace std;
int n,m;
vector<int>ans;
int main(){
while(scanf("%d %d",&n,&m),~n||~m){
ans.clear();
int p = ;
for(; p*(p - ) < (m<<); p++);
for(int i = ; i <= n - p; ++i) ans.push_back(i);
int tmp = n - p + (m - ((p-)*(p-)>>)) + ;
ans.push_back(tmp);
for(int i = n; i > n - p; --i)
if(i != tmp) ans.push_back(i);
for(int i = ; i < ans.size(); ++i)
printf("%d%c",ans[i],i + == ans.size()?'\n':' ');
}
return ;
}
 

ZJU 2425 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. Raspberry Pi 3 FAQ --- connect automatically to 'mirrors.zju.edu.cn' when downloading and how to accelerate download

    modify the software source: The software source is a place where several free application for linux ...

  7. Inversion Sequence(csu 1555)

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

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

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

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

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

随机推荐

  1. php八大设计模式之适配器模式

    将一个抽象被具体后的结果转换成另外一个需求所需的格式. 在生活中也处处有适配器的出现,比如转换头,就是让两种不同的规格合适的搭配在一起. <?php header("content-t ...

  2. 创建支持SSH服务的镜像

    一.基于commit命令创建 docker commit CONTAINER [REPOSITORY [:TAG]] 1.使用ubuntu镜像创建一个容器 docker run -it ubuntu ...

  3. Winscp远程连接Linux主机,上传和下载文件

    1.安装Winscp.这里不再赘述,网上搜索下载安装就可以 2.点击桌面Winscp快捷键,打开Winscp 3.在打开的页面上填写远程主机的IP,用户名和密码,点击保存,会在页面的左边出现一个站点, ...

  4. awk 基础的用法

    基本的awk执行过程#passwd文件的第二行的第一列和第二列[root@oldboyedu01-nb ~]# awk -F ":" 'NR==2{print $1,$2}' /e ...

  5. 数据库中Select For update语句的解析

    ----------- Oracle -----------------– Oracle 的for update行锁 键字: oracle 的for update行锁 SELECT-FOR UPDAT ...

  6. 洛谷——P1970 花匠

    https://www.luogu.org/problem/show?pid=1970 题目描述 花匠栋栋种了一排花,每株花都有自己的高度.花儿越长越大,也越来越挤.栋栋决定 把这排中的一部分花移走, ...

  7. Visual Studio2008 和2010 执行程序出现的黑框马上消失解决方法

    1 在程序最后加         system("PAUSE");  要注意包括头文件#include"stdlib.h"   //system须要调用这个   ...

  8. windows開始菜单和任务栏图标显示空白而且点击时候显示项目已被移动或删除

    这几天实验室老常常自己主动断电.这是非常蛋疼的一件事,这不上次断电就出事了.来电后开机,点击任务栏上的程序全都显示为无法打开此项目,该项目已被移动.删除.原因是图标缓存丢失,可能是突然断电引起的,也有 ...

  9. h5语音播放(移动端)

    <!--语音导航 start--> <div style="border:0px solid red;width:100%;height:72px;position:rel ...

  10. Mac OSX 平台安装 MongoDB

    Mac OSX 平台安装 MongoDB MongoDB 提供了 OSX 平台上 64 位的安装包,你可以在官网下载安装包. 下载地址:https://www.mongodb.com/download ...