文章
68
粉丝
691
获赞
26
访问
577.8k
别忘了排序。。。示例杀我
#include<iostream>
#include<string>
#include<string.h>
#include<cstdio>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
#define MAX 1005
#define ll int
#define vec vector<ll>
#define P pair<ll,ll>
ll n, w, a[MAX];
int main() {
while (cin >> n >> w) {
ll cnt = 0, sum = 0, i = 1;
for (i = 1; i <= n; i++)cin >> a[i];
sort(a + 1, a + n + 1);
i = 1;
while (i <= n) {
if (a[i] + sum > w) {
sum = 0; cnt++;
}
else sum += a[i++];
}
if (sum > 0)cnt++;
cout << cnt << endl;
}
}
登录后发布评论
暂无评论,来抢沙发