IEEEXtreme 10.0 - Painter's Dilemma
这是 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 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
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的更多相关文章
- IEEEXtreme 10.0 - Inti Sets
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Inti Sets 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank.c ...
- IEEEXtreme 10.0 - Ellipse Art
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Ellipse Art 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank ...
- IEEEXtreme 10.0 - Counting Molecules
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Counting Molecules 题目来源 第10届IEEE极限编程大赛 https://www.hac ...
- IEEEXtreme 10.0 - Checkers Challenge
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Checkers Challenge 题目来源 第10届IEEE极限编程大赛 https://www.hac ...
- IEEEXtreme 10.0 - Game of Stones
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Game of Stones 题目来源 第10届IEEE极限编程大赛 https://www.hackerr ...
- IEEEXtreme 10.0 - Playing 20 Questions with an Unreliable Friend
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Playing 20 Questions with an Unreliable Friend 题目来源 第1 ...
- IEEEXtreme 10.0 - Full Adder
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Full Adder 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank. ...
- IEEEXtreme 10.0 - N-Palindromes
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - N-Palindromes 题目来源 第10届IEEE极限编程大赛 https://www.hackerra ...
- IEEEXtreme 10.0 - Mysterious Maze
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Mysterious Maze 题目来源 第10届IEEE极限编程大赛 https://www.hacker ...
随机推荐
- 使用Hystrix进行微服务降级管理
前言:目前我们的项目是微服务架构,基于dubbo框架,服务之间的调用是通过rpc调用的.刚开始没有任何问题,项目运行健康.良好.可是过了一段时间,线上总有人反应查询订单失败,等过了一段时间才能查到.这 ...
- json数据的格式
JSON的具体形式 1.对象是一个无序的“‘名称/值’对”集合.一个对象以“{”开始,以“}”结束.每个“名称”后跟一个“:”,“‘名称/值’对”之间使用“,”分隔. 举个例子: { name:&qu ...
- get与post请求简单理解
一般在浏览器中输入网址访问资源都是通过GET方式:在FORM提交中,可以通过Method指定提交方式为GET或者POST,默认为GET提交 Http定义了与服务器交互的不同方法,最基本的方法有4种,分 ...
- .Net并行编程之二:并行循环
本篇内容主要包括: 1.能够转化为并行循环的条件 2.并行For循环的用法:Parallel.For 3.并行ForEach的用法Parallel.ForEach 4.并行LINQ(PLINQ)的用法 ...
- SQL Server 2008如何开启数据库的远程连接
SQL Server 2008默认是不允许远程连接的,如果想要在本地用SSMS连接远程服务器上的SQL Server 2008,远程连接数据库.需要做两个部分的配置: 1,SQL Server Man ...
- base64解码
网络传输经常用base64编码的数据,因此我们需要将其解码成正常字符集合. base64.h #ifdef __cplusplus extern "C" { #endif char ...
- 使用EA软件画数据库图表
使用EA软件可以画出数据库的图表并生成SQL语句,非常方便,下面介绍一下步骤 1.先创建一个默认的工程 2.新建一个视图 3.在视图中添加一个图表 4.使用图表工具箱画表 没有出现toolbox的话, ...
- Eclipse中 如何实现 多行同时编辑
在编辑的时候按下 SHIFT + ALT +A 之后 鼠标变为 + 号 选择要同时编辑几行 即可编辑(现在eclipse好像只能是编辑一块地方 不能像vs那样 任何地方可以同时编辑 这点很 ...
- 一小时了解数据挖掘⑤数据挖掘步骤&常用的聚类、决策树和CRISP-DM概念
一小时了解数据挖掘⑤数据挖掘步骤&常用的聚类.决策树和CRISP-DM概念 接前面系列4篇: 一小时了解数据挖掘①:解析常见的大数据应用案例 一小时了解数据挖掘②:分类算法的应用和成熟案例解析 ...
- java多线程机制2(安全问题)
线程状态图: ================================================================================= /* * 线程安全问题 ...