2142. 放书

zzpzbl

测试数据有点皮

Twisted9

一本一本放,装不下就多加一个箱子,一开始想多了…

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;
}
}

yuanfang

一开始按照贪心的思路做的,但实际上就是模拟!

#include <cstdio>
#include <cstring>
const int MAXN = 60;
int book[MAXN], box[MAXN];
int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        int n, k;
        scanf("%d%d", &n, &k);
        if (n == 0) {
            printf("0\n");
        } else {
            int count = 1;
            memset(box, 0, sizeof(box));
            for (int i = 0; i < n; ++i) {
                scanf("%d", &book[i]);
                if (box[count] + book[i] <= k) {
                    box[count] += book[i];
                } else {
                    box[++count] = book[i];
                }
            }
            printf("%d\n", count);
        }
    }
    return 0;
}
Sun_shine

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)

你当前正在回复 博客/题目
存在问题!