题目来源: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. 模拟Hibernate框架的小demo

    该程序为尚学堂马士兵老师讲解,模拟了hibernate的原理,主要应用了字符串拼接,反射知识. step1,新建数据库 use jd; create table _student( _id int(1 ...

  2. codeblock快捷键

    一款开源的C/C++ IDE(集成开发环境),基于wxWidgets GUI体系,跨平台支持. 从别处粘贴的,方便以后看,啦啦啦…… 编辑器 快捷键 功能 Ctrl+Z 恢复上一次操作 Ctrl+Sh ...

  3. linux脚本:ftp自动传输文件

    使用Shell脚本实现ftp的自动上传下载 http://liwenge.iteye.com/blog/566515 open 192.168.1.171 user guest 123456cd /h ...

  4. HDU 3923 Invoker 【裸Polya 定理】

    参考了http://blog.csdn.net/ACM_cxlove?viewmode=contents           by---cxlove 的模板 对于每一种染色,都有一个等价群,例如旋转, ...

  5. stdcall、cdecl、fastcall、thiscall 、naked call的汇编详解

    函数调用规范   当高级语言函数被编译成机器码时,有一个问题就必须解决:因为CPU没有办法知道一个函数调用需要多少个.什么样的参数.即计算机不知道怎么给这个函数传递参数,传递参数的工作必须由函数调用者 ...

  6. jsp字段判空

    是对象吧String jsp的写法 <% if(str == null) { %> str is null <% } else { %> str not null <% ...

  7. UTL_RAW

    The UTL_RAW package provides SQL functions for manipulating RAW data types. 该包的功能其实可以用来加密: SELECT    ...

  8. keepalived 结合mysql 自动切换

    启动keepalived:/usr/local/sbin/keepalived -D -d -S 0 master ip:192.168.32.6 master:/root/sbin# cat /et ...

  9. JDBC连接SQL server与ADO.NET连接Sql Server对比

    JDBC连接SQL server与ADO.NET连接Sql Server对比 1.JDBC连接SQL server 1)java方面目前有很多驱动能够驱动连接SQL servernet.   主流的有 ...

  10. Java+7入门经典 -1 简介

    第一章 Java简介 1.1 Java概览 applet程序可嵌入网页; Java会通过隐式检测禁止Java applet的恶意代码; Java Server Pages-JSP 创建服务器应用程序, ...