Sicily 14514. Bread Sorting,sicily14514

來源:互聯網
上載者:User

Sicily 14514. Bread Sorting,sicily14514

題目

思路
題意是說將一組數排列成另外一組數,排序的方式是每次取出3個連續的的數字,然後abc可以cab,也就整體右移,超出位置的到最左邊。
問能否排列成目標數組。
額,實在是不會做。在網上找到了好幾個人的題解,稍加重寫並比較效能好了。
好像和樹狀數組有關。
如代碼區所示。

代碼
第一種方法,用時0.9s:
by Per Austrin

//Sample solution for the Bread Sorting problem in NCPC 2012 by Per Austrin//Rewrite by Weiping Huang//0.9s#include <stdio.h>#include <algorithm>using namespace std;int n;char text[600005];int A[100005], B[100005], C[100005];void Readin(int * num) {    gets(text);    int sum = 0, k = 0;    for (int i = 0; text[i] != '\0'; i++) {        if (text[i] == ' ') {            num[k++] = sum;            sum = 0;        }        else {            sum = 10 * sum + (text[i] & 15);        }    }    num[k] = sum;    //for (int i = 0; i < n; i++) scanf("%d", num + i);}int inversions(int n, int * N) {    if (n < 2) return 0;    int m = n / 2;    int r = inversions(m, N) + inversions(n - m, N + m);    int x = 0, y = m;    while (x < m && y < n) {        if (N[y] < N[x]) {            r += m - x;            y++;        }        else x++;    }    sort(N, N + n);    return r;}int main() {    while (~scanf("%d\n", &n)) {        Readin(A);        Readin(B);        for (int i = 0; i < n; i++) C[B[i]] = i;        for (int i = 0; i < n; i++) A[i] = C[A[i]];        printf("%sossible\n", inversions(n, A) & 1 ? "Imp" : "P");    }    return 0;}

第二種方法,用時0.06s:
By Lukáš Poláček (lukasP)

//Sample solution for the Bread Sorting problem in NCPC 2012 by Lukáš Poláček (lukasP)//Rewrite by Weiping Huang//0.06s#include <stdio.h>#include <string.h>int n;char text[600005];bool seen[100005];int A[100005], B[100005];void ReadinA(int * num) {    gets(text);    int sum = 0, k = 0;    for (int i = 0; text[i] != '\0'; i++) {        if (text[i] == ' ') {            num[k++] = sum;            sum = 0;        }        else {            sum = 10 * sum + (text[i] & 15);        }    }    num[k] = sum;    //for (int i = 0; i < n; i++) scanf("%d", num + i);}void ReadinB(int * num) {    gets(text);    int sum = 0, k = 0;    for (int i = 0; text[i] != '\0'; i++) {        if (text[i] == ' ') {            num[A[k++] - 1] = sum;            sum = 0;        }        else {            sum = 10 * sum + (text[i] & 15);        }    }    num[A[k] - 1] = sum;    //for (int i = 0; i < n; i++) scanf("%d", num + i);}int main() {    while (~scanf("%d\n", &n)) {        ReadinA(A);        ReadinB(B);        memset(seen, false, sizeof(bool) * n);        int swaps = n;        for (int i = 0; i < n; i++) {            if (!seen[i]) {                swaps--;                for (int j = i; !seen[j]; j = B[j] - 1) seen[j] = true;            }        }        printf("%sossible\n", swaps & 1 ? "Imp" : "P");    }    return 0;}

第三種方法,用時0.51s:
By Lukáš Poláček (lukasP)

//Sample solution for the Bread Sorting problem in NCPC 2012 by Lukáš Poláček (lukasP)//Rewrite by Weiping Huang//0.51s#include <stdio.h>#include <string.h>#include <math.h>int n;char text[600005];int A[100005], B[100005];struct Buckets {    int n, b_size;    int s[100005];    bool t[100005];    void clear(int n) {        b_size = int(sqrt((double)n) + 1.1);        for (int i = (n + b_size - 1) / b_size - 1; i >= 0; i--) s[i] = 0;        for (int i = n - 1; i >= 0; i--) t[i] = false;    }    void update(int pos) {        t[pos] = true;        s[pos / b_size]++;    }    int query(int val) {        int b_ind = val / b_size;        int res = 0;        for (int i = 0; i < b_ind; i++) res += s[i];        for (int i = b_ind * b_size; i < val; i++) res += t[i];        return res;    }}s, t;void Readin(int * num) {    gets(text);    int sum = 0, k = 0;    for (int i = 0; text[i] != '\0'; i++) {        if (text[i] == ' ') {            num[k++] = sum - 1;            sum = 0;        }        else {            sum = 10 * sum + (text[i] & 15);        }    }    num[k] = sum - 1;    //for (int i = 0; i < n; i++) scanf("%d", num + i);}int main() {    while (~scanf("%d\n", &n)) {        long long ia = 0, ib = 0;        s.clear(n);        t.clear(n);        memset(A, 0, sizeof(int) * n);        memset(B, 0, sizeof(int) * n);        Readin(A);        for (int i = 0; i < n; i++) {        ia += i - s.query(A[i]);        s.update(A[i]);        }        Readin(B);        for (int i = 0; i < n; i++) {        ib += i - t.query(B[i]);        t.update(B[i]);        }        printf("%sossible\n", ((ia & 1) != (ib & 1)) ? "Imp" : "P");    }    return 0;}

第四種方法,用時0.33s:
By Andreas Björklund

//Sample solution for the Bread Sorting problem in NCPC 2012 by Andreas Björklund//Rewrite by Weiping Huang//0.33s#include <stdio.h>#include <string.h>#include <math.h>int n, tpn;char text[600005];int A[1 << 17], B[1 << 17];int sum1, sum2;int count(int* T, int who){    int ix = who;    int cnt = 0;    while (ix <= tpn) {        int inc = ix & -ix;        cnt ^= (ix <= who) ? T[ix - 1] : 0;        T[ix - 1] ^= (ix >= who);        ix ^= inc;        ix |= (inc << 1);    }    return cnt & 1;}void ReadinAndCount() {    sum1 = sum2 = 0;    gets(text);    int sum = 0;    for (int i = 0; text[i] != '\0';) {        if (text[i] == ' ') {            i++;            while (text[i] == ' ') i++;            sum1 ^= count(A, sum);            sum = 0;        }        else {            sum = 10 * sum + (text[i] & 15);            i++;        }    }    sum1 ^= count(A, sum);    sum = 0;    gets(text);    for (int i = 0; text[i] != '\0';) {        if (text[i] == ' ') {            i++;            while (text[i] == ' ') i++;            sum2 ^= count(B, sum);            sum = 0;        }        else {            sum = 10 * sum + (text[i] & 15);            i++;        }    }    sum2 ^= count(B, sum);    sum = 0;}int main() {    while (~scanf("%d\n", &n)) {        tpn = 1;        while (tpn < n) tpn <<= 1;        for (int i = 0; i < n; i++) A[i] = B[i] = 0;        ReadinAndCount();        printf("%sossible\n", sum1 != sum2 ? "Imp" : "P");    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.