标签:while code sort bin const return 二分查找 ORC 排序
题目链接:http://codeforces.com/problemset/problem/1007/A
题目大意:就是给出长度为n的序列(a[i]),让你对它进行重新排序(b[i]),使b[i]>a[i] 的数目最多
思路:
二分就是了,找到第一个大于key的就是最完美的选择(但是要注意如果选过的不能在选) 算是二分查找的一种变形吧
AC代码:
1 #include <cstdio> 2 #include <string> 3 #include <iostream> 4 #include <algorithm> 5 #include <cstdbool> 6 #include <string.h> 7 #include <math.h> 8 9 10 using namespace std; 11 12 const int maxn = 1e5+5; 13 int a[maxn],b[maxn]; 14 int vis[maxn]= {0}; 15 16 int binarySearch(int b[],int vis[],int n,int key) 17 { 18 int left = 0,right = n-1; 19 while (left <= right) 20 { 21 int mid = (right + left) / 2; 22 if (b[mid] > key) 23 right = mid - 1; 24 else 25 left = mid + 1; 26 } 27 while (vis[left]!=0 && b[left] > key) 28 left++; 29 if (b[left] <= key) 30 return -1; 31 else{ 32 vis[left]++; 33 return left; 34 } 35 } 36 37 38 39 int main() 40 { 41 int n,cnt = 0; 42 cin >> n; 43 for (int i=0;i<n;i++) 44 { 45 cin >> a[i]; 46 b[i] = a[i]; 47 } 48 sort(b,b+n); 49 for (int i=0;i<n;i++) 50 { 51 int ant = binarySearch(b,vis,n,a[i]); 52 if (ant >= 0) 53 cnt++; 54 } 55 printf("%d\n",cnt); 56 return 0; 57 }
标签:while code sort bin const return 二分查找 ORC 排序
原文地址:https://www.cnblogs.com/-Ackerman/p/11169162.html