0
已解决
http://judge.codingtang.com/problem/1258/
题目:
题目描述 Description
小明来河边玩耍,看到一位老爷爷站在岸边发愁,便上前询问。老爷爷讲出了烦闷的事:他要将买来的货物运到河对岸去,但是一次最多只能运两件货物,且一次运输的重量之和不能超过一个给定的整数。为了保证运输的次数尽可能少来减少成本,请你给小明想想办法。
输入描述 Input Description
输入两行,第一行为货物的件数n和每次运输的重量上限w,用空格隔开。
第二行为每件货物的重量。
【数据范围】
对于20%的数据,n<=100
对于70%的数据,n<=10000
对于100%的数据,n<=30000
输出描述 Output Description
输出一行,包含一个整数,即最少的运输次数。
样例输入 Sample Input
10 100
20 20 90 90 50 40 80 60 70 40
样例输出 Sample Output
6
#include <iostream>
#include <algorithm>
using namespace std;
int a[30010];
int main()
{
int n,w,j=1,count=0;
cin>>n>>w;
int k=n,m=n;
for(int i=1;i<=n;i++)cin>>a[i];
sort(a+1,a+n+1);
while(j<=k)
{
if(a[j]+a[k]<=w)
{
j++;
count++;
m-=2;
}
k--;
}
if(m!=0)count+=m;
cout<<count;
}
60分
错哪了?
Why?
求各位大佬帮个忙
0
已采纳
0
0
0
0
0
0
0
long long int n,a[31000],m;
long long int s=0,i=1,j;
输入>>n>>m;
j=n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
sort(a+1,a+n+1);
while(i<=j)
{
if(a[i]+a[j]<=m)
{
s++;
i++;
j--;
}
else
{
s++;
j--;
}
}
输出<<s;
//要加
#include<algorithm>
0





