UV 11525, uva11525

Source: Internet
Author: User

UV 11525, uva11525
Ultraviolet A 11525-Permutation

Question Link

Given a k number, the nth number is arranged in full order. Because n is large, the input method is Σ.K1SIPartition (KI)!

Idea: In full arrangement, it is easy to see that the si corresponds to the number k in the array, so the problem is to find the number k at a time, and then remove the number, the tree array is easy to implement.

Code:

#include <cstdio>#include <cstring>#define lowbit(x) (x&(-x))const int N = 50005;int t, k, bit[N];void add(int x, int v) {    while (x <= k) {bit[x] += v;x += lowbit(x);    }}int get(int x) {    int ans = 0, num = 0;    for (int i = 16; i >= 0; i--) {ans += (1<<i);if (ans >= k || bit[ans] + num >= x)    ans -= (1<<i);else num += bit[ans];    }    return ans + 1;}int main() {    scanf("%d", &t);    while (t--) {memset(bit, 0, sizeof(bit));scanf("%d", &k);for (int i = 1; i <= k; i++)    add(i, 1);int num;for (int i = 1; i <= k; i++) {    scanf("%d", &num);    num++;    int v = get(num);    printf("%d%c", v, i == k ? '\n' : ' ');    add(v, -1);}    }    return 0;}





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.