4个WA,求助

P1478 陶陶摘苹果(升级版)

LeonTexius @ 2024-09-24 18:29:15

#include<bits/stdc++.h>
using namespace std;
int n,s,a,b;
int apples[5005][4];
int can;

int main()
{
    scanf("%d %d",&n,&s);
    scanf("%d %d",&a,&b);
    for(int i = 1;i <= n;i++)
    {
        scanf("%d %d",&apples[i][1],&apples[i][2]);
        if(apples[i][1] < (a + b))
        {
            apples[i][3] = -1;

        }
    }
    for (int i = 1; i < n; i++) {

        bool swapped = false;
        for (int j = 1; j < n - i; j++) {
            if ((apples[j][1] > apples[j + 1][1]) && apples[j][3] == 0 && apples[j + 1][3] == 0) 
            {

                swap(apples[j][1],apples[j + 1][1]);
                swap(apples[j][2],apples[j + 1][2]);
                swap(apples[j][3],apples[j + 1][3]);
                swapped = true;
            }
        }
        if (swapped == false)
            break;
    }
    for(int i = 1;i <= n;i++)
    {
        if((apples[i][1] <= (a + b)) && (apples[i][2] <= s) && apples[i][3] == 0)
        {
            can++;
            s -= apples[i][2];
        }
        if(apples[i][2] < s)
        {
            break;
        }
    }
    printf("%d",can);

    return 0;

}

by Sheep_YCH @ 2024-09-24 18:34:51

@AshtonHuang

您貌似把这道题想复杂了,这道题只需要排个序就行


by Sheep_YCH @ 2024-09-24 18:36:01

@AshtonHuang

#include<iostream>
#include<algorithm>
using namespace std;
long long n, s, a, b, i, ans = 0;
struct a1{
    long long x;
    long long y;
};
a1 apple[1000001];
bool cmp(a1 s1, a1 s2) {
    return s1.y < s2.y;
}
int main() {
    cin >> n >> s >> a >> b;
    for(i=1; i<=n; i++) cin >> apple[i].x >> apple[i].y;
    sort(apple+1, apple+n+1, cmp);
    for(i=1; i<=n; i++) {
        if(a + b >= apple[i].x) {
            s-= apple[i].y;
            if(s < 0) {
                cout << ans;
                return 0;
            }
            ans ++;
        }   
    }
    cout << ans;

    return 0;
}

大概就是这样的,很好理解,贪心+排序就行


by LeonTexius @ 2024-09-24 18:37:01

@Sheep_YCH 拴Q


|