Skip to content

Instantly share code, notes, and snippets.

View balamark's full-sized avatar
🎯
Focusing

Mark balamark

🎯
Focusing
View GitHub Profile
@balamark
balamark / 10306.cpp
Last active August 29, 2015 14:16
variation of coin change DP.
#include <cstdio>
#include <numeric>
#include <algorithm>
#include <cstring>
using namespace std;
int T, m, S, S2;
int ctype[40][2];
int memo[300][300];
//smallest amount of e-coins needed
int way(int a, int b){
@balamark
balamark / 11951.cpp
Created February 26, 2015 01:28
2D range sum in O(n^3)
#include <numeric>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int T,N,M,K;
int strip[128][128];
// s k
// |-+-+-+-+-+
@balamark
balamark / remember.cpp
Last active August 29, 2015 14:16
[note] for_each() and getline()
#include <algorithm>
#include <cstdio> //sscanf
#include <string> //getline, c_str()
#include <iostream> //cin
using namespace std;
int t, n, l1, l2;
int main(){
int i=1;
@balamark
balamark / cubes.cpp
Created March 13, 2015 06:41
codeforce #295 div1 cubes
#include <set>
#include <unordered_set>
#include <algorithm>
#include <utility>
#include <cstdio>
#include <map>
using namespace std;
typedef pair<int, int> pii;
const long long mod = 1000000009;
int m, x, y;
@balamark
balamark / 11060.cpp
Last active August 29, 2015 14:17
BFS-based topological sort
#include <vector>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <functional>
#include <cstring> //memset
using namespace std;
@balamark
balamark / 796.cpp
Created April 5, 2015 19:00
set lambda compare with pair<int, int>
/* Note we compare both first and second element. If we only compare the first, inserting with equal key will fail.
* e.g. {4,5} {4,6} will only insert {4,5} although {4,6} is also a valid pair we want here.
*/
auto comp = [](const ii &l, const ii &r) {return l.first==r.first?l.second<r.second:l.first<r.first;};
/*
* Use decltype to inspects the declared type of an entity or queries the return type of an expression.
*/
set<ii, decltype(comp)> bridges(comp);
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <set>
#include <string>
#include <map>
#include <functional>
using namespace std;
int V1, V2, T, D;
int bt(int v, int level, int sum){
@balamark
balamark / better_298b.cc
Created April 13, 2015 14:20
clean way. approaching from two endpoints.
/*
* updown /\ up / down \
* / \ / \
* / \ / \
* v1 v2 v1 v2
*/
int ans=v1+v2;
t-=2;
REP(k,t){
@balamark
balamark / prime.cpp
Created April 21, 2015 03:55
Sieve of Eratosthenes
void gen_primes() {
int i,j;
for(i=0;i<MAX;i++) primes[i] = 1;
for(i=2;i<=(int)sqrt(MAX);i++)
if (primes[i])
for(j=i;j*i<MAX;j++) primes[i*j] = 0;
}
@balamark
balamark / z.cpp
Last active August 29, 2015 14:19
z function
//http://e-maxx-eng.github.io/string/z-function.html
vector<int> z_alg(string s){
int n=s.size();
vector<int> v(n, 0);
for(int i=1, l=0, r=0;i<n;++i){
if(i<=r){ // we can use the already calcuated z-value to "init" the v[i]
v[i]=min(v[i-l], r-i+1); // I-L not i-1
}
else{ // i>r trivial bf
while(i+v[i]<n && s[v[i]]==s[i+v[i]])