前言:这个题目就是我们小时候学的的活动规划烧饼,我们要先算出我们最大耗时是多少,然后再对我们的活动进行规划
题目地址
我们这个题目还要求算出k(执行次数)我的做法是写两遍代码,其实也可以存起来,因为最大操作次数为 2n
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
const int N = (int)1e5 + 10;
int n, m;
ll a[N];
int main() {
cin >> n >> m;
ll limi = 0;
ll sum = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i]; limi = max(limi, a[i]);
sum += a[i];
}
// 限制好一个limi
ll t = (sum % m) ? sum / m + 1 : sum / m;
limi = max(limi, t);
int l = 0, cnt = 1;
ll k = 0;
for (int i = 1; i <= n; i++) {
if (l + a[i] >= limi) {
//cout << i << " " << cnt << " " << l << " " << limi << endl;
k++;
l = (l + a[i]) % limi; cnt++;
if (l) {
//cout << i << " " << cnt << " " << 0 << " " << l << endl;
k++;
}
}
else {
//cout << i << " " << cnt << " " << l << " " << l + a[i] << endl;
l += a[i];
k++;
}
}
cout << k << endl;
l = 0, cnt = 1;
for (int i = 1; i <= n; i++) {
if (l + a[i] >= limi) {
cout << i << " " << cnt << " " << l << " " << limi << endl;
l = (l + a[i]) % limi; cnt++;
if (l) {
cout << i << " " << cnt << " " << 0 << " " << l << endl;
}
}
else {
cout << i << " " << cnt << " " << l << " " << l+a[i] << endl;
l += a[i];
}
}
return 0;
}