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. 微信小程序 获取数组长度

    wxml中直接 {{array.length}} js中 array.length 小程序调用API返回的数据全部都是异步的:所以前提是要确保array中的数据,是存在的

  2. [NOIP2014提高组]寻找道路

    题目:洛谷P2296.Vijos P1909.codevs3731.UOJ#19. 题目大意:给你一张有向图,边权为1,让你找一条s到t的最短路径,但这条路径上所有点的出边所指向的点都与终点连通.如果 ...

  3. 通过JMeter来测试Quick Easy FTP Server的上传与下载性能

    FTP性能测试 1.1背景说明 本测试选用的是一个小型的FTP服务器软件:Quick Easy FTP Server.Quick Easy FTP Server是一个全中文的FTP服务器软件,反应迅速 ...

  4. 15 hbase 学习(十五)缓存机制以及可以利用SSD作为存储的BucketCache

    下面介绍Hbase的缓存机制:  a.HBase在读取时,会以Block为单位进行cache,用来提升读的性能 b.Block可以分类为DataBlock(默认大小64K,存储KV).BloomBlo ...

  5. C++学习笔记11-面向对象2

     1.  仅仅能初始化直接基类 一个类仅仅能初始化自己的直接基类.直接就是在派生列表中指定的类.假设类C 从类B 派生,类B 从类A 派生,则B 是C 的直接基类.尽管每一个C 类对象包括一个A 类部 ...

  6. Android Support Library 23.2用法简析

    写在前面的几句话 前几天谷歌发布了android-support-library-23.2支持库,这一次23.2版本增加了一些新的支持库以及新的功能.接下来这篇文章,就是对这些新功能部分做简单的用法介 ...

  7. Armbian hostname and WiFi configuration

    In previous post i have described installation of Armbian on Orange Pi PC Plus. Now is the time for ...

  8. 16.C语言可变参数

    //可变参数实现多个参数求和 1 #define _CRT_SECURE_NO_WARNINGS #include <stdlib.h> #include <stdio.h> ...

  9. 实测Untangle - Linux下的安全网关

    UntangleGateway是一个Linux下开源的的网关模块,支持垃圾过滤.URL阻截.反病毒蠕虫等多种功能,其实他的功能还远不止这些,经过一段时间研究本人特制作本视频供大家参考. 本文出自 &q ...

  10. 用Linux建立多应用系统备份服务器

    用Linux建立多应用系统备份服务器 本文旨在结合自己的工作实际,利用LinuxFTP服务器建立了一个多系统备份服务器异地备份策略. 1 建立LinuxFTP服务器 使用了Red Hat Enterp ...