SRM 582 Div II Level Three: ColorTheCells, Brute Force 算法
题目来源: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 算法的更多相关文章
- SRM 582 Div II Level One: SemiPerfectSquare
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12580 比较简单,代码如下: #include <ios ...
- SRM 582 Div II Level Two SpaceWarDiv2
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12556 #include <iostream> # ...
- SRM 212 Div II Level Two: WinningRecord,Brute Force
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3003&rd=5858 比较简单. 代码如下: #inc ...
- SRM 223 Div II Level Two: BlackAndRed,O(N)复杂度
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3457&rd=5869 解答分析:http://comm ...
- SRM 207 Div II Level Two: RegularSeason,字符串操作(sstream),多关键字排序(操作符重载)
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=2866&rd=5853 主要是要对字符串的操作要熟悉,熟 ...
- SRM 577 Div II Level Two: EllysRoomAssignmentsDiv2
题目来源: http://community.topcoder.com/tc?module=ProblemDetail&rd=15497&pm=12521 这个问题要注意的就是只需要直 ...
- SRM 583 Div II Level One:SwappingDigits
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12609 #include <iostream> # ...
- SRM 583 Div II Level Three:GameOnABoard,Dijkstra最短路径算法
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12556 用Dijkstra实现,之前用Floyd算法写了一个, ...
- SRM 219 Div II Level One: WaiterTipping,小心约分
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12609&rd=15503 这题目看上去so easy, ...
随机推荐
- Codeforces Round #262 (Div. 2) 460C. Present(二分)
题目链接:http://codeforces.com/problemset/problem/460/C C. Present time limit per test 2 seconds memory ...
- STL之Queue(Q)
STL的Queue(数据结构中的队列): 特点:FIFO 先进先出: 自适应容器(即容器适配器) 栈适配器STL queue STL中实现的Queue: 用list来实现queue: queue ...
- [Swust OJ 1084]--Mzx0821月赛系列之情书(双线程dp)
题目链接:http://acm.swust.edu.cn/problem/1084/ Time limit(ms): 1000 Memory limit(kb): 65535 Descriptio ...
- DOM操作HTML文档
概述 之前写过一些关于DOM方法的知识,理论方法的偏多,所以,这篇博客主要是实践方面的Demo,如果,大家觉得理论方面有所欠缺,大家可以看看这几篇博客:JavaScript总结(一.基本概念)和Jav ...
- Android:ServiceDemo
效果图: layout的main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLay ...
- ITextSharp 初次接触
官网:http://www.itextpdf.com/ (英文好的建议看这里) 下面我就对itextsharp做一个初步的介绍,并把最近封装的一个用于生成pdf的类库提供给需要的朋友,对于大神你可以 ...
- 提高你开发效率的十五个Visual Studio 2010使用技巧
提高你开发效率的十五个Visual Studio 2010使用技巧 相信做开发的没有不重视效率的.开发C#,VB的都知道,我们很依赖VS,或者说,我们很感谢VS.能够对一个IDE产生依赖,说明这个ID ...
- Android学习笔记:ListView简单应用--显示文字列表
在activity中的编写如下代码: final List<String> items = new ArrayList<String>(); //设置要显示的数据,这里因为是例 ...
- u-boot分析——struct gd_t与struct bd_t
gd_t和bd_t是u-boot中两个重要的数据结构,在初始化操作很多都要靠这两个数据结构来保存或传递.分别定义在./include/asm/global_data.h和./include/asm/u ...
- 基于visual Studio2013解决算法导论之052深度优先
题目 深度优先 解决代码及点评 // 深度优先.cpp : 定义控制台应用程序的入口点. // // 图的邻接表表示.cpp : 定义控制台应用程序的入口点. // #include < ...