希更展?用MD
by 斯蒂芬·库里30 @ 2019-07-23 16:30:22
~~不要用标题突出头文件的重要性~~
by RebelAlliance @ 2019-07-23 16:31:05
~~希更展?用MD~~
by Zenurik @ 2019-07-23 16:43:08
1.你不要在定义数组的时候用变量,这样会不合法,你就定义之后多的不用就可以了,题目说所有数据:n<=5000 a<=50 b<=200 s<=1000;你就定义q【5001】,w【1001】就可以;
2.排序你可以用结构体,定义苹果类型的结构体,按照所需力气排序
3.本题用贪心算法非常简单,你可以试试(我没看完你的代码)
by wbz123 @ 2019-07-25 06:45:05
我给你看下我的代码(刚试过,过了)
by wbz123 @ 2019-07-25 07:04:26
#include<iostream>
#include<algorithm>
using namespace std;
struct apple{
int xi;
int yi;
}c[5001];
bool cmp(apple a,apple b)
{
return a.yi<b.yi;
}
int main()
{
int n,s,a,b,sum=0;
cin>>n>>s>>a>>b;
int high;
high=a+b;
for(int i=1;i<=n;i++)
{
cin>>c[i].xi>>c[i].yi;
}
sort(c+1,c+n+1,cmp);
for(int i=1;i<=n;i++)
{
if(high>=c[i].xi&&s>=c[i].yi)
{
sum++;
s-=c[i].yi;
}
}
cout<<sum;
return 0;
}
by wbz123 @ 2019-07-25 07:04:38
(别抄)
by wbz123 @ 2019-07-25 07:05:03
```cpp
#include<bits/stdc++.h>
using namespace std;
struct apple{
int h;
int cost;
bool operator <(const apple &b)const{
return b.cost>cost;
}
}m[5001];
int n,s,a,b,num,len=0,cnt=0;
int main()
{
scanf("%d%d%d%d",&n,&s,&a,&b);num=n;
for(int i=1;i<=n;i++){
scanf("%d%d",&m[i].h,&m[i].cost);
}
sort(m+1,m+n+1);
while(s>=0&&len<=n&&n!=0){
if(m[++len].h<=a+b){
cnt++;
num--;
s-=m[len].cost;
if(s<0)cnt--;
}
}
cout<<cnt;
return 0;
}
```
by 信息学好好玩 @ 2019-08-04 13:20:09