题目来源: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)复杂度的更多相关文章

  1. SRM 207 Div II Level Two: RegularSeason,字符串操作(sstream),多关键字排序(操作符重载)

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=2866&rd=5853 主要是要对字符串的操作要熟悉,熟 ...

  2. SRM 577 Div II Level Two: EllysRoomAssignmentsDiv2

    题目来源: http://community.topcoder.com/tc?module=ProblemDetail&rd=15497&pm=12521 这个问题要注意的就是只需要直 ...

  3. SRM 582 Div II Level One: SemiPerfectSquare

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12580 比较简单,代码如下: #include <ios ...

  4. SRM 582 Div II Level Two SpaceWarDiv2

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12556 #include <iostream> # ...

  5. SRM 582 Div II Level Three: ColorTheCells, Brute Force 算法

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12581 Burte Force 算法,求解了所有了情况,注意  ...

  6. SRM 583 Div II Level One:SwappingDigits

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12609 #include <iostream> # ...

  7. SRM 583 Div II Level Three:GameOnABoard,Dijkstra最短路径算法

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12556 用Dijkstra实现,之前用Floyd算法写了一个, ...

  8. SRM 219 Div II Level One: WaiterTipping,小心约分

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12609&rd=15503 这题目看上去so easy, ...

  9. SRM 212 Div II Level One: YahtzeeScore

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=1692&rd=5858 比较简单. 代码如下: #inc ...

随机推荐

  1. java学习之总结

    学的时候忘记写博客,现在java SE学完了一口气把写的代码发了上来没有做什么补充,其中有很多知识漏掉了,学的有点不扎实,接下来写写项目来稳好基础

  2. 点菜系统 pickview的简单实用

    使用pickview的时候多想想tableview的使用,观察两者的相同之处 pickview的主要用途用于选择地区  生日年月日  和点餐 示例代码 简单的pickview点餐系统//  ViewC ...

  3. [POJ 1521]--Entropy(哈夫曼树)

    题目链接:http://poj.org/problem?id=1521 Entropy Time Limit: 1000MS    Memory Limit: 10000K Description A ...

  4. Eclipse开启与关闭代码自动提示功能

        Eclipse代码里面的代码提示功能默认是关闭的,只有输入“.”的时候才会提示功能,用vs的用户可能不太习惯 这种,vs是输入任何字母都会提示,下面说一下如何修改eclipse配置,开启代码自 ...

  5. 网络编程TCP协议-聊天室

    网络编程TCP协议-聊天室(客户端与服务端的交互); <span style="font-size:18px;">1.客户端发数据到服务端.</span> ...

  6. ecshop后台添加栏目

    ecshop后台增加模块菜单详细教程 一:ecshop后台管理 admin\includes\inc_menu.php 添加上你要添加的功能admin\includes\inc_priv.php 对应 ...

  7. IOS-TextField功能方法详解

    //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, ...

  8. django-extensions

    命令行: admin后台管理扩展 后面会出现个放大镜实现搜索补齐功能. 交互式的 Python Shells(shell_plus) 实现自动导入 如果遇到apps中包含的的models名字出现冲突, ...

  9. 观django-messages包笔记

    django_messages是一个提供注册用户之间互相发送消息的django app.最近在研究其实现机制,安装测试非常容易,导入包,配好url以及syncdb生成数据库即可使用. 一.收获一: 我 ...

  10. DW8051调试终结

    都不记得自己到底揪心了多久 —— 归根结底还是自己太菜了.终于找到了DW8051移植的bug. 这么大的一个图居然没有看到,真是气煞老夫也. 在原来移植的基础之上加两个反相器就OK 了