//n个数m个询问小于等于x的数 #include<iostream> #include<cstdio> using namespace std; int a[100]; int n,m; int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&a[i]); a[0]=-1; for(int i=1;i<=m;i++) { int x,left=1,right=n,mid; scanf("%d",&x); while(left<=right) { mid=(left+right)/2; if(a[mid]<=x)left=mid+1; else right=mid-1; } printf("%d ",a[right]); } return 0; }
//快排 #include<iostream> #include<cstdio> using namespace std; int n,a[100]; void qsort(int l,int r) { int i=l,j=r,mid=a[(l+r)/2]; while(i<=j) { while(a[i]<mid)i++; while(a[j]>mid)j--; if(i<=j)swap(a[i],a[j]),i++,j--; } if(l<j)qsort(l,j); if(r>i)qsort(i,r); } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); qsort(1,n); for(int i=1;i<=n;i++) printf("%d ",a[i]); return 0; }
//归并排序求逆序对 #include<iostream> #include<cstdio> using namespace std; int n,tot,a[100],temp[100]; void merge_sort(int left,int right) { if(left==right)return; int mid=(left+right)/2; merge_sort(left,mid); merge_sort(mid+1,right); int p=left,i=left,j=mid+1; while(i<=mid&&j<=right) { if(a[i]>a[j]) { tot=tot+mid-i+1;temp[p++]=a[j++]; } else temp[p++]=a[i++]; } while(i<=mid)temp[p++]=a[i++]; while(j<=right)temp[p++]=a[j++]; for(int i=left;i<=right;i++)a[i]=temp[i]; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); merge_sort(1,n); cout<<tot<<endl; for(int i=1;i<=n;i++) printf("%d ",a[i]); return 0; }