Bubble Sort

Time Limit: 2000ms
Memory Limit: 262144KB

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

 
解题:我们发现假设当前位选择1,那么发现比1大的并且没有使用的有b个,那么当前为选1,后面就还有$2^{b-1}$种方式,所以贪心的选,只要使得后面的方案数大于当前的k即可,如果少于了怎么办?比如当前是1,然后把当前的值改为2,k减去当前位选1的时候的方案数,类似搞,直到后面的方案的数不少于k
 
代码写的确实烂了点
 #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的更多相关文章

  1. Java中的经典算法之冒泡排序(Bubble Sort)

    Java中的经典算法之冒泡排序(Bubble Sort) 神话丿小王子的博客主页 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一 ...

  2. Bubble Sort (5775)

    Bubble Sort Problem Description   P is a permutation of the integers from 1 to N(index starting from ...

  3. Bubble Sort [ASM-MIPS]

    # Program: Bubble sort # Language: MIPS Assembly (32-bit) # Arguments: 5 unordered numbers stored in ...

  4. HDU 5775 Bubble Sort(冒泡排序)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  5. 2016 Multi-University Training Contest 4 Bubble Sort(树状数组模板)

    Bubble Sort 题意: 给你一个1~n的排列,问冒泡排序过程中,数字i(1<=i<=n)所到达的最左位置与最右位置的差值的绝对值是多少 题解: 数字i多能到达的最左位置为min(s ...

  6. 快速幂取模 POJ 3761 bubble sort

    题目传送门 /* 题意:求冒泡排序扫描k次能排好序的全排列个数 数学:这里有一个反序列表的概念,bj表示在j左边,但大于j的个数.不多说了,我也是看网上的解题报告. 详细解释:http://blog. ...

  7. 冒泡排序(Bubble Sort)

    常见的排序算法有Bubble Sort.Merge Sort.Quick Sort 等,所有排序算的基本法思想都是把一个无限大的数据规模通过算法一步步缩小,指导最后完成排序. 这里分享一下Buuble ...

  8. [算法] 冒泡排序 Bubble Sort

    冒泡排序(Bubble Sort,台湾另外一种译名为:泡沫排序)是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没 ...

  9. HDU 5775 Bubble Sort (线段树)

    Bubble Sort 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...

随机推荐

  1. [Usaco2005 Jan]Sumsets 求和

    Description Farmer John commanded his cows to search for different sets of numbers that sum to a giv ...

  2. Tarjan UVALive 6511 Term Project

    题目传送门 /* 题意:第i个人选择第a[i]个人,问组成强联通分量(自己连自己也算)外还有多少零散的人 有向图强联通分量-Tarjan算法:在模板上加一个num数组,记录每个连通分量的点数,若超过1 ...

  3. 使用Apache Commons IO组件读取大文件

    Apache Commons IO读取文件代码如下: Files.readLines(new File(path), Charsets.UTF_8); FileUtils.readLines(new ...

  4. Hanlder + 弱引用防内存漏泄示例*

    Hanlder + 弱引用防内存漏泄示例: public class MainActivity extends AppCompatActivity { public final MyHandler h ...

  5. 1270 数组的最大代价 dp

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1270&judgeId=194704 一开始贪心,以为就两种情况, ...

  6. (七)Mybatis总结之注解开发

    请移步到 https://www.cnblogs.com/lxnlxn/p/5996707.html

  7. js 宿主对象的属性和方法总结

    (1)属性:       //height,width;           a=document.documentElement.clientHeight;           //文档可视高度,由 ...

  8. Swift 基础语法入门(一)

    一.变量和常量 1.声明常量和变量 用let来声明常量  let radius = 10 用var来声明变量 var age = 20   或者是var x = 0.0, y = 0.0, z = 0 ...

  9. vue-webpack所构建好的项目中增加Eslint

    首先在package.json中配置eslint模块: 在终端运行命令:npm install 然后在build文件夹中web pack.base.conf.js配置eslint 接下来在在项目中新建 ...

  10. 关于docker入门教程

    简介:docker入门教程 docker入门教程翻译自docker官方网站的Docker getting started 教程,官方网站:https://docs.docker.com/linux/s ...