using namespace std;
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; }
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; }
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;
}
EOJ上好像还有类似的骑车的题