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 ...
随机推荐
- [CF Round #278] Tourists
给定一个n个点m条边的无向图,求图上的两点的所有的简单路径之间的最小边. 蓝链 $ n,m,q \leq 100000, w_i \leq 10 ^7$ Solution 考虑建立用缩点双来建立广义圆 ...
- poj 1258 Agri-Net prim模板 prim与dijkstra的区别
很裸地求最小生成树的题目.题意就不多说了,最重要的就是记录一下学会了prim算法. 初学prim,给我的第一感觉就是和dijkstra好像啊,感觉两者的区别还是有的: 1:prim是求最小生成树的算法 ...
- Class.getResourceAsStream()与ClassLoader.getResourceAsStream()获取资源时的路径说明
Class.getResourceAsStream(): com.xusweeter.iot.ws.vodafone.config.VodafoneServiceConfig.class.getRes ...
- ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第四天(非原创)
文章大纲 一.课程介绍二.今日内容介绍三.参考资料下载四.参考文章 一.课程介绍 一共14天课程(1)第一天:电商行业的背景.淘淘商城的介绍.搭建项目工程.Svn的使用.(2)第二天:框架的整合.后台 ...
- time模块、datetime模块讲解
time模块清楚三种格式的时间相互转换 import time# 时间分为三种格式#1.时间戳start= time.time()time.sleep(3)stop= time.time()print ...
- spark shuffle:分区原理及相关的疑问
一.分区原理 1.为什么要分区?(这个借用别人的一段话来阐述.) 为了减少网络传输,需要增加cpu计算负载.数据分区,在分布式集群里,网络通信的代价很大,减少网络传输可以极大提升性能.mapreduc ...
- mysql执行语句汇总
插入select的数据 INSERT INTO `test1`( order_id, goods_id, goods_name, goods_sn, product_id, goods_number, ...
- 洛谷 P1339 [USACO09OCT]热浪Heat Wave (堆优化dijkstra)
题目描述 The good folks in Texas are having a heatwave this summer. Their Texas Longhorn cows make for g ...
- Zed Shaw:程序员的常见健康问题
Zed Shaw:程序员的常见健康问题 原文作者Zed Shaws是一位作家.软件开发人员.音乐人(下文中提到吉他手),于2010年发布<Learn Python The Hard Way> ...
- 强大的云存储与应用管理工具DzzOffice1.0 Beta(大桌子办公)发布下载
之前在9月份我们发布了一份内测版,得到了1000多位朋友参与下载测试.经过2个月,结合测试后朋友们反馈的问题,和开发建议.终于完成了这次Beta版的开发.感谢这两个月中参与测试,和帮助我们完善程序的朋 ...