Блог пользователя pritishn

Автор pritishn, 6 лет назад, По-английски

Guys please share your approaches for the problems you solved. How many did you guys solve?

Problem 1

There are N cities numbered from 1 to N connected by M bidirectional roads. A concert is going to be held in each city and i_th city concert costs A[i] amount. Travelling through the roads also costs some amount given.

For each city i from 1 to N : find the minimum amount a person from city i has to spend to visit a concert in any of the city and come back to own city.
It may not be guarenteed that each city is reachable from other city.

N,M<=10^5

=====================================
Problem 2

Given rooted tree of N vertices from 1 to N. Every vertex must not have same color as it's p-th ancestor. Every vertex must have either red or green color. Calculate the number of ways to color the tree.

N,p<=10^5

=====================================

Problem 3

You are going to make a necklace of N beads using K different colored beads.
There are unlimited beads of each color.
You need to find the expected value of number of distinct colors used, if every necklace is equiprobable to be made.
Two necklaces are considered same if after some rotation they are identical.
You need to find this value for each N from 1 to M, modulo 1e9+7.

K,M<=10^5

=====================================
Problem 4

Find lexicographically smallest permutation of 1 to N where there are exactly B good indices.
A good index i is one where the element at this index is greater than all it's previous elements(at indices<i).

N,B<=10^5

=====================================
Problem 5

Given an array of length N. You can change any number to anything else. Calculate the minimum number of changes needed to make the array such that every subarray of size K will have Bitwise Xor = 0.

N,K<=10000
A[i]<=2^10-1

=====================================
Problem 6

Indumati has been given an array of A ones and B zeroes.
One operation is:
-> Select any C elements from the array and delete them from the array and append their average (in irreducable fraction from) to the array.
It's guarenteed that A+B-C is multiple of C-1.
Indumati repeats this operation until only 1 number is remaining.

Your task is to calculate the number of distinct values the fraction can take, modulo 1e9+7.

A,B<=2000
2<=C<=2000

=====================================

  • Проголосовать: нравится
  • +143
  • Проголосовать: не нравится

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +9 Проголосовать: не нравится

1st question: Simply double the length of each pre-existing roads. And add N new nodes where ith node is connected with ith city with road distance A[i] . Use multi-source-dikstra and get the answer.

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +1 Проголосовать: не нравится

    could you elaborate a bit on the Use multi-source-dikstra and get the answer part?

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +12 Проголосовать: не нравится

    I solved it using normal djikstra. The answer for city with minimum cost to have a concert will be the city itself. Then we update the answer for all its neighbours similar to djikstra. Initally the answer for all vertices will be the cost of their concert itself

    • »
      »
      »
      6 лет назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      yeah, i realized later that it's not needed to make extra nodes. XD

    • »
      »
      »
      6 лет назад, скрыть # ^ |
       
      Проголосовать: нравится +1 Проголосовать: не нравится

      But wouldn't that give shortest cost of all the nodes from the initial minimum cost concert city.. Can u please explain a bit more.. Thanks a lot

      • »
        »
        »
        »
        6 лет назад, скрыть # ^ |
        Rev. 3  
        Проголосовать: нравится +19 Проголосовать: не нравится

        Okay,
        Let ans[i] = answer for city i
        arr[i] = cost of having a concert in city i
        weight[i][j] = weight of edge between i and j

        Firstly, we need to observe that
        ans[i] = min(arr[i],ans[j]+2*weight[i][j]) for all j such that there is a edge between i and j,
        which means that we can either attend our own concert or go to any neighbour j attend some concert from that city and comeback, which is same as the ans[j] with added cost for round trip to that city.

        But we cannot do this directly, we need to process the cities in a particular order. And another important observation will be that if c is the city with minimum cost to host a concert than ans[c] = arr[c] bcoz going to some other city will definitely increase the cost (try to see this with some test cases).

        Now since we know the answer for city c than we can update the answer for all the neighbours j with ans[j] = min(ans[j],2*weight[c][j]+ans[c]).

        We will initialize ans[i] = arr[i] (cost to attend a concert in their own city)

        Now since the graph is not guaranteed to be connected we will insert all the nodes {ans[i],i} into our set and then similar to djikstra we will remove the vertex with minimum cost and update the answer for all its neighbours.

        feel free to ask if something is still not clear.

        • »
          »
          »
          »
          »
          6 лет назад, скрыть # ^ |
          Rev. 2  
          Проголосовать: нравится 0 Проголосовать: не нравится

          thanks for the explanation.but i have a doubt that we are doing dijkstra from the source c (the city with the minimum cost) wouldn't that give the shortest cost of all the other node from the source 'c' only..also ans[i] = min(arr[i],ans[j]+2*weight[i][j]) what if there is another edge k such that shotest path is from i-->k-->j and not direct 1-->j.can u please explain me these 2 doubts?

          • »
            »
            »
            »
            »
            »
            6 лет назад, скрыть # ^ |
             
            Проголосовать: нравится +1 Проголосовать: не нравится

            For the first doubt : We are trying to process the cities in the increasing order of their answers, so when we consider node c we try to update the answer for all its neighbours, but it does'nt mean that the shortest path will be from node c only.

            Let's consider a small graph :
            n = 3
            edges(u,v,w) (source,dest,weight) : {1,2,40} , {2,3,2}
            arr = {2,100,4}
            Here, c = 1 (city with minimum cost) Initially, ans = {2,100,4}
            elements in set({ans[i],i}) = {(2,1),(4,3),(100,2)} First we will process 1 and update the value of 2 so ans[2] = min(100,2+2*40) = 82 and erase 1
            Then process city 3 and again update the value of 2 so ans[2] = min(82,4+2*2) = 8

            So finally ans = {2,8,4}
            For node 3 the min ans was from node 2 but not with minimum cost node (c = 1).

            For your second doubt take a sample test case and try to dry run the algorithm.

        • »
          »
          »
          »
          »
          6 лет назад, скрыть # ^ |
           
          Проголосовать: нравится 0 Проголосовать: не нравится

          Sorry for late response, but the cities j which are visited as 'concert' cities by other neighbor cities will always have ans[j] = arr[j] right ?

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +3 Проголосовать: не нравится

    Thanks for recommending me to try. I got this one during the contest. xD

  • »
    »
    6 лет назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится

    What I did was: 1: Make a multiset ms of pairs storing {A[v], v}

    2: Repeat this until the multiset ms is not empty

    2.1: Let top be the element at ms.begin(), here curr_vertex = top.second

    2.2: For every neighboring vertex x of curr_vertex, update A[x] = dist[curr_vertex][x] * 2 + A[curr_vertex], if A[x] < RHS and update the same in the multiset.

    3: Return the vector A

    This approach got me full marks!

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    I tried to solve it by first creating the MST and then applying DP on this tree to get the values for each node. It turned out to give me a WA. Can someone please explain why this didn't work?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +54 Проголосовать: не нравится

Problem A was copied- Link.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +12 Проголосовать: не нравится

2nd question : Count all the vertices having depth < B . Answer is 2^(number of all such nodes)

»
6 лет назад, скрыть # |
Rev. 4  
Проголосовать: нравится +17 Проголосовать: не нравится

I solved 4 problems(1-4)

Problem 2 : For each node with depth < p, it is valid to color them either Red/Blue, for the rest of the nodes colors are determined by the nodes with depth < p

Problem 3 : Using linearity of expectation, fix a color find the total number of valid necklaces which have atleast one bead with this color, finding ways is straight-forward application of Burnside Lemma

Problem 4 : Use this construction — (1, 2, 3,..,b-1, n, b, b+1,....,n-1)

My sad story — Wasted more than 1.5 hours on P2 because dfs doesn't work, Wasted another 1.5 hour on P3 due to a stupid bug in Eulier totient function

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

4th Just put Max number at Bth position.

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится -22 Проголосовать: не нравится

My previous experience with CodeAgon has been very deadly like last year so I didn't give it this year thinking a great waste of time. One of my seniors (Orange on Codeforces) was selected on 6/6 and one of the seniors (Violet on Codeforces) was not selected on 5/6 and I sat for the intern test and was blown away!!..

CodeAgon was I think is meant for 2000+ rated. You can see the selects of last year, most were 2200+ rated

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I solved 4, how many did you solve? also this was my first codeagon so do you know what is the cutoff?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +11 Проголосовать: не нравится

Short editorial for Concert.

1. Ans for the city X with the cheapest concert is same as cost of concert in X.
2. For all cities C which are neibhours of the cheapest city update the cost of concert as:
	Minimum of:
		a) actual cost of concert in city C.
		b) 2 * (cost of going to X from C) + cost of concert in X.
3. Delete X from the graph and goto 1.
»
6 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

I solved $$$3$$$ problems

  • The first problem. I doubled the weight of each edge and then conducted multiple source BFS to find out the closest concert for each city.
  • The second problem. The entire tree is uniquely determined by the colouring of vertices who’s level is at most $$$P$$$. Count the number of such vertices and raise $$$2$$$ to that power.
  • The fourth problem. $$$[1,2,... B - 1, A, B, B + 1, ... A - 1]$$$
»
6 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

Solved only 2, I put a lotttt of time on problem 5th, but couldn't converge to an answer.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +21 Проголосовать: не нравится

Do we get a ranklist?

»
6 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится +10 Проголосовать: не нравится

Can anyone share approach for F problem ? We were given an array A and some value 1<=B<=N , where N is size of array ( 1<=N<=10000) we need to find minimum number of changes we need to make in order to make XOR of all segment (continuous subarray) of size B equal to zero .

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +19 Проголосовать: не нравится

How to solve problem E, I wasted more than an hour but couldn't solve it :/

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +18 Проголосовать: не нравится

Just for the getting approximation of rank, how many of you solved 5 or more.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +28 Проголосовать: не нравится

Do anyone how to solve problem F.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +40 Проголосовать: не нравится

For problem 3:

Let F(n,k) denote the number of distinct necklaces of n beads that can be made with at most k colours.

Let $$$X_i$$$ be an Indicator Random variable which is equal to 1 if the $$$i^{th}$$$ colour is included in a necklace. So for a given necklace number of distinct colours = $$$\sum_{i=1}^{i=k}{X_i}$$$.

We need to calculate $$$E[\sum_{i=1}^{i=k}{X_i}] = \sum_{i=1}^{i=k}{E(X_i)} = \sum_{i=1}^{i=k}{P(X_i=1)} = k*P(X_1 = 1)$$$.

Now $$$P(X_1 = 0) = \frac{F(n,k-1)}{F(n,k)}$$$

$$$ Ans = k * (1 - \frac{F(n,k-1)}{F(n,k)})$$$

The value of F(n,k) can be be found here: https://en.wikipedia.org/wiki/Necklace_(combinatorics)

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +19 Проголосовать: не нравится

How to optimize the 5th(xor problem) problem? I got 255/500 points after implementing n*1024 dp with 1024 extra iterations for each state.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

pritishn, update the blog with the concise problem statements. It will be helpful for the ones who have not participated.

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +3 Проголосовать: не нравится

1) Can someone rate the problems in terms of CF difficulty ?
2) Also was there no penalty on wrong submissions ?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

has anybody solved last question -6 ???

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Can we do A using the rerooting technique? If not, I wonder why it doesn't work? I tried it, but failed on the main tests.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone explain how to solve 5th question(XOR) ?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +15 Проголосовать: не нравится

5th ques can be solved using simples obs, array should be periodic with periodicity k.use dp to check answer without changing any element,if we have to change, then it is optimal to change only one among i from 1 to k.As that element can simply be xor of all other elements.This can be calculated in linear time using freq array.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится -46 Проголосовать: не нравится

Why does CodeNation ask so difficult problems? What use do they have solving such weird and stupid problems in real life? Its just plain discrimination for students who don't like CP but are forced to do it. Abomination of a company indeed. I couldn't touch any one problem even.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

is there any article about how multisource dijkstra works... or can some one explain it plz

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In problem 5 constraints were n,k<=10000 and ai<=2^10-1

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Was it a hiring test conducted by codenation?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

I heard that a similar hiring event takes place in January as well. What is the difference between the two?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +11 Проголосовать: не нравится

Does anyone have any idea how, where and when the ranklist will be released

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +17 Проголосовать: не нравится

On how many questions does one get an interview call?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

Any chance of getting an interview on solving 4.5 problems ?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится -38 Проголосовать: не нравится

Upvote if solved 5 or more.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +2 Проголосовать: не нравится

I still don't understand the first question concert can anyone explain in detail

  • »
    »
    6 лет назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится +1 Проголосовать: не нравится

    Think of it this way.
    There are concerts in every city, and one person in every city who wants to attend the concert. The concert costs A[i] bucks in each city.
    Every person, has a choice to either watch the concert in his own city, or travel to any other city and watch it there.
    You will obviously not watch the concert in your own city, if it is more expensive than travelling to another city + watching concert + coming back.
    (cost of travelling is weight of edge)
    So now for each resident, you need to find the minimum cost you have to pay for him to attend any one of the N concerts.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +14 Проголосовать: не нравится

Any updates on when the ranklist will be released?

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

did anyone solve E? can you tell if this code would work or not?(xor problem)

int n;
cin>>n;
int k;
cin>>k;
vector<int> v(n);
map<int, int> mp[k];
for(int i=0;i<n;i++)
    {cin>>v[i];
       mp[i%k][v[i]]++;
    }



int dp[k][1000];
int abhitak=0;

for(int i=0;i<k;i++)
{
    int temp=INT_MAX;
    for(int j=0;j<1000;j++)
    {
       int cc = n/k;
       if(i+1<=n%k)
         cc++;
       dp[i][j]=INT_MAX;
       for(auto k : mp[i])
       {
         dp[i][j] = min(dp[i][j],(i-1>=0 ? dp[i-1][j^(k.first)] : 0) + (cc-k.second));
       }
       dp[i][j]=min(dp[i][j],abhitak+cc);

       temp=min(temp,dp[i][j]);
    }
    abhitak=temp;
}


cout<<dp[k-1][0]<<endl;
»
6 лет назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

I am pretty sure like problem 1 (https://mirror.codeforces.com/contest/938/problem/D) problem 5 was also copied from codeforces, I remember seeing that before. will UPD if I find the original statement.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Is there any update for the results?

»
5 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +3 Проголосовать: не нравится

If anyone wants to practice 5th problem, can do that here.

Explanation, sorry for the plug but explanations on leetcode weren't that helpful, so I wrote one myself.