Xecades @ 2019-02-01 16:32:41
code:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
using namespace std;
int n,s,a,b;
struct stu
{
int height;
int need;
} apples[5010];
bool cmp(stu a,stu b)
{
return a.need<b.need;
}
int main()
{
int take=0;
cin>>n>>s>>a>>b;
for(int i=1; i<=n; i++)
{
cin>>apples[i].height>>apples[i].need;
}
sort(&apples[0],&apples[n],cmp);
for(int i=1; i<=n && s>0; i++)
{
if(apples[i].height<=a+b)
{
take++;
s-=apples[i].need;
}
}
cout<<take;
return 0;
}
by 万弘 @ 2019-02-01 16:44:41
for(int i=1; i<=n; i++)
{
cin>>apples[i].height>>apples[i].need;
}
sort(&apples[0],&apples[n],cmp);/我觉得应该是sort(apples+1,apples+n+1,cmp);
by 万弘 @ 2019-02-01 16:45:17
因为你输入时下标从1开始
by Xecades @ 2019-02-01 16:45:46
@万弘 哦谢谢
by Xecades @ 2019-02-01 16:47:27
貌似没有什么用...还多了个RE https://www.luogu.org/recordnew/show/15993857
by 111l @ 2019-02-01 16:47:38
@万弘 好像也不对,我试了一下这个代码,样例都没过
by Xecades @ 2019-02-01 16:49:11
如何?
by 万弘 @ 2019-02-01 16:58:50
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
using namespace std;
int n,s,a,b;
struct stu
{
int height;
int need;
} apples[5010];
bool cmp(stu a,stu b)
{
return a.need<b.need;
}
int main()
{
int take=0;
cin>>n>>s>>a>>b;
for(int i=1; i<=n; i++)
{
cin>>apples[i].height>>apples[i].need;
}
sort(apples+1,apples+n+1,cmp);
for(int i=1; i<=n ; i++)
{
if(apples[i].height<=a+b&&s>=apples[i].need)//改了这里
{
take++;
s-=apples[i].need;
}
}
cout<<take;
return 0;
}
by 111l @ 2019-02-01 16:59:27
1.你没有加特判(n=0时)
2.在循环内特判(s-apples[i].need<0)时输出take-1
3.sort改成 sort(apples+1,apples+n+1,cmp)
然后你就能a了
by 111l @ 2019-02-01 17:01:15
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
using namespace std;
int n,s,a,b;
struct stu{
int height;
int need;
} apples[1111111];
bool cmp(stu a,stu b){
return a.need<b.need;
}
int main(){
int take=0;
cin>>n>>s>>a>>b;
if(n==0){//特判*1
puts("0");
return 0;
}
a+=b;
for(int i=1; i<=n; i++){
cin>>apples[i].height>>apples[i].need;
}
sort(apples+1,apples+n+1,cmp);
for(int i=1;s>=0;i++){
if(apples[i].height<=a){
take++;
if(s-apples[i].need<0){//特判*2
printf("%d",take-1);
return 0;
}
s-=apples[i].need;
}
}
cout<<take;
return 0;
}
by Xecades @ 2019-02-01 17:01:16
@光头 @万弘 感谢各位大佬