传送门
题意:n个格子,m种颜色,每次操作可以让连续p[i]个格子染为m[i]的颜色,染色会覆盖掉之前的颜色,求m次操作使得每个格子都被染色且每种颜色都出现至少一次。
构造:首先按出现颜色最多的贪心方法构造,即把每种颜色的左边界设为i;然后把最后一种颜色的右边界设为n,求要占满所有格子每个颜色左边界的最小值b[i],然后取b[i]和i的最大值即为答案
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 1e5 + 6;
int n, m;
int a[maxn];
int ans[maxn];
int main()
{while (cin >> n >> m){ll sum = 0;for (int i = 1; i <= m; i++){cin >> a[i];sum += a[i];}int ok = 1;if (m > n) { ok = 0; }if (sum < n) { ok = 0; }for (int i = 1; i <= m; i++){ans[i] = i;if (i + a[i] - 1 > n){ok = 0;}}ans[m + 1] = n + 1;for (int i = m; i >= 1; i--){ans[i] = max(i, ans[i + 1] - a[i]);}if (!ok)cout << -1 << endl;else{for (int i = 1; i <= m; i++)cout << ans[i] << " ";cout << endl;}}
}