E. Painting The Fence
time limit per test
1.0 s
memory limit per test
256 MB
input
standard input
output
standard output
The fence consists of n
planks, arranged from left to right. Monocarp has m different types of paint, and the number of planks that can be painted in color i is ai (there's not enough paint to color any more planks). The total amount of paint is just enough to paint exactly n planks — in other words, the sum of all ai is equal to n. Each plank should be painted into exactly one color.
Monocarp has to paint the fence in such a way that the length of every contiguous segment consisting of planks of the same color is not greater than k
.
Find a suitable way to paint the fence, or say that it is impossible.
Input
The first line contains three integers n
, m and k (1≤n≤2⋅105,1≤m,k≤n)— the number of planks in the fence, the number of different colors of paint and the maximum length of a contiguous segment of planks with the same color, respectively.
The second line contains a sequence of integers a1,a2,…,am
(1≤ai≤n), where ai is the number of planks that can be painted with color i. The sum of all values of ai is equal to n.
Output
If it is impossible to paint the fence in such a way that the length of every contiguous segment consisting of planks of the same color is not greater than k
, print −1.
Otherwise print n
integers, i-th of them should be equal to the index of the color of the i-th plank. If there are multiple possible answers, print any of them.
Examples
Input
Copy
5 2 1 2 3
Output
Copy
2 1 2 1 2
Input
Copy
8 2 3 1 7
Output
Copy
-1
Input
Copy
10 3 2 5 2 3
Output
Copy
1 1 3 1 1 2 3 1 2 3
Note
In the first example the first, third and fifth planks should have color 2
, and all other planks should have color 1.
In the second example it is impossible to paint the fence in the required way, so the answer is −1
.解题思路:贪心每次找数目最大的数字SS.id,取k个进行排队,然后找第二多的数字S2.id,选一个排在后面, 取出的SS.v,S2.v对数目进行修改,再放入队列中,
但是每次选最多数目时,可能会出现和上次第二多数目S2.id为相同id,则需分类讨论,
贪心保证每次排队都不超过k,且尽可能连续得多。
结构体类型优先队列 运算符必须重载
#include<bits/stdc++.h> using namespace std; int n,m,k; struct node { int id,v; bool operator < (const node & S) const//优先队列 比较符号《必须》重载 { return v<S.v;//默认大值优先级高 } }a[200005]; priority_queue<node> que; vector<int > ans; int main() { scanf("%d%d%d",&n,&m,&k); for(int i=0;i<m;i++) { scanf("%d",&a[i].v); a[i].id=i+1; que.push(a[i]); } node SS,S2; int tt=n; int last=0; int lastnum=0; while(tt) { int t; SS=que.top(); que.pop(); if(SS.id==last)//最后一个数和当前最大数量的数字相同时 { if(lastnum==k)//已经达到最大连续个数 换下一个队列值 { node T=que.top(); que.pop(); que.push(SS); SS=T; t=min(k,SS.v); } else//未达最大连续个数 贪心继续加 { t=min(k-lastnum,SS.v); } } else t=min(k,SS.v);//和最后一个数字不同 S2=que.top(); que.pop(); SS.v-=t; if(t==0) //tt!=0 当前第二大的已经为0 则不合法 { printf("-1\n"); return 0; } for(int i=0;i<t;i++) { ans.push_back(SS.id); last=SS.id; lastnum=t; } tt-=t; if(S2.v==0&&tt) { printf("-1\n"); return 0; } else if(tt) { ans.push_back(S2.id); last=S2.id; lastnum=1; tt-=1; S2.v-=1; } que.push(SS); que.push(S2); } for(int i=0;i<n;i++) { printf("%d ",ans[i]); } } //12 3 2 //5 5 2 //1 1 2 2 1 2 2 3 1 1 2 3
E. Painting The Fence 优先队列+贪心
最新推荐文章于 2021-07-19 20:58:19 发布