#include<iostream>
using namespace std;
void quickSort(int a[], int b[], int start, int end) {
if (start >= end)
return;
int mid = a[end];
int left = start, right = end - 1;
while (left < right) {
while (a[left] < mid && left < right)
left++;
while (a[right] >= mid && left < right)
right--;
swap(a[left], a[right]);
swap(b[left], b[right]);
}
if (a[left] >= a[end])
{
swap(a[left], a[end]);
swap(b[left], b[end]);
}
else
left++;
quickSort(a, b, start, left - 1);
quickSort(a, b, left + 1, end);
}
int main()
{
int n;
int One = 0;
int zero = 0;
int number = 0;
int max = 0;
int temp = 0;
cin >> n;
int* a, * b, * c, * d, * e;
a = new int[n];
b = new int[n];
c = new int[n];
d = new int[n];
for (int i = 0; i < n; i++)
{
cin >> a[i] >> b[i];
c[i] = 0; d[i] = 0;
}
quickSort(a, b, 0, n - 1);
for (int i = 0; i < n; i++)
if (b[i] == 1)
One++;
for (int i = 0; i < n; i++)
{
if (b[i] == 0)
{
c[i] = zero + One;
zero++;
if (i > 0 && a[i] == a[i - 1])
c[i] = c[i - 1];
}
else
{
c[i] = One + zero;
One--;
if (i > 0 && a[i] == a[i - 1])
c[i] = c[i - 1];
}
}
for (int i = 0; i < n; i++)
{
if (max < c[i])
max = c[i];
}
for (int i = 0; i < n; i++)
{
if (c[i] == max)
{
d[number] = i;
number++;
}
}
max = a[d[0]];
for (int i = 0; i < number; i++)
{
if (max < a[d[i]])
max = a[d[i]];
}
cout << max << endl;
delete[]a;
delete[]b;
delete[]c;
delete[]d;
return 0;
}
刚开始用的是冒泡排序,结果超时了,后来改成快速排序才通过。 先创建四个数组a,b,c,d,其中a数组存储y值,b数组存储result值,c,d数组初始化为0,然后用快速排序,将a数组从小到大排序(b数组亦然),然后发现每个c的值与当前b[i]值前面的0的个数以及b[i]之后1的个数的有关,且这两个数按规律变化,故可以利用这两个值快速计算。
|