SRM 223 Div II Level Two: BlackAndRed,O(N)复杂度
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3457&rd=5869
解答分析:http://community.topcoder.com/tc?module=Static&d1=match_editorials&d2=srm223
这道题目最直接最暴力的算法就是遍历每个位置,然后查看是否满足条件,满足条件的话则立刻停止遍历,
这样算法的时间复杂度为O(N^2)。不过还有一个更高效的方法,其时间复杂度为O(N),只需进行一次遍历
就可以了。该题目的作者给出了最好的解答,如下:
To visualize the O(N) solution, make a graph where the value at position x is the number of black cards in the first x cards, minus the number of red cards in the first x cards. x increases as each new card is turned over, and the graph goes up one unit if it is black, and down one unit if it is red.
Here is graph for the "RBBRRRRBBBRBBRRBRBBRRRRBBBBBRRRB":
/\
/\ /\ /\ / \
\/ \ /\/ \/\/ \ / \/
\ / \ /
\/ \/
Notice that this graph ends up at the same level at which it starts, because there are an equal number of red and black cards. If the value ever dips below the starting value (as it does in this graph), that means you lose the game. Cutting the deck is equivalent to moving the same number of line segments from the front of the graph to the end. So finding the right place to cut the deck is equivalent to finding the right place to cut the graph such that the value never dips below the starting value. This point is, obviously, at the minimum of the graph. If there are more than one, you select the left-most minimum (corresponding to the cut with the fewest cards.) In this example, that point is seven units from the left, so the answer is 7.
实现该算法的代码如下:
#include <string>
#include <vector>
using namespace std; class BlackAndRed {
public:
int cut(string deck) {
int res = 0;
int minpoint = 0;
int point = 0;
for (int i = 0; i < deck.size(); i++) {
if ('R' == deck[i]) {
--point;
} else {
++point;
}
if (point < minpoint) {
minpoint = point;
res = i + 1;
}
}
return res;
}
};
SRM 223 Div II Level Two: BlackAndRed,O(N)复杂度的更多相关文章
- 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 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 582 Div II Level Three: ColorTheCells, Brute Force 算法
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12581 Burte Force 算法,求解了所有了情况,注意 ...
- 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, ...
- SRM 212 Div II Level One: YahtzeeScore
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=1692&rd=5858 比较简单. 代码如下: #inc ...
随机推荐
- Qt技巧:Win7下打包发布Qt程序(解释的比较清楚,把exe和dll伪装合并成一个文件)
转自:http://www.stardrad.com/blog/qt-5%E7%A8%8B%E5%BA%8F%E5%9C%A8windows%E4%B8%8A%E7%9A%84%E5%8F%91%E5 ...
- hibernate 事务的隔离级别 5.1
脏读不可重复读幻读可序列化(符合事务的四个特性的正常情况 ) 解释: 脏读:事务A对数据1做了更新,但是还没有来得及提交 此时事务B对数据1进行了查询获得了事务A更新后的数据, 但是事务A因为一些原因 ...
- Windows Phone 8初学者开发—第2部分:安装Windows Phone SDK 8.0
原文 Windows Phone 8初学者开发—第2部分:安装Windows Phone SDK 8.0 原文地址:http://channel9.msdn.com/Series/Windows-Ph ...
- HDU4731+找规律
规律题!!! /* */ #include<algorithm> #include<iostream> #include<string.h> #include< ...
- swift菜鸟入门视频教程-07-闭包
本人自己录制的swift菜鸟入门,欢迎大家拍砖.有什么问题能够在这里留言. 主要内容: 闭包表达式(Closure Expressions) 跟随闭包(Trailing Closures) 值捕获(C ...
- C++ cout 如何保留小数输出
参考 : http://upliu.net/how-cout-out-2-precision.html 大家都知道用 C 语言中 printf () 函数可以非常方便控制保留 几位小数输出 不过在 C ...
- Docker 安装命令
curl -sSL https://get.daocloud.io/docker | sh
- Oracle Dedicated server 和 Shared server(专用模式 和 共享模式) 说明(转)
一. 官网说明 在DBCA 建库的时候,有提示让我们选择连接类型,这里有两种类型:专用服务器模式和共享服务器模式.默认使用专用模式.如下图: Oracle 官方文档对这两种文档的说明如下: Abou ...
- 基于visual Studio2013解决C语言竞赛题之0603打印素数
题目
- 提高mysql查询效率的六种方法
1,表设计一定要优化,冗余数据最少,少用连接查询.如果在实际应用中,使用了极其复杂的连接,子查询,则数据表的设计得要重新考虑了. 2,尽量用char而不是varchar,因为固定长度得string用起 ...