Saturday, January 29, 2022

CSES. Road Reparation

1.Problem

https://cses.fi/problemset/task/1675


2.Idea

Standard MST with UnionFind implementation


3.Source

 int n, m;  
 struct Edge {  
      int u, v, c;  
      bool operator < (const Edge &a)const {  
           return c < a.c;  
      }  
 } Edges[N];  
 struct UF {  
      vector<int> Parent;  
      vector<int> Size;  
      int Count;  
      void init(int n) {  
           Count = 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);  
                Count--;  
                Parent[b] = a;  
                Size[a] += Size[b];  
           }  
      }  
 };  
 void solve()  
 {  
      cin >> n >> m;  
      REP(i, m) {  
           cin >> Edges[i].u >> Edges[i].v >> Edges[i].c;  
           Edges[i].u--;  
           Edges[i].v--;  
      }  
      sort(Edges, Edges + m);  
      UF uf;  
      uf.init(n);  
      Int ret = 0;  
      REP(i, m) {  
           int u = Edges[i].u;  
           int v = Edges[i].v;  
           int c = Edges[i].c;  
           if (uf.find_set(u) != uf.find_set(v)) {  
                ret += c;  
                uf.union_sets(u, v);  
           }  
      }  
      if (uf.Count == 1) cout << ret << endl;  
      else cout << "IMPOSSIBLE" << endl;  
 }  

Sunday, January 23, 2022

CF R767 C. Meximum Array

Problem.

https://codeforces.com/contest/1629/problem/C

Idea.

Find mex for all suffix a[i:n-1]
Split the array greedily.


Source.

 void solve()  
 {  
      int t; cin >> t;  
      while (t--) {  
           int n; cin >> n;  
           vector<int> a(n);  
           REP(i, n) cin >> a[i];  
           // find all suffix mex a[i:n-1]  
           vector<int> mex(n);  
           vector<int> cnt(n + 10, 0);  
           int now = 0;  
           for (int i = n - 1; i >= 0; i--) {  
                cnt[a[i]]++;  
                if (a[i] == now) {  
                     while (cnt[now]) now++;  
                }  
                mex[i] = now;  
           }  
           // split the array  
           vector<int> ret;  
           int cur = 0;  
           while (cur < n) {  
                ret.push_back(mex[cur]);  
                int i = cur;  
                set<int> st;  
                while (i < n) {  
                     if (a[i] < mex[cur]) st.insert(a[i]);  
                     if (st.size() == mex[cur]) break;  
                     i++;  
                }  
                cur = i + 1;  
           }  
           cout << ret.size() << endl;  
           for (int x : ret) cout << x << " "; cout << endl;  
      }  
 }  

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]