求调

P1478 陶陶摘苹果(升级版)

hct_ @ 2023-05-02 10:18:45

如何让力气按照苹果高度从小到大的顺序排序

代码:

#include <iostream>
#include <cstring>
#include <iomanip>
#include <cmath>
#include <map>
#include <stack>
#include <algorithm>
#include <string>
#include <queue>
#include <deque>
#include <vector>
using namespace std;
struct tree{
    int x,y;
    bool operator < (const tree &o) const{
        return y<o.y;
    }
};
int A[1145140],B[1145140];
int main() {
    tree tre;
    int n,s,a,b,cnt=0;
    cin>>n>>s>>b>>a;
    for(int i=0;i<n;i++){
        cin>>tre.x>>tre.y;
        A[i]=tre.x,B[i]=tre.y;

    }
    sort(A,A+n);
//  cout<<endl<<endl<<endl;
//  for(int i=0;i<n;i++){
//      cout<<A[i]<<" "<<B[i]<<endl;
//  }
//  cout<<endl<<endl<<endl;
    for(int i=0;i<n;i++){
        if(A[i]<=a+b && B[i]<=s){
            s-=B[i];
            cnt++;
//          cout<<i;
        }

        }
    cout<<cnt;
    }

by IAKIOIpwp @ 2023-05-03 20:45:29

#include <bits/stdc++.h>
using namespace std;
struct tree{
    int x,y;
};
bool cmp(tree a,tree b){
    return a.y<b.y;
}
int main() {
    tree tre[100001];
    int n,s,a,b,cnt=0;
    cin>>n>>s>>b>>a;
    for(int i=1;i<=n;i++){
        cin>>tre[i].x>>tre[i].y;
    }
    sort(tre+1,tre+n+1,cmp);
    for(int i=1;i<=n;i++){
        if(tre[i].x<=a+b&&tre[i].y<=s){
            s-=tre[i].y;
            cnt++;
        }
    }
    cout<<cnt;
    return 0;
}

by IAKIOIpwp @ 2023-05-03 20:47:48

OK 已改 记得关住一下我!!!


|