#include <stdio.h> #include <stdlib.h>
//快速排序法 void quick_sort(int* a, int* b, int low, int high) { if (low >= high) { return; }//递归要有一个条件使它停下来。
//使数组按右边的大小从上到下依次增大
int left = low;
int right = high;
int temp = rand() % (high - low) + low;//没有此步会运行超时,此步可理解为
//rand随机生成一个数后面膜上(high - low) 是使随机值不会超过high和low直接
//的个数后面再加上low是使temp指向low后面的数,因为后面的low会变,会分为
//两个半区 加上low可是它指向基准值(快速排顺的特点先随便从数组中选一个数,排序完左边的数小于基准值,右边的数大于基准值)后面的数
int pivotb = *(b + temp);
int pivota = *(a + temp);
// *(b + temp) = pivotb;
// *(a + temp) = pivota;
a[temp] = a[low];
b[temp] = b[low];
while (right > left)
{
while (right > left && *(b + right) >= pivotb)
{
right--;应为函数为void型所以直接加;,
}
if (right > left)
{
*(b + left) = *(b + right);
*(a + left) = *(a + right);
}
while (right > left && *(b + left) <= pivotb)
{
left++;
}
if (right > left)
{
*(b + right) = *(b + left);
*(a + right) = *(a + left);
}
if (left >= right)
{
*(b + left) = pivotb;
*(a + left) = pivota;
}
}
// if (left - 1 > low)
// {
// quick_sort(a, b, low, left - 1);//递归思想
// }
// //对基准值右边的所有数据再次进行快速查找(递归)
// if (left + 1 < high)
// {
// quick_sort(a, b, left + 1, high);
// }
quick_sort(a, b, right + 1, high);递归思想
quick_sort(a, b, low, right - 1);
}
int main() { int num; scanf("%d", &num); int raws[num]; int rawf[num]; for (int i = 0; i < num; i++) { scanf("%d %d", &raws[i], &rawf[i]); } //调用-快排 quick_sort(raws, rawf, 0, num - 1);//数组,0 ,num-1 /*for (int i = 0; i < 8; i++) { printf("\n%d %d\n", raws[i], rawf[i]); printf("");
}*/
//选择数据
int curend = rawf[0];//贪心
int count = 1;如果后一个数的左边大于前一个数的右边就count++因为符合条件
for (int k = 1; k < num; k++) {
if (raws[k] >= curend) {
curend = rawf[k];
count++;
}
}
printf("%d", count);
return 0;
}
标签:right,int,high,快排,算法,num,low,贪心,left From: https://blog.51cto.com/u_16220477/8204504