1045. 箱子打包

uuunique

include

using namespace std;

include

const int maxn=1e5+10;
int length[maxn];
int main(){
int n,l;
scanf(“%d%d”,&n,&l);
for(int i=0;i<n;i++){
scanf(“%d”,&length[i]);
}
sort(length,length+n);
int left=0;
int right=n-1;
int answer=0;
while(left<=right){
if(length[left]+length[right]<=l)
{
left++;
right–;
}
else{
right–;
}
answer++;
}
printf(“%d\n”,answer);
return 0;
}

Brown897

EOJ上好像还有类似的骑车的题

#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e5+5;
int arr[maxn];

int main() {
    int n,l,ans=0;
    cin>>n>>l;
    for (int i = 0; i < n; ++i) {
        cin>>arr[i];
    }
    sort(arr,arr+n);
    int left=0,right=n-1;
    while(left<=right){
        if (left == right){
            ans++;
            break;
        }
        if (arr[left]+arr[right]<=l){
            ans++;
            left++;
            right--;
        }else{
            ans++;
            right--;
        }
    }
    cout<<ans<<endl;
    return 0;
}
你当前正在回复 博客/题目
存在问题!