题解 P1478 【陶陶摘苹果(升级版)】

· · 题解

刚好一位新(da)手(lao)问了一下我这一题,那我就再过来刷了一遍,顺便发一波题解吧

众所周知,本蒟蒻的代码码风是极其奇葩的

【分析】

首先,条件反射地打完了读入优化......

题目的要求是对于已知的 n,s,h(=a+b) ,查询在接下来的苹果中,最多能选取多少个苹果使其力气总和小于 s ,且高度小于 h 。

那么,作为一个有着极其强的节省空间愿望和不开全局变量的蒟蒻, a,b 就不存在主函数和全局了吧,(浪费空间?)......

对于输入的苹果,如果高度已经大于 h 了,那这个苹果我们就根本不要看(cun),因为摘不到它,对答案没有贡献,眼不见,心不烦,直接不要;而如果小于等于 h ,我们直接存它的力气即可,因为高度也已经对答案没有贡献了

所以,h 什么的写在函数里面就可以了吧 我这什么垃圾癖好

而如果对于这样的输入方式,能输入进来的一定是能摘取的苹果

因此,我们专门用一个变量来记录实际输入(除去那些没储存的)的数量

这样之后,很明显,我们优先选择力气花费尽可能小的绝对最划算(贪心思路)

那我们就直接按从小到大排序,然后一个一个试能不能摘即可

【代码】

那本蒟蒻就放代码了......

因为不想开全局变量的特殊癖好,用了一点引用和指针,各位见谅

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define f(a,b,c) for(register int a=b;a<=c;a++)
#define g(a,b,c) for(register int a=b;a>=c;a--)
#define Max(a,b) ((a>b)?a:b)
#define Min(a,b) ((a<b)?a:b)
#define File(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
typedef long long int ll;
typedef unsigned long long int ull;
const int MAXN=5010;
inline ll read(){
    register ll ans=0;register char c=getchar();register bool neg=0;
    while((c<'0')|(c>'9')) neg^=!(c^'-'),c=getchar();
    while((c>='0')&(c<='9')) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
    return neg?-ans:ans;
}
inline void print(int x) { char s[20]; sprintf(s,"%d",x); puts(s); }
//以上属于条件反射的结果,各位看看就好
inline void init(int &d_N,int &d_S,int *&ar_d_Apple){
    int d_Tmpn=read(); d_S=read();
    int d_H=read()+read();
    f(i,1,d_Tmpn){
        if(read()>d_H) { read(); continue; }
        *(ar_d_Apple+d_N)=read();
        d_N++;
    }
    sort(ar_d_Apple,ar_d_Apple+d_N);
}
int main(){
    int d_N=0,d_S,d_Cnt=0,*ar_d_Apple=new int[MAXN];
    init(d_N,d_S,ar_d_Apple);
    f(i,0,d_N-1){
        d_S-=ar_d_Apple[i];//先扣除
        if(d_S<0) break;//小于0说明刚刚那个摘不了
        d_Cnt++;//摘得了,计数器+1
    }
    print(d_Cnt);
    return 0;
}