[ACM_模拟] ZJUT 1155 爱乐大街的门牌号 (规律 长为n的含k个逆序数的最小字典序)
Description
Input
Output
Sample Input
3
5 1
4 2
2 0
Sample Output
1 2 3 5 4
1 3 4 2
题目大意:给一个N个自然数(1-N),要求找出逆序对数为K的最小字典序排列的那一个排列方式
解题思路:想要最小字典序那么前面的数字就尽量少的向后面去,因此先计算出满足K个逆序数最少需要LMMN位数字(也就是后LMMN要调动) ②\又因为剩下的数字第一位要尽量小,所以剩下的LMNN-1个数必然倒序排列。③\又因为后LMMN-1个数倒序排列有NXN=(LMMN-1)*(LMMN-2)/2个逆序数,所以剩下数字的第一位的逆序数为K-NXN ④\所以可以确定剩下数字的排列方式。
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int getLastMostMoveNum(int k){
return (int)ceil((1.0+sqrt(1.0+*k))/);
}
int getNumOfNiXu(int k){
return k*(k-)/;
}
int main(){
int T;cin>>T;
while(T--){
int N,K,i;
cin>>N>>K;
if(K==){//顺序输出即可
for(i=;i<N;i++)
cout<<i<<' ';
cout<<N<<'\n';
}else{//存在逆序对
int LMMN=getLastMostMoveNum(K);//计算后LMMN位要变化顺序
for(i=;i<=N-LMMN;i++)//输出前面不用调序的序列
cout<<i<<' ';
int Last[];//保存剩下的数字
for(int j=;i<=N;j++,i++)Last[j]=i;
int NXN=getNumOfNiXu(LMMN-);//计算LMMN-1个数完全逆序的逆序数NXN
//所以要用后LMMN位组成K个逆序数就要把Last[K-NXN]放到第一位,其他完全倒序
cout<<Last[K-NXN];
for(int j=LMMN-;j>=;j--)if(j!=K-NXN)cout<<' '<<Last[j];
cout<<'\n';
}
}return ;
}
[ACM_模拟] ZJUT 1155 爱乐大街的门牌号 (规律 长为n的含k个逆序数的最小字典序)的更多相关文章
- [ACM_模拟] ZJUT OJ 1139 七龙珠 (追及类问题,s-t图像,模拟)
Description 话说孙悟饭与小林正在与刚造访地球的赛亚人贝吉塔交战,因为连贝吉塔的手下纳巴的实力也远在他俩之上,由于差距悬殊,小林不得不设脱离战场,去寻找正在修炼中的悟空求救,而赛亚人一伙 ...
- [ACM_模拟] POJ1068 Parencodings (两种括号编码转化 规律 模拟)
Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two diff ...
- [ACM_模拟][ACM_数学] LA 2995 Image Is Everything [由6个视图计算立方体最大体积]
Description Your new company is building a robot that can hold small lightweight objects. The robo ...
- [ACM_模拟] UVA 10881 Piotr's Ants[蚂蚁移动 数组映射 排序技巧]
"One thing is for certain: there is no stopping them;the ants will soon be here. And I, for one ...
- [ACM_模拟][ACM_暴力] Lazier Salesgirl [暴力 懒销售睡觉]
Description Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making ...
- [ACM_模拟] ACM - Draw Something Cheat [n个长12的大写字母串,找出交集,按字母序输出]
Description Have you played Draw Something? It's currently one of the hottest social drawing games o ...
- [ACM_模拟] ZOJ 3713 [In 7-bit 特殊输出规则 7bits 16进制]
Very often, especially in programming contests, we treat a sequence of non-whitespace characters as ...
- [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)
Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...
- [ACM_模拟] The Willy Memorial Program (poj 1073 ,联通水管注水模拟)
Description Willy the spider used to live in the chemistry laboratory of Dr. Petro. He used to wande ...
随机推荐
- 多线程之 Final变量 详解
原文: http://www.tuicool.com/articles/2Yjmqy 并发编程网:http://ifeve.com/java-memory-model/ 总结: Final 变量在并发 ...
- android开源框架android-async-http使用
原文地址:http://www.open-open.com/lib/view/open1369637365753.html android-async-http开源框架可以是我们轻松的获取网络数据或者 ...
- perl中读取外部文件
打开一个在电脑G盘111文件下的一个文件 #!/usr/bin/perl -w use strict; open(IN,"G:/111/mylove.txt"); while($ ...
- 关于form验证的处理片断
public virtual void SignIn(s_User user, bool createPersistentCookie) { var now = DateTime.UtcNow.ToL ...
- androidannotations 简单配置
1.build.gradle 需要添加的内容 标注的颜色是新建项目之后,build.gradle文件需要添加的内容. buildscript { repositories { jcenter() } ...
- html5 -js判断undefined类型
js判断undefined类型 今天使用showModalDialog打开页面,返回值时.当打开的页面点击关闭按钮或直接点浏览器上的关闭则返回值是undefined所以自作聪明判断 var reVal ...
- Selenium2+python自动化11-定位一组元素find_elements
前言 前面的几篇都是讲如何定位一个元素,有时候一个页面上有多个对象需要操作,如果一个个去定位的话,比较繁琐,这时候就可以定位一组对象. webdriver 提供了定位一组元素的方法,跟前面八种定位方式 ...
- 用T-sql 实现Oracle Connect by 的功能
; with subDepartment as ( select BesonDepartmentID, DepartmentName, ParentBesonDepartmentID, 1 as Hi ...
- android中如何发送及接收数据(两种方法)?
1.如在MainActivity.java中的按钮点击时设置: //发送数据方法1--简单型 i.putExtra("txt", "没错,我就是刚传来的信息!" ...
- calc()问题
什么是calc()? 学习calc()之前,我们有必要先知道calc()是什么?只有知道了他是个什么东东?在实际运用中更好的使用他. calc() 从字面我们可以把他理解为一个函数function.其 ...