求助..

P1478 陶陶摘苹果(升级版)

Xiang @ 2019-02-11 21:22:34

#include<bits/stdc++.h>
#define FOR(i,l,r) for(int i=1;i<=r;i++) 
using namespace std;

int main(){
int n,s,a,b,t=0; cin>>n>>s;cin>>a>>b;
int xi[n+10],yi[n+10];
for(int i=1;i<=n;i++) {cin>>xi[i]>>yi[i];}
sort(yi+1,yi+n);
for(int m=1;m<=n;m++)
{ 
if(s<yi[m]&&t!=0) {cout<<t-1;return 0;}
else if(a+b>=xi[m]) {t=t+1;s-=yi[m];} 
else continue;
}
cout<<t;
return 0;
}

by Xiang @ 2019-02-11 21:55:00

@Bambusoideae 唔..力气从小到大排完序了吖


by Xiang @ 2019-02-11 22:01:11

换了冒泡(好像可以不换..但不会快排)过了 谢谢大佬们~!

include<bits/stdc++.h>

#define FOR(i,l,r) for(int      i=l;i<=r;i++) 
using namespace std;
int xi[5010],yi[5010];
void BubbleSort(int array[], int n)
{
int i, j, k ,t ;
for(i=1; i<n; i++)
    for(j=1; j<=n-i; j++)
    {
        if(array[j]>array[j+1])
        {
            k=array[j];
            array[j]=array[j+1];
            array[j+1]=k;
            t=xi[j];
            xi[j]=xi[j+1];
            xi[j+1]=t;
        }
    }
}
int main(){
int n,s,a,b,t=0;    cin>>n>>s;cin>>a>>b;
for(int i=1;i<=n;i++)   {cin>>xi[i]>>yi[i];}
BubbleSort(yi, n);

for(int m=1;m<=n;m++)
{ 
if(s<yi[m]&&t!=0) {if(s>=0){cout<<t;return 0;} else {cout<<t-1;return 0; }}
else if(a+b>=xi[m]) {t=t+1;s-=yi[m];} 
else continue;
}
cout<<t;

return 0;
}

上一页 |