Sicily 14514. Bread Sorting



题目



思路

题意是说将一组数排列成另外一组数,排序的方式是每次取出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;
}
时间: 2024-10-16 13:24:44

Sicily 14514. Bread Sorting的相关文章

Sicily:1351.Multi-key Sorting

Sicily : 1351. Multi-key Sorting Sicily上的这道题,我刚开始做以为是要除去一个序列中连续且重复的片段,但后来发现 去重之后的序列依然可能不是最短的,比如说: 序列:{ 1.2.3.1.2.1.2 } 这个序列,消去连续且重复的片段之后变为: { 1.2.3.1.2 } 而实际上,序列:3.1.2才是真正的最短序列. 在此,我先给出本题的解法:从序列的最后一个开始遍历整个序列,将重复遍历的元素删去,最后剩下的序列就是最短序列. 那么,为什么这样的方法是可行的呢

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

(转)sicily题目分类

Sicily题目分类 ·         [数据结构/图论] 1310 Right-Heavy Tree   笛卡尔树相关,复杂度O(N)或O(NlogN). ·1426 Phone List         电话号码前缀检索,trie树相关. ·1443 Printer Queue      基本队列操作. ·1149 等价表达式         判断表达式是否等价(递归求解) ·1136 山海经             n长序列里求m次区间询问的最大连续子区间和.线段树/RMQ ·1252

HDU 5122 K.Bro Sorting(模拟——思维题详解)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5122 Problem Description Matt's friend K.Bro is an ACMer. Yesterday, K.Bro learnt an algorithm: Bubble sort. Bubble sort will compare each pair of adjacent items and swap them if they are in the wrong o

CodeForces - 844C Sorting by Subsequences (排序+思维)

You are given a sequence a1,?a2,?...,?an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also wil

U3D sorting layer, sort order, order in layer, layer深入辨析

1,layer是对游戏中所有物体的分类别划分,如UIlayer, waterlayer, 3DModelLayer, smallAssetsLayer, effectLayer等.将不同类的物体划分到不同的层,便于相机拣选,在相机的cullmask中可以选择渲染哪些层,不选择的层则不会渲染.还可以用于射线检测对象的拣选,可以指定只对某些层的对象进行射线检测. 2,canvas默认是屏幕空间的2D对象,在屏幕空间时仅具有sort order属性,当把它设置为世界空间时,sort order属性消失

Hdoj 5195 DZY Loves Topological Sorting 【拓扑】+【线段树】

DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 922 Accepted Submission(s): 269 Problem Description A topological sort or topological ordering of a directed graph i

hdu 2838 Cow Sorting 树状数组

hdu2838 ------希望30号驾校科目一顺利考完,4月即将过去说好的30篇博客也没完成, 真是忙起来就会烦躁什么都不想做,勿忘心安.... <Cow Sorting> 这题本来兴高采烈的想用java做一遍,结果做出来之后无限超内存,真是啊,做题的时候java这种东西还是轻易不要动了.还有感觉要把数字都要离散化的,结果后台数据不需要离散化. 题意:给一个n代表n个牛,然后再给n个数我觉得是n以内的数(包括n).虽然体面上没说.然后只能相邻两个数字交换位置,会让牛产生怒气值,值为互相移动的

K.Bro Sorting(杭电5122)(2014ACM/ICPC亚洲区北京站)

K.Bro Sorting Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 67    Accepted Submission(s): 39 Problem Description Matt's friend K.Bro is an ACMer. Yesterday, K.Bro learnt an algorithm: Bubbl