悲催的移寝
Time Limit: 1000MS Memory Limit: 131072KB 64bit IO Format: %lld & %llu
Submit
Status
Description
对于csuxushu来说,能够在CSU(California State University)上学是他一生的荣幸,而换校区搬寝室则是他最大的不幸。顺利通过省赛选拔,开心的csuxushu现在需要着手准备搬家事宜。
对于好学的csuxushu来说,唯一头疼的就是书籍的搬运了,因为他的书实在是太多了。为了防止书籍在运输过程中受损,他打算定制特制的储物保护箱。为了简单起见,我们假设他所有的书都是一样规格。由于是批量定制,这些储物箱都有相同的长度L,宽度和厚度都是一本书的宽度和厚度。csuxushu有一个特殊的要求,那就是每个储物箱最多只能放两本书。当然,他需要运走所有的N本书,并且每一个储物箱里书的总长度不能超过L。
尽管校队队员给了csuxushu许多奖励,csuxushu还是想定尽可能少的储物箱,以求节约经费,然而面对这么困难的问题他显然是一脸懵逼,这时候他想到了你——全CSU最厉害的程序员来帮助他解决这个问题。
Input
第一行一个整数T,代表有T组数据,之后一个空行并且接下来每两组数据之间有一个空行。
每组数据第一行,为1个整数n (1 ≤ n ≤ 10^5),代表有n本书。第二行,为1个整数L ,1≤l ≤ 10000 ,为特制储物箱长度。接下来n行,分别为每本书的长度li,li≤l。
Output
每组数据输出一行,即csuxushu至少需要定多少储物箱。
Sample Input
1
10
80
70
15
30
35
10
80
20
35
10
30
Sample Output
6
题目有坑 要输出两个空行
#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<set>
using namespace std;
const int MAX=100005;
int n,l;
int books[MAX]={0};
int used[MAX];
bool cmp(int a,int b)
{
return a<b;
}
int main()
{
int T,ans;
cin>>T;
while(T--)
{
ans=0;
int x=0;
memset(used,0,sizeof(used));
cin>>n>>l;
int mark=n-1;
for (int i=0;i<n;i++)
cin>>books[i];
sort(books,books+n,cmp);
for (int i=0;i<n;i++)
{
for (;mark>=i;mark--)
{
if (x>=n) break;
if (books[i]+books[mark]<=l&&!used[i]&&!used[mark])
{
used[i]=true;
used[mark]=true;
ans++;
x+=2;
break;
}
else if(!used[mark])
{
books[mark]=true;
x+=1;
ans++;
}
}
}
cout<<ans<<endl<<endl;
}
}