样例:
input:
6 5
5 6 7 8 9 10
output:
4
解释:
这个例子中,最少要使用魔法送达四个,
比如送达时间为6,7, B, 9这四个订单,
然后正常送达5和1 0的两个订单。
当然也存在其他的送达方法,但可以证明不存在小于需要用四次魔法的方法,
CPP版本
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int undeliverableNumsV(vector<int> v, int time) {
sort(v.begin(), v.end());
int re = v.size();
int cur = 0;
for (auto it : v)
if ((it - cur) >= 5) {
cur += time;
re--;
}
return re;
}
int main() {
int n = 0, time = 0;
cin >> n >> time;
vector<int> v;
//for (int value; cin >> value; v.push_back(value));
for (int i = 0, value = 0; i < n; ++i) {
cin >> value;
v.push_back(value);
}
cout << undeliverableNumsV(v, time);
return 0;
}
C版本
#include <stdio.h>
int undeliverableNums(int v[], int size, int time) {
int re = size, cur = 0;
for (int i = 0; i < size; i++)
if ((v[i] - cur) >= 5) {
cur += time;
re--;
}
return re;
}
int main() {
int n = 0, time = 0, nums[10000] = { 0 };
scanf_s("%d %d", &n, &time);
for (int i = 0, value = 0; i < n; ++i) {
scanf_s("%d", &value);
nums[i] = value;
}
printf("%d", undeliverableNums(nums, n, time));
return 0;
}