题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12581

Burte Force 算法,求解了所有了情况,注意 next_permutation 函数的用法。

#include <iostream>
#include <vector>
#include <limits>
#include <algorithm> using namespace std; class ColorTheCells
{
private:
vector <int> dryingTime; /* 需要干燥的时间 */
vector <long> dryedTime; /* 正好干燥的时间,0表示还没有paint */
vector <int> paint_order; /* paint格子的顺序 */
public:
long calc(int seq);
int minimalTime(vector <int> dryingTime);
}; #define MOVE_TIME 1L
#define PAINT_TIME 1L
#define INFINITY_TIME LONG_MAX /* 参考Ueddy代码实现 */
int ColorTheCells::minimalTime(vector<int> dryingTime)
{
int num = dryingTime.size();
long minTime = INFINITY_TIME;
int i, j; this->dryingTime = dryingTime;
paint_order.clear(); /* paint 的顺序 */
dryedTime.clear(); for (i = 0; i < num; i++) {
paint_order.push_back(i);
dryedTime.push_back(0);
} /* brute force,枚举所有的情况,选出最小时间 */
do {
for (i = 0; i < (1<<num); i++) {
for (j = 0; j < num; j++) {
dryedTime[j] = 0;
}
minTime = min(minTime, calc(i));
}
} while (next_permutation(paint_order.begin(), paint_order.end())); return minTime;
} /**
* seq为一个序列,0表示在被paint位置左边paint, 1表示在右边paint,paint的顺序由
* paint_order指定。
*/
long ColorTheCells::calc(int seq)
{
long time; /* 所用时间 */
int num = dryingTime.size();
int paint_dir; /* paint方向,在被paint位置左边还是右边paint */
int cur_pos; /* 当前位置 */
int paint_pos; /* paint位置,不是被paint的位置 */
int bias; /* 从当前位置向左走还是向右走 */ cur_pos = 0;
time = 0;
for (int i = 0; i < num; i++) {
paint_dir = seq & 1;
seq /= 2;
paint_dir = paint_dir ? 1 : -1;
paint_pos = paint_order[i] + paint_dir;
if (paint_pos < 0 || paint_pos >= num) { /* 位置不合法 */
return INFINITY_TIME;
} bias = 1; /* 从当前位置向右走 */
if (cur_pos - paint_pos > 0) { /* 从当前位置向左走 */
bias = -1;
} /* 用for循环到达paint位置,中间遇到还没有干燥的格子要等待 */
for (; cur_pos != paint_pos; cur_pos += bias) {
if (dryedTime[cur_pos+bias] > time) { /* 等待干燥 */
time = dryedTime[cur_pos+bias];
}
time += MOVE_TIME; /* 移动时间 */
}
time += PAINT_TIME; /* 到达目的地,paint时间 */
dryedTime[paint_order[i]] = time + dryingTime[paint_order[i]]; /* 更新该格子干燥时间 */
} return time;
}

SRM 582 Div II Level Three: ColorTheCells, Brute Force 算法的更多相关文章

  1. SRM 582 Div II Level One: SemiPerfectSquare

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12580 比较简单,代码如下: #include <ios ...

  2. SRM 582 Div II Level Two SpaceWarDiv2

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12556 #include <iostream> # ...

  3. SRM 212 Div II Level Two: WinningRecord,Brute Force

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3003&rd=5858 比较简单. 代码如下: #inc ...

  4. SRM 223 Div II Level Two: BlackAndRed,O(N)复杂度

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3457&rd=5869 解答分析:http://comm ...

  5. SRM 207 Div II Level Two: RegularSeason,字符串操作(sstream),多关键字排序(操作符重载)

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=2866&rd=5853 主要是要对字符串的操作要熟悉,熟 ...

  6. SRM 577 Div II Level Two: EllysRoomAssignmentsDiv2

    题目来源: http://community.topcoder.com/tc?module=ProblemDetail&rd=15497&pm=12521 这个问题要注意的就是只需要直 ...

  7. SRM 583 Div II Level One:SwappingDigits

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12609 #include <iostream> # ...

  8. SRM 583 Div II Level Three:GameOnABoard,Dijkstra最短路径算法

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12556 用Dijkstra实现,之前用Floyd算法写了一个, ...

  9. SRM 219 Div II Level One: WaiterTipping,小心约分

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12609&rd=15503 这题目看上去so easy, ...

随机推荐

  1. git 使用过程(一、准备工作)

    没有多余的废话,只是纯粹的捞干的记录下一自己使用Git的过程.(正在学习中,也是刚接触,记录过程可能有点乱.等熟悉之后在重新整理一下) 一.先下载git客户端 网址:http://msysgit.gi ...

  2. Opencv2系列学习笔记2(图像的其它操作)

    本节主要涉及到图像的领域.算术操作以及如何操作图像感兴趣的区域. 一:邻域操作 以下例子主要对图像进行锐化.基于拉普拉斯算子<后面讨论>.这幅图像的边缘部分将得到放大,细节部分将更加的锐利 ...

  3. 深入理解GCD ( 二 )

    转自@nixzhu的GitHub主页(译者:Riven.@nixzhu),原文<Grand Central Dispatch In-Depth: Part 2/2> 欢迎来到GCD深入理解 ...

  4. Python框架 Flask 项目实战教程

    本文目的是为了完成一个项目用到的flask基本知识,例子会逐渐加深.最好对着源码,一步一步走.下载源码,运行pip install -r requirements.txt 建立环境python db_ ...

  5. Codeforces 482 - Diverse Permutation 构造题

    这是一道蛮基础的构造题. - k         +(k - 1)      -(k - 2) 1 + k ,    1 ,         k ,             2,    ....... ...

  6. 【Dior风格/舒适防风面料/抗静电里衬/大身撞色拼接/精致平驳领/时尚便西款/蓝绿色】玛萨玛索男装网购商城

    [Dior风格/舒适防风面料/抗静电里衬/大身撞色拼接/精致平驳领/时尚便西款/蓝绿色]玛萨玛索男装网购商城 [特价商品] Dior风格/舒适防风面料/抗静电里衬/大身撞色拼接/精致平驳领/时尚便西款 ...

  7. stm32之CAN总线基础

    can总线协议概述: CAN是Controller Area Network的缩写,由德国博世公司开发:CAN通过ISO11891以及ISO11519进行了标准化:     CAN总线的特点: 1.多 ...

  8. UVa 10330 Power Transmission / 最大流

    最大流 这题有很多起点和终点 在取2个点(0和n+1) 作为唯一的起点和终点 此外每个点也有容量限制 建图时每条边上的容量为这条边和2个端的容量的最小值 然后EK就行 #include <cst ...

  9. [置顶] 深入浅出MongoDB(三)环境搭建

    上次的博文深入浅出MongoDB(二)概述中我们已经将MongoDB的相关概念讲解了一下,接下来我们继续进行MongoDB学习.在学习之前,大家首先需要在自己的电脑上安装MongoDB. 1.安装 安 ...

  10. android:android:background="#00000000",透明效果

    ImageButton中设置 android:background="#00000000",可以达到透明效果 具体的源码 管理-->文件中的 viewpager0829.ra ...