PAT (Advanced Level) 1101. Quick Sort (25)

树状数组+离散化

#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<algorithm>
using namespace std;

const int maxn=100000+10;
int a[maxn],ans[maxn],c[maxn],b[maxn];
int n;
map<int,int>m;

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

void update(int a,int b)
{
    for(int i=a;i<=n;i=i+lowbit(i)) c[i]+=b;
}

int get(int a)
{
    int res=0;
    for(int i=a;i>0;i=i-lowbit(i)) res+=c[i];
    return res;
}

void lsh()
{
    sort(b+1,b+1+n);
    for(int i=1;i<=n;i++) m[b[i]]=i;
}

int main()
{
    scanf("%d",&n);
    int k=0;
    memset(c,0,sizeof c);
    m.clear();
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        b[i]=a[i];
    }
    lsh();
    for(int i=1;i<=n;i++)
    {
        if(m[a[i]]==i)
        {
            int num=get(m[a[i]]-1);
            if(num==m[a[i]]-1) ans[k++]=m[a[i]];
        }
        update(m[a[i]],1);
    }
    sort(ans,ans+k);
    printf("%d\n",k);
    for(int i=0;i<k;i++)
    {
        printf("%d",b[ans[i]]);
        if(i<k-1) printf(" ");
    }
    printf("\n");
    return 0;
}
时间: 2024-10-26 05:01:10

PAT (Advanced Level) 1101. Quick Sort (25)的相关文章

【PAT甲级】1101 Quick Sort (25 分)

题意: 输入一个正整数N(<=1e5),接着输入一行N个各不相同的正整数.输出可以作为快速排序枢纽点的个数并升序输出这些点的值. trick: 测试点2格式错误原因:当答案为0时,需要换行两次??.....这是为何 AAAAAccepted code: 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 int a[100007]; 5 int b[100007]; 6 int

1101. Quick Sort (25)【快排】——PAT (Advanced Level) Practise

题目信息 1101. Quick Sort (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are

1101. Quick Sort (25)

There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are moved to its left and those larger than the pivot to its right. G

PAT甲题题解-1101. Quick Sort (25)-大水题

快速排序有一个特点,就是在排序过程中,我们会从序列找一个pivot,它前面的都小于它,它后面的都大于它.题目给你n个数的序列,让你找出适合这个序列的pivot有多少个并且输出来. 大水题,正循环和倒着循环一次,统计出代码中的minnum和maxnum即可,注意最后一定要输出'\n',不然第三个测试会显示PE,格式错误. #include <iostream> #include <cstdio> #include <algorithm> #include <map&

PAT (Advanced Level) 1083. List Grades (25)

简单排序. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algorithm> using namespace std

PAT (Advanced Level) 1114. Family Property (25)

简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; struct X { int id; int Father,Mother; int k;

PAT (Advanced Level) 1085. Perfect Sequence (25)

可以用双指针(尺取法),也可以枚举起点,二分终点. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; int n; long long k; long l

PAT (Advanced Level) 1071. Speech Patterns (25)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algorithm> using namespace std;

PAT (Advanced Level) 1109. Group Photo (25)

简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<string> #include<algorithm> #include<iostream> using namespace std