tutorialtime

Unique Occurrences solution codeforces

Unique Occurrences solution codeforces You are given a tree, consisting of nn vertices. Each edge has an integer value written on it. Let f(v,u)f(v,u) be the number of values that appear exactly once on the edges of a simple path between vertices vv and uu. Calculate the sum of f(v,u)f(v,u) over all pairs of vertices vv and uu such that 1≤v<u≤n1≤v<u≤n. Input The first line contains a single integer nn (2≤n≤5⋅1052≤n≤5⋅105) — the number …

Unique Occurrences solution codeforces Read More »

Labyrinth Adventures solution codeforces

Labyrinth Adventures solution codeforces You found a map of a weirdly shaped labyrinth. The map is a grid, consisting of nn rows and nn columns. The rows of the grid are numbered from 11 to nn from bottom to top. The columns of the grid are numbered from 11 to nn from left to right. The labyrinth has nn layers. The first layer is the bottom left corner (cell (1,1)(1,1)). …

Labyrinth Adventures solution codeforces Read More »

Required Length solution codeforces

Required Length solution codeforces You are given two integer numbers, nn and xx. You may perform several operations with the integer xx. Each operation you perform is the following one: choose any digit yy that occurs in the decimal representation of xx at least once, and replace xx by x⋅yx⋅y. You want to make the length of decimal representation of xx (without leading zeroes) equal to nn. What is …

Required Length solution codeforces Read More »

Double Sort solution codeforces

Double Sort solution codeforces You are given two arrays aa and bb, both consisting of nn integers. In one move, you can choose two indices ii and jj (1≤i,j≤n1≤i,j≤n; i≠ji≠j) and swap aiai with ajaj and bibi with bjbj. You have to perform the swap in both arrays. You are allowed to perform at most 104104 moves (possibly, zero). Can you make both arrays sorted in a non-decreasing order at the end? If you …

Double Sort solution codeforces Read More »

Unordered Swaps solution codeforces

  Unordered Swaps solution codeforces Alice had a permutation pp of numbers from 11 to nn. Alice can swap a pair (x,y)(x,y) which means swapping elements at positions xx and yy in pp (i.e. swap pxpx and pypy). Alice recently learned her first sorting algorithm, so she decided to sort her permutation in the minimum number of swaps possible. She wrote down all the swaps in the order in which she performed them …

Unordered Swaps solution codeforces Read More »

Circular Spanning Tree solution codeforces

  Circular Spanning Tree solution codeforces There are nn nodes arranged in a circle numbered from 11 to nn in the clockwise order. You are also given a binary string ss of length nn. Your task is to construct a tree on the given nn nodes satisfying the two conditions below or report that there such tree does not exist: For each node ii (1≤i≤n)(1≤i≤n), the degree of …

Circular Spanning Tree solution codeforces Read More »

AND Sorting solution codeforces

  AND Sorting solution codeforces You are given a permutation pp of integers from 00 to n−1n−1 (each of them occurs exactly once). Initially, the permutation is not sorted (that is, pi>pi+1pi>pi+1 for at least one 1≤i≤n−11≤i≤n−1). The permutation is called XX-sortable for some non-negative integer XX if it is possible to sort the permutation by performing the operation below some finite number of times: Choose two indices ii and jj (1≤i<j≤n)(1≤i<j≤n) such that pi&pj=Xpi&pj=X. …

AND Sorting solution codeforces Read More »

LIS or Reverse LIS? solution codeforces

  LIS or Reverse LIS? solution codeforces You are given an array aa of nn positive integers. Let LIS(a)LIS(a) denote the length of longest strictly increasing subsequence of aa. For example, LIS([2,1–,1,3–])LIS([2,1_,1,3_]) = 22. LIS([3–,5–,10–––,20–––])LIS([3_,5_,10_,20_]) = 44. LIS([3,1–,2–,4–])LIS([3,1_,2_,4_]) = 33. We define array a′a′ as the array obtained after reversing the array aa i.e. a′=[an,an−1,…,a1]a′=[an,an−1,…,a1]. The beauty of array aa is defined as min(LIS(a),LIS(a′))min(LIS(a),LIS(a′)). Your task is to determine the maximum possible beauty of the array aa if you can …

LIS or Reverse LIS? solution codeforces Read More »