#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
#include <bits/stdc++.h>
using num = int64_t;
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define REPI(t, n) for (num t = 0; t < n; ++t)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
#ifdef TESTING
#define DEBUG(...) __VA_ARGS__
#else
#define DEBUG(...)
#endif
bool possible(vector<vector<num>> &jobs, num D, num M) {
num N = sz(jobs);
vector<num> jobsLeft(N);
num idx = 0;
REPI(i, N) {
if (idx+D < i) return false;
jobsLeft[i] = sz(jobs[i]);
num spent = 0;
while (spent < M) {
if (idx > i) break;
num canSpend = min(M-spent, jobsLeft[idx]);
jobsLeft[idx] -= canSpend;
spent += canSpend;
if (jobsLeft[idx] == 0) ++idx;
}
}
while ((idx < N) && (jobsLeft[idx] == 0)) ++idx;
return (idx == N);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
num N, D, M;
cin >> N >> D >> M;
vector<vector<num>> jobs(N);
REPI(i, M) {
num x;
cin >> x;
--x;
jobs[x].push_back(i+1);
}
num lo = 1, hi = M;
while (lo < hi) {
num mid = (lo+hi) >> 1;
if (possible(jobs, D, mid)) hi = mid;
else lo = mid+1;
}
queue<num> jobsPending;
vector<vector<num>> ans(N);
REPI(i, N) {
for (num job : jobs[i]) jobsPending.push(job);
REPI(j, lo) {
if (jobsPending.empty()) break;
num curJob = jobsPending.front();
jobsPending.pop();
ans[i].push_back(curJob);
}
}
cout << lo << "\n";
REPI(i, N) {
for (num job : ans[i]) cout << job << " ";
cout << "0\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
4560 KB |
Output is correct |
2 |
Correct |
17 ms |
4560 KB |
Output is correct |
3 |
Correct |
15 ms |
4560 KB |
Output is correct |
4 |
Correct |
17 ms |
4584 KB |
Output is correct |
5 |
Correct |
15 ms |
4560 KB |
Output is correct |
6 |
Correct |
16 ms |
4552 KB |
Output is correct |
7 |
Correct |
15 ms |
4604 KB |
Output is correct |
8 |
Correct |
15 ms |
4552 KB |
Output is correct |
9 |
Correct |
22 ms |
8380 KB |
Output is correct |
10 |
Correct |
23 ms |
8444 KB |
Output is correct |
11 |
Correct |
19 ms |
3440 KB |
Output is correct |
12 |
Correct |
31 ms |
6456 KB |
Output is correct |
13 |
Correct |
51 ms |
11776 KB |
Output is correct |
14 |
Correct |
86 ms |
15692 KB |
Output is correct |
15 |
Correct |
76 ms |
15176 KB |
Output is correct |
16 |
Correct |
110 ms |
19968 KB |
Output is correct |
17 |
Correct |
137 ms |
28852 KB |
Output is correct |
18 |
Correct |
123 ms |
26288 KB |
Output is correct |
19 |
Correct |
150 ms |
31800 KB |
Output is correct |
20 |
Correct |
135 ms |
28920 KB |
Output is correct |