摘要: 对只会说废话而不去坚持的自己非常生气, 一次又一次地浪费人生, 真对不起.~~~在时间限制是1秒的情况下, 时间复杂度代入结果, 对解题策略的提示.10^6 --------- 游刃有余10^7 --------- 勉勉强强10^8 --------- 很悬, 仅限循环体十分简单的情况~~~最大子序列和问题, 的O(n)思路, 关键的问题在于, 负值前缀不可能出现在最大和的子序列中.~~~有些绝对值参与计算的题目, 一个解决思路是, 思考在什么情况下能把绝对值运算符去掉,比如: http://codeforces.com/contest/359/problem/B~~~ 阅读全文
posted @ 2013-11-05 00:32 NextLife 阅读(210) 评论(2) 推荐(0)
摘要: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1285这道题就比较单纯啦, 生成可重集的排列+dfs.为了练习 我就不用stl了. 1 // 2 // main.cpp 3 // uva10344 4 // 5 // Created by ello on 1/7/14. 6 // Copyright (c) 2014 NextLife. All rights reserved. 7 // 8 9 #include 10 #inc 阅读全文
posted @ 2014-01-07 02:48 NextLife 阅读(314) 评论(0) 推荐(0)
摘要: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=237这道题真是郁闷, TLE + WATLE是因为, 没有做好剪枝, 其中一个剪枝方法是, 假设之后所有的票都选中, 加上之前的利润, 总利润如果还是没有记录的max利润大, 那么就放弃递归, 返回上一层.WA的原因是脑残, 本来需要一个数组记录各个站点的人数来判断选中当前票是否会超出限额, 我一时短路, 用了一个错误的算法来判断超出限额, 导致W 阅读全文
posted @ 2014-01-07 01:27 NextLife 阅读(265) 评论(0) 推荐(0)
摘要: http://oj.leetcode.com/problems/trapping-rain-water/思路 就是 先从左往右走, 将递增产生的水坑给算出来, 再从右往左左走, 将递增的水坑给算出来.代码写的很劣, 居然ac了, 我得看看题解去. 1 class Solution { 2 public: 3 int trap(int A[], int n) { 4 int re = 0; 5 int cur = 0; 6 for (int i = 1; i = A[cur]; ++i) ++cur; 7 for (int ... 阅读全文
posted @ 2014-01-06 21:02 NextLife 阅读(210) 评论(0) 推荐(0)
摘要: http://oj.leetcode.com/problems/container-with-most-water/从最外侧向内侧搜索, 在长度最长的情况下 保证高度尽量高. 注意如果左边第一个直线是所有直线里最长的情况.所以我采用左右各遍历一次的方法a了.然后看了题解发现自己好弱智. 还是贴自己的沙茶代码吧. 1 class Solution { 2 public: 3 int maxArea(vector &height) { 4 int re = 0; 5 int cur = 0; 6 int i = (int)height.siz... 阅读全文
posted @ 2014-01-03 22:53 NextLife 阅读(145) 评论(0) 推荐(0)
摘要: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=480图深度遍历水题, 千万别忘记visited标志位的重置. 1 #include 2 #include 3 using namespace std; 4 5 int gn = 0; 6 int gm = 0; 7 int longest = 0; 8 #define MAX_N 26 9 10 typedef struct _graph {11 int nv;12 int ne; 阅读全文
posted @ 2013-12-22 21:46 NextLife 阅读(261) 评论(0) 推荐(0)
摘要: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=580好几天没刷题, 罪过= =看推理动漫, 玩编程游戏去了= =又是简单回溯, 注意和8皇后的区别. 1 #include 2 #include 3 4 using namespace std; 5 6 #define MAX_SIZE 10 7 int gn = 0; 8 char gmap[MAX_SIZE][MAX_SIZE] = {0}; 9 int gmax = 0;10 阅读全文
posted @ 2013-12-21 01:10 NextLife 阅读(306) 评论(0) 推荐(0)
摘要: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=152简单回溯, 这代码被我写的, 各种vector, map, set, 离了STL就活不下去的样子...代码有如下几点需要注意:1.回溯时判重用set, 自定义类型用set的时候需要重载" 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 using namesp 阅读全文
posted @ 2013-12-15 23:59 NextLife 阅读(283) 评论(0) 推荐(0)
摘要: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=37纯模拟, 要先考虑好用什么数据结构能方便的支持模拟中的各种操作, 我选择的是链表, 于是直接用的STL的List, STL真是很实用, 点赞. 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 using namespace std; 9 10 #define M... 阅读全文
posted @ 2013-12-15 15:35 NextLife 阅读(278) 评论(0) 推荐(0)
摘要: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1415这个水的要死的题...哪能看出来是回溯, 为啥刘汝佳大神要放到回溯分类里... 1 #include 2 #include 3 #include 4 5 using namespace std; 6 int main(int argc, const char * argv[]) 7 { 8 int N = 0, Q = 0; 9 int id = 1;10 while... 阅读全文
posted @ 2013-12-10 00:20 NextLife 阅读(195) 评论(0) 推荐(0)
摘要: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=670这道题 就是0-1的子集生成嘛, 无非是控制一下1的个数就好, 但是遇到的问题是怎么按照字典序输出,好吧, 我沙茶 我用的最笨的方法, 得出一个解就塞到vector里,然后sort一下,一起输出......后来一想, 子集生成还有二进制的方法, 不需要什么vector了!这道题还要注意的是输出格式: "Print a blank line between datase 阅读全文
posted @ 2013-12-08 21:02 NextLife 阅读(192) 评论(0) 推荐(0)
摘要: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=107&page=show_problem&problem=1039可重复子集生成问题, 看不懂题意的沙茶各种wa 1 // 2 // main.cpp 3 // uva10098 4 // 5 // Created by ello on 12/5/13. 6 // Copyright (c) 2013 NextLife. All rights reserved. 7 // 8 9 #include 10 # 阅读全文
posted @ 2013-12-06 01:53 NextLife 阅读(255) 评论(0) 推荐(0)
摘要: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=82老套路了, dfs + 剪枝. 我超时了好几次啊 失误,失误,忘记剪枝了. 1 // 2 // main.cpp 3 // uva146 4 // 5 // Created by ello on 12/3/13. 6 // Copyright (c) 2013 NextLife. All rights reserved. 7 // 8 9 #include 10 #include 阅读全文
posted @ 2013-12-05 22:17 NextLife 阅读(172) 评论(0) 推荐(0)
摘要: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=107&page=show_problem&problem=67不会打扑克的人伤不起, 实际上就是子集生成 + 各种规则模拟. 规则中几个要注意的点:A是最大的牌, 也可以是最小的牌, 也就是顺子的情况有可能是TJQKA, 也可能是A2345.这道题一开始题意也没读懂, 我以为玩家只能看透最上面的一张牌, 那样的话题目就是用贪心来解了. 1 #include 2 #include 3 #include 4 # 阅读全文
posted @ 2013-12-02 23:22 NextLife 阅读(393) 评论(0) 推荐(0)
摘要: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2146这道题 我用dfs生成所有子集秒掉了, 但是, 末子大神给出了不用dfs的更快的方法, 枚举就行了, 膜拜= =待我想想----补充----今天苦于思索末子大神的算法, 最终找到几个反例, 感觉应该是不对的.不过我在网上确实找到了不用dfs枚举的算法, 实际上核心就是P个二进制位组成的串, 他的所有子集, 其实就是二进制的0枚举到2^P-1. 待我晚上回家,把这个算法写一下. 阅读全文
posted @ 2013-11-28 01:02 NextLife 阅读(244) 评论(0) 推荐(0)
摘要: http://codeforces.com/problemset/problem/368/B做出这道题 沙茶我好高兴啊.想了好长时间= = 比赛快结束的时候突然开窍了, wa一次, 第二次ac,总之, 一般这种比较密集的运算需求, 一个常见的思路, 就是预处理,在输入的时候就打好一个从ai到an不重复数字个数的表, 时间复杂度如果用哈希的话, 差不多就是O(n).#include #include #include using namespace std;int an[100010] = {0};int am[100010] = {0};int c[100010] = {0};int main 阅读全文
posted @ 2013-11-27 02:49 NextLife 阅读(315) 评论(0) 推荐(0)
摘要: http://codeforces.com/problemset/problem/368/A足以秒杀的一道题. 1 #include 2 #include 3 using namespace std; 4 5 int a[110] = {0}; 6 7 int main() 8 { 9 int n = 0, d = 0;10 scanf("%d%d", &n, &d);11 for (int i = 0; i < n; ++i) {12 scanf("%d", &a[i]);13 }14 int m = 0;15 scan. 阅读全文
posted @ 2013-11-27 02:47 NextLife 阅读(184) 评论(0) 推荐(0)
摘要: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=107&page=show_problem&problem=1108开始和大家一起刷白书的课后题了! 第一道, 枚举题. 1 #include 2 #include 3 #include 4 using namespace std; 5 6 bool solve(vector > & v, int n, int i, int j) { 7 8 int lnum = 0, unum = 0; 9 阅读全文
posted @ 2013-11-26 02:07 NextLife 阅读(206) 评论(0) 推荐(0)
摘要: http://codeforces.com/contest/366/problem/A简单的要死但却wa了两次的题, 原因是变量名写错了, 却没有仔细检查, 误以为解法上有玄机.... 1 #include 2 #include 3 #include 4 #include 5 using namespace std; 6 7 int main() 8 { 9 int n = 0;10 scanf("%d", &n);11 map > m;12 for (int i = 1; i t(4, 0);14 for (int j = 0; j... 阅读全文
posted @ 2013-11-25 13:10 NextLife 阅读(196) 评论(0) 推荐(0)
摘要: http://oj.leetcode.com/submissions/detail/1508385/最最简单的dp 1 class Solution { 2 public: 3 int flag[1000001] = {0}; 4 int f(int n) { 5 if (flag[n] != 0) return flag[n]; 6 if (1 == n) { 7 return 1; 8 } else if (2 == n) { 9 return 2;10 } el... 阅读全文
posted @ 2013-11-21 13:13 NextLife 阅读(228) 评论(0) 推荐(0)
摘要: http://codeforces.com/problemset/problem/365/B最开始我寻思这题分治题吧, 就开始分, 感觉分治还是写的少控制的不好, 4wa, 都快放弃了,后来群里halo大神说, 他用暴力解的O(n), 我顿悟了, 开始暴力裸写, 一次A掉.思路就是:遍历数组, 用两个"指针", l和r, 来标识找到的斐波那契数列, 每找到新的 都记录一下len(l,r), 并更新l和r值. 1 #include 2 #include 3 4 using namespace std; 5 6 int arr[100001] = {0}; 7 int num 阅读全文
posted @ 2013-11-20 02:40 NextLife 阅读(260) 评论(0) 推荐(0)
摘要: http://codeforces.com/problemset/problem/365/A应该是 我参加cf div2比赛以来 最简单的一道题了... 1 #include 2 #include 3 using namespace std; 4 5 bool f(int num, int k) { 6 int t = num; 7 int arr[10] = {0}; 8 while (t) { 9 int y = t % 10;10 arr[y] = 1;11 t /= 10;12 }13 for... 阅读全文
posted @ 2013-11-20 02:34 NextLife 阅读(283) 评论(0) 推荐(0)
摘要: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/这是大学生作业么... 1 class Solution { 2 public: 3 ListNode *deleteDuplicates(ListNode *head) { 4 // IMPORTANT: Please reset any member data you declared, as 5 // the same Solution instance will be reused for each test ca... 阅读全文
posted @ 2013-11-18 21:36 NextLife 阅读(194) 评论(0) 推荐(0)
摘要: http://oj.leetcode.com/problems/remove-element/排序, 然后 找到与目标相同的数列 都干掉 1 class Solution { 2 public: 3 int removeElement(int A[], int n, int elem) { 4 // IMPORTANT: Please reset any member data you declared, as 5 // the same Solution instance will be reused for each test case. 6 ... 阅读全文
posted @ 2013-11-18 21:16 NextLife 阅读(183) 评论(0) 推荐(0)
摘要: http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/分类讨论, 很简单, 注意下给左子树赋NULL值 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */10 ... 阅读全文
posted @ 2013-11-14 09:45 NextLife 阅读(173) 评论(0) 推荐(0)
摘要: http://oj.leetcode.com/problems/search-insert-position/简单二分. 1 class Solution { 2 public: 3 int searchInsert(int A[], int n, int target) { 4 // IMPORTANT: Please reset any member data you declared, as 5 // the same Solution instance will be reused for each test case. 6 i... 阅读全文
posted @ 2013-11-14 09:42 NextLife 阅读(145) 评论(0) 推荐(0)
摘要: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/哈哈, 状态不错, 虽然很水, 但是很快就ac了, 感觉还是很高兴~思路就是, 找到所有递增序列, 然后最低买, 最高卖. 1 class Solution { 2 public: 3 int maxProfit(vector &prices) { 4 int cur = 1; 5 int low = 0, high = 0; 6 int profit = 0; 7 while (cur ... 阅读全文
posted @ 2013-11-10 20:00 NextLife 阅读(157) 评论(0) 推荐(0)
摘要: http://oj.leetcode.com/problems/regular-expression-matching/这就是 小龙说的简单题, 真沙茶做了一晚上, 试了又试, 各种简化, 我已经快疯掉了, 结果超时了,网上查了下这道题居然有dp解法, 顺带发现了我代码里面的错误, 改了回来 AC了. 我是真沙茶.如果下个字符不是*好办, 如果是*就dfs. 1 class Solution { 2 public: 3 bool isMatch(const char *s, const char *p) { 4 // IMPORTANT: Please reset an... 阅读全文
posted @ 2013-11-09 02:33 NextLife 阅读(208) 评论(0) 推荐(0)
摘要: http://poj.org/problem?id=2386经典的dfs, dfs函数的作用是, 找到W附近八连通的所有W, 并将他们置为非W,以防重复访问, 这就好像一个传染的过程.然后程序主体遍历矩阵的每一个点, 如果某个点是W就进行dfs传染, 并对num加1. 曾经感觉最擅长的题, 刚才做起来竟然觉得生疏, 虽然过了是过了, 但没有秒杀很不爽, 我真是个沙茶. 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 using namespace std; 9 10 #d... 阅读全文
posted @ 2013-11-08 00:58 NextLife 阅读(332) 评论(0) 推荐(0)
摘要: http://oj.leetcode.com/problems/reverse-integer/简单题, 要注意结尾是0的情况的处理. 真沙茶同学什么时候才可以如此轻松的切难题呢. 1 class Solution { 2 public: 3 4 long long myPow(int n, int p) { 5 long long re = 1; 6 for (int i = 0; i vec;16 int t = x > 0 ? x : -x;17 int flag = 0;18 while... 阅读全文
posted @ 2013-11-07 23:23 NextLife 阅读(165) 评论(0) 推荐(0)
摘要: http://oj.leetcode.com/problems/same-tree/ 1 class Solution { 2 public: 3 bool isSameTree(TreeNode *p, TreeNode *q) { 4 // IMPORTANT: Please reset any member data you declared, as 5 // the same Solution instance will be reused for each test case. 6 if (p == NULL && q == N... 阅读全文
posted @ 2013-11-06 01:21 NextLife 阅读(162) 评论(0) 推荐(0)
摘要: http://oj.leetcode.com/problems/two-sum/这道题如果第二层循环用二分, 那么时间复杂度是O(nlogn), 而我的做法是哈希表unordered_map, 幸好LeetCode支持C++11. 1 class Solution { 2 public: 3 vector twoSum(vector &numbers, int target) { 4 // IMPORTANT: Please reset any member data you declared, as 5 // the same Solution ins... 阅读全文
posted @ 2013-11-06 00:01 NextLife 阅读(188) 评论(0) 推荐(0)
摘要: http://poj.org/problem?id=1852蚁书的第一道POJ题目, 我刚开始陷入了如何求最大时间的陷阱上, 实际上, 两蚂蚁相遇后返回都掉下去的时间是和他们各自朝离自己最远的一端走然后掉下去的时间是一样的. 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 using namespace std; 9 10 #define MAX_NUM 100000111 12 int arr[MAX_NUM] = {0};13 int num = 0;14 int n... 阅读全文
posted @ 2013-11-05 22:49 NextLife 阅读(211) 评论(0) 推荐(0)
摘要: http://oj.leetcode.com/problems/copy-list-with-random-pointer/我用hash表ac的, 但是有更优雅的方法, 我是沙茶, 我想不出, 又看了题解, 题解的方法我总结如下:每生成一个新结点, 就连到相应旧结点的next指针上, 这样形成了原链表和新链表每个结点依次先后相邻的一条链表,相当于一个合并的过程. 也许这步让人不明所以, 那么接下来的步骤就是关键, 把每个新结点的random指针指向该新结点前面那个旧节点的random指针的next, 这句话有点绕, 其实画个图更好理解. 懒的画图了, 使劲看会明白的. 最后一步就是把这个合并后 阅读全文
posted @ 2013-11-05 01:25 NextLife 阅读(221) 评论(0) 推荐(0)
摘要: http://oj.leetcode.com/problems/single-number/查了题解, 真特么巧妙.在一堆成双入对的情侣中间 寻找一个无人陪伴的屌丝. 方法是 把情侣全干死, 剩下的屌丝 昭然若揭.武器是: 异或class Solution {public: int singleNumber(int A[], int n) { // Note: The Solution object is instantiated only once and is reused by each test case. int r = A[0]; f... 阅读全文
posted @ 2013-11-05 01:20 NextLife 阅读(199) 评论(3) 推荐(0)
摘要: http://oj.leetcode.com/problems/linked-list-cycle/这道题也是随手一写就ac了, 之所以如此快是因为之前写过...这道题的O(n)解真的挺巧妙的而且也很简单, 两个指针a和b都指表头,a一次走2个结点, b一次走1个结点, 如果链表有环, 则a总有一天会等于b.class Solution {public: bool hasCycle(ListNode *head) { // IMPORTANT: Please reset any member data you declared, as // the same ... 阅读全文
posted @ 2013-11-05 01:08 NextLife 阅读(147) 评论(0) 推荐(0)
摘要: http://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/这道题是LeetCode上独立做出来的第一道题, dfs.这道题一开始总wa, 后来发现是stl string用错了, 我初始化是这么写的 string str(10); 导致所有str都是长10, 估计LeetCode会做检查, 所以就wa了, 后来就采用了先push, 递归后pop的方法,ac了.string str;string arr[10] = {" ", "", "abc", &quo 阅读全文
posted @ 2013-11-05 01:06 NextLife 阅读(223) 评论(0) 推荐(0)
摘要: http://oj.leetcode.com/problems/maximum-depth-of-binary-tree/史上最简单的题了... 1 class Solution { 2 public: 3 int maxDepth(TreeNode *root) { 4 // IMPORTANT: Please reset any member data you declared, as 5 // the same Solution instance will be reused for each test case. 6 if (ro... 阅读全文
posted @ 2013-11-05 01:06 NextLife 阅读(142) 评论(0) 推荐(0)