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 ...
随机推荐
- Stamps ans Envelope Sive UVA - 242
( ||{集合x}表示x中元素1||x中元素2||...||x的最后一个元素||(a,b)表示a||b) ans[i][j][k]表示考虑前i种邮票时取j个邮票能否得到面值kans[i][j][k]= ...
- ACM_折线中点
折线中点 Time Limit: 2000/1000ms (Java/Others) Problem Description: 给定平面上N个点P1, P2, ... PN,将他们按顺序连起来,形成一 ...
- 题解报告:hdu 1039 Easier Done Than Said?
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1039 Problem Description Password security is a trick ...
- 32位Oracle10g在64位CentOS下安装失败记录
环境信息:Alibaba Cloud Elastic Compute Service,CentOS Linux release 7.4.1708 (Core),16C/64GB. 使用32位Oracl ...
- Service官方教程(6)Bound Services主要用来实现通信服务,以及3种实现通信的方案简介。
1.Bound Services A bound service is the server in a client-server interface. A bound service allows ...
- 关于cocoapods安装与使用的总结
昨天晚上研究了很入的cocoapods,在各大论坛也看过了很多方法,这里把之前的方法做一个总结. 这里我把自己遇到的一些问题,大概的说了一下.也让广告初学者少走弯路. 先是来自code4app的文章: ...
- JSR-303原生支持的限制
@Null: 限制只能为null@NotNull: 限制必须不为null@AssertFalse: 限制必须为false@AssertTrue: 限制必须为true@DecimalMax(value) ...
- IE8下ajax兼容问题
function crossDomainAjax(url, successCallback) { // IE8 & 9 only Cross domain JSON GET request i ...
- Xamarin.Forms跨平台开发入门-第二部分:深入解析
英文原文: https://developer.xamarin.com/guides/xamarin-forms/getting-started/hello-xamarin-forms/deepdiv ...
- 程序员的职业方向: 是-->技术?还是-->管理?
岁之后还能不能再做程序员....... 绝大多数程序员最终的职业目标可能都是CTO,但能做到CEO的人估计会比较少,也有一少部分人自己去创业去当老板,也有部分人转行了,当老板的人毕竟是少数,转行的人都 ...