Quantcast
Viewing all 40121 articles
Browse latest View live

Telegram chatbot for competitive programmers

I created a telegram messenger chatbot for competitive programers.
Users can register their handles of codechef, codeforces, hackerearth, hackerrank and spoj to the bot and get their ratings inform of a spreadsheet ranklist in xlsx format. Get random questions from codechef and codeforces. Get details about upcoming competitions and set a reminder for them. Compile and run code through bot itself using hackerranks api. Get some links from geeks for geeks.... etc Here is the link to the bot
superCodingBot
Here is the link to the source code
superCodingBot

Contributions are welcome :)


Correct method for getting Input?

im using codechef for first time. what is the correct method for getting input?

solution 1:

#include <stdio.h>

#include <inttypes.h>
int i,b,n;
int main (){


scanf("%d",&n);
unsigned long long int temp[n],j;

for(i=0; i<n;i++){
    scanf("%llu",&temp[i]);}

    for(i=0; i<n;i++){
        b= 0;
    for(j=2; j<=temp[i]/2; j++){
        if((temp[i]%j) == 0){
            b++;
        if(b==2){break;}
        }
    }if(b==1){printf("YES\n");}
    else{printf("NO\n");}
}
return 0;
}

solution 2:

#include <stdio.h>

#include <inttypes.h>
int i,b,n;
int main (){


scanf("%d",&n);
unsigned long long int temp,j;

for(i=0; i<n;i++){
    scanf("%llu",&temp);


        b= 0;
    for(j=2; j<=temp/2; j++){
        if((temp%j) == 0){
            b++;
        if(b==2){break;}
        }
    }if(b==1){printf("YES\n");}
    else{printf("NO\n");}
}
return 0;
}

It is showing time limit exceed for both of them!!

CHEFCH - Editorial

PROBLEM LINK:

Practice
Contest

Author:Dmytro Berezin
Tester:Pushkar Mishra
Editorialist:Florin Chirica

PROBLEM

You're given a string of $+$ and $-$ symbols. A string is called a chain if it does not have two or more equal characters. Our target is to convert the given string into a chain. For doing that, you can flip a character (from $+$ to $-$ and vice versa) of the string. Find out minimum number of flips needed.

QUICK EXPLANATION

We can notice that by fixing the first character, the rest of the chain is also fixed. There are two ways to fix the first character, by putting either $+$ or $-$. For both of these possibilities, we can find minimum number of flips needed and take minimum of both to get our final answer.

EXPLANATION

Fixing first character fixes entire chain

We can notice that by fixing the first character, the rest of the chain is also fixed.

If the first character is $+$, the second one must be $-$, the third one must be + and so on. So, the final string will be something like $+-+-+-$ which is a valid chain.

Similarly, we can say the same when the first character of chain is $-$.

Computing minimum number of flips needed to convert a string $init$ into a fixed chain $fin$

Given two strings $init$ and $fin$ ($init$ is the initial string, $fin$ is the desired chain), the task now becomes to compute number of positions $i$ where $init[i] \neq fin[i]$. If we find such a position, we're forced to make a flip. Otherwise, we don't need any flip.

Computing the final answer

There are two possibilities for chain $fin$ given as follows.

  • $+-+- \dots$
  • $-+-+- \dots$

So we can try each one of these. For each case, we can compute the number of differing position between it and the initial array as stated above.

As we have to find overall minimum number of flips needed, we can take minimum of both of these cases.

Time Complexity

Since computing the number of differing position between two strings of size $n$ takes $O(n)$ time. We do this for two cases, so the overall complexity is also $O(n)$.


Subtask 1 Solution
As length of string $s$ is very small ($|s| \leq 10$), we can brute-force over all possible flips in $s$. As each character can be either $+$ or $-$, there are total $2^n$ possibilities which will be at max $1024$ for the given $n$.

Time Complexity
We are brute-forcing over all possible $2^n$ strings, for a fixed string we need $\mathbb{O}(n)$ time. So overall time required will $\mathbb{O}(2^n * n)$

AUTHOR'S AND TESTER'S SOLUTIONS:

Tester's solution

Setter's solution

How to gain reputation in Codechef?

I am new to codechef so please any one guide to how to increase reputation?

can u send me the logic to solve this question

There are N cities in a kingdom. These cities are aligned in a row. So, each city has two neighbours except first and last city. Each city has an F-number which denotes the maximum cities he can ask for help in case of an attack. Each F-number lies between 1 to N. Two cities cannot have same F-number. The Power of a city c is the sum of F-numbers of the cities that c asked for help. In case of an attack, to save time, representative of the city will go to the first city he wants help from and ask the city to propogate the message to its right neighbour. Right neighbour will propagate the message to its right neighbour and so on. This process will stop when the attacked city gets the help form as many cities as it wants. It is obvious that a city will try to get maximum power.

Now a neighbour kingdom with a collective power K has attacked the kingdom. In this attack a city will survive if its power is greater than the power of the attacking kingdom(K) otherwise the city will be destroyed. Now the king wants to prepare a plan to protect the kingdom from this threat. So he hired you. King wants to know which city with minimum F-number can survive. So help the king to save the kingdom.

INPUT SPECIFICATION Your function contains two arguments- An integer K denoting the collective power of the attacking kingdom and an One dimensional Integer array of Size N in which ith element denotes the F-number of the ith city. First line of input contains an Integer K.(1<=K<=10^12) Second line of input contains an Integer N denoting the size of Array. (1<=N<=10^5) Next N lines of input contains a single integer from 1 to N.

OUTPUT SPECIFICATION You must return an integer- minimum F-number of the cities that can survive. Return -1 if no city can survive.

EXAMPLES Sample Test Case 1- Input 5 3 1 2 3 Output 3 Explanation maximum power of the cities will be as following- 1st city- max(1,2,3)=3 2nd city- max((1+2),(2+3))=max(3,5)=5 3rd city- 6

Balanced Brackets(stack implementation)

I wrote this code for the problem Balanced Brackets but I can't seem to know why it gives segmentation fault in some test cases.
https://www.hackerrank.com/challenges/balanced-brackets/problem

char* isBalanced(char* s)
{
    // Complete this function
    char* stack = (char *)malloc(5000 * sizeof(char));
    int t=-1,i;
    for(i=0; s[i] !='\0'; i++)
    {
        if(t==-1)
        {
            stack[++t]=s[i];
        }
        else
        {
            if(stack[t]==s[i]-2 || stack[t]==s[i]-1)
            {
                t--;
            }
            else
            {
                stack[++t]=s[i];
            }
        }
    }
    char *yes="YES";
    char *no="NO";
    if(t==-1)
        return yes;
    else
        return no;
}

int main()
{
    int t;
    scanf("%i", &t);
    for(int a0 = 0; a0 < t; a0++)
    {
        char* s = (char *)malloc(512000 * sizeof(char));
        scanf("%s", s);
        int result_size;
        char* result = isBalanced(s);
        printf("%s\n", result);
    }
    return 0;
}

INSQ15_F - Editorial

PROBLEM LINK:

Practice
Contest

Author:vishfrnds
Tester:codefor6768
Editorialist:vishfrnds

DIFFICULTY:

MEDIUM

PREREQUISITES:

dijkstra's algorithm
basic graph theory

PROBLEM:

Given a graph of N nodes. i-th node has a height H[i], and cost C[i]. You can travel nodes in one of the mode either non-decreasing order of height or non-increasing order of height of nodes. Whenever you change mode on i-th node it will cost you C[i]. Find the path of minimum cost from node 1 to N.

QUICK EXPLANATION:

Split the given graph into two graphs each of N nodes. Where each graph have a subset directed edges of 0 cost, representing single mode only. Connect the corresponding nodes from both graph with undirected edge of cost C[i]. Add a source node and connect it with node 1 of both graphs with cost C[i]. Add a destination node and connect it with node N of both graphs with cost 0. Apply Dijkstra algorithm to find smallest cost path between source and destination.

EXPLANATION:

Make a copy of given graph G1, let it be G2. G1 will have a directed edge from node A to B if and only if H[A] <= H[B] and there was edge between A and B in orignal graph, delete all other edges. G2 will have a directed edge from node A to B if and only if H[A] >= H[B] and there was edge between A and B in orignal graph, delete all other edges.

Now G1 represents mode "up the road" (i.e. non-decreasing mode). Now G2 represents mode "down the road" (i.e. non-increasing mode).

All the edges in G1 and G2 have cost 0. This represent it will not cost you anything as long as you are in single graph.

Now to allow changing of mode we will connect corresponding nodes of both the graph with cost C[i] of that node. Thus, whenever you will change mode on i-th node it will cost C[i].

Now we will add a source and destination node. Source node will be connected to node 1 of G1 with cost C[1] and node 1 of G2 with cost C[1]. Because you need to pay to chose initial mode. Destination node will be connected to node N of G1 with cost 0 and node N of G2 with cost 0. Because it makes no difference wether you end up on G1's node N or G2's node N. So if given graph had n nodes e edges, final graph will have node N = 2 * n + 2 and edges E = e + n + 4

Now we will use dijkstra's algorithm to find minimum cost path from source to destination in this new graph.

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution.

 /* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
 * Created By : Vishwas Tripathi
 * CSE, MNNIT-ALLAHABAD
 * vishfrnds@gmail.com
 _._._._._._._._._._._._._._._._._._._._._.*/


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;

#define MP make_pair
#define pb push_back
#define X first
#define Y second

typedef long long ll;

#define INF (ll)1e18

long long dijk (int st, int end, vector<pair<int, int> > G[], int n) {
    vector<long long> dist (n, INF);
    dist[st] = 0;
    set<pair<long long, int> > q; // cost, node
    q.insert (make_pair (0, st));
    while (!q.empty ()) {
        pair<long long, int> top = *q.begin ();
        q.erase (q.begin ());
        int v = top.second;
        long long d = top.first;
        if (v == end)
            break;
        for (vector<pair<int, int> >::iterator it = G[v].begin (); it != G[v].end (); it++) {
            int v2 = it->first;
            long long cost = it->second;
            if (dist[v2] > dist[v] + cost) {
                if (dist[v2] != INF) {
                    q.erase (q.find (make_pair (dist[v2], v2)));
                }
                dist[v2] = dist[v] + cost;
                q.insert (make_pair (dist[v2], v2));
            }
        }
    }
    if (dist[end] == INF) {
        return -1;
    }
    return dist[end];
}

int main()
{
    int t, n, m, i, j, x, y;
    cin >> n >> m;
    vector<int> h(n + 1), c(n + 1);
    vector<pair<int, int> > G[2 * n + 2];
    for (i = 1; i <= n; i++) {
        cin >> h[i];
    }
    //G1 from 1 to N
    //G2 from N + 1 to 2 * N
    for (i = 1; i <= n; i++) {
        cin >> c[i];
        G[i].pb (MP (n + i, c[i]));
        G[n + i].pb (MP (i, c[i]));
    }
    for (i = 1; i < m; i++) {
        cin >> x >> y;
        if (h[x] <= h[y])
            G[x].pb (MP (y, 0));
        if (h[x] >= h[y])
            G[n + x].pb (MP (n + y, 0));
        if (h[y] <= h[x])
            G[y].pb (MP (x, 0));
        if (h[y] >= h[x])
            G[n + y].pb (MP (n + x, 0));
    }
    G[0].pb(MP (1, c[1]));
    G[0].pb(MP (n + 1, c[1]));
    G[n].pb(MP (2 * n + 1, 0));
    G[2 * n].pb(MP (2 * n + 1, 0));
    cout << dijk(0, 2 * n + 1, G, 2 * n + 2);
    return 0;
}

JULKA:spoj problem

can anyone please tell me what is wrong with this code..it give WA on spoj

include<bits stdc++.h="">

using namespace std; int main() { int l,m,t,i; char ta[101],ma[101]; int x[101],y[101]; for(int k=0;k<10;k++) { cin>>ta>>ma; l=strlen(ta); m=strlen(ma); int j=m-1,z,t=0; if((ta[l-1]%2==0 && ma[m-1]%2==0) || (ta[l-1]%2!=0 && ma[m -1]%2!=0)) { for(i=l-1;i>=0;i--) {
if(ta[i]<ma[j] &&="" j="">=0) { x[i]=((ta[i]-48)+10)-(ma[j]-48); ta[i-1]--; } else if(ta[i]>=ma[j] && j>=0) x[i]=(ta[i]-48)-(ma[j]-48); else x[i]=(ta[i]-48); j--;

}
x[l-1]/=2;
j=m-1;
for(i=l-1;i>=0;i--)
{
 if(j>=0)
    {
  z=x[i]+(ma[j]-48)+t;
  y[i]=z%10;
  t=z/10;
    }
    else if(j<0)
    {
    y[i]=x[i]+t;
    t=t/10;
    }
  j--;
}

for(i=0;i<l;i++)
{
    cout<<y[i];
}
cout<<"\n";
for(i=0;i<l;i++)
cout<<x[i];
//t--;

//else } cout<<"\n"; } }


ACM ICPC 2018 REGIONALS

I can't find the criteria for the selection of a team to the onsite round from the online round for IIITM Gwalior, Kanpur-Kolkata and Hindustan University.

And why the total number of slots for Amritapuri site decreased from 450 to 250 this year?

Can anyone help ?

someone pls help me in this INTEST problem

COPS - Editorial

PROBLEM LINK:

Practice
Contest

Author:Devendra Agarwal
Tester:Surya Kiran
Editorialist:Amit Pandey

DIFFICULTY:

Cakewalk

PREREQUISITES:

None

PROBLEM:

There are 100 houses in a lane, numbered from 1 to 100. N cops are positioned in some houses. A cop's running speed is $h$ houses per second and he can run for at max $t$ secs. Find number of houses where a thief can hide such that he won't be caught by any of the cops.

QUICK EXPLANATION:

For each house and each cop, we can check whether the cop can reach the house or not by checking whether distance between them is less than or equal to maximum distance a cop can travel ($h * t$). If some cop can reach a house, then the house is unsafe otherwise it is safe.

The editorial explains three methods of finding number of safe houses having time complexities of $\mathcal{O}(H N)$, $\mathcal{O}(H \, log N)$ and $\mathcal{O}(H + N)$ respectively, where $H$ denotes number of houses (is fixed to 100 in our problem) and $N$ denotes the number of cops.

Explanation

For a particular house, we want to find out whether this house can be checked by some cop or not. We know that a cop can cover a maximum of $h * t$ inter-house distances in $t$ secs. So, if the distance between the thief's hiding house and cop's house is less than or equal to $h * t$, then the cop can catch the thief. We just need to check whether the current house can be reached by any of the cops or not. If yes, then it is not safe otherwise it is safe.

So, we can describe the solution succinctly as follows.

ans = 0
for each house from 1 to 100:
    safe = true
    for each cop houses from 1 to N:
       if (the cop can reach the house)
          safe = false
    if (safe) ans += 1

Clearly the above implementation of the problem will take $\mathcal{O}(100 * N)$ time.

Faster Solution

Let us say thief is currently at house $p$ and we want to check whether he will be safe in this house or not. If we can find the nearest cops in both directions of the lane from current house, then we just need to check whether these nearest cops in either direction can reach the house $p$ in time or not.

We will describe a method for finding nearest cop in forward direction faster than $\mathcal{O}(N)$ time. Backward direction can be handled similarly.

Let us can create a sorted array of houses of cops. We want to find the first element in the array having value $\geq p$. This can be done by using binary search over the array. Time complexity of this will be $\mathcal{O}(N)$ per search operation in array.

You can also find the same thing using $\mathtt{lower}$_$\mathtt{bound}$ in set in C++. set maintain a balanced binary search tree underneath it, which takes $\mathcal{O}(log N)$ time for each $\mathtt{lower}$_$\mathtt{bound}$ query.

So, this solution runs in $\mathcal{O}(100 * log N)$ time. Can we make it faster?

Even Faster Solution

We have to find $nextCopHouse$/$prevCopHouse$ information for each house faster. Let us see how can find $nextCopHouse$ information faster. Let us make a boolean array $isCop$ of size $100$ where $isCop[i]$ denotes that there is a cop in $i$-th house or not.

Now, we go from house number 100 to 1 and update the $nextCopHouse$ information by maintaining the position of latest house having cop in it.

latestHouseHavingCop = -1;
for house p from 100 to 1:
    if (there is a cop in the house):
        latestHouseHavingCop = p;
    nextCopHouse[p] = latestHouseHavingCop;

Time complexity of this solution is $\mathcal{O}(N + 100)$.

AUTHOR'S, TESTER'S SOLUTIONS:

setter's solution
tester's solution

Getting segmentation fault. please help me.

How do I test my uploaded problem on CodeChef?

I am done with uploading my problem on CodeChef. I am getting an error Solutions to this problem cannot be sumbitted now!.

What should i do now ?

unable to submit my solution

CodeChef submission 15712334 (PYTH) plaintext list. Status: WA, problem CODG1704, contest CODG2017. By tushar2899 (tushar2899), 2017-10-08 22:14:45

i don't know what is the problem with this solution i tried so many test cases on it and it's working as required but still it is showing wrong answer on each and every submission will some who is the setter for this problem look into the matter

contest: https://www.codechef.com/CODG2017/problems/CODG1704/

Data Structures and Algorithms

Hi all, I need your help to make a list of most used data structures and algorithms along with their tutorials, implementation and some problems on them. It will be helpful to everyone in many ways. I request everyone to contribute to this list by providing links to tutorials, problems, etc. I will keep updating this list regularly.

  1. Binary Search :Tutorial, Problems, Tutorial, Implementation, Problem

  2. Quicksort :Tutorial, Implementation, Tutorial

  3. Merge Sort :Tutorial, Implementation, Tutorial

  4. Suffix Array :Tutorial, Tutorial, Implementation, Tutorial, Implementation, Problem, Problem

  5. Knuth-Morris-Pratt Algorithm (KMP) :Tutorial, Tutorial, Implementation, Tutorial, Problem

  6. Rabin-Karp Algorithm :Tutorial, Implementation, Tutorial, Problem, Problem

  7. Tries :Tutorial, Problems, Tutorial : I,II, Tutorial, Problem, Problem, Problem

  8. Depth First Traversal of a graph :Tutorial, Impelementation, Tutorial, Problems, Problem, Problem, Problem

  9. Breadth First Traversal of a graph :Tutorial, Impelementation, Tutorial, Problems, Problem, Problem, Problem, Flood Fill

  10. Dijkstra's Algorithm :Tutorial, Problems, Problem, Tutorial(greedy), Tutorial (with heap), Implementation, Problem, Problem

  11. Binary Indexed Tree :Tutorial, Problems, Tutorial, Original Paper, Tutorial, Tutorial, Problem, Problem,Problem, Problem, Problem, Problem, Problem

  12. Segment Tree (with lazy propagation) :Tutorial, Implementation, Tutorial, Tutorial, Problems, Implementation, Tutorial, Implementation and Various Uses, Persistent Segment Tree: I, II, problems same as BIT, Problem, Problem/HLD is used as well/

  13. Z algorithm :Tutorial, Problem, Tutorial, Tutorial, problems same as KMP.

  14. Floyd Warshall Algorithm :Tutorial, Implementation, Problem, Problem

  15. Sparse Table (LCP, RMQ) :Tutorial, Problems, Tutorial, Implementation(C++), Java implementation

  16. Heap / Priority Queue / Heapsort :Implementation, Explanation, Tutorial, Implementation, Problem, Chapter from CLRS

  17. Modular Multiplicative Inverse

  18. Binomial coefficients (nCr % M): Tutorial, Tutorial, Paper, Problem

  19. Suffix Automaton :Detailed Paper, Tutorial, Implementation (I), Tutorial, Implementation (II), Problem, Problem, Problem, Problem, Tutorial, Implementation

  20. Lowest Common Ancestor :Tutorial, Problems, Paper, Paper, Problem, Problem, Problem

  21. Counting Inversions :Divide and Conquer, Segment Tree, Fenwick Tree, Problem

  22. Euclid's Extended Algorithm

  23. Suffix Tree :Tutorial, Tutorial, Intro, Construction : I, II, Implementation, Implementation, Problem, Problem, Problem, Problem

  24. Dynamic Programming : Chapter from CLRS(essential), Tutorial, Problems, Problem, Problem, Problem, Problem, Tutorial, Problem, Problem, Problem, Longest Increasing Subsequence, Bitmask DP, Bitmask DP, Optimization, Problem, Problem, Problem, Problem, Problem, Problem, Problem, DP on Trees : I, II

  25. Basic Data Structures :Tutorial, Stack Implementation, Queue Implementation, Tutorial, Linked List Implementation

  26. Logarithmic Exponentiation

  27. Graphs :Definition, Representation, Definition, Representation, Problem, Problem

  28. Minimum Spanning Tree :Tutorial, Tutorial, Kruskal's Implementation, Prim's Implementation, Problem, Problem, Problem, Problem, Problem

  29. Efficient Prime Factorization

  30. Combinatorics :Tutorial, Problems, Problem, Tutorial

  31. Union Find/Disjoint Set :Tutorial, Tutorial, Problems, Problem, Problem, Problem

  32. Knapsack problem :Solution, Implementation

  33. Aho-Corasick String Matching Algorithm :Tutorial, Implementation, Problem, Problem, Problem, Problem

  34. Strongly Connected Components :Tutorial, Implementation, Tutorial, Problem, Problem, Problem

  35. Bellman Ford algorithm :Tutorial, Implementation, Tutorial, Implementation, Problem, Problem

  36. Heavy-light Decomposition :Tutorial, Problems, Tutorial, Implementation, Tutorial, Implementation, Implementation, Problem, Problem, Problem

  37. Convex Hull :Tutorial, Jarvis Algorithm Implementation, Tutorial with Graham scan, Tutorial, Implementation, Problem, Problem, Problem, Problem, Problem

  38. Line Intersection :Tutorial, Implementation, Tutorial, Problems

  39. Sieve of Erastothenes

  40. Interval Tree :Tutorial, Implementation, Problem, Problem, Problem, Problem, Problem, Problem, Tutorial

  41. Counting Sort

  42. Probabilities

  43. Matrix Exponentiation :Tutorial, Tutorial

  44. Network flow :(Max Flow)Tutorial : I,II, Max Flow(Ford-Fulkerson) Tutorial, Implementation, (Min Cut) Tutorial, Implementation, (Min Cost Flow)Tutorial : I,II,III, Dinic's Algorithm with Implementation, Max flow by Edmonds Karp with Implementation, Problem, Problem, Problem, Problem, Problem, Problem, Problem, Problem, Problem, Problem, Problem, Problem, Problem, Problem, Problem

  45. K-d tree :Tutorial, Tutorial, Implementation, Problem

  46. Deque

  47. Binary Search Tree :Tutorial, Implementation, Searching and Insertion, Deletion

  48. Quick Select :Implementation, Implementation

  49. Treap/Cartesian Tree :Tutorial(detailed), Tutorial, Implementation, Uses and Problems, Problem, Problem

  50. Game Theory :Detailed Paper, Tutorial, Problems, Grundy Numbers, Tutorial with example problems - I,II,III,IV, Tutorial, Problems, Problem, Problem, Problem, Problem, Problem, Problem, Problem, Problem, Problem, Problem, Problem, Nim

  51. STL (C++) :I,II, Crash Course

  52. Maximum Bipartite Matching

  53. Manacher's Algorithm :Implementation, Tutorial, Tutorial, Implementation, Tutorial, Implementation, Problem, Problem, Problem

  54. Miller-Rabin Primality Test : Code

  55. Stable Marriage Problem

  56. Hungarian Algorithm, Tutorial

  57. Sweep line Algorithm : I, II

  58. LCP :Tutorial, Implementation, Tutorial, Implementation

  59. Gaussian Elimination

  60. Pollard Rho Integer Factorization, problem

  61. Topological Sorting

  62. Detecting Cycles in a Graph : Directed - I, II Undirected : I

  63. Geometry : Basics, Tutorial

  64. Backtracking :N queens problem, Tug of War, Sudoku

  65. Eulerian and Hamiltonian Paths :Tutorial, Tutorial, (Eulerian Path and Cycle)Implementation, (Hamiltonian Cycle)Implementation

  66. Graph Coloring :Tutorial, Implementation

  67. Meet in the Middle :Tutorial, Implementation

  68. Arbitrary Precision Integer(BigInt), II

  69. Radix Sort, Bucket Sort

  70. Johnson's Algorithm :Tutorial, Tutorial, Implementation

  71. Maximal Matching in a General Graph :Blossom/Edmond's Algorithm, Implementation, Tutte Matrix, Problem

  72. Recursion : I,II, Towers of Hanoi with explanation

  73. Inclusion and Exclusion Principle : I, II

  74. Co-ordinate Compression

  75. Sqrt-Decomposition :Tutorial, Tutorial, Problem, Problem

  76. Link-Cut Tree :Tutorial, Wiki, Tutorial, Implementation, Problem, Problem, Problem, Problem

  77. Euler's Totient Function :Explanation, Implementation, Problems, Explanation, Problems

  78. Burnside Lemma :Tutorial, Tutorial, Problem

  79. Edit/Levenshtein Distance :Tutorial, Introduction, Tutorial, Problem, Problem

  80. Branch and Bound

  81. Math for Competitive Programming

  82. Mo's Algorithm : Tutorial and Problems


Giving away android chatting app!

print Hi developers , I created an android app for chatting called as 'Chativo' Now I am not able to upload it anywhere.... So I have decided to give it for free to an android developer , If you are ready to take responsibilty of the app , here are a few more things to note - 1) Please give credit , it wont be yours and mine either , it will be ours 2) You can change some details , but if its something big , contact me and I'll say if you can change or not Thank you :) I would love to see my app on the play store! Ask me and i'll decide who to give the app to

IIT Bombay event - all questions copied from codeforces

Hey I want to expose this event - International Coding Challenge | Techfest, IIT Bombay which is going on currently
link
All their problems are copied from codeforces. As far as i remember, this is not allowed on codechef, i don't know how this got ignored. This is a big issue, @admin, please look into this asap.
Their first question was copied from here
Second here
Third here
Fourth here
Fifth here
Sixth, havent found yet, why don't you ask IIT Bombay Techfest people.

How to solve SPOJ QTREE with centroid decomposition?

It's quite easy with HLD but I have no idea how to implement it so I went with centroid decomposition. Handling the max queries is relatively simple but I have no idea on how to to update. My idea is to make an array $A[LN][N]$ where $A[i][j]$ stores the max weight edge from centroid at $ith$ level to $j$. Let $X$ be the LCA of given nodes $a$ and $b$, then the answer to a query is simply $max(A[level(X)][a], A[level(X)][b])$. But how to handle the update queries? Is it possible with centroid decomposition at all?

Problem Link: http://www.spoj.com/problems/QTREE/

Some discusssion: http://codeforces.com/blog/entry/19542 (Although he says there "might" be a solution. I wanna know if there really is.)

Codigo2K17 :: KNIT Sultanpur

Greetings folks!

We would like to inform that KNIT Sultanpur is back with its annual flagship Coding Contest Codigo2k17. Just like the previous iterations, this year's Codigo will be even more exciting and challenging. The contest shall have 5 questions of varying difficulty. We, from KNIT Sultanpur, invite you all to the 3 hours intense coding "jam" starting off in few hours. The contest begins 9 PM tonight. Stay Tuned!

CHEFCH - Editorial

PROBLEM LINK:

Practice
Contest

Author:Dmytro Berezin
Tester:Pushkar Mishra
Editorialist:Florin Chirica

PROBLEM

You're given a string of $+$ and $-$ symbols. A string is called a chain if it does not have two or more equal characters. Our target is to convert the given string into a chain. For doing that, you can flip a character (from $+$ to $-$ and vice versa) of the string. Find out minimum number of flips needed.

QUICK EXPLANATION

We can notice that by fixing the first character, the rest of the chain is also fixed. There are two ways to fix the first character, by putting either $+$ or $-$. For both of these possibilities, we can find minimum number of flips needed and take minimum of both to get our final answer.

EXPLANATION

Fixing first character fixes entire chain

We can notice that by fixing the first character, the rest of the chain is also fixed.

If the first character is $+$, the second one must be $-$, the third one must be + and so on. So, the final string will be something like $+-+-+-$ which is a valid chain.

Similarly, we can say the same when the first character of chain is $-$.

Computing minimum number of flips needed to convert a string $init$ into a fixed chain $fin$

Given two strings $init$ and $fin$ ($init$ is the initial string, $fin$ is the desired chain), the task now becomes to compute number of positions $i$ where $init[i] \neq fin[i]$. If we find such a position, we're forced to make a flip. Otherwise, we don't need any flip.

Computing the final answer

There are two possibilities for chain $fin$ given as follows.

  • $+-+- \dots$
  • $-+-+- \dots$

So we can try each one of these. For each case, we can compute the number of differing position between it and the initial array as stated above.

As we have to find overall minimum number of flips needed, we can take minimum of both of these cases.

Time Complexity

Since computing the number of differing position between two strings of size $n$ takes $O(n)$ time. We do this for two cases, so the overall complexity is also $O(n)$.


Subtask 1 Solution
As length of string $s$ is very small ($|s| \leq 10$), we can brute-force over all possible flips in $s$. As each character can be either $+$ or $-$, there are total $2^n$ possibilities which will be at max $1024$ for the given $n$.

Time Complexity
We are brute-forcing over all possible $2^n$ strings, for a fixed string we need $\mathbb{O}(n)$ time. So overall time required will $\mathbb{O}(2^n * n)$

AUTHOR'S AND TESTER'S SOLUTIONS:

Tester's solution

Setter's solution

Viewing all 40121 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>