- 归并排序<可求逆序对>
1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 long long n, a[1000100], c[1000100]; 5 long long ans=0; 6 void merge(int l, int r) 7 { 8 if(l==r) return ; 9 int mid = (l + r) / 2; 10 merge(l,mid); 11 merge(mid+1,r); 12 int i = l, j = mid + 1, k = 1; 13 while (i <= mid && j <= r) 14 { 15 if (a[i] <= a[j]) 16 c[k++] = a[i++]; 17 else 18 { c[k++] = a[j++]; 19 ans+=mid-i+1; 20 } 21 } 22 while (i <= mid) c[k++] = a[i++]; 23 while (j <= r) c[k++] = a[j++]; 24 for (i = l, k = 1; i <= r; i++) 25 a[i] = c[k++]; 26 27 } 28 int main() 29 { 30 cin >> n; 31 for (int i = 1; i <= n; i++) 32 cin >> a[i]; 33 merge(1,n); 34 cout<<ans<<endl; 35 return 0; 36 }
- 快速幂
1 #include<iostream> 2 using namespace std; 3 //求b^P 4 int b,p; 5 void work() 6 { 7 long long ans=1; 8 int t=p,s=b; 9 while(t!=0) 10 { 11 if(t%2==1) ans=ans*s; 12 t/=2; 13 s=s*s; 14 } 15 } 16 int main() 17 { 18 cin>>b>>p; 19 work(); 20 cout<<ans<<endl; 21 return 0; 22 }
- 二分
1 #include<iostream> 2 using namespace std; 3 int a[10010],x; 4 int find(int x) 5 { 6 int l=1,r=n; 7 while(l+1<r) 8 { 9 int mid=(l+r)/2; 10 if(a[mid]==x) return mid; 11 if(a[mid]>x) r=mid; 12 if(a[mid]<x) l=mid; 13 } 14 if(a[l]==x) return l; 15 if(a[r]==x) return r; 16 return -1; 17 } 18 int main() 19 { 20 cin>>x; 21 cout<<find(x)<<endl; 22 return 0; 23 }
- quicksort
1 /* 2 Language:c++; 3 By Kaike; 4 Date:2016.09.25; 5 */ 6 #include<iostream> 7 using namespace std; 8 int n,a[10010]; 9 void quicksort(int l,int r) 10 { 11 if(l>r) return ; 12 int t=a[l]; 13 int i=l,j=r; 14 while(i!=j) 15 { 16 while(a[j]>=temp && i<j) 17 j--; 18 while(a[i]<=temp && i<j) 19 i++; 20 if(i<j) 21 swap(a[i],a[j]); 22 } 23 a[l]=a[i]; 24 a[i]=temp; 25 quicksort(l,i-1); 26 wuicksort(i+1,right); 27 return ; 28 } 29 int main() 30 { 31 cin>>n; 32 for(int i=1;i<=n;i++) 33 cin>>a[i]; 34 quicksort(1,n); 35 for(int i=1;i<=n;i++) 36 cout<<a[i]; 37 return 0; 38 }