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. session 存入 memcahce

    <?php header('content-type:text/html;charset=utf-8'); class RedisSessionHandler{ public $ttl; //失 ...

  2. 搭建Lvs负载均衡群集

    一.Lvs详解 lvs内核模型 1.模型分析 用户访问的数据可以进入调度器 匹配调度器分配的虚拟IP|IP+端口(路由走向) 根据调度器的算法确定匹配的服务器 2.调度条件:基于IP.基于端口.基于内 ...

  3. Unity C# 设计模式(六)原型模式

    定义:用原型实例指定创建对象的种类,并通过拷贝这些原型来创建新的对象. 优点: 1.原型模式向客户隐藏了创建新实例的复杂性 2.原型模式允许动态增加或较少产品类. 3.原型模式简化了实例的创建结构,工 ...

  4. Linux学习之socket编程(一)

    socket编程 socket的概念: 在TCP/IP协议中,“IP地址+TCP或UDP端口号”唯一标识网络通讯中的一个进程,“IP地址+端口号”就称为socket. 在TCP协议中,建立连接的两个进 ...

  5. COGS——T 21. [HAOI2005] 希望小学

    http://www.cogs.pro/cogs/problem/problem.php?pid=21 ★★   输入文件:hopeschool.in   输出文件:hopeschool.out    ...

  6. TI C66x DSP 四种内存保护问题 -之- 外设訪问corePac内部资源时的内存保护问题

    外设訪问corePac内部资源(L1,L2)时的内存保护等问题请參考以下两个blog.已经叙述的非常具体. "TI C66x DSP 系统events及其应用 - 2"," ...

  7. Linux软防火墙ACL匹配的优化点

    首先.请求不要再诬陷Netfilter.尽管它有一些固有性能损耗,但敬请不要将iptables和Netfilter等同,假设你要抓元凶,请直接说iptables,而不要说成Netfilter!     ...

  8. To new is C++; To malloc is C; To mix them is sin (混淆C++中的new和C中的malloc是一种犯罪)

    Introduction One of the most common questions that get asked during interviews for C++ programmers i ...

  9. STL_算法_局部排序(partial_sort、partial_sort_copy)

    C++ Primer 学习中. . . 简单记录下我的学习过程 (代码为主) /***************************************** // partial_sort(b, ...

  10. CSS 相对/绝对(relative/absolute)定位与jQuery的控制显示隐藏

    曾经写显示隐藏老是用jq方法控制: dom.show(); dom.hide(); 事实上这样还是有非常多缺陷的. 这是html结构: <div class="holi"&g ...