Xtreme 10.0 - Painter's Dilemma

这是 meelo 原创的 IEEEXtreme极限编程比赛题解

题目来源 第10届IEEE极限编程大赛
https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/painters-dilemma

Bob just got his first job as a house painter. Today, on his first day on the job, he has to paint a number of walls.
For those of you that have done some house painting before, you know that this is no easy task. Each wall has to be painted in multiple rounds, possibly with a different color of paint each time, and there needs to be sufficient time of waiting between consecutive rounds for the paint to dry.
To make optimal use of their time, house painters usually paint walls while waiting for other walls to dry, and hence interleave the rounds of painting different walls. But this also brings up another challenge for the painters: different walls may require different colors of paint, so they might have to replace the color on one of their paint brushes before painting that wall. But this requires washing the paint brush, waiting for it to dry, and then applying the new paint to the brush, all of which takes precious time.
The more experienced house painters circumvent the issue by bringing a lot of paint brushes. But Bob is not that fortunate, and only has two paint brushes!
Given a sequence of colors c1, c2, …, cN that Bob needs, in the order that he needs them, can you help him determine the minimum number of times he needs to change the color of one of his brushes? Both of his brushes will have no color to begin with.
Bob may ask you to compute this number for a few different scenarios, but not many. After all, he only needs to do this until he gets his first paycheck, at which point all his effort will have been worth the trouble, and he can go buy more paint brushes.

Input Format

The first line of input contains t, 1 ≤ t ≤ 5, which gives the number of scenarios.
Each scenario consists of two lines. The first line contains an integer N, the length of the sequence of colors Bob needs. The second line contains a sequence of N integers c1, c2, …, cN, representing the sequence of colors that Bob needs, in the order that he needs them. Each distinct color is represented with a distinct integer.

Constraints

1 ≤ N ≤ 500, 1 ≤ ci ≤ 20

Output Format

For each scenario, you should output, on a line by itself, the minimum number of times Bob needs to change the color of one of his brushes.

Sample Input

257 7 2 11 7109 1 7 6 9 9 8 7 6 7

Sample Output

36

Explanation

In the first scenario, Bob needs to paint using the colors 7, 7, 2, 11, and 7, in that order. He could start by applying color 7 to the first brush. Then he can use the first brush for the first two times. The third time he needs the color 2. He could apply that color to his second brush, and thus use his second brush for the third time. Next he needs the color 11, so he might apply this color to the first brush, and use the first brush this time. Finally, he needs the color 7 just as before. But the first brush no longer has this color, so we need to reapply it. Just as an example, he could apply 7 to the second brush, and then use the second brush. In total, he had to change the color of one of his brushes 4 times.
However, Bob can be smarter about the way he changes colors. For example, considering the same sequence as before, he could start by applying color 7 to the first brush, and use the first brush for the first two times. Then he could use the second brush twice, first by applying the color 2, and then by applying the color 11. This leaves the first brush with paint 7, which he can use for the last time. This leaves him with only 3 color changes in total.

题目解析
这是动态规划的题目

状态为
(时间t,第1把刷子的颜色c1,第2把刷子的颜色c2)
刷子的颜色除了20种颜色以外,还需要一种状态表示没有颜色

f(t, c1, c2)表示
画t种颜色,最终第1把刷子的颜色c1,第2把刷子的颜色c2最少换刷子的次数

状态转移函数为



其中p表示时间t需要刷子的颜色
只有c1==p或者c2==p的状态才可到达,其余状态用最大值表示不可达

初始状态为



21表示刷子没有颜色的状态

程序
C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

const int num_color = 20;
const int max_change = 100000;
const int max_time = 501;

int minChange(vector<int> &colors) {
    int count[max_time][num_color+1][num_color+1];
    for(int c1=0; c1<num_color+1; c1++) {
        for(int c2=0; c2<num_color+1; c2++) {
            count[0][c1][c2] = max_change;
        }
    }
    count[0][num_color][num_color] = 0;

    for(int t=1; t<=colors.size(); t++) {
        for(int c1=0; c1<num_color+1; c1++) {
            for(int c2=0; c2<num_color+1; c2++) {
                int min_change = max_change;
                if(c1 == colors[t-1] || c2 == colors[t-1]) {
                    for(int c=0; c<num_color+1; c++) {
                        min_change = min(min_change, count[t-1][c1][c] + 1);
                    }
                    for(int c=0; c<num_color+1; c++) {
                        min_change = min(min_change, count[t-1][c][c2] + 1);
                    }
                    count[t][c1][c2] = min(min_change, count[t-1][c1][c2]);
                }
                else {
                    count[t][c1][c2] = max_change;
                }
            }
        }
    }
    
    int min_change = max_change;
    for(int c1=0; c1<num_color+1; c1++) {
        min_change = min(min_change, count[colors.size()][c1][colors.back()]);
        min_change = min(min_change, count[colors.size()][colors.back()][c1]);
    }
    return min_change;
}


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int T;
    cin >> T;
    for(int t=0; t<T; t++) {
        int n_color;
        cin >> n_color;
        vector<int> colors;
        int color;
        for(int c=0; c<n_color; c++) {
            cin >> color;
            colors.push_back(color-1);
        }
        cout << minChange(colors) << endl;
    }
    return 0;
}

博客中的文章均为 meelo 原创,请务必以链接形式注明 本文地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,723评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,080评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,604评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,440评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,431评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,499评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,893评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,541评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,751评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,547评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,619评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,320评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,890评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,896评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,137评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,796评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,335评论 2 342

推荐阅读更多精彩内容