前言
本题真的很简单。P1478 陶陶摘苹果(升级版)。赶着去操场跑步锻炼身体,所以粗略写一下了。
解题思路:
本题解题思路相对简单,这道题属于基础的贪心题,一眼就能看出来在做选择的时候需要去贪最小的体力消耗,当然,前提是椅子高度加陶陶臂长之和可以够到这个高度。因此利用体力损耗先排个序,然后去贪最小体力损耗,外加满足高度条件就行,满足一个sum加一次,然后体力s减少一次。输出答案即可。
#include <stdio.h>
#include <stdlib.h>
struct node{
int height;
int strength;
};
void Merge(int left,int mid,int right, struct node a[],struct node b[]){
int i,j=mid+1,k=left;
for(i=left;i<=right;i++)
b[i]=a[i];
i=left;
while(i<=mid&&j<=right){
if(b[i].strength<b[j].strength){
a[k]=b[i];
k++;
i++;
}else{
a[k]=b[j];
k++;
j++;
}
}
while(i<=mid){
a[k]=b[i];
k++;
i++;
}
while(j<=right){
a[k]=b[j];
k++;
j++;
}
return;
}
void Mergesort(int left,int right,struct node a[],struct node b[]){
if(left>=right)
return;
int mid=(left+right)/2;
Mergesort(left,mid,a,b);
Mergesort(mid+1,right,a,b);
Merge(left,mid,right,a,b);
return;
}
int main(int argc, char *argv[]) {
int n,s,sum=0,i,a,b;
scanf("%d%d",&n,&s);
struct node apple[n],temp[n];
scanf("%d%d",&a,&b);
for(i=0;i<n;i++){
scanf("%d%d",&apple[i].height,&apple[i].strength);
}
Mergesort(0,n-1,apple,temp);
for(i=0;i<n;i++){
if(a+b>=apple[i].height){
if(s>=apple[i].strength){
sum++;
s-=apple[i].strength;
}else{
break;
}
}else{
continue;
}
}
printf("%d",sum);
return 0;
}