#include <bits/stdc++.h> using namespace std; int k,n,i,ile; void QuickSort(int left,int right,int *tab) { long long i=left, j=right; int sr=tab[(left+right)/2]; while(i<=j) { while(tab[i]<sr) i++; while(tab[j]>sr) j--; if(i<=j) { swap(tab[i],tab[j]); i++; j--; } } if (left<j) QuickSort(left,j,tab); if (i<right) QuickSort(i,right,tab); } int main() { cin>>n>>k; int tab[n]; for(i=0;i<n;i++) { cin>>tab[i]; } QuickSort(0,n-1,tab); i=n-k-1; ile=k; while(tab[n-k]==tab[i]) { ile++; i--; } cout<<ile; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include <bits/stdc++.h> using namespace std; int k,n,i,ile; void QuickSort(int left,int right,int *tab) { long long i=left, j=right; int sr=tab[(left+right)/2]; while(i<=j) { while(tab[i]<sr) i++; while(tab[j]>sr) j--; if(i<=j) { swap(tab[i],tab[j]); i++; j--; } } if (left<j) QuickSort(left,j,tab); if (i<right) QuickSort(i,right,tab); } int main() { cin>>n>>k; int tab[n]; for(i=0;i<n;i++) { cin>>tab[i]; } QuickSort(0,n-1,tab); i=n-k-1; ile=k; while(tab[n-k]==tab[i]) { ile++; i--; } cout<<ile; return 0; } |