CodeForcesGym 100517B Bubble Sort
Bubble Sort
This problem will be judged on CodeForcesGym. Original ID: 100517B
64-bit integer IO format: %I64d Java class name: (Any)

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ;
int c[maxn];
void add(int i,int val) {
while(i < maxn) {
c[i] += val;
i += i&-i;
}
}
int sum(int i,int ret = ) {
while(i > ) {
ret += c[i];
i -= i&-i;
}
return ret;
}
bool used[maxn];
int n,ans[maxn];
LL k;
LL check(int x) {
if(x < ) return ;
LL ret = ;
for(int i = ; i < x; ++i){
if(ret >= k) return ret;
ret <<= ;
}
return ret;
}
int main() {
#define NAME "bubble"
freopen(NAME".in","r",stdin);
freopen(NAME".out","w",stdout);
while(scanf("%d%I64d",&n,&k),n||k) {
memset(used,false,sizeof used);
memset(c,,sizeof c);
for(int i = ; i <= n; ++i) add(i,);
int cur = ;
for(int i = ; i < n; ++i) {
while(used[cur]) ++cur;
int big = sum(n) - sum(cur);
if(check(big-) >= k) {
ans[i] = cur;
used[cur] = true;
add(cur,-);
} else {
int t = cur + ;
k -= check(big-);
while(used[t]) ++t;
int xx = sum(n) - sum(t);
while(xx && check(xx-) < k) {
k -= check(xx-);
++t;
while(used[t]) ++t;
xx = sum(n) - sum(t);
}
used[t] = true;
ans[i] = t;
add(t,-);
}
}
for(int i = ; i <= n; ++i)
if(!used[i]) {
ans[n] = i;
break;
}
for(int i = ; i <= n; ++i)
printf("%d%c",ans[i],i==n?'\n':' ');
}
return ;
}
/*
1 2 3 4 5
1 2 3 5 4
1 2 4 3 5
1 2 5 3 4
1 3 2 4 5
1 3 2 5 4
1 4 2 3 5
1 5 2 3 4
2 1 3 4 5
2 1 3 5 4
2 1 4 3 5
2 1 5 3 4
3 1 2 4 5
3 1 2 5 4
4 1 2 3 5
5 1 2 3 4
*/
CodeForcesGym 100517B Bubble Sort的更多相关文章
- Java中的经典算法之冒泡排序(Bubble Sort)
Java中的经典算法之冒泡排序(Bubble Sort) 神话丿小王子的博客主页 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一 ...
- Bubble Sort (5775)
Bubble Sort Problem Description P is a permutation of the integers from 1 to N(index starting from ...
- Bubble Sort [ASM-MIPS]
# Program: Bubble sort # Language: MIPS Assembly (32-bit) # Arguments: 5 unordered numbers stored in ...
- HDU 5775 Bubble Sort(冒泡排序)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- 2016 Multi-University Training Contest 4 Bubble Sort(树状数组模板)
Bubble Sort 题意: 给你一个1~n的排列,问冒泡排序过程中,数字i(1<=i<=n)所到达的最左位置与最右位置的差值的绝对值是多少 题解: 数字i多能到达的最左位置为min(s ...
- 快速幂取模 POJ 3761 bubble sort
题目传送门 /* 题意:求冒泡排序扫描k次能排好序的全排列个数 数学:这里有一个反序列表的概念,bj表示在j左边,但大于j的个数.不多说了,我也是看网上的解题报告. 详细解释:http://blog. ...
- 冒泡排序(Bubble Sort)
常见的排序算法有Bubble Sort.Merge Sort.Quick Sort 等,所有排序算的基本法思想都是把一个无限大的数据规模通过算法一步步缩小,指导最后完成排序. 这里分享一下Buuble ...
- [算法] 冒泡排序 Bubble Sort
冒泡排序(Bubble Sort,台湾另外一种译名为:泡沫排序)是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没 ...
- HDU 5775 Bubble Sort (线段树)
Bubble Sort 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...
随机推荐
- C语言经典程序1
//打印2-200的所有素数 (除了1和它本身,不能被小于它的其它数整除的数称为素数) ; ;i<;i++) //i遍历2-200 { ; //先默认这个数为素数 ;j<i-;j++) / ...
- codeforces 570 E. Pig and Palindromes (DP)
题目链接: 570 E. Pig and Palindromes 题目描述: 有一个n*m的矩阵,每个小格子里面都有一个字母.Peppa the Pig想要从(1,1)到(n, m).因为Peppa ...
- Monkey Banana Problem LightOJ - 1004
Monkey Banana Problem LightOJ - 1004 错误记录: 1.数组开小2.每组数据数组没有清空 #include<cstdio> #include<cst ...
- 贪心/数学 Codeforces Round #212 (Div. 2) A. Two Semiknights Meet
题目传送门 /* 贪心/数学:还以为是BFS,其实x1 + 4 * k = x2, y1 + 4 * l = y2 */ #include <cstdio> #include <al ...
- JEECMS9.3集成dubbo操作记录
需求描述: 门户及其他应用系统需要查询JEECMS9.3中发布的栏目及数据,而其他系统都是基于dubbo开发的,因此想要将JEECMS9.3中集成dubbo并对外提供内容管理服务. 需求实现: 1.添 ...
- Spring MVC 结合Velocity视图出现中文乱码的解决方案
编码问题一直是个很令人头疼的事,这几天搭了一个Spring MVC+VTL的web框架,发现中文乱码了,这里记录一种解决乱码的方案. 开发环境为eclipse,首先,检查Window->pref ...
- oracle 安装,启动 ,plsql 连接
1.下载oracle 服务器端,正常安装,在选择桌面类或者是服务器类的时候选择服务器类. 2.下载oracle 客户端解压版 下载地址 链接:https://pan.baidu.com/s/1mi ...
- myeclipse 跟踪struts 源码失败
解决办法: 找到工程jar包所在的位置,点击右键:properties 点击external folder 找到 这个包下的src文件夹 导入之后, 源码会变色
- install nginx error
the error info : the HTTP gzip module requires the zlib library.You can either disable the module by ...
- linux 安装 mongo 3.4
要求:linux 安装 mongo 3.4 大体上,按照官网提供的方法来做. 系统是ubuntu 16.04 安装的是mongo3.4.8 社区版 1. 导入导入包管理系统使用的公钥 ...