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 ...
随机推荐
- SSL/TLS原理详解
本文大部分整理自网络,相关文章请见文后参考. 关于证书授权中心CA以及数字证书等概念,请移步 OpenSSL 与 SSL 数字证书概念贴 ,如果你想快速自建CA然后签发数字证书,请移步 基于OpenS ...
- fedora 设置命令别名
用命令 alias 举例: alias ggw="g++ -g -Wall" ggw 是自定义的别名,可根据需要进行修改设置,等于后面的则是别名的具体含义,在终端输入ggw就像当于 ...
- PDO--PHP Data Objects
PDO的环境配置:开启支持PDO 在php.ini配置文件里开启: extension=php_pdo.dll extension=php_pdo_mysql.dll 在PDO操作中涉及到类:PDO. ...
- python手记(41)
python opencv图片融合 #!/usr/bin/env python #-*- coding: utf-8 -*- #code:myhaspl@qq.com import cv2 impor ...
- 数学之路(3)-机器学习(3)-机器学习算法-SVM[9]
我们应用SVM的非线性分类功能对手写数字进行识别,我们在这应用poly做为非线性核 svm = mlpy.LibSvm(svm_type='c_svc', kernel_type='poly',gam ...
- css中的@inport 与link
在html 代码中我们常常用分离的思想引入外部的css文件,常用的方法有2种,@import 语法: <style type="text/css" media="s ...
- 【JavaScript】标签样式中多出了element.style
是因为使用了js增加或修改了样式.
- 原生js仿jquery--animate效果
效果 代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- 以正确的方式开源 Python 项目
以正确的方式开源 Python 项目 大多数Python开发者至少都写过一个像工具.脚本.库或框架等对其他人也有用的工具.我写这篇文章的目的是让现有Python代码的开源过程尽可能清 晰和无痛.我不是 ...
- commoncrawl 源码库是用于 Hadoop 的自定义 InputFormat 配送实现
commoncrawl 源码库是用于 Hadoop 的自定义 InputFormat 配送实现. Common Crawl 提供一个示例程序 BasicArcFileReaderSample.java ...