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

Xtreme 10.0 - Painter's Dilemma

题目来源 第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 c1c2, …, 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 c1c2, …, 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

2
5
7 7 2 11 7
10
9 1 7 6 9 9 8 7 6 7

Sample Output

3
6

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 = ;
const int max_change = ;
const int max_time = ; int minChange(vector<int> &colors) {
int count[max_time][num_color+][num_color+];
for(int c1=; c1<num_color+; c1++) {
for(int c2=; c2<num_color+; c2++) {
count[][c1][c2] = max_change;
}
}
count[][num_color][num_color] = ;
for(int t=; t<=colors.size(); t++) {
for(int c1=; c1<num_color+; c1++) {
for(int c2=; c2<num_color+; c2++) {
int min_change = max_change;
if(c1 == colors[t-] || c2 == colors[t-]) {
for(int c=; c<num_color+; c++) {
min_change = min(min_change, count[t-][c1][c] + );
}
for(int c=; c<num_color+; c++) {
min_change = min(min_change, count[t-][c][c2] + );
}
count[t][c1][c2] = min(min_change, count[t-][c1][c2]);
}
else {
count[t][c1][c2] = max_change;
}
}
}
} int min_change = max_change;
for(int c1=; c1<num_color+; 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=; t<T; t++) {
int n_color;
cin >> n_color;
vector<int> colors;
int color;
for(int c=; c<n_color; c++) {
cin >> color;
colors.push_back(color-);
}
cout << minChange(colors) << endl;
}
return ;
}

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

IEEEXtreme 10.0 - Painter's Dilemma的更多相关文章

  1. IEEEXtreme 10.0 - Inti Sets

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Inti Sets 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank.c ...

  2. IEEEXtreme 10.0 - Ellipse Art

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Ellipse Art 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank ...

  3. IEEEXtreme 10.0 - Counting Molecules

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Counting Molecules 题目来源 第10届IEEE极限编程大赛 https://www.hac ...

  4. IEEEXtreme 10.0 - Checkers Challenge

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Checkers Challenge 题目来源 第10届IEEE极限编程大赛 https://www.hac ...

  5. IEEEXtreme 10.0 - Game of Stones

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Game of Stones 题目来源 第10届IEEE极限编程大赛 https://www.hackerr ...

  6. IEEEXtreme 10.0 - Playing 20 Questions with an Unreliable Friend

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Playing 20 Questions with an Unreliable Friend 题目来源 第1 ...

  7. IEEEXtreme 10.0 - Full Adder

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Full Adder 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank. ...

  8. IEEEXtreme 10.0 - N-Palindromes

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - N-Palindromes 题目来源 第10届IEEE极限编程大赛 https://www.hackerra ...

  9. IEEEXtreme 10.0 - Mysterious Maze

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Mysterious Maze 题目来源 第10届IEEE极限编程大赛 https://www.hacker ...

随机推荐

  1. 【HEOI 2018】制胡窜

    转载请注明出处:http://www.cnblogs.com/TSHugh/p/8779709.html YJQ的题解把思路介绍得很明白,只不过有些细节说得还是太笼统了(不过正经的题解就应该这个样子吧 ...

  2. 扶苏的bitset浅谈

    bitset作为C++一个非常好用的STL,在一些题目中巧妙地使用会产生非常不错的效果.今天扶苏来分享一点bitset的基础语法和应用 本文同步发布于个人其他博客,同时作为P3674题解发布. 本文感 ...

  3. 图像处理之均值滤波介绍及C算法实现

    1 均值滤波介绍 滤波是滤波是将信号中特定波段频率滤除的操作,是从含有干扰的接收信号中提取有用信号的一种技术. 均值滤波是典型的线性滤波算法,它是指在图像上对目标像素给一个模板,该模板包括了其周围的临 ...

  4. angular2 获取到的数据无法实时更新的问题

    在修改完组件数据之后调用下面两句: this.changeDetectorRef.markForCheck(); this.changeDetectorRef.detectChanges(); 注入到 ...

  5. 手脱UPX v0.89.6 - v1.02

    声明: 只为纪录自己的脱壳历程,高手勿喷 这个壳的脱法很多一般都一步直达的,步过我喜欢ESP定律 1.载入OD,在入口下一行ESP定律运行一次 > pushad ; //入口 BE mov es ...

  6. 删除rabbitmq中持久化的队列和数据

    在windows中的rabbitmq安装目录中的/sbin目录下: rabbitmqctl.bat stop_app rabbitmqctl.bat reset rabbitmqctl start_a ...

  7. 斯皮尔曼等级相关(Spearman’s correlation coefficient for ranked data)

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...

  8. DINSTINCT

    DISTINCT是对结果集进行去重,有三点需要注意. 1.在大多数情况下(两者后面所跟的字段相同时),DISTINCT跟group by是等效的,此时DISTINCT可以看作group by的一个特例 ...

  9. Spring整合JMS(一)——基于ActiveMQ实现 (转)

    *注:别人那复制来的 1.1     JMS简介 JMS的全称是Java Message Service,即Java消 息服务.它主要用于在生产者和消费者之间进行消息传递,生产者负责产生消息,而消费者 ...

  10. js实现数组排序

    1. JavaScript的sort()方法 var array = [1,4,-8,-3,6,12,9,8]; function compare(val1,val2){ return val1-va ...