UVA 11525 Permutation

Permutation

https://ac.nowcoder.com/acm/problem/117139

Permutation

Permutation - UVA 11525 - Virtual Judge (vjudge.net)

题目描述

给定整数n和k输出1~k的所有排列中,按照字典序从小到大排序后的第n个

n可能很大,本题用k个整数来间接给出n方式如下

输出满足条件的1~k的排列

样例

4
3
2 1 0
3
1 0 0
4
2 1 1 0
4
1 2 1 0
3 2 1
2 1 3
3 2 4 1
2 4 3 1

算法1

(树状数组倍增求k小数 + 康托展开)
前导
  1. 康托展开:

    给定的询问形式就是一个康托展开(参考文献)

    问题就是从康托展开中还原原序列

  2. 树状数组的结构

    1. 对于节点i,如果它是左子节点,那么父节点的编号就是i + lowbit(i);如果他是右子节点,那么父节点的编号就是i - lowbit(i)
    2. 节点保存着以结尾的一段连续区间和
    3. 树状数组每一层都是一个长度为的区间

分析:
  • 本题的核心思想就是用树状数组维护一个数组
  • 表示数字还未被使用
  • 表示数字已经被使用
  • 假设进行完x操作,剩下的数字个数为k - x
  • 在目前数字集合中的排名就是
  • 这个值我们可以用树状数组维护

倍增 :
  • 本题的问题不是求一个的排名是多少而是给定一个排名求在当前集合中这个排名的数字是多少

  • 由于树状数组是一个树的结构

  • 节点保存着以结尾的一段连续区间和

  • 树状数组每一层都是一个长度为的区间

  • 树根覆盖的区间最长,长度为

  • 我们对这棵树进行搜索

    int up = log(n) / log(2);
    int k = 0,tot = 0;
    for(int i = up;i >= 0;i --)
    {
        if((1 << i) + k <= n && tot + tr[k + (1 << i)] <= x)
        //如果tot + tr[k + (1 << i)] <= x走到右子树,否则走到左子树
        {
            tot += tr[k + (1 << i)];
            k += (1 << i);//走到右子树
        }
    }

时间复杂度

参考文献

UVA11525 Permutation[康托展開 樹狀數組求第k小值] - 开发者知识库 (itdaan.com)

UVA11525 Permutation - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

C++ 代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
// #include <unordered_map>
#include <vector>
#include <queue>
#include <set>
#include <bitset>
#include <cmath>
#include <map>

#define x first
#define y second

#define P 131

#define lc u << 1
#define rc u << 1 | 1

using namespace std;
typedef long long LL;
const int N = 50010;
int tr[N];
int s[N];
int n;

int lowbit(int x)
{
    return x & -x;
}

void add(int x,int k)
{
    // cout << k << endl;
    for(int i = x;i <= n;i += lowbit(i)) tr[i] += k;
}

int sum(int x)
{
    int res = 0;
    for(int i = x;i;i -= lowbit(i)) res += tr[i];
    return res;
}

int query(int x)
{
    int up = log(n) / log(2);
    int k = 0,tot = 0;
    for(int i = up;i >= 0;i --)
    {
        if((1 << i) + k <= n && tot + tr[k + (1 << i)] <= x)
        {
            tot += tr[k + (1 << i)];
            k += (1 << i);
        }
    }
    return k + 1;
}

void solve()
{
    scanf("%d",&n);
    for(int i = 1;i <= n;i ++) add(i,1);
    for(int i = 1;i <= n;i ++) scanf("%d",&s[i]);
    for(int i = 1;i <= n;i ++)
    {
        int t = query(s[i]);
        printf("%d%c",t," \n"[i == n]);
        add(t,-1);
    }
} 

int main()
{
    int _ = 1;

    // freopen("network.in","r",stdin);
    // freopen("network.out","w",stdout);
    // init(N - 1); 

    // std::ios_base::sync_with_stdio(0);
    // cin.tie(0);
    // cin >> _;

    scanf("%d",&_);
    while(_ --)
    {
        // scanf("%lld%lld",&n,&m);
        solve();
        // test();
    }
    return 0;
}

注:还有一种二分 + 树状数组的做法但是时间复杂度是


算法2

(线段树求k小数 + 康托展开)
  • 维护上述的
  • 如果递归到左子树
  • 递归到右子树同时

时间复杂度

参考文献

C++ 代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
// #include <unordered_map>
#include <vector>
#include <queue>
#include <set>
#include <bitset>
#include <cmath>
#include <map>

#define x first
#define y second

#define P 131

#define lc u << 1
#define rc u << 1 | 1

using namespace std;
typedef long long LL;
const int N = 50010;
struct Node
{
    int l,r;
    int sum;
}tr[N * 4];
int n;

void pushup(int u)
{
    tr[u].sum = tr[lc].sum + tr[rc].sum;
}

inline void build(int u,int l,int r)
{
    if(l == r)
    {
        tr[u] = Node({l,r,1});
        return;
    }
    int mid = (l + r) >> 1;
    tr[u] = Node({l,r});
    build(lc,l,mid);
    build(rc,mid + 1,r);
    pushup(u);
}

void modify(int u,int x,int k)
{
    if(tr[u].l == x && tr[u].r == x)
    {
        tr[u].sum = k;
        return;
    }
    int mid = (tr[u].l + tr[u].r) >> 1;
    if(x <= mid) modify(lc,x,k);
    else modify(rc,x,k);
    pushup(u);
}

int query(int u,int k)
{
    if(tr[u].l == tr[u].r)  return tr[u].l;
    int mid = (tr[u].l + tr[u].r) >> 1;
    if(tr[lc].sum >= k) return query(lc,k);
    else return query(rc,k - tr[lc].sum);
}

void solve()
{
    scanf("%d",&n);
    build(1,1,n);
    for(int i = 1;i <= n;i ++)
    {
        int s;
        scanf("%d",&s);
        int t = query(1,s + 1);
        printf("%d%c",t," \n"[i == n]);
        modify(1,t,0);
    }
} 

int main()
{
    int _ = 1;

    // freopen("network.in","r",stdin);
    // freopen("network.out","w",stdout);
    // init(N - 1); 

    // std::ios_base::sync_with_stdio(0);
    // cin.tie(0);
    // cin >> _;

    scanf("%d",&_);
    while(_ --)
    {
        // scanf("%lld%lld",&n,&m);
        solve();
        // test();
    }
    return 0;
}
全部评论

相关推荐

头像
10-22 20:13
中南大学 Java
序言大家好呀。我是希晨er,一个初入职场的程序猿小登最近上班摸鱼刷到了一篇文章:10年深漂,放弃高薪,回长沙一年有感,还有聊聊30岁大龄程序员过往的心路历程,突然就有点感慨。我如今也做出了和大明哥一样的抉择,只是更早。此外我22年的人生,好像从来没好好记录过。正好现在工作不太忙,就想把这些经历写下来,也希望能得到社区里各位前辈的指点个人背景我是03年出生的西安娃,父母都是普通打工人。刚从中南大学软件工程专业毕业半年,现在在老家的央企过着躺平摆烂的日子成长轨迹从农村到城市的童年我家并不是西安的,只是爸妈在西安上班,从小学之后就把我接到了西安。后来老家房子拆了,爷爷奶奶也搬了过来。农村的生活,我觉...
Yki_:看哭了,恋爱那一段你女朋友说你不够关心她,可你毕竟也愿意遇到矛盾写几千字来和她慢慢分析;说不愿意给她花钱,我感觉可能只是消费观不一样;如果她想留在长沙,也应该提前跟你说开。不过她也许会心疼你放弃大厂offer转向数字马力?我也因为同样的原因有过一段幸福而充满遗憾的感情,不过跟爱情相比确实前途更重要一点。至于offer的选择,换我我也会这么选。把这些旧事记录下来以后,接下来就好好向前看吧,加油兄弟
🍊晨光随笔
点赞 评论 收藏
分享
09-01 11:31
门头沟学院 Java
buul:七牛云的吧,感觉想法是好的,但是大家没那么多时间弄他这个啊。。。不知道的还以为他是顶尖大厂呢还搞比赛抢hc,只能说应试者的痛苦考察方是无法理解的,他们只会想一出是一出
点赞 评论 收藏
分享
09-17 10:53
四川大学 C++
牛客91242815...:会写标书没有任何卵用,鉴定为横向垃圾导师的受害者
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务