求助:#3和#5过不了

P1478 陶陶摘苹果(升级版)

```cpp 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:44:41


因为你输入时下标从1开始
by 万弘 @ 2019-02-01 16:45:17


@[万弘](/space/show?uid=73142) 哦谢谢
by Xecades @ 2019-02-01 16:45:46


貌似没有什么用...还多了个RE https://www.luogu.org/recordnew/show/15993857
by Xecades @ 2019-02-01 16:47:27


@[万弘](/space/show?uid=73142) 好像也不对,我试了一下这个代码,样例都没过
by 111l @ 2019-02-01 16:47:38


如何?
by Xecades @ 2019-02-01 16:49:11


```cpp #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 万弘 @ 2019-02-01 16:58:50


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 16:59:27


```cpp #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 111l @ 2019-02-01 17:01:15


@[光头](/space/show?uid=114206) @[万弘](/space/show?uid=73142) 感谢各位大佬
by Xecades @ 2019-02-01 17:01:16


|