num = int(input())
for i in range(num):
n, k = list(map(int, input().split()))
books = [int(x) for x in input().split()]
if(len(books) == 0):
print(0)
else:
boxes = [0]
index = 0
for kg in books:
if boxes[index] + kg > k:
boxes.append(kg)
index += 1
else:
boxes[index] += kg
print(index + 1)
测试数据有点皮
一本一本放,装不下就多加一个箱子,一开始想多了…
include
include
using namespace std;
int main()
{
int T,n,k;
cin >> T;
while(T–)
{
int a,b;
cin >> n >> k;
b = k;
int res = 1;
for(int i = 0;i < n;i++)
{
cin >> a;
if(b - a >= 0)
b = b-a;
else
{
b = k - a;
res++;
}
}
if(n)
cout << res << endl;
else
cout << “0” << endl;
}
}
一开始按照贪心的思路做的,但实际上就是模拟!
num = int(input())
for i in range(num):
n, k = list(map(int, input().split()))
books = [int(x) for x in input().split()]
if(len(books) == 0):
print(0)
else:
boxes = [0]
index = 0
for kg in books:
if boxes[index] + kg > k:
boxes.append(kg)
index += 1
else:
boxes[index] += kg
print(index + 1)