Moo university-financial Aid
Test instructions
A private school student has to apply for a scholarship, but the amount is limited. Therefore, the school would like to select N logarithm from c if the amount does not exceed F.
Give three number n,c,f. Represents the n logarithm to be obtained in the C logarithm respectively.
Each logarithm represents the score, with the amount of the application. Requires that the total amount of n logarithm not exceed F in the condition, then obtain the middle of the student's highest achievement. (n is even)
Algorithm Analysis:
There are two methods of solving the subject, one is to use priority queue, and one is two;
First, the use of heap implementation
First of all, the implementation of the heap, we can now head to the end of the N/2 processing, because the number of N/2 must not become the median number is the answer. Then, scan the left and right to record the current minimum amount.
Finally, the maximum median is drawn on both sides of the clamp.
How to deal with left and right scan?
I take the head as an example (the tail scan is also the same), first get a maximum amount in the queue. is newer than the current hypothesis, which is larger than the current. This way the queue will always keep the N/2 minimum amount before the current score. A reason. Finally, the maximum value is obtained by adding the tail.
Two or two-point implementation
Two points or a better thought, that is, the answer to the question asked for two points.
Here we first sort the scores and then two points.
However, the binary return value of this question is somewhat special.
Please see the code for details.
Heap Implementation version:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace std;const int MAXN = 100000 + 20;struct node{int score,aid,id;}; int n,c,f; Node cow_score[maxn],cow_aid[maxn];bool cmp_score (const node& a,const node& b) {return A.score < B.score;} BOOL Cmp_aid (const node& a,const node& b) {return a.aid < B.aid;} int main () {cin >> N >> C >> F; for (int i = 0;i < C;++i) {cin >> cow_score[i].score >> Cow_score[i].aid; } sort (Cow_score,cow_score + c,cmp_score); for (int i = 0;i < C;++i) {cow_score[i].id = i; } memcpy (Cow_aid,cow_score,sizeof (Node) * C); Sort (cow_aid,cow_aid + c,cmp_aid); int ans = -1,lb = 0,ub = C; while (ub-lb > 1) {int mid = (UB + lb) >> 1; int lft = 0,rht = 0,sum = Cow_score[mid].aid; for (int i = 0;i < C;++i) {if ((Cow_aid[i].id < mid) && (sum + cow_aid[i].aid <= F)&& (LfT < N/2)) {sum + = Cow_aid[i].aid; ++lft; } else if ((Cow_aid[i].id > Mid) && (sum + cow_aid[i].aid <= F) && (Rht < N/2)) { sum + = Cow_aid[i].aid; ++rht; }} if ((LfT < N/2) && (Rht < N/2)) {ans =-1; Break } else if (LfT < N/2) {lb = mid; } else if (Rht < N/2) {UB = mid; } else{ans = cow_score[mid].score; LB = mid; }} cout << ans << endl; return 0;}
Two-point implementation version:
#include <iostream> #include <algorithm> #include <queue> #include <cstdio>using namespace std; const int MAXN = 100000 + 10;int n,f,c;struct cow_score{int score,aid,rval,lval; Cow_score (): Rval (0), lval (0) {}; BOOL operator < (const cow_score& RHS) const{//Sort score return score < Rhs.score; }};struct cmp_score{//Sort aid bool Operator () (Cow_score A,cow_score b) {return A.aid < B.aid; }}; Cow_score Cow[maxn];int Main () {while (~scanf ("%d%d%d", &n,&c,&f)) {N/= 2; for (int i = 0;i < C;++i) {scanf ("%d%d", &cow[i].score,&cow[i].aid); } sort (Cow,cow + C); Priority_queue<cow_score,vector<cow_score>,cmp_score> Left_aid; Priority_queue<cow_score,vector<cow_score>,cmp_score> Right_aid; Cow_score tmp; int sumaid = 0; for (int i = 0;i < N;++i) {sumaid + = Cow[i].aid; Left_aiD.push (Cow[i]); } for (int i = N;i < C-n;++i) {//enum left cow[i].lval = Sumaid; TMP = Left_aid.top (); if (Tmp.aid > Cow[i].aid) {left_aid.pop (); Left_aid.push (Cow[i]); Sumaid = Sumaid-tmp.aid + cow[i].aid; }} sumaid = 0; for (int i = C-n;i < C;++i) {sumaid + = Cow[i].aid; Right_aid.push (Cow[i]); } for (int i = C-n-1;i >= n;--i) {//enum to the right cow[i].rval = Sumaid; TMP = Right_aid.top (); if (Tmp.aid > Cow[i].aid) {right_aid.pop (); Right_aid.push (Cow[i]); Sumaid = Sumaid-tmp.aid + cow[i].aid; }} int maxscore =-1; for (int i = N;i < C-n;++i) {//Clamp intermediate if (Cow[i].lval + cow[i].rval + cow[i].aid <= F&&maxsco Re < cow[i].score) {maxscore = Cow[i].score; }} printf ("%d\n", Maxscore); } return 0;}
poj2010 Moo university-financial Aid