diff --git a/HackerRank.vcxproj b/HackerRank.vcxproj index b44b4f8..8a9500c 100644 --- a/HackerRank.vcxproj +++ b/HackerRank.vcxproj @@ -94,8 +94,21 @@ true true + + true + true + + + true + + + true + + + true + true true @@ -104,7 +117,12 @@ true true - + + true + + + true + true true @@ -113,10 +131,31 @@ true true + + true + + + true + + + true + + + true + true true + + true + + + true + + + true + true true @@ -129,14 +168,28 @@ true true + + true + true + true true + + true + + + true + true + true true + + true + true true @@ -165,18 +218,37 @@ true true + + true + + + true + true true + + true + true true + + true + true true + + true + + + true + + true true @@ -185,6 +257,9 @@ true true + + true + true true @@ -193,6 +268,9 @@ true true + + true + true true @@ -201,6 +279,16 @@ true true + + true + + + true + + + true + true + true true @@ -213,6 +301,9 @@ true true + + true + true true @@ -233,6 +324,18 @@ true true + + true + + + true + + + true + + + true + true true @@ -241,6 +344,12 @@ true true + + true + + + true + true true @@ -251,6 +360,7 @@ true + true true @@ -260,10 +370,16 @@ true true + + true + true true + + true + true true @@ -300,6 +416,9 @@ true true + + true + true true @@ -320,10 +439,20 @@ true true + + true + + + false + true + true true + + true + true true @@ -336,6 +465,9 @@ true true + + true + true true @@ -352,6 +484,9 @@ true true + + true + true true @@ -360,18 +495,44 @@ true true + + true + true + true true + + true + + + true + + + true + + + true + true true + + true + true true + + true + + + true + true + true true @@ -384,6 +545,10 @@ true true + + true + true + true true @@ -392,6 +557,13 @@ true true + + true + + + true + true + diff --git a/HackerRank.vcxproj.filters b/HackerRank.vcxproj.filters index 94d8a75..3c8aa7e 100644 --- a/HackerRank.vcxproj.filters +++ b/HackerRank.vcxproj.filters @@ -27,6 +27,9 @@ Source Files + + Source Files + @@ -254,5 +257,164 @@ Source Files + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + \ No newline at end of file diff --git a/acm-icpc-team.cpp b/acm-icpc-team.cpp new file mode 100644 index 0000000..f24c1f0 --- /dev/null +++ b/acm-icpc-team.cpp @@ -0,0 +1,39 @@ +//acm-icpc-team.cpp +//ACM ICPC Team +//Weekly Challenges - Week 6 +//Author: derekhh + +#include +#include +#include +using namespace std; + +char str[1010]; +vector> v; + +int main() +{ + int n, m; + scanf("%d%d", &n, &m); + getchar(); + for (int i = 0; i < n; i++) + { + scanf("%s", str); + bitset<1000> b(str); + v.push_back(b); + } + + int best = 0, cnt = 0; + for (int i = 0; i < n; i++) + { + for (int j = i + 1; j < n; j++) + { + bitset<1000> t = v[i] | v[j]; + if (t.count() > best) best = t.count(), cnt = 1; + else if (t.count() == best) cnt++; + } + } + printf("%d\n", best); + printf("%d\n", cnt); + return 0; +} \ No newline at end of file diff --git a/akhil-and-gf.cpp b/akhil-and-gf.cpp new file mode 100644 index 0000000..9ab0aa7 --- /dev/null +++ b/akhil-and-gf.cpp @@ -0,0 +1,42 @@ +//akhil-and-gf.cpp +//Akhil and GF +//Ad Infinitum - Math Programming Contest August'14 +//Author: derekhh + +#include +using namespace std; + +long long mod_mul(long long x, long long y, long long n) +{ + if (!x) return 0; + return (((x & 1)*y) % n + (mod_mul(x >> 1, y, n) << 1) % n) % n; +} + +long long ModExp(long long a, long long b, long long n) +{ + long long c = 1, d = a; + while (b) + { + if (b & 1) c = mod_mul(c, d, n); + d = mod_mul(d, d, n); + b >>= 1; + } + return c; +} + +int main() +{ + int t; + cin >> t; + while (t--) + { + long long n, m; + cin >> n >> m; + long long tmp = ModExp(10, n, 9 * m); + tmp--; + if (tmp < 0) tmp += 9 * m; + tmp /= 9; + cout << tmp << endl; + } + return 0; +} \ No newline at end of file diff --git a/alternating-characters.cpp b/alternating-characters.cpp new file mode 100644 index 0000000..08000a2 --- /dev/null +++ b/alternating-characters.cpp @@ -0,0 +1,40 @@ +//alternating-characters.cpp +//Alternating Characters +//Weekly Challenges - Week 10 +//Author: derekhh + +#include +#include +using namespace std; + +int main() +{ + int t; + cin >> t; + while (t--) + { + string str; + cin >> str; + int len = (int)str.size(); + int cnt = 0, ans = 0; + for (int i = 0; i < len; i++) + { + if (i == 0 || str[i] == str[i - 1]) + cnt++; + else + { + if (cnt > 1) + { + ans += cnt - 1; + } + cnt = 1; + } + } + if (cnt > 1) + { + ans += cnt - 1; + } + cout << ans << endl; + } + return 0; +} \ No newline at end of file diff --git a/black-box-1.cpp b/black-box-1.cpp new file mode 100644 index 0000000..53309d0 --- /dev/null +++ b/black-box-1.cpp @@ -0,0 +1,62 @@ +//black-box-1.cpp +//The Black Box +//Weekly Challenges - Week 8 +//Author: derekhh + +#include +#include +#include +using namespace std; + +vector v; +vector vv; + +int main() +{ + int n; + cin >> n; + int sz = 0; + while (n--) + { + int num; + cin >> num; + if (num > 0) + { + v.push_back(num); + sz++; + } + else + { + vector::iterator pos = find(v.begin(), v.end(), -num); + v.erase(pos); + sz--; + } + vv.clear(); + vv = v; + sort(vv.rbegin(), vv.rend()); + + int sig = 1; + while (sig <= vv[0]) + sig <<= 1; + sig >>= 1; + + for (int t = 0; sig >= 1; sig >>= 1) + { + int i = t; + while (i < sz && (vv[i] & sig) == 0) i++; + if (i >= sz) continue; + swap(vv[t], vv[i]); + for (int j = 0; j < sz; ++j) + { + if (j != t && (vv[j] & sig) != 0) + vv[j] = vv[j] ^ vv[t]; + } + t++; + } + int result = 0; + for (int q = 0; q < sz; q++) + result ^= vv[q]; + printf("%d\n", result); + } + return 0; +} \ No newline at end of file diff --git a/bus-station.cpp b/bus-station.cpp new file mode 100644 index 0000000..8bcb894 --- /dev/null +++ b/bus-station.cpp @@ -0,0 +1,47 @@ +//bus-station.cpp +//Bus Station +//101 Hack July'14 +//Author: derekhh + +#include +#include +using namespace std; + +int a[100000]; +set s; + +int main() +{ + int n; + cin >> n; + for (int i = 0; i < n; i++) + cin >> a[i]; + + int cur = a[0]; + s.insert(cur); + for (int i = 1; i < n; i++) + { + cur += a[i]; + s.insert(cur); + } + + for (set::iterator it = s.begin(); it != s.end();++it) + { + if (cur % (*it) == 0) + { + bool flag = true; + for (int j = 1; j <= cur / (*it); j++) + { + set::iterator tt = s.find((*it) * j); + if (tt == s.end()) + { + flag = false; + break; + } + } + if (flag) + cout << *it << " "; + } + } + return 0; +} \ No newline at end of file diff --git a/candles-2.cpp b/candles-2.cpp new file mode 100644 index 0000000..f4d1a82 --- /dev/null +++ b/candles-2.cpp @@ -0,0 +1,72 @@ +#include +#include +using namespace std; + +int h[50000], c[50000]; + +const int MAXN = 50010; +const int MOD = 1000000007; + +class FenwickTree +{ +private: + long long tree[MAXN]; + +public: + FenwickTree() + { + memset(tree, 0, sizeof(tree)); + } + + long long get(int idx) + { + return query(idx) - query(idx - 1); + } + + void set(int idx, long long val) + { + long long curr = get(idx); + update(idx, val - curr); + } + + void update(int idx, long long val) + { + while (idx <= MAXN) + { + tree[idx] = (tree[idx] + val) % MOD; + idx += (idx & -idx); + } + } + + long long query(int idx) + { + long long sum = 0; + while (idx > 0) + { + sum = (sum + tree[idx]) % MOD; + idx -= (idx & -idx); + } + return sum; + } +}; + +FenwickTree s[128]; + +int main() +{ + int n, k; + cin >> n >> k; + s[0].update(1, 1); + for (int i = 0; i < n; i++) + { + cin >> h[i] >> c[i]; + h[i]++; c[i]--; + for (int j = 0; j < (1 << k); j++) + { + long long val = s[j].query(h[i] - 1); + s[j | (1 << c[i])].update(h[i], val); + } + } + cout << s[(1 << k) - 1].query(50001) << endl; + return 0; +} \ No newline at end of file diff --git a/cavity-map.cpp b/cavity-map.cpp new file mode 100644 index 0000000..e06ec25 --- /dev/null +++ b/cavity-map.cpp @@ -0,0 +1,51 @@ +//cavity-map.cpp +//Cavity Map +//101 Hack July'14 +//Author: derekhh + +#include +#include +using namespace std; + +string map[110]; +bool marked[110][110]; + +int dx[4] = { -1, 1, 0, 0 }; +int dy[4] = { 0, 0, -1, 1 }; + +int n; + +bool valid(int tx, int ty) +{ + return 0 <= tx && tx < n && 0 <= ty && ty < n; +} + +int main() +{ + cin >> n; + for (int i = 0; i < n; i++) + cin >> map[i]; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + int val = map[i][j] - '0'; + int cnt = 0; + for (int k = 0; k < 4; k++) + { + int ti = i + dx[k]; + int tj = j + dy[k]; + if (valid(ti, tj) && val > map[ti][tj] - '0') + cnt++; + } + if (cnt == 4) + cout << 'X'; + else + cout << map[i][j]; + + if (j == n - 1) + cout << endl; + } + } + +} \ No newline at end of file diff --git a/chief-hopper.cpp b/chief-hopper.cpp new file mode 100644 index 0000000..a1cb02a --- /dev/null +++ b/chief-hopper.cpp @@ -0,0 +1,45 @@ +//chief-hopper.cpp +//Chief Hopper +//Weekly Challenges - Week 12 +//Author: derekhh + +#include +using namespace std; + +int h[100001]; + +int main() +{ + int n; + cin >> n; + int max = 0; + for (int i = 1; i <= n; i++) + { + cin >> h[i]; + if (h[i] > max) + max = h[i]; + } + int lo = 0, hi = max, mid; + while (hi > lo) + { + mid = (lo + hi) >> 1; + double energy = mid; + for (int i = 1; i <= n; i++) + { + if (h[i] > energy) + energy -= (h[i] - energy); + else + energy += (energy - h[i]); + + if (energy < 0) + break; + } + + if (energy < 0) + lo = mid + 1; + else + hi = mid; + } + cout << lo << endl; + return 0; +} \ No newline at end of file diff --git a/chocolate-in-box.cpp b/chocolate-in-box.cpp new file mode 100644 index 0000000..9c1b17d --- /dev/null +++ b/chocolate-in-box.cpp @@ -0,0 +1,27 @@ +//chocolate-in-box.cpp +//Chocolate in Box +//Weekly Challenges - Week 7 +//Author: derekhh + +#include +using namespace std; + +int a[1000000]; + +int main() +{ + int n; + cin >> n; + int val = 0; + for (int i = 0; i < n; i++) + { + cin >> a[i]; + val ^= a[i]; + } + int cnt = 0; + for (int i = 0; i < n; i++) + if ((val ^ a[i]) < a[i]) + cnt++; + cout << cnt << endl; + return 0; +} \ No newline at end of file diff --git a/cipher.cpp b/cipher.cpp new file mode 100644 index 0000000..661d084 --- /dev/null +++ b/cipher.cpp @@ -0,0 +1,25 @@ +//cipher.cpp +//Cipher +//Weekly Challenges - Week 10 +//Author: derekhh + +#include + +int a[1000000]; + +int main() +{ + int n, k; + scanf("%d %d\n", &n, &k); + int head = 0, curr = 0; + for (int i = 0; i < n; i++) + { + if (i - head == k) + curr ^= a[head++]; + a[i] = (getchar() - '0') ^ curr; + printf("%d", a[i]); + curr ^= a[i]; + } + printf("\n"); + return 0; +} \ No newline at end of file diff --git a/closest-number.cpp b/closest-number.cpp new file mode 100644 index 0000000..ef4eb8e --- /dev/null +++ b/closest-number.cpp @@ -0,0 +1,49 @@ +//closest-number.cpp +//Closest Number +//Weekly Challenges - Week 5 +//Author: derekhh + +#include +#include +#include +using namespace std; + +int main() +{ + int t; + cin >> t; + while (t--) + { + int a, b, x; + cin >> a >> b >> x; + if (b < 0) + { + //Now a ^ b \in [0,1) + if (x != 1) cout << 0 << endl; + else + { + if (a == 1) cout << 1 << endl; + else cout << 0 << endl; + } + } + else if (b == 0) + { + if (x != 1) cout << 0 << endl; + else cout << 1 << endl; + } + else + { + double tmp = pow(a, b); + int lo = (int)(1.0 * tmp / x); + double best = INT_MAX; + int ans = 0; + for (int i = lo - 5; i <= lo + 5; i++) + { + if (abs(1.0*i*x - tmp) < best) + best = abs(1.0*i*x - tmp), ans = i; + } + cout << ans * x << endl; + } + } + return 0; +} \ No newline at end of file diff --git a/computing-analytics.cpp b/computing-analytics.cpp new file mode 100644 index 0000000..7d7abe0 --- /dev/null +++ b/computing-analytics.cpp @@ -0,0 +1,73 @@ +//computing-analytics.cpp +//Computing Analytics +//Addepar Hackathon +//Author: derekhh + +#include +#include +using namespace std; + +unordered_set l[100001], r[100001]; +unordered_set leftMinusOne[100001]; + +int main() +{ + int n, c, q; + cin >> n >> c >> q; + for (int i = 0; i < c; i++) + { + int a, b; + cin >> a >> b; + l[b].insert(a); + leftMinusOne[b].insert(a - 1); + r[a].insert(b); + } + + for (int i = 0; i < q; i++) + { + int a, b; + cin >> a >> b; + if (a > b) swap(a, b); + + bool found = false; + if (r[a].find(b) != r[a].end()) + { + found = true; + goto end; + } + + // merge + for (unordered_set::iterator it = r[a].begin(); it != r[a].end(); ++it) + { + if (leftMinusOne[b].find(*it) != leftMinusOne[b].end()) + { + found = true; + goto end; + } + } + + // [a, c] and [b+1, c] + + for (unordered_set::iterator it = r[a].begin(); it != r[a].end(); ++it) + { + if (r[b + 1].find(*it) != r[b + 1].end()) + { + found = true; + goto end; + } + } + + // [c, a-1] and [c,b] + for (unordered_set::iterator it = l[a - 1].begin(); it != l[a - 1].end(); ++it) + { + if (l[b].find(*it) != l[b].end()) + { + found = true; + goto end; + } + } + end: + cout << (found ? "YES" : "NO") << endl; + } + return 0; +} \ No newline at end of file diff --git a/consecutive-subsequences.cpp b/consecutive-subsequences.cpp new file mode 100644 index 0000000..e670bf6 --- /dev/null +++ b/consecutive-subsequences.cpp @@ -0,0 +1,35 @@ +//consecutive-subsequences.cpp +//Consecutive Subsequences +//Weekly Challenges - Week 6 +//Author: derekhh + +#include +#include +using namespace std; + +int cnt[100]; + +int main() +{ + int t; + cin >> t; + while (t--) + { + memset(cnt, 0, sizeof(cnt)); + int n, k, num, sum = 0; + cin >> n >> k; + cnt[0] = 1; + for (int i = 0; i < n; i++) + { + cin >> num; + sum += num; + if (sum >= k) sum %= k; + cnt[sum]++; + } + long long ret = 0; + for (int i = 0; i < k; i++) + ret += (long long)cnt[i] * (cnt[i] - 1) / 2; + cout << ret << endl; + } + return 0; +} \ No newline at end of file diff --git a/consistent-salaries.cpp b/consistent-salaries.cpp new file mode 100644 index 0000000..5df0f05 --- /dev/null +++ b/consistent-salaries.cpp @@ -0,0 +1,47 @@ +//consistent-salaries.cpp +//Consistent Salaries +//Addepar Hackathon +//Author: derekhh + +#include +#include +using namespace std; + +set s; + +int e[200001], p[200001], t[200001]; + +int main() +{ + int n, q; + cin >> n >> q; + for (int i = 2; i <= n; i++) + cin >> p[i]; + for (int i = 1; i <= n; i++) + cin >> e[i]; + for (int i = 2; i <= n; i++) + if (e[i] > e[p[i]]) + s.insert(i); + + while (q--) + { + int a, b; + cin >> a >> b; + + t[a] += b; + + if (a != 1) + { + if (e[a] + t[a] > e[p[a]]) + s.insert(a); + else + s.erase(a); + } + + if (s.size() != 0) + cout << "BAD" << endl; + else + cout << "GOOD" << endl; + } + return 0; +} \ No newline at end of file diff --git a/counter-game.cpp b/counter-game.cpp new file mode 100644 index 0000000..b4fb258 --- /dev/null +++ b/counter-game.cpp @@ -0,0 +1,32 @@ +//counter-game.cpp +//Counter game +//Weekly Challenges - Week 8 +//Author: derekhh + +#include +using namespace std; + +int main() +{ + int t; + cin >> t; + while (t--) + { + unsigned long long n; + cin >> n; + int cnt = 0; + while (n != 1) + { + int len = 0; + unsigned long long tmp = n; + while (tmp != 0) tmp /= 2, len++; + unsigned long long a = (unsigned long long)1 << (len - 1); + if (n == a) n /= 2; + else n -= a; + cnt++; + } + if (cnt % 2 == 0) cout << "Richard" << endl; + else cout << "Louise" << endl; + } + return 0; +} \ No newline at end of file diff --git a/die-hard-3.cpp b/die-hard-3.cpp new file mode 100644 index 0000000..a915d8b --- /dev/null +++ b/die-hard-3.cpp @@ -0,0 +1,29 @@ +//die-hard-3.cpp +//Die Hard 3 +//Weekly Challenges - Week 7 +//Author: derekhh + +#include +#include +using namespace std; + +int gcd(int a, int b) +{ + return b ? gcd(b, a%b) : a; +} + +int main() +{ + int t; + cin >> t; + while (t--) + { + int a, b, c; + cin >> a >> b >> c; + if (c % gcd(a, b) == 0 && c <= max(a, b)) + cout << "YES" << endl; + else + cout << "NO" << endl; + } + return 0; +} \ No newline at end of file diff --git a/divisibility-of-power.cpp b/divisibility-of-power.cpp new file mode 100644 index 0000000..4c7b8e4 --- /dev/null +++ b/divisibility-of-power.cpp @@ -0,0 +1,72 @@ +//divisibility-of-power.cpp +//Divisibility of Power +//Ad Infinitum - Math Programming Contest August'14 +//Author: derekhh + +#include +#include +using namespace std; + +long long mulmod(long long a, long long b, long long m) +{ + long long res = 0; + while (a != 0) + { + if (a & 1) res = (res + b) % m; + a >>= 1; + b = (b << 1) % m; + } + return res; +} + +long long ModExp(long long a, long long b, long long n) +{ + long long c = 1, d = a; + while (b) + { + if (b & 1) c = mulmod(c, d, n); + d = mulmod(d, d, n); + b >>= 1; + } + return c % n; +} + +long long a[300001]; + +int foo(long long a, long long b) +{ + if (a < 2) return (int)a; + if (b == 0) return 1; + if (b == 1) + { + if (a > 64) return 64; + return (int) a; + } + if (a > 8 || b > 6) return 64; + int ret = (int)pow(a, b); + return ret > 64 ? 64 : ret; +} + +int main() +{ + long long n; + scanf("%lld", &n); + for (int i = 1; i <= n; i++) + scanf("%lld", &a[i]); + long long q; + scanf("%lld", &q); + while (q--) + { + long long i, j, x; + scanf("%lld%lld%lld", &i, &j, &x); + int res = 1; + int k = min(j, i + 6); + if (i + 7 <= j && a[i + 7] == 0) k--; + for (; k > i; k--) + res = foo(a[k], res); + long long ans = ModExp(a[i], res, x); + if (ans == 0) printf("Yes\n", i, j, x); + else printf("No\n", i, j, x); + } + return 0; +} \ No newline at end of file diff --git a/dynamic-summation.cpp b/dynamic-summation.cpp new file mode 100644 index 0000000..3c20cf0 --- /dev/null +++ b/dynamic-summation.cpp @@ -0,0 +1,316 @@ +//dynamic-summation.cpp +//Dynamic Summation +//Weekly Challenges - Week 6 +//Author: derekhh + +#include +#include +using namespace std; + +const int MAXN = 100000; + +vector v[MAXN + 1]; +vector child[MAXN + 1]; +bool visited[MAXN + 1]; +int startTime[MAXN + 1], endTime[MAXN + 1]; + +int dfs(int now, int start) +{ + startTime[now] = start; + visited[now] = true; + int sz = (int)v[now].size(); + for (int i = 0; i < sz; i++) + { + if (!visited[v[now][i]]) + { + child[now].push_back(v[now][i]); + start = dfs(v[now][i], start + 1); + } + } + endTime[now] = start; + return start; +} + +const int NMOD = 5; +int MOD[NMOD] = { 11 * 101 * 13 * 97 * 17 * 89, 19 * 83 * 23 * 81 * 25 * 29, 31 * 79 * 37 * 73 * 41, 43 * 47 * 49 * 53 * 59, 61 * 64 * 67 * 71 }; +int p[26] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101 }; + +int modidx[102]; + +int gcd(int a, int b) +{ + return b ? gcd(b, a%b) : a; +} + +void init() +{ + for (int i = 1; i <= 101; i++) + { + for (int j = 0; j < NMOD; j++) + { + if (gcd(i, MOD[j]) != 1) + { + modidx[i] = j; + break; + } + } + } +} + +long long tree[NMOD][4 * MAXN], lazy[NMOD][4 * MAXN]; + +long long ext_euclid(long long a, long long b, long long& x, long long& y) +{ + long long t, d; + if (b == 0) { x = 1; y = 0; return a; } + d = ext_euclid(b, a%b, x, y); + t = x, x = y, y = t - a / b*y; + return d; +} + +/* +long long linear_modular_equation_system(long long B[], long long W[], long long k) +{ + long long d, x, y, m, n = 1; + long long a = 0; + for (long long i = 0; i < k; i++) + n *= W[i]; + for (long long i = 0; i < k; i++) + { + m = n / W[i]; + d = ext_euclid(W[i], m, x, y); + while (y < 0) y += W[i]; + a += y * (m * B[i] % n); + } + while (a < 0) a += n; + return a % n; +} +*/ + +long long linear_modular_equation_system(long long B[], long long W[], long long k) +{ + long long d, x, y, m, n = 1; + long long a = 0; + for (long long i = 0; i < k; i++) + n *= W[i]; + for (long long i = 0; i < k; i++) + { + m = n / W[i]; + d = ext_euclid(W[i], m, x, y); + a = (a + y*m*B[i]) % n; + } + if (a>0) return a; + else return a + n; +} + +void updateTree(long long node, long long a, long long b, long long i, long long j, long long value, long long modidx) +{ + if (lazy[modidx][node] != 0) + { + tree[modidx][node] = (tree[modidx][node] + (long long)(b - a + 1) * lazy[modidx][node]) % MOD[modidx]; + + if (a != b) + { + lazy[modidx][node * 2] = (lazy[modidx][node * 2] + lazy[modidx][node]) % MOD[modidx]; + lazy[modidx][node * 2 + 1] = (lazy[modidx][node * 2 + 1] + lazy[modidx][node]) % MOD[modidx]; + } + + lazy[modidx][node] = 0; + } + + if (a > b || a > j || b < i) return; + + if (a >= i && b <= j) + { + tree[modidx][node] = (tree[modidx][node] + (long long)(b - a + 1) * value) % MOD[modidx]; + + if (a != b) + { + lazy[modidx][node * 2] = (lazy[modidx][node * 2] + value) % MOD[modidx]; + lazy[modidx][node * 2 + 1] = (lazy[modidx][node * 2 + 1] + value) % MOD[modidx]; + } + + return; + } + + updateTree(node * 2, a, (a + b) / 2, i, j, value, modidx); + updateTree(node * 2 + 1, (a + b) / 2 + 1, b, i, j, value, modidx); + + tree[modidx][node] = (tree[modidx][node * 2] + tree[modidx][node * 2 + 1]) % MOD[modidx]; +} + +long long queryTree(long long node, long long a, long long b, long long i, long long j, long long modidx) +{ + if (a > b || a > j || b < i) return 0; + + if (lazy[modidx][node] != 0) + { + tree[modidx][node] = (tree[modidx][node] + (long long)(b - a + 1) * lazy[modidx][node]) % MOD[modidx]; + if (a != b) + { + lazy[modidx][node * 2] = (lazy[modidx][node * 2] + lazy[modidx][node]) % MOD[modidx]; + lazy[modidx][node * 2 + 1] = (lazy[modidx][node * 2 + 1] + lazy[modidx][node]) % MOD[modidx]; + } + lazy[modidx][node] = 0; + } + + if (a >= i && b <= j) return tree[modidx][node]; + + long long q1 = queryTree(node * 2, a, (a + b) / 2, i, j, modidx); + long long q2 = queryTree(node * 2 + 1, (a + b) / 2 + 1, b, i, j, modidx); + long long tmp = q1 + q2; + return tmp % MOD[modidx]; +} + +int ModExp(long long a, long long b, int mod) +{ + a %= mod; + long long c = 1, d = a; + while (b) + { + if (b & 1) c = (c*d) % mod; + d = (d*d) % mod; + b >>= 1; + } + return (int)c; +} + +int calc(long long a, long long b, int mod) +{ + long long sum1 = ModExp(a, b, mod); + long long sum2 = ModExp(a + 1, b, mod); + long long sum3 = ModExp(b + 1, a, mod); + return (sum1 + sum2 + sum3) % mod; +} + +int binarySearchChild(int root, int st, int en) +{ + int lo = 0, hi = (int)child[root].size(), mid = 0; + + while (lo < hi - 1) + { + mid = (lo + hi) / 2; + int st_mid = startTime[child[root][mid]]; + int en_mid = endTime[child[root][mid]]; + + if (st >= st_mid && en <= en_mid) return mid; + else if (en <= st_mid) hi = mid; + else lo = mid + 1; + } + return lo; +} + +int main() +{ + init(); + + int n; + scanf("%d", &n); + for (int i = 0; i < n - 1; i++) + { + int x, y; + scanf("%d%d", &x, &y); + v[x].push_back(y); + v[y].push_back(x); + } + dfs(1, 1); + + int q; + scanf("%d", &q); + getchar(); + while (q--) + { + char ch; + int r, t, m; + long long a, b; + ch = getchar(); + if (ch == 'U') + { + scanf("%d%d%lld%lld", &r, &t, &a, &b); + getchar(); + + int sr = startTime[r], er = endTime[r]; + int st = startTime[t], et = endTime[t]; + + int val[NMOD]; + + for (int i = 0; i < NMOD; i++) + val[i] = calc(a, b, MOD[i]); + + if (sr == st && er == et) + { + for (int i = 0; i < NMOD; i++) + updateTree(1, startTime[1], endTime[1], startTime[1], endTime[1], val[i], i); + } + else if (sr > st && er <= et) + { + for (int i = 0; i < NMOD; i++) + updateTree(1, startTime[1], endTime[1], startTime[1], endTime[1], val[i], i); + int childIdx = binarySearchChild(t, sr, er); + for (int i = 0; i < NMOD; i++) + updateTree(1, startTime[1], endTime[1], startTime[child[t][childIdx]], endTime[child[t][childIdx]], MOD[i] - val[i], i); + } + else + { + for (int i = 0; i < NMOD; i++) + updateTree(1, startTime[1], endTime[1], st, et, val[i], i); + } + } + else + { + scanf("%d%d%d", &r, &t, &m); + getchar(); + + if (m == 1) + { + printf("0\n"); + continue; + } + + int sr = startTime[r], er = endTime[r]; + int st = startTime[t], et = endTime[t]; + + int _m = m; + + long long W[6], B[6], k = 0; + for (int i = 0; i < 26; i++) + { + int tmp = 1; + bool flag = false; + while (m % p[i] == 0) + { + flag = true; + m /= p[i]; + tmp *= p[i]; + } + if (flag) + W[k++] = tmp; + } + + if (sr == st && er == et) + { + for (int i = 0; i < k; i++) + B[i] = queryTree(1, startTime[1], endTime[1], startTime[1], endTime[1], modidx[W[i]]) % W[i]; + printf("%lld\n", linear_modular_equation_system(B, W, k) % _m); + } + else if (sr > st && er <= et) + { + int childIdx = binarySearchChild(t, sr, er); + for (int i = 0; i < k; i++) + { + long long tmp1 = queryTree(1, startTime[1], endTime[1], startTime[1], endTime[1], modidx[W[i]]) % W[i]; + long long tmp2 = queryTree(1, startTime[1], endTime[1], startTime[child[t][childIdx]], endTime[child[t][childIdx]], modidx[W[i]]) % W[i]; + B[i] = (tmp1 + W[i] - tmp2) % W[i]; + } + printf("%lld\n", linear_modular_equation_system(B, W, k) % _m); + } + else + { + for (int i = 0; i < k; i++) + B[i] = queryTree(1, startTime[1], endTime[1], st, et, modidx[W[i]]) % W[i]; + printf("%lld\n", linear_modular_equation_system(B, W, k) % _m); + } + } + } + return 0; +} \ No newline at end of file diff --git a/emma-and-sum-of-products.cpp b/emma-and-sum-of-products.cpp new file mode 100644 index 0000000..1eba80c --- /dev/null +++ b/emma-and-sum-of-products.cpp @@ -0,0 +1,42 @@ +//emma-and-sum-of-products.cpp +//Emma and sum of products +//Ad Infinitum - Math Programming Contest August'14 +//Author: derekhh + +#include +#include +using namespace std; + +const int MOD = 100003; + +int a[30001], n; +int f[30001], g[30001]; + +void foo() +{ + f[0] = 1; f[1] = a[1]; + for (int i = 2; i <= n;i++) + { + g[0] = 1; + for (int j = 1; j <= i; j++) + g[j] = (f[j] + (long long)f[j - 1] * a[i]) % MOD; + memcpy(f, g, sizeof(g)); + } +} + +int main() +{ + cin >> n; + for (int i = 1; i <= n; i++) + cin >> a[i]; + foo(); + int q; + cin >> q; + while (q--) + { + int k; + cin >> k; + cout << f[k] << endl; + } + return 0; +} \ No newline at end of file diff --git a/euler001.cpp b/euler001.cpp new file mode 100644 index 0000000..27f557d --- /dev/null +++ b/euler001.cpp @@ -0,0 +1,25 @@ +//euler001.cpp +//Project Euler #1: Multiples of 3 and 5 +//ProjectEuler + +//Author: derekhh + +#include +using namespace std; + +long long foo(long long n, int t) +{ + return (t + (n - 1) / t * t) *((n - 1) / t) / 2; +} + +int main() +{ + int t; + cin >> t; + while (t--) + { + long long n; + cin >> n; + cout << foo(n, 3) + foo(n, 5) - foo(n, 15) << endl; + } + return 0; +} \ No newline at end of file diff --git a/even-odd-query.cpp b/even-odd-query.cpp new file mode 100644 index 0000000..198762b --- /dev/null +++ b/even-odd-query.cpp @@ -0,0 +1,29 @@ +//even-odd-query.cpp +//Even Odd Query +//Weekly Challenges - Week 5 +//Author: derekhh + +#include +using namespace std; + +int A[100001]; + +int main() +{ + int n; + cin >> n; + for (int i = 1; i <= n; i++) + cin >> A[i]; + int q; + cin >> q; + while (q--) + { + int x, y; + cin >> x >> y; + if ((x + 1 <= n) && (A[x + 1] == 0) && (x != y)) + cout << "Odd" << endl; + else + cout << (A[x] % 2 == 0 ? "Even" : "Odd") << endl; + } + return 0; +} \ No newline at end of file diff --git a/favourite-sequence.cpp b/favourite-sequence.cpp new file mode 100644 index 0000000..7058206 --- /dev/null +++ b/favourite-sequence.cpp @@ -0,0 +1,58 @@ +//favourite-sequence.cpp +//Favorite sequence +//Weekly Challenges - Week 12 +//Author: derekhh + +#include +#include +#include +#include +#include +#include +using namespace std; + +vector gr[1000001]; +int indeg[1000001]; +priority_queue, greater> pq; + +int main() +{ + int n; + scanf("%d", &n); + int maxval = -1; + for (int i = 0; i < n; i++) + { + int len; + scanf("%d", &len); + int prev; + for (int j = 0; j < len; j++) + { + int tmp; + scanf("%d", &tmp); + if (tmp > maxval) maxval = tmp; + if (j != 0) gr[prev].push_back(tmp), indeg[tmp]++; + prev = tmp; + } + } + for (int i = 1; i <= maxval; i++) + { + if (indeg[i] == 0 && !gr[i].empty()) + pq.push(i); + } + while (!pq.empty()) + { + int next = pq.top(); + printf("%d ", next); + pq.pop(); + int sz = (int)gr[next].size(); + for (int i = 0; i < sz; i++) + { + int node = gr[next][i]; + indeg[node]--; + if (indeg[node] == 0) + pq.push(node); + } + } + printf("\n"); + return 0; +} \ No newline at end of file diff --git a/find-point.cpp b/find-point.cpp new file mode 100644 index 0000000..49a91b3 --- /dev/null +++ b/find-point.cpp @@ -0,0 +1,19 @@ +//find-point.cpp +//Find Point +//Author: derekhh + +#include +using namespace std; + +int main() +{ + int t; + cin >> t; + while (t--) + { + int px, py, qx, qy; + cin >> px >> py >> qx >> qy; + cout << 2 * qx - px << " " << 2 * qy - py << endl; + } + return 0; +} \ No newline at end of file diff --git a/game-of-thrones.cpp b/game-of-thrones.cpp new file mode 100644 index 0000000..e3d2e1e --- /dev/null +++ b/game-of-thrones.cpp @@ -0,0 +1,30 @@ +//game-of-thrones.cpp +//Game Of Thrones - I +//Author: derekhh + +#include +#include +using namespace std; + +int cnt[26]; + +int main() +{ + string str; + cin >> str; + for (int i = 0; i < (int)str.size(); i++) + cnt[str[i] - 'a']++; + int even = 0, odd = 0; + for (int i = 0; i < 26; i++) + { + if (cnt[i] % 2 == 0) even++; + else odd++; + } + + if (odd <= 1) + cout << "YES" << endl; + else + cout << "NO" << endl; + + return 0; +} \ No newline at end of file diff --git a/gneck.cpp b/gneck.cpp new file mode 100644 index 0000000..f456106 --- /dev/null +++ b/gneck.cpp @@ -0,0 +1,82 @@ +//gneck.cpp +//Girlfriend & Necklace +//Weekly Challenges - Week 8 +//Author: derekhh + +#include +#include +using namespace std; + +const int MOD = 1000000007; + +struct Matrix +{ + long long val[3][3]; + int row, col; +}; + +Matrix m, b; + +Matrix mul(Matrix a, Matrix b) +{ + Matrix ret; + ret.row = a.row; + ret.col = b.col; + for (int i = 0; i < a.row; i++) + { + for (int j = 0; j < b.col; j++) + { + ret.val[i][j] = 0; + for (int k = 0; k < a.col; k++) + ret.val[i][j] = (ret.val[i][j] + a.val[i][k] * b.val[k][j]) % MOD; + } + } + return ret; +} + +Matrix pow(long long n) +{ + Matrix ret; + ret.row = ret.col = 3; + memset(ret.val, 0, sizeof(ret.val)); + ret.val[0][0] = ret.val[1][1] = ret.val[2][2] = 1; + Matrix cur = m; + while (n) + { + if (n % 2) ret = mul(ret, cur); + cur = mul(cur, cur); + n /= 2; + } + return ret; +} + +int main() +{ + int t; + cin >> t; + + b.row = 3; + b.col = 1; + memset(b.val, 0, sizeof(b.val)); + b.val[0][0] = b.val[1][0] = b.val[2][0] = 1; + + while (t--) + { + long long n; + cin >> n; + + m.row = 3; m.col = 3; + memset(m.val, 0, sizeof(m.val)); + m.val[0][0] = 1; + m.val[0][1] = 1; + m.val[1][2] = 1; + m.val[2][0] = 1; + + Matrix ans_m = mul(pow(n - 2), b); + long long ans = 0; + for (int i = 0; i < 3; i++) + ans = (ans + ans_m.val[i][0]) % MOD; + cout << ans << endl; + } + return 0; +} \ No newline at end of file diff --git a/grid-challenge.cpp b/grid-challenge.cpp new file mode 100644 index 0000000..167d859 --- /dev/null +++ b/grid-challenge.cpp @@ -0,0 +1,35 @@ +#include +#include +using namespace std; + +char ch[101][101]; + +int main() +{ + int t; + cin >> t; + while (t--) + { + int n; + cin >> n; + getchar(); + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + ch[i][j] = getchar(); + getchar(); + } + for (int i = 0; i < n; i++) + sort(ch[i], ch[i] + n); + bool flag = true; + for (int i = 0; i < n; i++) + for (int j = 0; j < n - 1; j++) + if (ch[j][i] > ch[j + 1][i]) + flag = false; + if (flag) + cout << "YES" << endl; + else + cout << "NO" << endl; + } + return 0; +} \ No newline at end of file diff --git a/guessing-numbers.cpp b/guessing-numbers.cpp new file mode 100644 index 0000000..93eb240 --- /dev/null +++ b/guessing-numbers.cpp @@ -0,0 +1,26 @@ +//guessing-numbers.cpp +//Guessing Numbers +//Addepar Hackathon +//Author: derekhh + +#include +#include +#include +#include +using namespace std; + +double p[200000]; + +int main() +{ + int n, k; + cin >> n >> k; + for (int i = 0; i < n; i++) + cin >> p[i]; + double win = 0; + sort(p, p + n, greater()); + for (int i = 0; i < min((1 << k) - 1, n); i++) + win += p[i]; + printf("%.8lf\n", win); + return 0; +} \ No newline at end of file diff --git a/icecream-parlor.cpp b/icecream-parlor.cpp new file mode 100644 index 0000000..e8e1d8f --- /dev/null +++ b/icecream-parlor.cpp @@ -0,0 +1,51 @@ +//icecream-parlor.cpp +//Ice Cream Parlor +//Author: derekhh + +#include +#include +#include +using namespace std; + +vector v[10001]; + +int main() +{ + int t; + cin >> t; + while (t--) + { + int m; + cin >> m; + int n; + cin >> n; + for (int i = 1; i <= 10000; i++) + v[i].clear(); + for (int i = 1; i <= n; i++) + { + int num; + cin >> num; + v[num].push_back(i); + } + for (int i = 1; i <= 10000; i++) + { + if (i != m - i) + { + if (!v[i].empty() && !v[m - i].empty()) + { + cout << min(v[i][0], v[m - i][0]) << " " << max(v[i][0], v[m - i][0]) << endl; + break; + } + } + else + { + if (v[i].size() >= 2) + { + cout << v[i][0] << " " << v[i][1] << endl; + break; + } + } + } + } + return 0; +} \ No newline at end of file diff --git a/jagia-playing-with-numbers.cpp b/jagia-playing-with-numbers.cpp new file mode 100644 index 0000000..702fb89 --- /dev/null +++ b/jagia-playing-with-numbers.cpp @@ -0,0 +1,106 @@ +//jagia-playing-with-numbers.cpp +//Jaggu Playing with Balloons +//Weekly Challenges - Week 5 +//Author: derekhh + +#include +#include +using namespace std; + +long long tree[1000001]; +int bitcount[2000000]; + +void init() +{ + for (int i = 0; i <= 2000000; i++) + for (int j = 0; j < 31; j++) + if (i&(1 << j)) + bitcount[i]++; +} + +void update(int idx, int val) +{ + while (idx <= 1000000) + { + tree[idx] += val; + idx += (idx & -idx); + } +} + +long long read(int idx) +{ + long long sum = 0; + while (idx > 0) + { + sum += tree[idx]; + idx -= (idx & -idx); + } + return sum; +} + +void update(int pos, int M, int plus) +{ + int N = 1000000; + int sum = 0; + for (int i = 1; i <= 50; i++) + { + int back = pos, j; + for (j = 1; j <= 1000; j++) + { + int s, in = bitcount[pos]; + if (pos == 48576) break; + update(pos, M); + for (int k = 0;; k++) + { + s = pos + (1 << k); + if (bitcount[s] <= in) + { + in = bitcount[s]; + pos = s; + if (pos > N) break; + update(pos, M); + } + } + pos -= N; + } + + sum += (1000 - j + 1) * M; + pos = back + plus; + if (pos > N) pos -= N; + } + + update(48576, sum); + update(48640, sum); + update(49152, sum); + update(65536, sum); + update(131072, sum); + update(262144, sum); + update(524288, sum); +} + +int main() +{ + int q; + scanf("%d\n", &q); + init(); + while (q--) + { + char ch; + scanf("%c", &ch); + if (ch == 'U') + { + int pos, M, plus; + scanf("%d %d %d\n", &pos, &M, &plus); + update(pos, M, plus); + } + else + { + int pos1, pos2; + scanf("%d%d\n", &pos1, &pos2); + long long sum1 = read(pos1 - 1); + long long sum2 = read(pos2); + printf("%lld\n", sum2 - sum1); + } + } + return 0; +} \ No newline at end of file diff --git a/jim-and-the-jokes.cpp b/jim-and-the-jokes.cpp new file mode 100644 index 0000000..92cb4ff --- /dev/null +++ b/jim-and-the-jokes.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +using namespace std; + +long long cnt[38]; + +int main() +{ + int n; + cin >> n; + for (int i = 0; i < n; i++) + { + int m, d; + cin >> m >> d; + int tmp = d; + bool valid = true; + while (tmp) + { + if (tmp % 10 >= m) valid = false; + tmp /= 10; + } + if (!valid) continue; + int val = 0; + tmp = d; + int tt = 1; + while (tmp) + { + val += (tmp % 10) * tt; + tt *= m; + tmp /= 10; + } + cnt[val]++; + } + long long ans = 0; + for (int i = 0; i < 38; i++) + ans += cnt[i] * (cnt[i] - 1) / 2; + cout << ans << endl; + return 0; +} \ No newline at end of file diff --git a/jim_beam.cpp b/jim_beam.cpp new file mode 100644 index 0000000..8256719 --- /dev/null +++ b/jim_beam.cpp @@ -0,0 +1,54 @@ +//jim_beam.cpp +//Jim Beam +//Ad Infinitum - Math Programming Contest August'14 +//Author: derekhh + +#include +#include +using namespace std; + +#define EPS 1e-8 +#define geq(x,y) ((x)+EPS>=(y)) + +struct Point +{ + double x, y; + Point(double x0 = 0, double y0 = 0) :x(x0), y(y0){} +}; + +struct Line +{ + Point p1, p2; +}; + +double times(Point p0, Point p1, Point p2) +{ + return (p1.x - p0.x)*(p2.y - p0.y) - (p1.y - p0.y)*(p2.x - p0.x); +} + +bool LineSegIntersect(Line L1, Line L2) +{ + return(geq(max(L1.p1.x, L1.p2.x), min(L2.p1.x, L2.p2.x)) + && geq(max(L2.p1.x, L2.p2.x), min(L1.p1.x, L1.p2.x)) + && geq(max(L1.p1.y, L1.p2.y), min(L2.p1.y, L2.p2.y)) + && geq(max(L2.p1.y, L2.p2.y), min(L1.p1.y, L1.p2.y)) + && times(L1.p1, L2.p1, L1.p2)*times(L1.p1, L2.p2, L1.p2) <= EPS + &×(L2.p1, L1.p1, L2.p2)*times(L2.p1, L1.p2, L2.p2) <= EPS); +} + +int main() +{ + int t; + cin >> t; + while (t--) + { + Line L1, L2; + L1.p1 = Point(0, 0); + cin >> L2.p1.x >> L2.p1.y >> L2.p2.x >> L2.p2.y >> L1.p2.x >> L1.p2.y; + if (LineSegIntersect(L1, L2)) + cout << "NO" << endl; + else + cout << "YES" << endl; + } + return 0; +} \ No newline at end of file diff --git a/john-and-gcd-list.cpp b/john-and-gcd-list.cpp new file mode 100644 index 0000000..ea4d0be --- /dev/null +++ b/john-and-gcd-list.cpp @@ -0,0 +1,37 @@ +//john-and-gcd-list.cpp +//John and GCD list +//Weekly Challenges - Week 8 +//Author: derekhh + +#include +using namespace std; + +int a[1000]; + +int gcd(int a, int b) +{ + return b ? gcd(b, a%b) : a; +} + +int lcm(int a, int b) +{ + return a * b / gcd(a, b); +} + +int main() +{ + int t; + cin >> t; + while (t--) + { + int n; + cin >> n; + for (int i = 0; i < n; i++) + cin >> a[i]; + cout << a[0] << " "; + for (int i = 1; i < n; i++) + cout << lcm(a[i - 1], a[i]) << " "; + cout << a[n - 1] << endl; + } + return 0; +} \ No newline at end of file diff --git a/kundu-and-tree.cpp b/kundu-and-tree.cpp new file mode 100644 index 0000000..8b80fa7 --- /dev/null +++ b/kundu-and-tree.cpp @@ -0,0 +1,68 @@ +//kundu-and-tree.cpp +//Kundu and Tree +//Weekly Challenges - Week 5 +//Author: derekhh + +#include +#include +using namespace std; + +const int MOD = 1000000007; +const int MAXN = 100000; +vector v[MAXN + 1]; +bool visited[MAXN + 1]; +int connected[MAXN + 1]; + +void dfs(int now, int num) +{ + connected[num]++; + visited[now] = true; + int sz = (int)v[now].size(); + for (int i = 0; i < sz; i++) + if (!visited[v[now][i]]) + dfs(v[now][i], num); +} + +int dp[MAXN + 1][4]; + +int main() +{ + int n; + scanf("%d", &n); + for (int i = 0; i < n - 1; i++) + { + int a, b; + char c; + scanf("%d %d %c", &a, &b, &c); + if (c == 'b') + { + v[a].push_back(b); + v[b].push_back(a); + } + } + int conn = 0; + for (int i = 1; i <= n; i++) + { + if (!visited[i]) + { + conn++; + dfs(i, conn); + } + } + + //printf("%d\n", conn); + for (int i = 1; i <= conn; i++) + dp[i][1] = dp[i - 1][1] + connected[i]; + + for (int i = 2; i <= 3; i++) + { + for (int j = i; j <= conn; j++) + { + dp[j][i] = (dp[j - 1][i] + (long long)dp[j - 1][i - 1] * connected[j]) % MOD; + //printf("dp[%d][%d] = %d\n", j, i, dp[j][i]); + } + } + + printf("%d\n", dp[conn][3]); + return 0; +} \ No newline at end of file diff --git a/lexicographic-steps.cpp b/lexicographic-steps.cpp new file mode 100644 index 0000000..0671131 --- /dev/null +++ b/lexicographic-steps.cpp @@ -0,0 +1,64 @@ +//lexicographic-steps.cpp +//Lexicographic paths +//Weekly Challenges - Week 9 +//Author: derekhh + +#include +using namespace std; + +int n, m; +int c[21][21]; + +void init() +{ + for (int i = 1; i <= 20; i++) + { + c[i][0] = c[i][i] = 1; + for (int j = 1; j < i; j++) + c[i][j] = c[i - 1][j] + c[i - 1][j - 1]; + } +} + +int main() +{ + init(); + int t; + scanf("%d", &t); + while (t--) + { + int k; + scanf("%d%d%d", &n, &m, &k); + int cx = 0, cy = 0; + while (cx != n || cy != m) + { + int nH = n - cx, nV = m - cy; + if (nH == 0) + { + printf("V"); + cy++; + } + else if (nV == 0) + { + printf("H"); + cx++; + } + else + { + int temp = c[nH + nV - 1][nH - 1]; + if (k >= temp && temp > 0) + { + printf("V"); + k -= temp; + cy++; + } + else + { + printf("H"); + cx++; + } + } + } + printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/matrix-tree.cpp b/matrix-tree.cpp new file mode 100644 index 0000000..22427a2 --- /dev/null +++ b/matrix-tree.cpp @@ -0,0 +1,44 @@ +//matrix-tree.cpp +//Matrix Tree +//Ad Infinitum - Math Programming Contest August'14 +//Author: derekhh + +#include +#include +using namespace std; + +int w[100001]; +vector v[100001]; +bool visited[100001]; +long long ans; + +const int MOD = 1000000007; + +void dfs(int root, int p) +{ + visited[root] = true; + int sz = (int)v[root].size(); + for (int i = 0; i < sz; i++) + if (!visited[v[root][i]]) + dfs(v[root][i], root); + ans = (ans * (w[root] - w[p] + MOD)) % MOD; +} + +int main() +{ + ans = 1; + int n; + cin >> n; + for (int i = 1; i <= n; i++) + cin >> w[i]; + for (int i = 0; i < n - 1; i++) + { + int a, b; + cin >> a >> b; + v[a].push_back(b); + v[b].push_back(a); + } + dfs(1, 0); + cout << ans << endl; + return 0; +} \ No newline at end of file diff --git a/minimum-average-waiting-time.cpp b/minimum-average-waiting-time.cpp new file mode 100644 index 0000000..5f3969b --- /dev/null +++ b/minimum-average-waiting-time.cpp @@ -0,0 +1,84 @@ +//minimum-average-waiting-time.cpp +//Minimum Average Waiting Time +//Weekly Challenges - Week 6 +//Author: derekhh + +#include +#include +#include +#include +using namespace std; + +struct Type +{ + int t, l; +}; + +struct cmp +{ + bool operator() (Type& a, Type& b) + { + return a.l > b.l; + } +}; + +bool operator < (Type a, Type b) +{ + return a.t < b.t; +} + +vector v; +priority_queue, cmp> pq; + +int main() +{ + int n; + cin >> n; + for (int i = 0; i < n; i++) + { + int t, l; + cin >> t >> l; + Type tmp; tmp.t = t; tmp.l = l; + v.push_back(tmp); + } + sort(v.begin(), v.end()); + long long cur_t = 0; + long long tot = 0; + int idx = 0; + + while (true) + { + int i; + for (i = idx; i < n; i++) + { + if (v[i].t <= cur_t) + pq.push(v[i]); + else + { + idx = i; + break; + } + } + + if (i == n) idx = n; + + if (!pq.empty()) + { + Type tmp = pq.top(); + tot += cur_t + tmp.l - tmp.t; + cur_t += tmp.l; + pq.pop(); + } + + else + { + if (idx != n) + cur_t = v[idx].t; + } + + if (idx == n && pq.empty()) break; + } + + cout << tot / n << endl; + return 0; +} \ No newline at end of file diff --git a/priyanka-and-toys.cpp b/priyanka-and-toys.cpp new file mode 100644 index 0000000..b0101c3 --- /dev/null +++ b/priyanka-and-toys.cpp @@ -0,0 +1,25 @@ +//priyanka-and-toys.cpp +//Priyanka and Toys +//Weekly Challenges - Week 12 +//Author: derekhh + +#include +#include +using namespace std; + +int w[100000]; + +int main() +{ + int n; + cin >> n; + for (int i = 0; i < n; i++) + cin >> w[i]; + sort(w, w + n); + int ans = 1, prev = w[0]; + for (int i = 1; i < n; i++) + if (w[i] - prev > 4) + prev = w[i], ans++; + cout << ans << endl; + return 0; +} \ No newline at end of file diff --git a/reverse-game.cpp b/reverse-game.cpp new file mode 100644 index 0000000..41391f9 --- /dev/null +++ b/reverse-game.cpp @@ -0,0 +1,24 @@ +//reverse-game.cpp +//Reverse Game +//Ad Infinitum - Math Programming Contest August'14 +//Author: derekhh + +#include +using namespace std; + +int pos[100000]; + +int main() +{ + int t; + cin >> t; + while (t--) + { + int n, k; + cin >> n >> k; + for (int i = 0, j = n - 1; i < n; i += 2, j--) pos[j] = i; + for (int i = 1, j = 0; i < n; i += 2, j++) pos[j] = i; + cout << pos[k] << endl; + } + return 0; +} \ No newline at end of file diff --git a/risk-management.cpp b/risk-management.cpp new file mode 100644 index 0000000..10c76e8 --- /dev/null +++ b/risk-management.cpp @@ -0,0 +1,93 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +const int MAXN = 300 + 1; + +double a[MAXN][MAXN], x[MAXN], g[MAXN], nx[MAXN], tmp[MAXN]; + +double eval(int n, double x[]) +{ + double ret = 0; + for (int i = 1; i <= n; i++) + for (int j = 1; j <= n; j++) + ret += x[i] * x[j] * a[i][j]; + return ret; +} + +double ans; +double bestx[MAXN]; + +void foo(int n) +{ + double score = eval(n, x); + int test = 100; + while (test--) + { + double best = DBL_MIN; + int bestidx = 0; + for (int i = 1; i <= n; i++) + { + double tmp = score; + for (int j = 1; j <= n; j++) + { + if (i != j) + tmp -= 4 * a[i][j] * x[i] * x[j]; + } + + if (tmp > best) + { + best = tmp; + bestidx = i; + } + } + + if (best <= score) + break; + + score = best; + x[bestidx] = -x[bestidx]; + } + + if (score > ans) + { + ans = score; + memcpy(bestx, x, sizeof(x)); + } +} + +int main() +{ + int n; + cin >> n; + + for (int i = 1; i <= n; i++) + for (int j = 1; j <= n; j++) + cin >> a[i][j]; + + ans = DBL_MIN; + + int test = 100; + while (test--) + { + for (int i = 1; i <= n; i++) + { + int rnd = rand(); + if (1.0 * rnd / RAND_MAX > 0.5) + x[i] = -1; + else + x[i] = 1; + } + foo(n); + } + + for (int i = 1; i <= n; i++) + cout << bestx[i] << " "; + cout << endl; + + return 0; +} \ No newline at end of file diff --git a/robot.cpp b/robot.cpp new file mode 100644 index 0000000..da84e19 --- /dev/null +++ b/robot.cpp @@ -0,0 +1,34 @@ +//robot.cpp +//Robot +//Weekly Challenges - Week 10 +//Author: derekhh + +#include +#include +#include +using namespace std; + +const int MAXN = 500000; +int v[MAXN + 1], p[MAXN + 1]; +long long sv[MAXN + 1], f[MAXN + 1]; + +int main() +{ + int n; + cin >> n; + for (int i = 1; i <= n; i++) + { + cin >> v[i] >> p[i]; + sv[i] = sv[i - 1] + v[i]; + } + f[1] = 0; + for (int i = 2; i <= n; i++) + { + f[i] = INT_MAX; + for (int j = 1; j < i; j++) + if (p[j] + j >= i) + f[i] = min(f[i], f[j] + v[j]); + } + cout << sv[n] - f[n] << endl; + return 0; +} \ No newline at end of file diff --git a/sansa-and-xor.cpp b/sansa-and-xor.cpp new file mode 100644 index 0000000..9374acf --- /dev/null +++ b/sansa-and-xor.cpp @@ -0,0 +1,30 @@ +//sansa-and-xor.cpp +//Sansa and XOR +//Weekly Challenges - Week 9 +//Author: derekhh + +#include +#include +using namespace std; + +int main() +{ + int t; + cin >> t; + while (t--) + { + int n; + cin >> n; + int ans = 0; + for (int i = 0; i < n; i++) + { + int num; + cin >> num; + long long times = (long long)(i + 1) * (n - i); + if (times % 2 == 1) + ans ^= num; + } + cout << ans << endl; + } + return 0; +} \ No newline at end of file diff --git a/sherlock-and-square.cpp b/sherlock-and-square.cpp new file mode 100644 index 0000000..0fdcaa7 --- /dev/null +++ b/sherlock-and-square.cpp @@ -0,0 +1,34 @@ +//sherlock-and-square.cpp +//Sherlock and Square +//Weekly Challenges - Week 11 +//Author: derekhh + +#include +using namespace std; + +const int MOD = 1000000007; + +int ModExp(int a, int b, int n) +{ + long long c = 1, d = a; + while (b) + { + if (b & 1) c = (c*d) % n; + d = (d*d) % n; + b >>= 1; + } + return (int)c; +} + +int main() +{ + int t; + cin >> t; + while (t--) + { + int n; + cin >> n; + cout << (ModExp(2, n + 1, MOD) + 2) % MOD << endl; + } + return 0; +} \ No newline at end of file diff --git a/special-numbers.cpp b/special-numbers.cpp new file mode 100644 index 0000000..a328613 --- /dev/null +++ b/special-numbers.cpp @@ -0,0 +1,42 @@ +//special-numbers.cpp +//Special Numbers +//Addepar Hackathon +//Author: derekhh + +#include +using namespace std; + +int d[100001]; +int f[100001]; + +void init() +{ + for (int i = 1; i <= 100000; i++) + { + for (int j = 1; j*j <= i; j++) + { + if (i % j == 0) + { + if (j != i / j) + d[i] += 2; + else + d[i] += 1; + } + } + + if (!f[d[i]]) f[d[i]] = i; + } +} + +int main() +{ + init(); + int n; + cin >> n; + int ret = 0; + for (int i = 1; i <= 100000; i++) + if (f[i] <= n) + ret += f[i]; + cout << ret << endl; + return 0; +} \ No newline at end of file diff --git a/stepping-stones-game.cpp b/stepping-stones-game.cpp new file mode 100644 index 0000000..9da08a4 --- /dev/null +++ b/stepping-stones-game.cpp @@ -0,0 +1,25 @@ +//stepping-stones-game.cpp +//Stepping Stones Game +//Ad Infinitum - Math Programming Contest August'14 +//Author: derekhh + +#include +#include +using namespace std; + +int main() +{ + int t; + cin >> t; + while (t--) + { + long long n; + cin >> n; + long long tmp = (long long)sqrt(2.0 * n); + if (tmp * (tmp + 1) == 2 * n) + cout << "Go On Bob " << tmp << endl; + else + cout << "Better Luck Next Time" << endl; + } + return 0; +} \ No newline at end of file diff --git a/strange-numbers.cpp b/strange-numbers.cpp new file mode 100644 index 0000000..31348b2 --- /dev/null +++ b/strange-numbers.cpp @@ -0,0 +1,58 @@ +//strange-numbers.cpp +//Strange numbers +//Weekly Challenges - Week 11 +//Author: derekhh + +#include +#include +#include +using namespace std; + +set s; +unsigned long long q[500]; +int head, tail; + +int lenDigit(unsigned long long num) +{ + int digits = 0; + do + { + num /= 10; + digits++; + } while (num != 0); + return digits; +} + +int main() +{ + const long long upperBound = 1000000000000000000LL; + head = tail = 0; + for (int i = 0; i <= 9; i++) + { + q[tail++] = i; + s.insert(i); + } + while (head < tail) + { + unsigned long long num = q[head++]; + int len = lenDigit(num); + for (int i = len; i <= len + 2; i++) + { + unsigned long long candidate = num * i; + if (s.find(candidate) == s.end() && lenDigit(candidate) == i && candidate <= upperBound) + { + q[tail++] = candidate; + s.insert(candidate); + } + } + } + int t; + cin >> t; + while (t--) + { + unsigned long long l, r; + cin >> l >> r; + cout << count_if(q, q + tail, [l, r](unsigned long long i) {return i >= l && i <= r; }) << endl; + } + return 0; +} \ No newline at end of file diff --git a/string-function-calculation.cpp b/string-function-calculation.cpp new file mode 100644 index 0000000..7f86902 --- /dev/null +++ b/string-function-calculation.cpp @@ -0,0 +1,110 @@ +//string-function-calculation.cpp +//String Function Calculation +//Weekly Challenges - Week 7 +//Author: derekhh + +#include +#include +#include +using namespace std; + +#define nb nexta +#define head height +#define rank b + +const int maxn = 100010; +char s[maxn]; +int n, id[maxn], height[maxn], b[maxn], nexta[maxn]; + +bool cmp(const int& i, const int& j) +{ + return s[i] < s[j]; +} + +void SuffixSort() +{ + int i, j, k, h; + for (i = 0; i < n; i++) id[i] = i; + sort(id, id + n, cmp); + for (i = 0; i < n; i++) + { + if (i == 0 || s[id[i]] != s[id[i - 1]]) + b[id[i]] = i; + else b[id[i]] = b[id[i - 1]]; + } + for (h = 1; h < n; h <<= 1) + { + for (i = 0; i < n; i++) + head[i] = nexta[i] = -1; + for (i = n - 1; i >= 0; i--) + { + if (id[i]) + { + j = id[i] - h; + if (j < 0) j += n; + nexta[j] = head[b[j]]; + head[b[j]] = j; + } + } + j = n - h; + nexta[j] = head[b[j]]; + head[b[j]] = j; + for (i = k = 0; i < n; i++) + if (head[i] >= 0) + for (j = head[i]; j >= 0; j = nexta[j]) + id[k++] = j; + for (i = 0; i < n; i++) + if (i>0 && id[i] + h < n&&id[i - 1] + h < n&&b[id[i]] == b[id[i - 1]] && b[id[i] + h] == b[id[i - 1] + h]) + nb[id[i]] = nb[id[i - 1]]; + else + nb[id[i]] = i; + for (i = 0; i < n; i++) + b[i] = nb[i]; + } +} + +void GetHeight() +{ + int i, j, h; height[0] = 0; + for (i = 0; i < n; i++) + rank[id[i]] = i; + for (h = 0, i = 0; i < n; i++) + { + if (rank[i] > 0) + { + j = id[rank[i] - 1]; + while (s[i + h] == s[j + h])++h; + height[rank[i]] = h; + if (h>0) --h; + } + } +} + +int st[maxn], top; + +int main() +{ + cin >> s; + n = strlen(s); + top = 0; + SuffixSort(); + GetHeight(); + height[n] = 0; + int best = n; + st[top++] = 0; + for (int i = 1; i < n + 1; i++) + { + //cout << height[i] << " "; + while (top != 0 && height[i] < height[st[top - 1]]) + { + int val = height[st[top - 1]]; + top--; + best = max(best, val * (top == 0 ? i : i - st[top - 1])); + } + + if (top == 0 || height[i] >= height[st[top - 1]]) + st[top++] = i; + } + cout << best << endl; + return 0; +} \ No newline at end of file diff --git a/swappermutation.cpp b/swappermutation.cpp new file mode 100644 index 0000000..e5bbf24 --- /dev/null +++ b/swappermutation.cpp @@ -0,0 +1,59 @@ +//swappermutation.cpp +//Swap Permutation +//Weekly Challenges - Week 5 +//Author: derekhh + +#include +#include +using namespace std; + +const int MOD = 1000000007; +long long f[2501][2501], fs[2501][2501]; +long long g[2501][2501]; + +int main() +{ + int n, k; + cin >> n >> k; + f[1][0] = fs[1][0] = 1; + for (int i = 1; i <= k; i++) + fs[1][i] = 1; + + for (int i = 2; i <= n; i++) + { + for (int j = 0; j <= k; j++) + { + if (j != min(i - 1, j)) + f[i][j] = (fs[i - 1][j] + MOD - fs[i - 1][j - min(i - 1, j) - 1]) % MOD; + else + f[i][j] = fs[i - 1][j]; + + //printf("f[%d][%d] = %d\n", i, j, f[i][j]); + + if (j == 0) + fs[i][j] = f[i][j] % MOD; + else + fs[i][j] = (fs[i][j - 1] + f[i][j]) % MOD; + } + } + + g[0][0] = 1; + for (int i = 1; i <= n; i++) + for (int j = 1; j <= i; j++) + if (i == j) + g[i][j] = 1; + else + g[i][j] = (g[i - 1][j - 1] + (i - 1) * g[i - 1][j]) % MOD; + + int ret1 = 0, ret2 = 0; + for (int i = 0; i <= k; i++) + if (i % 2 == k % 2) + ret1 = (ret1 + f[n][i]) % MOD; + + for (int i = 0; i <= n; i++) + if (n - i <= k) + ret2 = (ret2 + g[n][i]) % MOD; + + cout << ret1 << " " << ret2 << endl; + return 0; +} \ No newline at end of file diff --git a/towers.cpp b/towers.cpp new file mode 100644 index 0000000..f08e481 --- /dev/null +++ b/towers.cpp @@ -0,0 +1,60 @@ +//towers.cpp +//Towers +//Weekly Challenges - Week 10 +//Author: derekhh + +#include +#include +using namespace std; + +const int MOD = 1000000007; + +struct Type +{ + int val[15][15]; +} mat; + +Type mul(Type a, Type b) +{ + Type ret; + memset(ret.val, 0, sizeof(ret.val)); + for (int i = 0; i < 15; i++) + for (int j = 0; j < 15; j++) + for (int k = 0; k < 15; k++) + ret.val[i][j] = (ret.val[i][j] + (long long) a.val[i][k] * b.val[k][j]) % MOD; + return ret; +} + +Type pow(Type a, long long n) +{ + Type ret; + memset(ret.val, 0, sizeof(ret.val)); + for (int i = 0; i < 15; i++) + ret.val[i][i] = 1; + while (n != 0) + { + if (n % 2 == 1) + ret = mul(ret, a); + a = mul(a, a); + n /= 2; + } + return ret; +} + +int main() +{ + long long n, k; + cin >> n >> k; + memset(mat.val, 0, sizeof(mat.val)); + for (int i = 0; i < k; i++) + { + int h; + cin >> h; + mat.val[0][h - 1] = 1; + } + for (int i = 1; i <= 14; i++) + mat.val[i][i - 1] = 1; + Type ret = pow(mat, n); + cout << (ret.val[0][0] * 2) % MOD << endl; + return 0; +} \ No newline at end of file diff --git a/tree-pruning.cpp b/tree-pruning.cpp new file mode 100644 index 0000000..5505486 --- /dev/null +++ b/tree-pruning.cpp @@ -0,0 +1,22 @@ +//tree-pruning.cpp +//Tree Pruning +//Weekly Challenges - Week 11 +//Author: derekhh + +#include +using namespace std; + +int w[100000]; + +int main() +{ + int n, k; + cin >> n >> k; + for (int i = 0; i < n; i++) + cin >> w[i]; + for (int i = 0; i < n - 1; i++) + { + int a, b; + } + return 0; +} \ No newline at end of file diff --git a/triplets.cpp b/triplets.cpp new file mode 100644 index 0000000..d9b0ec6 --- /dev/null +++ b/triplets.cpp @@ -0,0 +1,83 @@ +//triplets.cpp +//Triplets +//Author: derekhh + +#include +#include +#include +#include +#include +using namespace std; + +const int MAXN = 100000; +int v[MAXN], h[MAXN]; +set s; +map m; + +class FenwickTree +{ +private: + long long tree[MAXN]; + +public: + FenwickTree() + { + memset(tree, 0, sizeof(tree)); + } + + long long get(int idx) + { + return query(idx) - query(idx - 1); + } + + void set(int idx, long long val) + { + long long curr = get(idx); + update(idx, val - curr); + } + + void update(int idx, long long val) + { + while (idx <= MAXN) + { + tree[idx] += val; + idx += (idx & -idx); + } + } + + long long query(int idx) + { + long long sum = 0; + while (idx > 0) + { + sum += tree[idx]; + idx -= (idx & -idx); + } + return sum; + } +}; + +FenwickTree a, b, c; + +int main() +{ + int n; + cin >> n; + for (int i = 0; i < n; i++) + { + cin >> v[i]; + s.insert(v[i]); + } + int cnt = 1; + for (set::iterator it = s.begin(); it != s.end(); ++it) + m.insert(make_pair(*it, cnt++)); + for (int i = 0; i < n; i++) + { + h[i] = m[v[i]]; + a.set(h[i], 1); + b.set(h[i], a.query(h[i] - 1)); + c.set(h[i], b.query(h[i] - 1)); + } + cout << c.query(MAXN) << endl; + return 0; +} \ No newline at end of file diff --git a/triplets.md b/triplets.md new file mode 100644 index 0000000..42f0471 --- /dev/null +++ b/triplets.md @@ -0,0 +1,98 @@ +# Triplets + +Let's consider a simplified version of the original problem. Instead of counting the number of *distinct* ascending triples, let's count the number of ascending tuples first. + +So the problem here is very easy. For each number a[i], we only need to know how many numbers before a[i] are smaller than this number. Many data structures can support this type of query but for this problem I would recommend [Fenwick tree](http://en.wikipedia.org/wiki/Fenwick_tree). You may also check this [TopCoder tutorial page](http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=binaryIndexedTrees) to learn more about this data structure. + +In short, a Fenwick tree can support two types of operations in logarithmic time complexity. One is to add a number x to a particular number a[i]. The other is to query the sum of numbers from a[1] to a[i]. + +Therefore, the number of integers smaller than a[i] is equal to a query operation in the Fenwick tree for the range [1..i-1]. So we've solved the "simplified version" within O(NlogN) time. + +How should we extend this solution to the original problem? We now create three Fenwick trees. For the first Fenwick tree T1, T1[i] == 1 indicates there exists a number i in the array; for the second tree T2, T2[i] indicates the number of ascending tuples ending with i; for the third tree T3, T3[i] indicates the number of ascending triplets ending with i. Now we can see there are connections between T1 and T2 and T3. The value of T2[i] equals to the sum of T1[1] to T1[i]. Similarly, the value of T3[i] equals to the sum of T2[1] to T2[i]. + +My code follows: + +```C++ +//triplets.cpp +//Triplets +//Author: derekhh + +#include +#include +#include +#include +#include +using namespace std; + +const int MAXN = 100000; +int v[MAXN], h[MAXN]; +set s; +map m; + +class FenwickTree +{ +private: + long long tree[MAXN]; + +public: + FenwickTree() + { + memset(tree, 0, sizeof(tree)); + } + + long long get(int idx) + { + return query(idx) - query(idx - 1); + } + + void set(int idx, long long val) + { + long long curr = get(idx); + update(idx, val - curr); + } + + void update(int idx, long long val) + { + while (idx <= MAXN) + { + tree[idx] += val; + idx += (idx & -idx); + } + } + + long long query(int idx) + { + long long sum = 0; + while (idx > 0) + { + sum += tree[idx]; + idx -= (idx & -idx); + } + return sum; + } +}; + +FenwickTree a, b, c; + +int main() +{ + int n; + cin >> n; + for (int i = 0; i < n; i++) + { + cin >> v[i]; + s.insert(v[i]); + } + int cnt = 1; + for (set::iterator it = s.begin(); it != s.end(); ++it) + m.insert(make_pair(*it, cnt++)); + for (int i = 0; i < n; i++) + { + h[i] = m[v[i]]; + a.set(h[i], 1); + b.set(h[i], a.query(h[i] - 1)); + c.set(h[i], b.query(h[i] - 1)); + } + cout << c.query(MAXN) << endl; + return 0; +} diff --git a/upstairs.cpp b/upstairs.cpp new file mode 100644 index 0000000..c2868d4 --- /dev/null +++ b/upstairs.cpp @@ -0,0 +1,44 @@ +//upstairs.cpp +//Upstairs +//Addepar Hackathon +//Author: derekhh + +#include +using namespace std; + +int a[100000]; + +int main() +{ + int n; + cin >> n; + int sum = 0; + for (int i = 0; i < n; i++) + cin >> a[i]; + for (int i = 1; i < n; i++) + if (a[i] > a[i - 1]) + sum += a[i] - a[i - 1]; + int best = sum, bestval = -2; + for (int i = 0; i < n - 1; i++) + { + int sum_new = sum; + if (i - 1 >= 0 && a[i] > a[i - 1]) + sum_new -= a[i] - a[i - 1]; + if (a[i + 1] > a[i]) + sum_new -= a[i + 1] - a[i]; + if (i + 2 < n && a[i + 2] > a[i + 1]) + sum_new -= a[i + 2] - a[i + 1]; + + if (i - 1 >= 0 && a[i + 1] > a[i - 1]) + sum_new += a[i + 1] - a[i - 1]; + if (a[i] > a[i + 1]) + sum_new += a[i] - a[i + 1]; + if (i + 2 < n && a[i + 2] > a[i]) + sum_new += a[i + 2] - a[i]; + + if (sum_new < best) + best = sum_new, bestval = i; + } + cout << bestval + 1 << endl; + return 0; +} \ No newline at end of file diff --git a/xor-and-sum.cpp b/xor-and-sum.cpp new file mode 100644 index 0000000..966f19b --- /dev/null +++ b/xor-and-sum.cpp @@ -0,0 +1,62 @@ +//xor-and-sum.cpp +//Xor and Sum +//101 Hack July'14 +//Author: derekhh + +#include +#include +using namespace std; + +string sa, sb; +int a[500000], b[500000]; +int cnt[500000][2]; + +const int MOD = 1000000007; + +int main() +{ + cin >> sa; + cin >> sb; + int la = sa.size(), lb = sb.size(); + for (int i = 0; i < la; i++) + a[la - i - 1] = sa[i] - '0'; + for (int i = 0; i < lb; i++) + b[lb - i - 1] = sb[i] - '0'; + + cnt[0][0] = (b[0] == 0); + cnt[0][1] = (b[0] == 1); + for (int i = 1; i < lb + 314159; i++) + { + cnt[i][0] = cnt[i - 1][0] + (b[i] == 0); + cnt[i][1] = cnt[i - 1][1] + (b[i] == 1); + } + + long long ret = 0, cur = 0; + for (int i = 0; i < lb + 314159; i++) + { + int cnt0, cnt1; + if (i < 314159) + { + cnt0 = cnt[i][0] + 314159 - i; + cnt1 = cnt[i][1]; + } + else + { + cnt0 = 314159 + cnt[lb - 1][0] - cnt[i - 314159 - 1][0]; + cnt1 = cnt[lb - 1][1] - cnt[i - 314159 - 1][1]; + } + + long long sum0 = a[i] * cnt0; + long long sum1 = (a[i] ^ 1) * cnt1; + + long long sum = sum0 + sum1; + + if (i == 0) cur = 1; + else cur = (cur * 2) % MOD; + + long long tmp = (cur * sum) % MOD; + ret = (ret + tmp) % MOD; + } + cout << ret << endl; + return 0; +} \ No newline at end of file diff --git a/yaxis.cpp b/yaxis.cpp new file mode 100644 index 0000000..8b99e01 --- /dev/null +++ b/yaxis.cpp @@ -0,0 +1,108 @@ +//yaxis.cpp +//Yaxis +//Addepar Hackathon +//Author: derekhh + +#include +#include +#include +#include +#include +using namespace std; + +struct Type +{ + int m, b; +}; + +bool operator < (Type t1, Type t2) +{ + return t1.b < t2.b; +} + +Type l[200000]; +set s; +map m; +int h[200000]; + +class FenwickTree +{ +private: + long long tree[200001]; + +public: + FenwickTree() + { + memset(tree, 0, sizeof(tree)); + } + + long long get(int idx) + { + return query(idx) - query(idx - 1); + } + + void set(int idx, long long val) + { + long long curr = get(idx); + update(idx, val - curr); + } + + void update(int idx, long long val) + { + while (idx <= 200000) + { + tree[idx] += val; + idx += (idx & -idx); + } + } + + long long query(int idx) + { + long long sum = 0; + while (idx > 0) + { + sum += tree[idx]; + idx -= (idx & -idx); + } + return sum; + } +}; + +FenwickTree a, b; +long long a1[200000], a2[200000]; +long long b1[200000], b2[200000]; + +int main() +{ + int n; + cin >> n; + for (int i = 0; i < n;i++) + cin >> l[i].m >> l[i].b; + sort(l, l + n); + for (int i = 0; i < n; i++) + s.insert(l[i].m); + int cnt = 1; + for (set::iterator it = s.begin(); it != s.end(); ++it) + m.insert(make_pair(*it, cnt++)); + for (int i = 0; i < n; i++) + { + h[i] = m[l[i].m]; + a.set(h[i], 1); + a1[i] = a.query(h[i] - 1); + b1[i] = a.query(200000) - a.query(h[i]); + } + for (int i = n - 1; i >= 0; i--) + { + b.set(h[i], 1); + a2[i] = b.query(h[i] - 1); + b2[i] = b.query(200000) - b.query(h[i]); + } + long long ret = 0; + for (int i = 0; i < n; i++) + { + ret += a1[i] * a2[i]; + ret += b1[i] * b2[i]; + } + cout << ret << endl; + return 0; +} \ No newline at end of file