标签:
1 #include<iostream> 2 using namespace std; 3 int ans=0;//逆序对。 4 int n; 5 int a[100010],c[100010]; 6 void work(int l,int r) 7 { 8 int mid=(l+r)/2; 9 if(r>l+1) 10 { 11 work(l,mid-1); 12 work(mid,r); 13 int tmp=1,i,j; 14 for(i=1,j=mid;i<mid &&j<=r; ) 15 { 16 if(a[i]>a[j]) 17 { 18 c[tmp++]=a[j++]; 19 ans+=mid-i;//找逆序对. 20 } 21 else 22 c[tmp++]=a[i++]; 23 } 24 if(j<=r) 25 for(;j<=r;j++) c[tmp++]=a[j]; 26 else 27 for(;i<mid;i++) c[tmp++]=a[i]; 28 for(int i=l;i<=r;i++) a[i]=c[i]; 29 } 30 else 31 { 32 if(l+1==r) 33 if(a[l]>a[r]) 34 { swap(a[l],a[r]); ans++;} 35 } 36 } 37 int main() 38 { 39 cin>>n; 40 work(1,n); 41 cout<<ans<<endl; 42 return 0; 43 }
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 }
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 }
标签:
原文地址:http://www.cnblogs.com/Kaike/p/5905753.html