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 ...
随机推荐
- XPath与多线程爬虫
XPath是一门在xml中查询信息的语言安装使用XPath 1.安装lxml库 window:pip install lxmllinux:sudo pip install lxml国内安装缓慢,建议到 ...
- haproxy 访问www.zjdev.com 自动跳转到appserver_8001 对应的nginx
# # acl zjdev_7_req hdr_beg(host) -i www.zjdev.com # use_backend appserver_8001 if zjdev_7_req
- 省部级干部list
省部级干部表 省(市.自治区) 省(市)委书记 省(市)人大主任 省(市)长 省(市)政协主席 北京市 郭金龙 杜德印 王安顺 吉林 天津市 黄兴国 肖怀远 黄兴国 臧献甫 上海市 韩正 殷一璀(女) ...
- 同时支持多家云平台的管理工具HybridFox
偶然间发现了这个firefox上的开元插件 号称支持AWS,Eucalyptus,OpenStack,OpenNebula 目的是通过一个入口实现异种云平台的管理 主要功能包括: Manage Ima ...
- 怎样使用windows命令行,用notepad打开某文件夹下面的所有文件
http://zhidao.baidu.com/question/2138815012359999388.html __________________________________________ ...
- [Swust OJ 610]--吉祥数
题目链接:http://acm.swust.edu.cn/problem/610/ Time limit(ms): 1000 Memory limit(kb): 65535 Description ...
- FileDescriptor
FileDescriptor 在java中的java.io包下面 public final class FileDescriptor { ... } 官方的解释: 文件描述符类的实例用作与基础机器有关 ...
- ios7禁止默认划动返回
self.navigationController.interactivePopGestureRecognizer.enabled = NO; 或 在使用之前先要判断是否ios7,不然会导致crash ...
- 为什么要用BASE64
BASE64和其他相似的编码算法通常用于转换二进制数据为文本数据,其目的是为了简化存储或传输.更具体地说,BASE64算法主要用于转换二进 制数据为ASCII字符串格式.Java语言提供了一个非常好的 ...
- python IDE ulipad配置使用
一直认为认为python自带的编辑器idle实在是太简陋了,连显示行号的功能都没有,也非常不好编辑.找了下windows平台下的Ide,发现ulipad很多人推荐使用,就开始安装了. 首先去官网: h ...