Poj2010 Moo University-financial aid

Source: Internet
Author: User

Moo University-financial aid


Question:

A student in a private school applies for a scholarship, and the amount of the school is limited. Therefore, the school wants to select n logarithm from C if the amount does not exceed f.

Three numbers N, C, and F are given. Returns the N logarithm in the C logarithm. Each logarithm represents the score and the applied amount respectively. If the total amount in the N logarithm is not greater than F, the intermediate score is the highest. (N is even)


Algorithm analysis:

There are two solutions for this question: First queue and second queue;

1. Implement with heap

First, let's talk about the heap implementation method. Now we can process n/2 at the beginning and end, because the number of n/2 at the end cannot be the median, that is, the answer. Then, the current minimum amount is recorded on the left and right scans. Finally, the maximum median is obtained by the two-side clip.

How do I handle left and right scans?

Take the header as an example (the scanning at the end is the same). First, we get the maximum amount in the queue. If it is larger than the current one, we will update it. In this way, the queue will always maintain the minimum amount of N/2 before the current score. The last principle. Finally, the maximum value is obtained by adding the beginning and end.


Ii. Binary implementation

The second part is better than the second part, that is, the second part of the answer required by the question. Here we first sort the scores and then divide them into two parts. However, the binary return value for this question is a bit special.


For details, see the code.

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;}



Binary 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. aid <B. aid ;}}; cow_score cow [maxn]; Int main () {While (~ Scanf ("% d", & N, & C, & F) {n/= 2; for (INT I = 0; I <C; ++ I) {scanf ("% d", & cow [I]. score, & cow [I]. aid);} Sort (cow, cow + C); Round <cow_score, vector <cow_score>, cmp_score> left_aid; Round <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) {// enumerate the 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) {// enumerate 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) {// The center of the clip if (COW [I]. lval + cow [I]. rval + cow [I]. aid <= F & maxscore <cow [I]. score) {maxscore = cow [I]. score ;}} printf ("% d \ n", maxscore);} return 0 ;}



Zookeeper

Poj2010 Moo University-financial aid

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.