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 ...
随机推荐
- mysql各个版本下载地址
之所记录下来是因为我找了好久才找到:这下记着了:http://downloads.mysql.com/archives/community/ 希望对没有找到的朋友有帮助
- mysql之修改字符编码
目录 统一修改字段编码 修改单个字段编码 修改表字符编码 统一修改字段编码: alter table `tablename` convert to character set utf8; 修改表字符编 ...
- P1372 又是毕业季I
题目背景 “叮铃铃铃”,随着高考最后一科结考铃声的敲响,三年青春时光顿时凝固于此刻.毕业的欣喜怎敌那离别的不舍,憧憬着未来仍毋忘逝去的歌.1000多个日夜的欢笑和泪水,全凝聚在毕业晚会上,相信,这一定 ...
- koa2实现简易的webpack-dev-server
koa2实现简易的webpack-dev-server热更新 原文https://github.com/zhuangZhou/Blog/issues/3 闲来无事,用koa2撸了一个简易的webpac ...
- 掌握Spark机器学习库-09.6-LDA算法
数据集 iris.data 数据集概览 代码 package org.apache.spark.examples.examplesforml import org.apache.spark.ml.cl ...
- VM virtualBox设置无缝全屏
设置之前:
- Windows 如何使用telnet管理虚拟机Linux
Linux远程登录的工具很多,如putty,SecureCRT…… 其实借助Windows的telnet工具就可以在命令提示符轻松的登录到Linux系统进行操作了. 虽然telnet很简单,但还是要进 ...
- C/C++ 函数模板、全局变量、register、存储周期
1.函数声明时可以简写,如: int max(int,int): 2.函数模板: 格式: template <typename haha>或template <class haha& ...
- CentOS 7 samba server 配置
samba是linux上的文件共享服务软件,相当与Windows上的共享文件夹,当然也是要在同一网段上的. 当前用的版本是4.4.4,好吧!下面介绍怎么去安装配置它,here we go! 1. 安装 ...
- chsh - 改变登录 shell
总览 (SYNOPSIS) chsh [ -s shell ] [ -l ] [ -u ] [ -v ] [ username ] 描述 (DESCRIPTION) chsh 用于 改变 用户的 登录 ...