Day1 - A - Painting the sticks

来源:互联网 发布:mysql查询当前时间 编辑:程序博客网 时间:2024/06/06 04:36

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;}



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 孩子不自信容易紧张怎么办 在人多时候紧张怎么办 考科目二很紧张怎么办 明天出成绩很紧张怎么办 一紧张就射精了怎么办 孩子在幼儿园表现不好怎么办 小孩子在幼儿园表现的不好怎么办 学籍档案写错了怎么办 发展报告填错了怎么办 学生发展报告丢了怎么办 大一新生入学团员档案袋丢失怎么办 三年级孩子语文成绩差怎么办 运气不好时 我们该怎么办 猫走了财运变差怎么办 特别在意别人的眼光怎么办 入职两个月没有业绩怎么办 在学舞蹈中孩子怕疼怎么办 我生二胎怕疼怎么办 孩子的执行力差怎么办 怀孕三个月不想要孩子怎么办 生完孩子老是掉头发怎么办 5岁半宝宝有狐臭怎么办? 4岁宝宝脾气倔怎么办 小孩子摔到后脑勺吐了怎么办 学生报告册丢了怎么办 错过宝宝语言敏感期怎么办 父亲骂我很难听怎么办 90后父母催婚怎么办 和隔壁邻居吵架了怎么办 教师上课期间孩子出问题责任怎么办 孩子一考试妈妈就焦虑怎么办 妈妈爱发脾气孩子也发脾气怎么办 孩子对家人有抵触情绪怎么办 孩子因为没耐心哭闹怎么办 宝宝哭闹真是没耐心怎么办 对自己孩子没耐心怎么办 跳了把腰伤了怎么办 爸爸对妈妈家暴怎么办 逆反心强23岁怎么办 觉得自己心理有问题怎么办 小孩心理脆弱过于敏感怎么办?