题目:http://codeforces.com/contest/1066/problem/D
开始没看懂题意,后来看了note才明白。。。
Maksim has nn objects and mm boxes, each box has size exactly kk. Objects are numbered from 11 to nn in order from left to right, the size of the ii-th object is aiai.
Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the ii-th object fits in the current box (the remaining size of the box is greater than or equal to aiai), he puts it in the box, and the remaining size of the box decreases by aiai. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.
Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.
Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).
Input
The first line of the input contains three integers nn, mm, kk (1≤n,m≤2⋅1051≤n,m≤2⋅105, 1≤k≤1091≤k≤109) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤k1≤ai≤k), where aiai is the size of the ii-th object.
Output
Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.
Examples
input
Copy
5 2 6 5 2 1 4 2
output
Copy
4
input
Copy
5 1 4 4 2 3 4 1
output
Copy
1
input
Copy
5 3 3 1 2 3 1 1
output
Copy
5
解释一下,意思是他从第一个物品开始装,装不下了就换下一个物品,如果当前物品不能全部装入箱子,他会扔掉第一个物品,从第二个开始装。
理解了这个意思就简单多了,很明显装完之后最后一个物品一定被装进去了,那么我们可以从后往前找,看最多装几个,模拟一下就好了。
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int a[N];
int main()
{
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
int cnt = 0;
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
}
int ret = k;
int i;
for (i = n; i >= 1; i--)
{
if (a[i] <= ret)
{
ret -= a[i];
cnt++;
}
else
{
m--;
if (m == 0)
{
break;
}
ret = k - a[i];
cnt++;
}
}
printf("%d\n", cnt);
return 0;
}