Each of Farmer John's N (1 <= N <= 50,000) cows likes to graze in a certain part of the pasture, which can be thought of as a large one-dimeensional number line. Cow i's favorite grazing range starts at location Si and ends at location Ei (1 <= Si < Ei; Si < Ei <= 100,000,000).
Most folks know the cows are quite selfish; no cow wants to share any of its grazing area with another. Thus, two cows i and j can only graze at the same time if either Si >= Ej or Ei <= Sj. FJ would like to know the maximum number of cows that can graze at the same time for a given set of cows and their preferences.
Consider a set of 5 cows with ranges shown below:
... 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
... |----|----|----|----|----|----|----|----|----|----|----|----|----
Cow 1: <===:===> : : :
Cow 2: <========:==============:==============:=============>:
Cow 3: : <====> : : :
Cow 4: : : <========:===> :
Cow 5: : : <==> : :
These ranges represent (2, 4), (1, 12), (4, 5), (7, 10), and (7, 8), respectively.
For a solution, the first, third, and fourth (or fifth) cows can all graze at the same time. If the second cow grazed, no other cows could graze. Also, the fourth and fifth cows cannot graze together, so it is impossible for four or more cows to graze.
输入描述:
* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains the two space-separated integers: Si and Ei
输出描述:
* Line 1: A single integer representing the maximum number of cows that can graze at once.
Line 1: A single integer, N(2 <= N <= 32000)
Lines 2..N+1: Each line contains two space-separated positive integers that describe a leash. The first is the location of the leash's stake; the second is the length of the leash.(1 <= length <= 1e7)
输出描述:
Line 1: A single integer that is the minimum number of cuts so that each leash is cut at least once.
/*
解题思路:
1. 题目描述已给出边界条件
2. 题目描述得将数据进行有序化后相加比较就可
3. 有序化后使用两个指针从两头同步提取数据相加比较
4. 根据题目要求记录满足条件组合数
5. 注意数据输出格式
*/
# include <cstdio>
# include <cstring>
# include <iostream>
# include <algorithm>
using namespace std;
const int N = 30010;
// quick_sort
void quick_sort (int num[], int low, int high) {
int i, j, temp;
int tmp = num[low];
i = low;
j = high;
if (i > j) {
return;
}
while (i != j) {
while (num[j] >= tmp && j > i) {
j --;
}
while (num[i] <= tmp && j > i) {
i ++;
}
if (j > i) {
temp = num[j];
num[j] = num[i];
num[i] = temp;
}
}
num[low] = num[i];
num[i] = tmp;
quick_sort(num, low, i-1);
quick_sort(num, j+1, high);
}
int main () {
int n, max;
int num[N];
scanf("%d", &max);
scanf("%d", &n);
for (int i = 0 ; i < n ; i ++) {
scanf("%d", &num[i]);
}
quick_sort(num, 0, n-1);
int left = 0;
int right = n-1;
int cnt = 0;
while (left <= right) {
if (num[left] + num[right] <= max) {
left ++;
right --;
cnt ++;
} else {
right --;
cnt ++;
}
}
printf ("%d", cnt);
return 0;
}