全WA求助

P1478 陶陶摘苹果(升级版)

OceanBrawl @ 2021-10-01 23:12:51

#include <bits/stdc++.h>
using namespace std;

int n,s,apple[5005],power[5005];
int a,b;
int x=0;
int main(){
    cin>>n>>s>>a>>b;
    for (int i=0;i<n;i++){
        cin>>apple[i]>>power[i];
    }//输入摘第i个苹果的距离和力量
    while (1){
        int minn=0,i;
        for (i=1;i<n;i++){
            if (power[i]<minn){minn=i;}
        }//求出摘苹果最小所需力量的下标
        if (s>=power[i-1]){
            if (a+b>=power[i-1]){
                x++;
                s-=power[i-1];
            }
            for (int j=i-1;j<n-1;j++){
                apple[j]=apple[j+1];
                power[j]=power[j+1];
                }//移位
            n--;
        }
        else{cout<<x-1;break;}
    }
    return 0;
}

by WGXWGXWGX @ 2021-10-02 00:08:05

@Chenweiye20081025 结构体不香吗??


by OceanBrawl @ 2021-10-02 08:13:53

@WGXWGXWGX 但凡我学过应该就会用他了,可是我甚至不知道那是啥


by WGXWGXWGX @ 2021-10-02 08:21:31

@Chenweiye20081025 ```

#include<bits/stdc++.h>
using namespace std;
struct apple{
    int high;
    int st;
} a[6000];
bool cmp(apple x,apple y){
    if(x.st<y.st)
    return 1;
    return 0;
}
int sum;
int main(){
    int n,s,a1,b,i;
    cin>>n>>s>>a1>>b;
    for(i=1;i<=n;i++)
    cin>>a[i].high>>a[i].st;
    sort(a+1,a+n+1,cmp);
    for(i=1;i<=n;i++){
        if(a1+b>=a[i].high&&a[i].st<=s){
            s-=a[i].st;
            sum++;
        }
        if(s<0)break;
    }
    cout<<sum<<endl;
    return 0;
}

|