求纠错:)

P1478 陶陶摘苹果(升级版)

zhuzhuzhu @ 2019-01-28 21:55:19

如题,60分求助(勿喷)(中意的盘一盘

include<iostream>

include<cstdio>

include<cmath>

include<algorithm>

using namespace std; int main() { int n,a,s,x[10000],b,y[100000],z=1,js=0,total=0; cin>>n>>s>>a>>b; for(int i=1;i<=n;i++) cin>>x[i]>>y[i]; for(int i=1;i<=n;i++) { if(x[i]<=a+b) y[z++]=y[i]; } sort(y,y+z-1); int q=1;// while((total<s)&&(q<=z-1)) { total+=y[q]; q++; js++; if(total>s) { js--; break; } } cout<<js; }


by agicy @ 2019-01-28 21:55:55

希望更丰富的展现?使用Markdown


by zhuzhuzhu @ 2019-01-28 21:58:49

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
    int n,a,s,x[10000],b,y[100000],z=1,js=0,total=0;
    cin>>n>>s>>a>>b;
    for(int i=1;i<=n;i++)
    cin>>x[i]>>y[i];
    for(int i=1;i<=n;i++)
    {
        if(x[i]<=a+b)
        y[z++]=y[i];
    }
    sort(y,y+z-1);
    int q=1; 
    while((total<s)&&(q<=z-1))
    {
        total+=y[q];
        q++;
        js++;
        if(total>s)
        {
            js--;
            break;
        }
    }
    cout<<js;
}

by agicy @ 2019-01-28 22:03:04

@zhuzhuzhu

对于下面这组数据,你的程序会WA

输入:

7 15
20 130
120 3
150 2
110 7
180 1
50 8
200 0
140 3

你的程序输出:

3

正确答案是:

4

by lygmh @ 2019-01-29 09:51:12

改的面目全非,请谅解。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int main() {
    int n,a,s,x[5001],b,y[5001],js=0;
    cin>>n>>s;
    cin>>a>>b;
    for(int i=1; i<=n; i++)
        cin>>x[i]>>y[i];
    int f;
    for(int i=1; i<=n; i++) { //xuanpai
        f=1;
        for(int j=1; j<=n-i+1; j++) {
            if(y[f]<y[j]) {
                f=j;
            }
        }
        swap(y[f],y[n-i+1]);
        swap(x[f],x[n-i+1]);
    }
    int q=1;
    while(s>=0&&q<=n) {
        if(x[q]<=a+b) {
            s-=y[q];

            js++;
            if(s<0) {
                js--;
            }
        }
        q++;
    }
    cout<<js;
    return 0;
}

|