上一页 1 ··· 15 16 17 18 19 20 21 22 23 ··· 43 下一页
摘要: http://community.topcoder.com/stat?c=problem_statement&pm=12790&rd=15708这道题只有两个操作,一是加一,二是数组所有元素乘以二,求最少操作次数从全0数组变成目标数组。我的方法是从目标数组反推全0数组,如果有奇数就变成偶数,全偶数就除以二。这个方法是可以过的,就是效率拙计:import java.util.*;public class IncrementAndDoubling{ public int getMin(int[] A) { int count = 0; int len = A... 阅读全文
posted @ 2013-11-24 18:13 阿牧遥 阅读(284) 评论(0) 推荐(0)
摘要: http://community.topcoder.com/stat?c=problem_statement&pm=12854&rd=15709这道题DIV1 250的,还有点意思。看起来是O(n^2)的题,最后可以到O(n)。方法是从目标字符串的最后往前和原字符串逐个字符比较,匹配则比较下一个,不... 阅读全文
posted @ 2013-11-24 17:35 阿牧遥 阅读(283) 评论(0) 推荐(0)
摘要: http://codility.com/demo/take-sample-test/pi2012又是一道单调栈的题目。首先这道题目n^2是最朴素的做法。继续优化,因为和顺序有关就不好排序。然后,看到可以分解成求左方向最值和右方向最值的问题。此时要变成O(n)就要么贪心,要么DP,要么单调栈,要么有更多规律未发现。这里有这么一个特点,考虑求左方向最值,当先有4,再有5之后,那么之前的4就没有意义了。所以用单调栈。如果用函数,代码可以更简洁。#include vector solution(const vector &A) { // write your code in C++98 i.. 阅读全文
posted @ 2013-11-21 23:50 阿牧遥 阅读(408) 评论(0) 推荐(0)
摘要: http://codility.com/demo/take-sample-test/hydrogenium2013用Dijkstra求最短路径,同时和D[i]比较判断是不是能到。用了优先队列优化,复杂度是(m+n)*log(n)。同时,写Dijkstra的时候一般要用dist数组,这里只拿它做访问标示。中间有个坑就是两个点之间可以多条路径,fail了半天。#include #include #define pp pairint solution(const vector &A, const vector &B, const vector &C, const vector 阅读全文
posted @ 2013-11-21 22:55 阿牧遥 阅读(230) 评论(0) 推荐(0)
摘要: http://codility.com/demo/take-sample-test/omega2013这题有点意思。首先经过思考,想到从底部往上扫,去迎接掉下来的disc。但这样仍然是不行的。后来看了答案,需要转化一下。因为从上往下看时,井里面下面大的圆都被上面小的圆遮住了,所以可以削去。int solution(vector &A, vector &B) { // write your code in C++98 for (int i = 0; i 0 && A[i] > A[i-1]) { A[i] = A[i-1]; } } ... 阅读全文
posted @ 2013-11-21 21:22 阿牧遥 阅读(177) 评论(0) 推荐(0)
摘要: http://codility.com/demo/take-sample-test/arrayinversioncount求逆序对数,归并排序并记录逆序次数。// you can also use includes, for example:// #include int merge(vector ... 阅读全文
posted @ 2013-11-21 12:24 阿牧遥 阅读(540) 评论(0) 推荐(0)
摘要: Codility Certificate题目。求product最大值,product为长度*出现次数,例子"abababa"如下:"a", whose product equals 1 * 4 = 4,"ab", whose product equals 2 * 3 = 6,"aba", whose product equals 3 * 3 = 9,"abab", whose product equals 4 * 2 = 8,"ababa", whose product eq 阅读全文
posted @ 2013-11-20 23:24 阿牧遥 阅读(513) 评论(0) 推荐(0)
摘要: Java 5以前的线程同步采用syncronized和wait,notify,notifyAll来实现,比较粗糙。之后有了Lock和Condition。ReentrantLock的简单lock,unlock相当于syncronized。而通过condition的signal和await,可以实现更细粒度的控制。http://www.cnblogs.com/yaowukonga/archive/2012/08/27/2658329.htmlhttp://blog.csdn.net/vernonzheng/article/details/8288251http://blog.csdn.net/fw 阅读全文
posted @ 2013-11-18 22:14 阿牧遥 阅读(721) 评论(0) 推荐(0)
摘要: 粗糙的实现,能够完成“12*3+4*11/(2-5)*2”之类的整形运算。方法是通过两个栈,一个记录操作数,一个记录操作符,然后用map记录优先级。/和*的优先级要一样,否则会有4*11/2是22还是20这样的错误。括号的处理是"("入栈,")"开始回退到"("。因为除号的两个操作数有顺序,弹出栈的时候要注意。#include #include #include #include #include using namespace std;// case:// 1+2*3+5-6/7 = 12// 12*3+4*11/2-5*2 = 4 阅读全文
posted @ 2013-11-17 01:27 阿牧遥 阅读(325) 评论(0) 推荐(0)
摘要: 这题很简单,一开始用了set。但后来一想这样其实是n*logn的,而且没有利用所有的数都在0..N-1之间。那么可以直接用vector当hashset。// you can also use includes, for example:// #include int solution(const vector &A) { // write your code in C++98 vector hashset; int n = A.size(); hashset.resize(n, false); int cover = -1; for (int i = 0; ... 阅读全文
posted @ 2013-11-14 23:16 阿牧遥 阅读(342) 评论(0) 推荐(0)
上一页 1 ··· 15 16 17 18 19 20 21 22 23 ··· 43 下一页