20分求助。。。

P1478 陶陶摘苹果(升级版)

Dongbaitao @ 2020-02-09 11:23:15

#include<iostream>
using namespace std;
int main()
{
    int n,s,a,b,h,t=0;
    cin>>n>>s>>a>>b;
    h=a+b;
    int x[n],y[n];
    for(int i=0;i<n;i++)
    {
        cin>>x[i]>>y[i];
    }
    for(int j=0;j<n;j++)
    {
        s-=y[j];
        if(x[j]<h&&s>0)
        {
            t++;
        }
        else if(s<=0)
        {
            break;
        }
    }
    cout<<t<<endl;
    return 0; 
} 

看着没问题啊,为什么过不了,求助大佬QAQ


by Frainstak @ 2020-02-09 11:24:34

请提供错误样例


by Dongbaitao @ 2020-02-09 11:27:26

只有第四个能过


by why666 @ 2020-02-09 11:39:04

emm dalao您怎么能一上来就减Yi呢 减Yi的前提是能摘到苹果吧(a+b>Xi)


by why666 @ 2020-02-09 11:42:32

哦不是,应该是a+b>=Xi 还有哇您难道不应该先排序吗(雾


by why666 @ 2020-02-09 11:42:59

@Dongbaitao


by Dongbaitao @ 2020-02-09 20:31:13

哦。。。


by Dongbaitao @ 2020-02-09 20:32:33

那应该怎么写QAQ


by Dongbaitao @ 2020-02-09 20:33:04

@why666


by Dongbaitao @ 2020-02-10 08:14:22

#include<iostream>
using namespace std;
int main()
{
    int n,s,a,b,h,t=0;
    cin>>n>>s>>a>>b;
    h=a+b;
    int x[n],y[n];
    for(int i=0;i<n;i++)
    {
        cin>>x[i]>>y[i];
    }
    for(int j=0;j<n;j++)
    {
        if(h>=x[j]&&s>=y[j])
        {
            t++;
            s-=y[j];
        }
    }
    cout<<t<<endl;
    return 0; 
} 

我新做的代码,dalao们帮检查一下有问题吗


by why666 @ 2020-02-10 12:20:03

#include<iostream>
using namespace std;
int n,s,a,b,ans=0,t1,t2;
int xi[5000],yi[5000];
int main(){
    cin>>n>>s>>a>>b;
    a+=b;
    for(int i=1;i<=n;i++) cin>>xi[i]>>yi[i];
    for(int i=1;i<n;i++)
        for(int j=i+1;j<=n;j++){
            if(yi[i]>yi[j]){
                t1=xi[i],t2=yi[i];
                xi[i]=xi[j],yi[i]=yi[j];
                xi[j]=t1,yi[j]=t2;
            }
        }
    for(int i=1;i<=n;i++){
        if(xi[i]<=a&&yi[i]<=s){
            ans++;
            s-=yi[i];
        }
    }
    cout<<ans;
    return 0;
}

这是我的,您康康呗


| 下一页