80分,求助!

P1478 陶陶摘苹果(升级版)

m3o7o2n1 @ 2024-03-17 10:45:22

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,s;
    cin>>n>>s;
    int a,b;
    cin>>a>>b;
    int x[5001],y[5001];
    int j=0;
    int i;
    int sg[5001],sl[5001];
    for(i=0;i<n;i++){
        cin>>x[i]>>y[i];
        if(x[i]<=(a+b)){
            sg[j]=x[i];
            sl[j]=y[i];
            j++;
        }
    }
    sort(sl,sl+j);
    int g=0;
    for(i=0;s-sl[i]>=0;i++){
        s=s-sl[i];
        g++;
    }
    cout<<g;
    return 0;
}

by D23lhc @ 2024-03-17 11:08:08

你这我有点看不懂


by HEROBRINEH @ 2024-03-17 11:10:23

AC


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

by D23lhc @ 2024-03-17 11:12:36

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

by D23lhc @ 2024-03-17 11:13:53

@HEROBRINEH

你这什么思路


by D23lhc @ 2024-03-17 11:19:49

@HEROBRINEH

人家叫你调他的代码,帮他优化,而不是将题解一字不差的复制上来


by 88jnt @ 2024-05-15 13:05:56

@m3o7o2n1 测试点中有0 0

0 0


|