Day1 - A - Painting the sticks

A - Painting the sticks

Time Limit:3000MS     MemoryLimit:0KB     64bit IO Format:%lld &%llu

Description

Captain Raylikes sticks and spares no effort to find sticks of various colors. It isreally a tedious work which Ray has been tired of. As the ICPC Regional contestis coming, he has no time to do it. Consequently he orders his teammate Neal todo this work.

Neal does notlike this work either, so he decides just to paint some of the sticks andprovide them for Ray (Of course, he will not let Ray know this,otherwise ... ).


Neal tells Ray that he has found many sticks, all of which contain no more thanthree colors (denoted as 1, 2, 3). Ray divides each stick into n parts( n is no more than 30), and let Neal know exactly the wantedcolor of each part. Neal will paint each stick in several steps with thefollowing rules in each step:

1.     He can only paint at most three consecutive blocks at atime. (Each block contains one part or several consecutive parts)

2.     The color in the same block must be the same.

3.     After Neal finish painting the sticks, the color of thestick must be same as Ray wanted.

Now it is Neal'stime to paint the sticks. To finish this job as soon as possible, he wants tominimize the number of step.

You may assumethat all sticks are of no color at the beginning.

Input

The input filecontains multiple test cases. For each test case, there is a positive integer n ,described above, on the first line. Then nnumbers (each number is1, 2, or 3) come in the second line indicating the colors of n parts.Proceed to the end of the file.

Output

For each testcase, output the least number of steps that Neal needs to paint a stick. Pleaseoutput the results as in the Sample Output.

Sample Input

2

1 2

 

3

1 2 3

Sample Output

Case 1: 1

Case 2: 1

 

题目大意

给你一个颜色序列,需要你来涂色,连续且有相同颜色的可以作为一个块来涂色,每次最多可以涂3个块的颜色,问对于给定的序列,最少的涂色步骤。

题目分析

简单的线性分析,统计有多少的颜色块,3块一组,计算多少组即可O(n)的算法。

AC代码

#include <iostream>
using namespace std;

int main()
{
    int n,T=1;
    while (cin>>n && n)
    {
        int i,j=0,t,ans=0;
        for (i=1;i<=n;i++)
        {
            cin>>t;
            if(t!=j)
            {
                j=t;
                ans++;
            }
        }
        if(ans%3==0)
            cout<<"Case "<<T<<": "<<ans/3<<endl;
        else
            cout<<"Case "<<T<<": "<<ans/3+1<<endl;
        T++;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值