HackerRank savita-and-friends】的更多相关文章

题目传送门:https://www.hackerrank.com/challenges/unique-colors 感谢hzq大神找来的这道题. 考虑点分治(毕竟是路经统计),对于每一个颜色,它的贡献是独立的.我们可以在一次点分治中合在一块处理(为什么时间复杂度是对的呢,因为我们每次改动只会根据当前点的颜色进行变动,而不是所有颜色对着它都来一遍).每次先对重心单独计算答案贡献,此时也将当前区域的各个答案贡献计算出来,并以此为基础(之后称之为基准贡献,即代码中的tot).对于每一棵子树,我们先df…
Firt thought: an variation to LCS problem - but this one has many tricky detail. I learnt the solution from this link:https://github.com/wangyongliang/Algorithm/blob/master/hackerrank/Strings/Square%20Subsequences/main.cc And here is his code with my…
It is about how to choose btw. BFS and DFS. My init thought was to DFS - TLE\MLE. And its editorial gives a very neat BFS based idea which costs much less memory. https://www.hackerrank.com/challenges/beautiful-path/editorial…
It is marked as a NPC problem. However from the #1 code submission (https://www.hackerrank.com/CharlesOfria), it looks pretty much like a Brutal-Force (or simulation) based solution, mixed with some greedy strategies. To me the other NPC "Queens Revi…
传送门 今天在HackerRank上翻到一道高精度题,于是乎就写了个高精度的模板,说是模板其实就只有乘法而已. Extra long factorials Authored by vatsalchanana on Jun 16 2015 Problem Statement You are given an integer N. Print the factorial of this number. N!=N×(N−1)×(N−2)×⋯×3×2×1 Note: Factorials of N>20…
Great learning for me:https://www.hackerrank.com/rest/contests/master/challenges/lucky-numbers/hackers/turuthok/download_solution Basically it is memorized search application. And we follow a discrete strategy: split it into digits and go digit by di…
This is 'Difficult' - I worked out it within 45mins, and unlocked HackerRank Algorithm Level 80 yeah! So the idea is straight forward: 1. sort the input array and calculate partial_sum()2. find the negative\positive boundary with the accumulated give…
A sly knapsack problem in disguise! Thanks to https://github.com/bhajunsingh/programming-challanges/tree/master/hackerrank/algorithms/the-indian-jobLesson learnt: The Italian\Indian job is two-way 01 Knapsack. And some complext problem can be convert…
The most interesting, flexible and juicy binary tree problem I have ever seen. I learnt it from here: https://codepair.hackerrank.com/paper/5fIoGg74?b=eyJyb2xlIjoiY2FuZGlkYXRlIiwibmFtZSI6IkJsdWVCaXJkMjI0IiwiZW1haWwiOiJoZWFsdGh5dG9ueUBnbWFpbC5jb20ifQ%…
Something to learn: Rotation ops in AVL tree does not require recursion. https://github.com/andreimaximov/hacker-rank/blob/master/data-structures/tree/self-balancing-tree/main.cpp node *create_node(int val) { node *pNew = new node; pNew->val = val; p…
https://www.hackerrank.com/contests/w9/challenges/lexicographic-steps 这题还是折腾很久的.题目意思相当于,比如有两个1两个0,那么找组成的数里第k大的.想法就是,如上例,假如K为4,那么先看后两位够了么C(2,2)=1,不够,那么看后三位C(3,2)=3,也不够,后四位是C(4,2)=6,够了,那么第一个1在倒数第4位.然后减去C(3,2)继续做. #include <iostream> #include <vecto…
https://www.hackerrank.com/contests/infinitum-aug14/challenges/jim-beam 学习了线段相交的判断法.首先是叉乘,叉乘的几何意义是有向的平行四边形的面积(除以2就是三角形的面积).如果ABD和ABC正负相反,说明C和D在AB两侧,同样的,再判断A和B是否在CD两侧即可.当某三角形面积为0时,需要判断是否在线段上. #include <iostream> using namespace std; typedef long long…
https://www.hackerrank.com/challenges/service-lane 用RMQ做的,其实暴力也能过~ #include <iostream> #include <vector> #include <cmath> using namespace std; int main() { int n, t; cin >> n >> t; vector<int> vec(n); for (int i = 0; i…
https://www.hackerrank.com/contests/w1/challenges/volleyball-match 此题不错,首先可以看出是DP,S(x, y)= S(x - 1, y) + S(x, y - 1).然后比赛结束状态需要认真判断.三来,最后数据量很大(接近10^9)远超一般DP的数据量,分配数组都不行,里面是有规律的.下面是大数据失败的代码: #include <vector> #include <iostream> #include <cs…
https://www.hackerrank.com/contests/w1/challenges/maximizing-xor/ 找了半天规律,答案竟然是暴力,伤感.我找到的方法是利用规律2^x XOR 2^x - 1会最大,感觉稍微效率高点. int maxXor(int l, int r) { if (l == r) return 0; int p = 1; for (int i = 0; i <= 10; i++) { if (p * 2 > r) break; p *= 2; } i…
https://www.hackerrank.com/contests/w2/challenges/cut-the-tree 树分成两部分,求两部分差最小.一开始想多了,后来其实求一下总和,求一下分部的和就行了. #include <cstdlib> #include <climits> #include <algorithm> #include <iostream> #include <vector> using namespace std;…
https://www.hackerrank.com/contests/w2/challenges/manasa-and-stones 简单题. #include<iostream> using namespace std; int main() { int T; cin >> T; while (T--) { int n, a, b; cin >> n >> a >> b; // a < b if ( a > b) { int tm…
https://www.hackerrank.com/contests/w3/challenges/sam-and-substrings DP.注意到以N[i]结尾的每个字符串的子数字和有规律: 53125 | 3 53 | 1 31 531 | 2 12 312 5312 sd[2] = 1 + 31 + 531 = 563sd[3] = 2 + 12 + 312 + 5312sd[3] = 2 + 10 + 2 + 310 + 2 + 5310 + 2sd[3] = 4 * 2 + 10 *…
https://www.hackerrank.com/contests/w3/challenges/the-love-letter-mystery 简单题. #include <cstdlib> #include <string> #include <iostream> using namespace std; int main() { int N; cin >> N; while (N--) { string s; cin >> s; int…
https://www.hackerrank.com/contests/w4/challenges/crush 第一眼觉得要用线段树,但据说会超时.其实这个可以通过生成pair排序来做. #include <vector> #include <cstdlib> #include <algorithm> #include <iostream> using namespace std; int main() { int N, M; cin >> N…
https://www.hackerrank.com/contests/w5/challenges/closest-number 简单题. #include <iostream> #include <cmath> using namespace std; int main() { int T; cin >> T; while (T--) { int a, b, x; cin >> a >> b >> x; if (b < 0 &…
https://www.hackerrank.com/contests/w5/challenges 简单题,注意整数的0次方是1,奇数. #include <iostream> #include <vector> using namespace std; int main() { int N; cin >> N; vector<int> A(N); for (int i = 0; i < N; i++) { cin >> A[i]; } i…
https://www.hackerrank.com/contests/w6/challenges/consecutive-subsequences 求数组中被k整除的子段和有几个.这个要利用sum[i..j] = sum[j] - sum[i-1],注意这样还要保留一个sum[-1]=0(根据下标的起点怎么定).那么能被k整除的子段sum[i..j]的sum[j]和sum[i-1] %k的值是一样的. #include <iostream> #include <vector> u…
https://www.hackerrank.com/contests/w6/challenges/acm-icpc-team 这道题在contest的时候数据量改小过,原来的数据量需要进行优化才能过.参考了:http://chasethered.com/2014/07/hackerrank-weekly-challenge-6-problem-1/ 首先是转化成int32的数组,然后N^N的复杂度两两比较求bit数.求bit中1的个数有几种做法: - x & (x - 1)- Hamming…
https://www.hackerrank.com/contests/w7/challenges/die-hard-3 首先,发现c <= max(a, b) 而且 c = aX + bY时有解.然后根据扩展欧几里得算法知道c % gcd(a, b) == 0时有解. #include <vector> #include <iostream> using namespace std; int gcd(int a, int b) { if (a % b == 0) { ret…
https://www.hackerrank.com/contests/w8/challenges/gneck 有点意思.是DP,最优解包含最优子问题.F(X)=F(X-1)+F(X-3).因为F(X-1)右边放个“R”和F(X-3)右边放"RRB"是左右可以互推的,包含全了最右是R和B的两种情况. #include <iostream> using namespace std; const uint64_t MOD = 1000000007; uint64_t resol…
https://www.hackerrank.com/contests/w8/challenges/john-and-gcd-list 简单题,GCD和LCM. #include <vector> #include <iostream> using namespace std; int gcd(int a, int b) { if (a % b == 0) { return b; } else { return gcd(b, a % b); } } int lcm(int a, i…
https://www.hackerrank.com/contests/illuminati/challenges/tree-covering 这道题先是在上次交流讨论了一下,然后两位百度的朋友先写完代码share出来了,觉得是道很好的题,就做了一下.https://gist.github.com/coder32167/6964331 https://gist.github.com/snakeDling/6965299基本思想是贪心.根据题意,所选的点必然是叶子节点,那么首先找出树的直径,直径上…
Hackerrank 2020 February 2014 解题报告 比赛链接 Sherlock and Watson (20分) 题意:给定一个数组,向右平移K次,然后有Q个询问,问第x位置上是几 做法:直接模拟即可 #include <iostream> using namespace std; int n,k,q; ],b[]; int main(){ ios::sync_with_stdio(); cin>>n>>k>>q; ;i<n;i++)…
题目链接:https://www.hackerrank.com/challenges/almost-sorted-interval 题目大意: 定义一个“几乎单调”区间(区间最小值在最左面,最大值在最右面) 给一个N的排列,求“几乎单调”区间的个数 N=100W  解法为O(n) 很好的思维题! 想了一下午,其实自己离正解已经不远了,,不过最后还是看了学长的ac代码 然后基本上秒懂了 具体思维方式不好说啊..贴个代码,以后又不会了的话慢慢回忆吧= =|| #include <iostream>…