1. 程式人生 > 實用技巧 >P1478 陶陶摘蘋果(升級版)

P1478 陶陶摘蘋果(升級版)

https://www.luogu.com.cn/problem/P1478

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n, s, a, b;
 4 struct apple{      //結構體定義蘋果的高度和摘蘋果所用的力氣 
 5     int x, y;
 6 };
 7 apple t[5005];     //用於存放各個蘋果 
 8 bool cmp(apple a, apple b){    //按照摘蘋果所用力氣大小從小到大排序 
 9     return a.y < b.y;
10 }
11 int ans;
12 int main() 13 { 14 cin>>n>>s; 15 cin>>a>>b; 16 for(int i=0; i<n; i++) 17 cin>>t[i].x>>t[i].y; 18 19 sort(t, t+n, cmp); //按照摘蘋果所用力氣大小從小到大排序 20 21 for(int i=0; i<n; i++){ 22 if(s>=t[i].y && a+b>=t[i].x){ //
力氣數大於當前蘋果所用力氣 且 高度大於等於蘋果高度 23 ans++; 24 //cout<<ans<<":"<<t[i].x<<" "<<t[i].y<<endl; //測試所摘蘋果 25 s-=t[i].y; 26 } 27 if(s<0)break; 28 } 29 cout<<ans; 30 return 0; 31 }