Chef Sereja and Sorting 2
来源:互联网 发布:2017淘宝网红店铺排名 编辑:程序博客网 时间:2024/06/04 19:42
我的想法是:先排序,从开头到结尾不断找到对应位置的值,交换[l,r]。那么l位置就是已经排好序的了。只有0.67分
优化了下find() 0.725
#include<cstdio>#include<algorithm>using namespace std;const int N=10002;int n,a[N],b[N],left[N],right[N];int find(int b,int val){if(a[b]==val) return b;for(int i=n;i>b;i--)if(a[i]==val)return i;}void swap(int l,int r){while(l<r){int t=a[l];a[l]=a[r];a[r]=t;l++,r--;}}int main(){while(~scanf("%d",&n)){for(int i=1;i<=n;i++){scanf("%d",a+i);b[i]=a[i];}sort(b+1,b+n+1);int cnt=0,l=1,r=1;for(int i=1;i<=n;i++){r=find(l,b[i]);if(r>l){left[cnt]=l;right[cnt]=r;swap(l,r);cnt++;}l++;}printf("%d\n",cnt);for(int i=0;i<cnt;i++)printf("%d %d\n",left[i],right[i]);}return 0;}
0 0
- Chef Sereja and Sorting 2
- codechef Chef and easy problem 2
- codechef Chef and Rainbow Array - 2
- 知识点总结2 External Sorting, Bucket Sorting, and Radix Sorting
- Codeforces Round #223 (Div. 2)A. Sereja and Dima&&B. Sereja and Stairs
- CodeChef Chef and Segments
- Chef and Digits
- codechef Chef and sequence
- codechef Chef and Swaps
- Codechef Chef and Reversing
- Codechef Chef and Frogs
- Meal WaitPerson and Chef
- Codechef Chef and Churu
- 【CodeChef】 Chef and Stones
- 【CodeChef】Chef and Interview
- codechef Chef and Churu
- [CC]Chef and Churu
- B. Sereja and Array
- (32)ExtJS之Panel中Absolute绝对位置布局
- tomcat的三个效率问题
- (33)ExtJS之Panel中的表格布局
- 转:土匪的2013书单推荐
- 电平触发方式注意
- Chef Sereja and Sorting 2
- Python 词法分析 识别数字
- (34)ExtJS之Panel中的标签页,通过items添加标签页示例
- Kafka Producer端自定义消息
- 数据结构复习笔记(一)
- 实用技巧:在Linux系统下享受IPv6的畅快
- 熟悉HTML
- margin和padding 的区别
- 黑马程序员----多线程2