Descriptionsios
奶牛大學:奶大招生,從C頭奶牛中招收N(N爲奇數)頭。它們分別得分score_i,須要資助學費aid_i。但願新生所需資助不超過F,同時得分中位數最高。求此中位數。spa
Input.net
Outputcode
Sample Inputblog
3 5 70 30 25 50 21 20 20 5 18 35 30
Sample Output排序
35
Hintip
#include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <queue> #include <string>1 #include <cstring> #include <map> #include <stack> #include <set> #include <sstream> #define IOS ios_base::sync_with_stdio(0); cin.tie(0); #define Mod 1000000007 #define eps 1e-6 #define ll long long #define INF 0x3f3f3f3f #define MEM(x,y) memset(x,y,sizeof(x)) #define Maxn 100005 #define P pair<int,int> using namespace std; P a[Maxn]; int N,C,F; // 牛i做爲中位數時,lower[i]表示分數低於它的牛的學費總和 int lower[Maxn],upper[Maxn]; int main() { cin>>N>>C>>F; int half=N/2; for(int i=0; i<C; i++) cin>>a[i].first>>a[i].second; //分數 學費 sort(a,a+C); { //求出lower[i] int total=0; priority_queue<int>q; for(int i=0; i<C; i++) { lower[i]=q.size()==half?total:INF; q.push(a[i].second); total+=a[i].second; if(q.size()>half) { //去掉一個學費最高的 total-=q.top(); q.pop(); } } } { //求出upper[i] int total=0; priority_queue<int>q; for(int i=C-1; i>=0; i--) { upper[i]=q.size()==half?total:INF; q.push(a[i].second); total+=a[i].second; if(q.size()>half) { //去掉一個學費最高的 total-=q.top(); q.pop(); } } } int ans=-1; for(int i=C-1; i>=0; i--) if(a[i].second+lower[i]+upper[i]<=F) { ans=a[i].first; break; } cout<<ans<<endl; return 0; }