2022 Shanghai Collegiate Programming Contest - H. Heirloom Painting

H. Heirloom Painting

题目描述

Little Desprado2, the great artist, has a small painting robot to do some artistic creations.
Today, he draws a ring divided by n grids, and there are m kinds of colors. He wants to paint the ring as he wants. However, because of some technical issues – using a heirloom printer nozzle to save cost, for example – the robot will paint exactly k continuous grids with the same color each time. In addition, the strong organic pigment can overlay the previous paintings, which means that the color applied later will replace the previous color.

Little Desprado2 wants to know the minimum number of times that his robot should paint from the empty grids to a given pattern, or it’s impossible to do so.

输入描述

The first line contains one integer T (1≤T≤105), denoting the number of test cases.

For each test case, the first line contains three integers n, m and k (1≤n,m≤106,1≤k≤n), denoting the number of grids, colors and grids the robot will paint each time, respectively. The second line contains n numbers c1, c2, …, cn (1≤ci≤m), ci denotes the color of the i-th grid that little Desprado2 wants.

There are no color at the beginning, and you can consider the uncolored grids with color −1 for simplicity.

It is guaranteed that sum of n over all test cases won’t exceed 106.

输出描述

For each test case, print a single integer in a separated line - the minimal times the robot should paint. If the mission is impossible, print −1.

输入样例1:

3
11 4 2
1 1 1 2 2 3 3 3 4 4 1
5 2 1
1 2 1 2 1
6 2 2
1 2 1 2 1 2

输出样例1:

6
5
-1

思路

若存在一段相同颜色的区间长度大于等于 k k k 那么涂出来的区间(长出来的部分一定小于 k)必定可以被其完全覆盖 且不会溢出到别的区间 故判断完合法性后只需将每段区间需要涂的次数求和即可

Code

#include<bits/stdc++.h>
using namespace std;
#define __T int csT;scanf("%d",&csT);while(csT--)
const int mod=998244353;

int n,m,k,ans,mx;
int c[1000003],v[1000003];
stack<int> st;
int ask(int i)//统计每段区间的长度
{
    int l=i,r=i,len=1;
    l=(l-1+n)%n;
    r=(r+1)%n;
    while(v[l]==0&&c[l]==c[i])
    {
        v[l]=1;
        l=(l-1+n)%n;
        ++len;
    }
    while(v[r]==0&&c[r]==c[i])
    {
        v[r]=1;
        r=(r+1)%n;
        ++len;
    }
    st.push(len);
    return len;
}
inline void sol()
{
    while(!st.empty())
    {
        st.pop();
    }
    mx=0;
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0;i<n;++i)
    {
        scanf("%d",&c[i]);
        v[i]=0;
    }
    for(int i=0;i<n;++i)
    {
        if(v[i]==0)
        {
            v[i]=1;//保证每个点只统计一次
            mx=max(mx,ask(i));
        }
    }
    if(mx<k)
    {
        puts("-1");
        return;
    }
    ans=(int)ceil((double)mx/(double)k);
    int tp,f=1;
    while(!st.empty())
    {
        tp=st.top();
        st.pop();
        if(tp==mx&&f==1)
        {
            f=0;
            continue;
        }
        ans+=(int)ceil((double)tp/(double)k);
    }
    printf("%d\n",ans);
}

int main()
{
    __T
    sol();
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柯西可乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值