cf 题解--E. Painting The Fence

https://codeforces.com/group/5yyKg9gx7m/contest/274333/problem/E

E. Painting The Fence

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 5 2 1 2 3 output 2 1 2 1 2 input 8 2 3 1 7 output -1 input 10 3 2 5 2 3 output 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.

题目描述:

有m种颜色a1,a2.....am个。a1+a2+...am=n。

打印一种排序,使的任一的连续段相同颜色不超过k。不存在输出-1。

分析:

不超过k,则可以每个颜色分成长度为k的p段。找到最多的那个颜色,他分成的段肯定是段数最多的,设为mx。我们要该颜色段不连续,就是要在它的mx-1个间隙中插入一个别的颜色。我们从最多的颜色之外的其他颜色找,看看能不能塞满最多颜色的间隙,不能就是-1的情况了。存入ins数组中。

因为最多的有p段,所以分成p轮。每一轮先把最大的打印,从ins中拿出一个来分隔,然后然后其他颜色依次打印。保证每段相同颜色不超过k。

代码:

 

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<deque>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=2e5+6;
struct node
{
    int c;
    int num;
}a[maxn],h;
bool cmp(struct node a,struct node b)
{
    return a.num>b.num;
}
int main()
{
    int n,m,k;
    scanf("%d%d%d",&n,&m,&k); 
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&a[i].num);
        a[i].c=i;
    }
    sort(a+1,a+m+1,cmp);
    int d=a[1].num/k;
    if(a[1].num%k==0) d--;
    if(d>n-a[1].num) printf("-1\n");
    else
    {
        int count=d;
        vector <node> ins;
        for(int i=2;i<=m;i++)
        {
            if(d>=a[i].num) 
            {
                d-=a[i].num;
                h.c=a[i].c;
                h.num=a[i].num;
                ins.push_back(h);
                a[i].num=0;
            }
            else
            {
                h.c=a[i].c;
                h.num=d;
                ins.push_back(h);
                a[i].num-=d;
                d=0;
            }
            if(d==0) break;
        }
        int p=0,last=-1;
        while(count<n)
        {
            last=-1;
            for(int i=1;i<=m;i++)
            {
                if(!a[i].num) continue;
                int up=a[i].num>=k?k:a[i].num;
                //ins的一个与后面要打印的重复,而且后面要打印的正好是k个 
                if(last==a[i].c&&up==k) up--;
                a[i].num-=up;
                for(int j=0;j<up;j++) printf("%d ",a[i].c);
                if(!ins.empty()&&i==1&&p<ins.size()) 
                {
                    if(ins[p].num>0)
                    printf("%d ",ins[p].c),ins[p].num--;
                    else
                    if(p+1<ins.size())
                    printf("%d ",ins[++p].c),ins[p].num--;
                    
                    last=ins[p].c;
                }
                count+=up;
            }
        }
        cout<<endl;        
    }
    return 0;
}

 

posted on 2020-04-02 14:29  Aminers  阅读(265)  评论(0编辑  收藏  举报

导航