Excellent Arrays Codeforces Solution

Excellent Arrays Codeforces Solution Let’s call an integer array a1,a2,…,ana1,a2,…,an good if ai≠iai≠i for each ii. Let F(a)F(a) be the number of pairs (i,j)(i,j) (1≤i<j≤n1≤i<j≤n) such that ai+aj=i+jai+aj=i+j. Let’s say that an array a1,a2,…,ana1,a2,…,an is excellent if: aa is good; l≤ai≤rl≤ai≤r for each ii; F(a)F(a) is the maximum possible among all good arrays of size nn. Given nn, ll and rr, calculate the number of excellent arrays modulo 109+7109+7.Input The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases. The first and only … Read more

Stringforces Codeforces Solution

Stringforces Codeforces Solution You are given a string ss of length nn. Each character is either one of the first kk lowercase Latin letters or a question mark. You are asked to replace every question mark with one of the first kk lowercase Latin letters in such a way that the following value is maximized. Let fifi be the maximum length substring of string ss, … Read more

Jumping Around Codeforces Solution

Jumping Around Codeforces Solution There is an infinite pond that can be represented with a number line. There are nn rocks in the pond, numbered from 11 to nn. The ii-th rock is located at an integer coordinate aiai. The coordinates of the rocks are pairwise distinct. The rocks are numbered in the increasing order of the coordinate, so a1<a2<⋯<ana1<a2<⋯<an. A robot frog … Read more

Manhattan Subarrays Codeforces Solution

Manhattan Subarrays Codeforces Solution Suppose you have two points p=(xp,yp)p=(xp,yp) and q=(xq,yq)q=(xq,yq). Let’s denote the Manhattan distance between them as d(p,q)=|xp−xq|+|yp−yq|d(p,q)=|xp−xq|+|yp−yq|. Let’s say that three points pp, qq, rr form a bad triple if d(p,r)=d(p,q)+d(q,r)d(p,r)=d(p,q)+d(q,r). Let’s say that an array b1,b2,…,bmb1,b2,…,bm is good if it is impossible to choose three distinct indices ii, jj, kk such that the points (bi,i)(bi,i), (bj,j)(bj,j) and (bk,k)(bk,k) form a bad triple. You are given an array a1,a2,…,ana1,a2,…,an. Calculate the number of good subarrays of aa. A subarray of the … Read more

Maximum Cost Deletion Codeforces Solution

Maximum Cost Deletion Codeforces Solution You are given a string ss of length nn consisting only of the characters 0 and 1. You perform the following operation until the string becomes empty: choose some consecutive substring of equal characters, erase it from the string and glue the remaining two parts together (any of them can be empty) in the same order. For example, if you erase … Read more