PAT (Advanced Level) 1101. Quick Sort (25)
2016-07-05 08:41
351 查看
树状数组+离散化
#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; }
相关文章推荐
- php中使用include(require)嵌套包含文件时的一些注意问题
- PAT (Advanced Level) 1099. Build A Binary Search Tree (30)
- Shortest Distance from All Buildings
- 源码推荐(0705):简仿映客直播App,UITableView右侧索引
- geeksforgeeks@ Sorting Elements of an Array by Frequency (Sort)
- Longest Consecutive Sequence
- HTML标签marquee实现滚动效果
- APUE(5)---标准I/O库 (3)
- JS与HTML结合使用marquee标签实现无缝滚动效果代码
- UIScrollView截取内部内容存入相册
- UIPickerView
- iOS第三方左对齐布局类——UICollectionViewLeftAlignedLayout
- 使用Storyboard时出现以下警告:Scene is unreachable due to lack of entry points and does not have an identifier
- sqlite错误处理:library routine called out of sequence
- 第三十四篇:在SOUI中使用异步通知
- Android系统应用---SystemUI之二:Statusbar显示流程以及系统状态图标更新分析
- 微软极品工具箱-Sysinternals Suite
- UIScrollView增加刷新
- CDMA系统的UIM卡介绍 ,IMSI,ESN,MDN
- 消息队列(Message Queue)简介及其使用