There are C classmates, each student has the score and the Financial Aid 2 attribute, now wants you to elect the n person, satisfies their financial aid sum is less than equals F and the median number is as large as possible. I used 2 priority queues, first in accordance with the financial aid from small to large sort, put the previous N people into the priority queue, and then pop the former N/2 personal, and put N/2 individuals in another priority queue, this priority queue is the largest heap, the first team is the largest financial aid, and then from the rest of the people to enumerate, Judging if the score is greater than the first priority queue first person's grade, if it is to determine whether the money is enough, if the satisfaction of the need to replace, pay attention to the replacement of the person's financial aid may be very low, if more than the second priority queue of the first person is cheaper, then put him in the second priority queue. began to open only a queue, wrong many times, the reason is that the former N/2 individuals only need to be cheap, do not need to score how good, so the enumeration when the replacement of the person can not be thrown away, because the former N/2 individual may be lower than he, but more expensive, specific look at the code.
#include <cstdio>i #include <cstring> #include <queue> #include <algorithm> #include <vector
> Using namespace std;
typedef long Long LL; struct Node {int s, E;}
A[110000];
struct Node2 {int x, y; Node2 () {} node2 (int x, int y): x (x), Y (y) {} bool operator < (const node2& RHS) Const {if (x!)
= rhs.x) return x > rhs.x;
return y > Rhs.y;
}
};
BOOL CMP (Node A, Node B) {if (A.E! = B.E) return A.E < B.E;
return A.S > B.S;
} int main () {int n, m;
LL F;
while (scanf ("%d%d%lld", &n, &m, &f)! = EOF) {priority_queue<node2> Q;
Priority_queue<int> Q2;
for (int i = 1; I <= m; i++) {scanf ("%d%d", &A[I].S, &A[I].E);
} sort (a+1, a+m+1, CMP);
LL sum = 0;
for (int i = 1; I <= n; i++) {sum + = A[I].E; Q.push (Node2 (A[I].S, a[I].E)];
} if (Sum > F) {printf (" -1\n");
} else {for (int i = 0; i < N/2; i++) {Q2.push (Q.top (). y);
Q.pop ();
} for (int i = n+1; I <= m; i++) {int x = Q.top (). x;
int y = Q.top (). Y;
if (x < A[I].S) {LL tmp = SUM-Y+A[I].E;
if (Q2.top () > y) {tmp = tmp-(Q2.top ()-y);
Q2.pop ();
Q2.push (y);
} if (TMP <= f) {sum = tmp;
Q.pop ();
Q.push (Node2 (A[I].S, A[I].E));
}}} printf ("%d\n", Q.top (). x); }
}
}