20分求助

P1478 陶陶摘苹果(升级版)

zzk_k @ 2023-08-16 14:55:57

#include<bits/stdc++.h>
using namespace std;
int x[10000];
int y[10000];
int main()
{
    int n,s,a,b,ans=0;
    cin>>n>>s;
    cin>>a>>b;
    for(int i=1;i<=n;i++)
    {
        cin>>x[i]>>y[i];
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n-i;j++)
        {
            if(y[i]>y[i+1])
            {
                swap(y[i],y[i+1]);
                swap(x[i],x[i+1]);
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(a+b>x[i])
            {
            s-=y[i];
            if(s>0){
                ans++;
            }
            }
    }
    cout<<ans;

    return 0;
}

by zhexian314 @ 2023-08-16 14:58:29

可以按高度先排比较好


by tflowder @ 2023-08-16 15:00:37

There are two wrongs in your code.

 for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n-i;j++)
        {
            if(y[j]>y[j+1]) // this 
            {
                swap(y[j],y[j+1]);
                swap(x[j],x[j+1]);
            }
        }
    }

and ...

for(int i=1;i<=n;i++)
    {
        if(a+b>=x[i])// this 
            {
            s-=y[i];
            if(s>=0){// this 
                ans++;
            }
            }
    }

by tflowder @ 2023-08-16 15:01:45

@zhouzikuo If you can, please follow me.


by zzk_k @ 2023-08-16 15:02:15

@zhexian314 按高度排序找力气小的会不会比较麻烦


by zzk_k @ 2023-08-16 15:02:47

@ksun48 怎么做


by tflowder @ 2023-08-16 15:04:21

#include<bits/stdc++.h>
using namespace std;
int x[10000];
int y[10000];
int main()
{
    int n,s,a,b,ans=0;
    cin>>n>>s;
    cin>>a>>b;
    for(int i=1;i<=n;i++)
    {
        cin>>x[i]>>y[i];
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n-i;j++)
        {
            if(y[j]>y[j+1])
            {
                swap(y[j],y[j+1]);
                swap(x[j],x[j+1]);
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(a+b>=x[i])
            {
            s-=y[i];
            if(s>=0){
                ans++;
            }
            }
    }
    cout<<ans;

    return 0;
}

by zzk_k @ 2023-08-16 15:07:27

thank you; i like Canada ,too.


|