Showing posts with label atcoder. Show all posts
Showing posts with label atcoder. Show all posts

Sunday, April 3, 2022

Educational DP Contest - Problem Tags

Contest Link:  Educational DP Contest / DP まとめコンテスト - AtCoder

First Half:



Second Half:

L - Deque (atcoder.jp) [Interval DP/区間DP]

N - Slimes (atcoder.jp) [Interval DP/区間DP]

M - Candies (atcoder.jp) [DP + Prefix Sum Optimization]

O - Matching (atcoder.jp) [Bit DP]

P - Independent Set (atcoder.jp) [DP on Trees/Simple]

V - Subtree (atcoder.jp) [DP on Trees/All around]

Q - Flowers (atcoder.jp) [DP + Range Min Query Optimization]

R - Walk (atcoder.jp) [Power of adjacency matrix]

S - Digit Sum (atcoder.jp) [Digit DP]

T - Permutation (atcoder.jp) [Insertion DP]

U - Grouping (atcoder.jp) [Bit DP with nested subset O(3^16)]

W - Intervals (atcoder.jp) [Inline DP + Segment Tree]

Z - Frog 3 (atcoder.jp) [DP + Convex Hull]

Y - Grid 2 (atcoder.jp) [DP + Inclusion-Exclusion Principle]

X - Tower (atcoder.jp) [Custom Sorting + Knapsack]


Sunday, January 2, 2022

競プロ典型 90 問

 ★2

004 - Cross Sum(★2) (atcoder.jp) [pre-processing the sum of each row and column]

022 - Cubic Cake(★2) (atcoder.jp) [find gcd=g of all side, and calculate the num of cuts to make g-sided cubes ]

024 - Select +/- One(★2) (atcoder.jp) [k should be equal or greater than sum of diffs, and reminder should be even]




055 - Select 5(★2) (atcoder.jp) [brute force, const opt]

078 - Easy Graph Problem(★2) (atcoder.jp) [tracking nodes' state in array]


★3


038 - Large LCM(★3) (atcoder.jp) [lcm, gcd, formula transformation]

046 - I Love 46(★3) (atcoder.jp) [mod hash array, counting]




044 - Shift and Swapping(★3) (atcoder.jp) [keep track of shift number, us it as offset]

007 - CP Classes(★3) (atcoder.jp) [upper_bound, lower_bound]

020 - Log Inequality(★3) (atcoder.jp) [long long int, log formula transformation]

032 - AtCoder Ekiden(★3) [next_permutation, set, brute-force + pruning]



076 - Cake Cut(★3) (atcoder.jp) [2 pointers, Caterpillar method]

075 - Magic For Balls(★3) (atcoder.jp) [Factorization prime divisors]



079 - Two by Two(★3) (atcoder.jp) [greedy, flipping from top left corner]


082 - Counting Numbers(★3) (atcoder.jp) [calculate answer for each digit length]

018 - Statue of Chokudai(★3) (atcoder.jp) [geometry, triangle, sin, cos, arctan]


★4








070 - Plant Planning(★4) (atcoder.jp) [Manhattan dist, find median]




085 - Multiplication 085(★4) (atcoder.jp) [divisor factoring, backtracking]



★5

029 - Long Bricks(★5) (atcoder.jp) [Simulate by using range set/range max segtree]


056 - Lucky Bag(★5) (atcoder.jp) [knapsack dp, sub sum problem]


013 - Passing(★5) (atcoder.jp) [Dijksra from start and end node]


060 - Chimera(★5) (atcoder.jp) [Do LIS twice from left & right side]

087 - Chokudai's Demand(★5) (atcoder.jp) [Use binary search to find upper & lower bound + Floyd-Warshall]


051 - Typical Shop(★5) (atcoder.jp) [meet-in-the-middle, binary search]



030 - K Factors(★5) (atcoder.jp) [Sieve Of Eratosthenes, prime factoring]

037 - Don't Leave the Spice(★5) (atcoder.jp) [Knapsack like dp optimized by RangeMax SegTree]

066 - Various Arrays(★5) (atcoder.jp) [Linearity of expectation, probability]

086 - Snuke's Favorite Arrays(★5) (atcoder.jp) [Bit partitioning, combinatorics, bit-brute force]

068 - Paired Information(★5) (atcoder.jp) [UnionFind, Set, Pre-processing queries]


★6

019 - Pick Two(★6) (atcoder.jp) [range dp, interval dp]

045 - Simple Grouping(★6) (atcoder.jp) [bitDP, nested subset loop O(3^n)]

062 - Paint All(★6) (atcoder.jp) [Simulate in reverse order of operations]

083 - Colorful Graph(★6) (atcoder.jp) [Sqrt decomposotion, split deg>=B, deg<B nodes]

054 - Takahashi Number(★6) (atcoder.jp) [Reduce edge number by changing graph]

080 - Let's Share Bit(★6) (atcoder.jp) [Combinatorics, inclusion & exclusion principle, bit operations]

015 - Don't be too close(★6) (atcoder.jp) [Combinatorics, nCk, Harmonic series = O(logN)]

011 - Gravy Jobs(★6) (atcoder.jp) [dp[i][j] - i days and j jobs ans, process jobs in deadline order]

031 - VS AtCoder(★6) [grundy, dp[i][j] - i blue & j white state grundy number]

088 - Similar but Different Ways(★6) (atcoder.jp) [Pigeonhole Principle, dfs + pruning]

009 - Three Point Angle(★6) (atcoder.jp) [drift angle, atan2, geometry]

049 - Flip Digits 2(★6) (atcoder.jp) [Interval Flipping, converting to graph, MST, UnionFind]

057 - Flip Flap(★6) (atcoder.jp) [Matrix rank, Gaussian method, Elimination method]

074 - ABC String 2(★6) (atcoder.jp) [Ad-hoc, Experiment, Potential, Semi-Invariant, 2^60=10^18]


★7

023 - Avoid War(★7) (atcoder.jp) [BitDP, Profiling, State Elimination]

077 - Planes on a 2D Plane(★7) (atcoder.jp) [Max Flow, Bipartite max matching, Find match]

047 - Monochromatic Diagonal(★7) (atcoder.jp) [String modification RGB->012, Rolling-hash]

005 - Restricted Digits(★7) (atcoder.jp) [Digit DP, doubling, pow matrix]

017 - Crossing Segments(★7) (atcoder.jp) [BIT, counting left/right ends]

089 - Partitions and Inversions(★7) (atcoder.jp) [DP, 2 pointers, BIT, index compression]




025 - Digit Product Equation(★7) (atcoder.jp) [dfs, prime divisors, brute-force]

035 - Preserve Connectivity(★7) (atcoder.jp) [Euler tour, lca, dfs, auxillary tree]



Tuesday, November 2, 2021

ARC106 B - Values

Problem:

B - Values (atcoder.jp)

Idea:

Check sum of nodes in the same connected components

Source:

int n, m;
Int a[N], b[N];

struct UF {
	vector<int> Parent;
	vector<int> Size;
	void init(int n) {
		Parent.resize(n);
		Size.resize(n);
		REP(i, n) {
			make_set(i);
		}
	}
	void make_set(int v) {
		Parent[v] = v;
		Size[v] = 1;
	}
	int find_set(int v) {
		if (v == Parent[v])
			return v;
		return Parent[v] = find_set(Parent[v]);
	}
	void union_sets(int a, int b) {
		a = find_set(a);
		b = find_set(b);
		if (a != b) {
			if (Size[a] < Size[b])
				swap(a, b);
			Parent[b] = a;
			Size[a] += Size[b];
		}
	}
};

void solve()
{
	cin >> n >> m;
	REP(i, n) cin >> a[i];
	REP(i, n) cin >> b[i];

	UF uf;
	uf.init(n);
	REP(i, m) {
		int u, v;
		cin >> u >> v;
		u--; v--;
		uf.union_sets(u, v);
	}
	map<int, Int> mp1, mp2;
	REP(i, n) {
		int par = uf.find_set(i);
		mp1[par] += a[i];
		mp2[par] += b[i];
	}

	for (auto x : mp1) {
		int par = x.first;
		if (mp1[par] != mp2[par]) {
			cout << "No" << endl;
			return;
		}
	}
	cout << "Yes" << endl;
}


int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(9);
	solve();
	return 0;
}

Monday, May 31, 2021

Segment Tree problems in Atcoder

A. Segment Trees

1.Range Add Update/Point Get Query
D - ぴょんぴょんトレーニング (atcoder.jp)
typhoon - 台風 (Typhoon) (atcoder.jp)



2.Range Min Query/Point Update
D - 括弧列 (atcoder.jp)



3.


4.


5.


6.


B. Segment Tree with Lazy Propagation

1.Range Add/Range Min Query
B - ドキドキデート大作戦高橋君 (atcoder.jp)

2.Range Set/Range Max Query
029 - Long Bricks(★5) (atcoder.jp)

3.Range Set/Range Sum Query + Digit Arrange
E - Replace Digits (atcoder.jp)

4.Range Add/Range Max Query (Inline DP)
W - Intervals (atcoder.jp)

5.Range Multi&Add/Range Sum Query
K - Range Affine Range Sum (atcoder.jp)

6.Range Add/Range Sum Query (2D Rectangles)
N - ビルの建設 (atcoder.jp)

7.Range Min Update/Range Min Query
F - Simplified Reversi (atcoder.jp)

8.Range Inverse Update/Range Inverse Count Query (Binary string)
L - Lazy Segment Tree (atcoder.jp)

9.

10.

11.


C. Some extras

1.Range Set/Range Or Query (Flatten Trees by Euler Tour, Segtee on Tree)
E. New Year Tree: Problem - E - Codeforces

2.

3.





Sunday, March 21, 2021

Atcoder.abc185_f F - Range Xor Query

1.Problem
F - Range Xor Query (atcoder.jp)

2.Idea
XOR segtree

3.Source

 Int seg[1 << 20];  
 void set_value(Int pos, Int val) {  
      pos += 1 << 19;  
      seg[pos] = seg[pos] ^ val;  
      while ((pos /= 2) > 0) {  
           seg[pos] = seg[pos * 2] ^ seg[pos * 2 + 1];  
      }  
 }  
 Int get_xor(int ql, int qr, int sl = 0, int sr = 1 << 19, int pos = 1) {  
      //no overlap  
      if (qr <= sl || sr <= ql) return 0;  
      //fit in the segment  
      if (ql <= sl && sr <= qr) return seg[pos];  
      //partially overlap  
      Int sm = (sl + sr) / 2;  
      Int lxor = get_xor(ql, qr, sl, sm, pos * 2);  
      Int rxor = get_xor(ql, qr, sm, sr, pos * 2 + 1);  
      return lxor ^ rxor;  
 }  
 void solve()  
 {  
      Int n, q, t, x, y;  
      cin >> n >> q;  
      for (int i = 0; i < n; i++) {  
           int x; cin >> x;  
           set_value(i, x);  
      }  
      for (int i = 0; i < q; i++) {  
           cin >> t >> x >> y;  
           if (t == 1) {  
                set_value(x - 1, y);  
           }  
           else {  
                cout << get_xor(x - 1, y) << endl;  
           }  
      }  
 }  

Saturday, February 6, 2021

Atcoder.arc068_c Snuke Line

1.Problem
https://atcoder.jp/contests/arc068/tasks/arc068_c

2.Idea
Using BIT like an imos method in order to update intervals.

3.Source

 class BIT { //1 -indexed  
      const int n;  
      vector<Int> bit;  
 public:  
      BIT(int _n = 0) : n(_n), bit(n + 1, 0) {}  
      void add(int i, const Int x = 1) { for (i++; i <= n; i += i&-i) bit[i] += x; }  
      Int sum(int i) { Int x = 0; for (i++; i; i -= i & -i) x += bit[i]; return x; }  
      Int sum(int i, int j) { return sum(j) - sum(i - 1); }  
 };  
 vector<pair<int, int>> range[N];  
 void solve()  
 {  
      int n, m;  
      cin >> n >> m;  
      REP(i, n) {  
           int l, r; cin >> l >> r; 
           // Holding the intervals by the length 
           range[r - l + 1].push_back(make_pair(l, r));  
      }  
      BIT bit(m + 1);  
      int up = n;  
      RREP(d, m) {  
           for (pair<int, int> p : range[d]) {  
                // Here is the imos in interval
                bit.add(p.first, 1);  
                bit.add(p.second + 1, -1);  
                up--;  
           }  
           int cnt = 0;  
           // This takes only log(d)
           for (int i = d; i <= m; i += d) {  
                cnt += bit.sum(i);  
           }  
           cout << cnt + up << endl;  
      }  
 }  

Thursday, February 4, 2021

Atcoder.abc127_f Absolute Minima

1.Problem
https://atcoder.jp/contests/abc127/submissions/5607909

2.Idea
Using BIT to find and calc the median value.
Credit

3.Source

 const int N = 200005;  
 //////////////////////////////  
 template<typename T>  
 struct BIT {  
      int n;  
      vector<T> dat;  
      BIT(int n = 0) {  
           initialize(n);  
      }  
      void initialize(int nin) {  
           n = nin;  
           dat.resize(n);  
           for (int i = 0; i<n; i++) dat[i] = 0;  
      }  
      T sum(int i) {  
           T s = 0;  
           while (i >= 0) {  
                s += dat[i];  
                i = (i & (i + 1)) - 1;  
           }  
           return s;  
      }  
      T sum_between(int i, int j) {  
           if (i > j) return 0;  
           return sum(j) - sum(i - 1);  
      }  
      void plus(int i, T x) {  
           while (i < n) {  
                dat[i] += x;  
                i |= i + 1;  
           }  
      }  
      // a[0]+...+a[ret] >= x  
      int lower_bound(T x) {  
           int ret = -1;  
           int k = 1;  
           while (2 * k <= n) k <<= 1;  
           for (; k>0; k >>= 1) {  
                if (ret + k < n && dat[ret + k] < x) {  
                     x -= dat[ret + k];  
                     ret += k;  
                }  
           }  
           return ret + 1;  
      }  
 };  
 Int T[N], A[N], B[N];  
 void solve()  
 {  
      int q;  
      cin >> q;  
      vector<int> as;  
      REP(i, q) {  
           cin >> T[i];  
           if (T[i] == 1) {  
                cin >> A[i] >> B[i];  
                as.push_back(A[i]);  
           }  
      }  
      sort(as.begin(), as.end());  
      as.erase(unique(as.begin(), as.end()), as.end());  
      map<int, int> mp;  
      int sz = as.size();  
      REP(i, sz) mp[as[i]] = i;  
      BIT<Int> bitnum(sz);  
      BIT<Int> bitsum(sz);  
      Int fix = 0;  
      int all = 0;  
      REP(i, q) {  
           if (T[i] == 1) {  
                int a = mp[A[i]];  
                bitsum.plus(a, A[i]);  
                bitnum.plus(a, 1);  
                fix += B[i];  
                all++;  
           }  
           else {  
                Int ans = fix;  
                int idx = bitnum.lower_bound((all + 1) / 2);  
                ans += bitsum.sum_between(idx + 1, sz - 1);  
                ans -= as[idx] * bitnum.sum_between(idx + 1, sz - 1);  
                ans -= bitsum.sum_between(0, idx - 1);  
                ans += as[idx] * bitnum.sum_between(0, idx - 1);  
                cout << as[idx] << " " << ans << endl;  
           }  
      }  
 }   

Sunday, January 24, 2021

Atcoder.chokudai_S001_h LIS

1.Problem
https://atcoder.jp/contests/chokudai_S001/tasks/chokudai_S001_h

2.Idea
Using BIT Max Range Query

3.Source

 const int N = 100005;  
 //////////////////////////////  
 class BIT {  //1 -indexed
      const int n;  
      vector<Int> bit;  
 public:  
      BIT(int _n = 0) : n(_n), bit(n + 1, 0) {}  
      void add(int i, const Int x = 1) { for (i++; i <= n; i += i&-i) bit[i] += x; }  
      Int sum(int i) { Int x = 0; for (i++; i; i -= i & -i) x += bit[i]; return x; }  
      Int sum(int i, int j) { return sum(j) - sum(i - 1); }  
 };  
 struct FenwickTreeMin {  
      vector<int> bit;  //0 -indexed
      int n;  
      const int INF = (int)1e9;  
      FenwickTreeMin(int n) {  
           this->n = n;  
           bit.assign(n, INF);  
      }  
      FenwickTreeMin(vector<int> a) : FenwickTreeMin(a.size()) {  
           for (size_t i = 0; i < a.size(); i++)  
                update(i, a[i]);  
      }  
      int getmin(int r) {  
           int ret = INF;  
           for (; r >= 0; r = (r & (r + 1)) - 1)  
                ret = min(ret, bit[r]);  
           return ret;  
      }  
      void update(int idx, int val) {  
           for (; idx < n; idx = idx | (idx + 1))  
                bit[idx] = min(bit[idx], val);  
      }  
 };  
 struct FenwickTreeMax {  
      vector<int> bit;  // 0 - indexed
      int n;  
      FenwickTreeMax(int n) {  
           this->n = n;  
           bit.assign(n, -INF);  
      }  
      FenwickTreeMax(vector<int> a) : FenwickTreeMax(a.size()) {  
           for (size_t i = 0; i < a.size(); i++)  
                update(i, a[i]);  
      }  
      int getMax(int r) {  
           int ret = -INF;  
           for (; r >= 0; r = (r & (r + 1)) - 1)  
                ret = max(ret, bit[r]);  
           return ret;  
      }  
      void update(int idx, int val) {  
           for (; idx < n; idx = idx | (idx + 1))  
                bit[idx] = max(bit[idx], val);  
      }  
 };  
 int n;  
 int a[N];  
 void solve()  
 {  
      cin >> n;  
      REP(i, n) cin >> a[i];  
      map<int, int> mp;  
      REP(i, n) mp[a[i]] = 0;  
      int i = 0;  
      for (auto it = mp.begin(); it != mp.end(); it++) {  
           it->second = i;  
           i++;  
      }  
      FenwickTreeMax ftm(n);  
      REP(i, n) {  
           int cur = mp[a[i]];  
           int tmp = ftm.getMax(cur - 1);  
           if(tmp == -INF) ftm.update(cur, 1);  
           else ftm.update(cur, tmp + 1);  
      }  
      cout << ftm.getMax(n - 1) << endl;  
 }  

Friday, July 3, 2020

Dynamic Problems list in AtCoder:

1.Linear DP / 1次元状態DP
https://atcoder.jp/contests/abc004/tasks/abc004_4
https://atcoder.jp/contests/abc104/tasks/abc104_d
https://atcoder.jp/contests/abc162/tasks/abc162_f


3.Multi-dimension / DP多次元状態DP
https://atcoder.jp/contests/abc145/tasks/abc145_f
https://atcoder.jp/contests/arc067/tasks/arc067_c
https://atcoder.jp/contests/tenka1-2019-beginner/tasks/tenka1_2019_d
C - ビーム (atcoder.jp) (Manhattan Distance)


Problem - E - Codeforces (Merge technique)



10.Probability DP / 確率DP

11.Expectation DP / 期待値DP
https://atcoder.jp/contests/dp/tasks/dp_j

  -LCS:最長共通部分列

  -Cadane's Algorithm (Consecutive SubArray):部分和


  -Hashmap (Consecutive SubArray):部分和

  -Brackets Editing: 括弧対応


 -2D Grid Traversal / グリッド探索

 - Cumulative Sum / 累積和

B - Numbers on Papers (atcoder.jp) (PrefixSum, Lower_Bound)


17.Graph DP / グラフ関連

18.Combinatorics / 数え上げ
https://atcoder.jp/contests/dp/tasks/dp_y
C - Kill/Death (atcoder.jp) (Partition Number)

19.Inline DP / インラインDP

20.Memoization / メモ化再帰

21. Binary Lifting / ダブリング

22. Math / 数学

23. Monge-DP

24. Alien-DP

25. Game Theory