优先队列 3,5点不过,难过,,,

P1478 陶陶摘苹果(升级版)

天下皆白_ @ 2019-03-19 20:10:18

#include <iostream>
#include <queue>
using namespace std;
const int maxn = 5005;
struct Apple{
    int height;     //苹果高度;
    int power;     //摘苹果所需力气;
    bool operator < (const Apple &a1)const{
        return power > a1.power;
    }
};
priority_queue<Apple> qu;
int main(){
    int n, s, a, b;     //苹果树----n;力气-----s; 椅子高度--a; 手伸长度--b;
    cin >> n >> s;
    cin >> a >> b;
    Apple arr[maxn];
    for(int i = 1; i <= n; i++){
        cin >> arr[i].height >> arr[i].power;
        qu.push(arr[i]);
    }
    int total = 0;      //摘得总苹果树;
    while(1){
        if(s < 0 || n == 0)
            break;
        Apple tt = qu.top();
        qu.pop();
        if(tt.height <= a + b && s - tt.power >=0)
            total++;
        s = s - tt.power;
        n--;
    }
    cout << total << endl;
    return 0;
}

by Hams_rain @ 2019-07-17 21:22:18

一道贪心被你连STL都搞出来了QAQ```

include<bits/stdc++.h>

include<algorithm>

using namespace std; struct pg{ int gaodu; int liqi; }; bool cmp(pg a,pg b)
{
return a.liqi<b.liqi;//从小到大排<,若要从大到小排则>
} int main () { int n=0,s=0,a=0,b=0,pig=0; pg xi[10001]={}; cin>>n; cin>>s; cin>>a>>b; for(int i=0;i<n;i++)cin>>xi[i].gaodu>>xi[i].liqi; sort(xi,xi+n,cmp); for(int i=0;i<n;i++){ if(s<0)break; if(xi[i].gaodu<=a+b){ if(s-xi[i].liqi<0)break; pig++; //cout<<xi[i].gaodu<<" "<<xi[i].liqi<<" "; } else continue; s=s-xi[i].liqi;//cout<<s<<endl; } cout<<pig<<endl; return 0;
}


看完代码不要抄!!!

by Hams_rain @ 2019-07-17 21:22:49

#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
struct pg{
    int gaodu;
    int liqi;
};
bool  cmp(pg a,pg b)  
{  
    return a.liqi<b.liqi;//从小到大排<,若要从大到小排则>  
}
int main () {
    int n=0,s=0,a=0,b=0,pig=0;
    pg xi[10001]={};
    cin>>n;
    cin>>s;
    cin>>a>>b;
    for(int i=0;i<n;i++)cin>>xi[i].gaodu>>xi[i].liqi;
    sort(xi,xi+n,cmp);
    for(int i=0;i<n;i++){
        if(s<0)break;
        if(xi[i].gaodu<=a+b){
            if(s-xi[i].liqi<0)break;
            pig++;
            //cout<<xi[i].gaodu<<" "<<xi[i].liqi<<" ";
        }
        else continue;
        s=s-xi[i].liqi;//cout<<s<<endl; 
    }
    cout<<pig<<endl;
    return 0;   
} 

|