Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all 39796 articles
Browse latest View live

Doubt in CAPIMOVE problem

$
0
0

Please explain "During this time infection uses teleport one time and infect all the planets that can be achieved in one teleport jump". If the infection was on a planet no 2 and it is directly linked to 3, 4. So, would the teleport be stopped with planets directly connected to 2, 3, 4? Or just with 2?


Getting started here, questions about strategy, etiquitte, etc.

$
0
0

Hi guys.

I've just started here, I have a few questions about how I approach this place. Firstly, let me just say that wow - some of the problems here are pretty difficult, even for easy!

Alright, so I've just bunch of the questions in the FAQ, here's a few more.

  1. What's the etiquette for posting here? For example are 'I'm working XYZ practice problem, I've got TLE error, here's my code, what should I do?' questions ok?

  2. What's the general strategy for solving problem, or doing time optimization? Are there any tools that you use? Like, one of the things I can see that would be problematic, is when you get a 'wrong answer' but you don't know what the 'expected vs output' is. Is this just something you need to deal with as a programmer, to think of the corner cases yourself?

  3. Is there any way to get additional test data?

  4. How do you organise your code chef files in your IDE? For example, your class name (in Java) must called Main, and it can't be part of any package, when you paste in to the submitter, otherwise it'll give you an error, wondering how people deal with that.

  5. code reusablity - I get the feeling that a lot of this is going to be using various appropriate algorithms. What I would tend to do is import the package that contains the algorithm. But given that the submission has to be a single file with no non-native imports, is the only thing you can do, is copy the code in for each algorithm?

  6. Where would you start in terms of first problems to solve? For example I would suggest doing the Enormous Input test as one of your first problems.

.

Find the length longest common prefix.

$
0
0

How to find the length longest common prefix?

TACHSTCK - Editorial

$
0
0

Problem Link:

Practice
Contest

Difficulty:

Cakewalk

Pre-requisites:

Ad Hoc

Problem:

Given N sticks of lengths L[1], L[2], ... L[N] and a positive integer D. Two sticks can be paired if the difference of their lengths is at most D. Pair up as many sticks as possible such that each stick is used at most once.

Quick Explanation:

Sort the sticks by their lengths and let L be the sorted array.

If L[1] and L[2] cannot be paired, then the stick L[1] is useless. Otherwise, there exists an optimal pairing where L[1] gets paired with L[2].

This gives rise to the following algorithm:

numpairs = 0 for ( i = 1; i < N; ) if (L[i] >= L[i+1] -D) // L[1] and L[2] can be paired numpairs++, // pair them up i += 2; else i++; // eliminate L[1]

Justifications:

  • If L[1] and L[2] cannot be paired then
           L[1] < L[2] - D
    But, L[2] <= L[i] for every i > 1
    So    L[1] < L[i] - D for every i > 1
    Hence, L[1] cannot be paired with anyone.

  • If L[1] and L[2] can be paired.
        Consider any optimal pairing and it can be transformed to a pairing where L[1] and L[2] are paired.
            a) If the optimal pairing pairs L[1] with L[2] then we are done.
            b) If only one of L[1] and L[2] is paired with someone, then we can replace that pair by (L[1], L[2]).
            c) If both L[1] and L[2] are paired and L[1] is paired with L[n] and L[2] with L[m], then we might as well form pairs (L[1], L[2]) and (L[n], L[m]).
                This is because
                    L[2] <= L[m] <= L[2] + D
                    L[2] <= L[n] <= L[1] + D <= L[2] + D
              ⇒   -D <= L[m] - L[n] <= D

Setter's Solution:

Can be found here

Tester's Solution:

Can be found here

CHEFTRI4 - Editorial

$
0
0

PROBLEM LINK:

Contest
Practice

Author:Misha Chorniy
Tester:Tuan Anh Tran Dang
Translators:Sergey Kulik (Russian), Team VNOI (Vietnamese) and Hu Zecong (Mandarin)
Editorialist:Tuan Anh Tran Dang

DIFFICULTY

HARD

PREREQUISITE

NONE

PROBLEM

Check whether 4 triangles can be joined to create one single triangle.

SOLUTION

I guess some of you might expected some nice and short solution for this problem but unfortunately I don’t know one. This problem is just about considering all possible cases and implement them correctly. I underestimated the difficulty of this problem and missed quite a few cases before discussed with the problem setter.

Let’s first consider a simple version of the problem with 2 or 3 triangles.

2 triangles problem

The only way to join two triangles is to pick the common side and checking whether the two joined angles can make 180 degree (see picture).

3 triangles problem

With 3 triangles we can first create a triangle from 2 of them and them try to combine the newly created triangle with the remaining. However this is not cover all possibilities (please take a look at the below picture).

This combination is not so hard to test. You just need to find 3 pairs of common sides and make sure the 3 joined angles can make 360 degree.

sub-case 1

With the mentioned two sub-problem solved we can clear a major scenarios of this problem in which there is 2 or 3 triangle out of the 4 combined to form a single triangle. We have the following way of combining them:

  • (((1 + 2) + 3) + 4)
  • ((1 + 1) + 3 + 4) // Note that this is different with the previous case since we join 3 triangles (in which one is created from 1 and 2) at one time.
  • ((1 + 2) + (3 + 4))
  • ((1 + 2 + 3) + 4)

Note that we should consider all possible permutations of 4 triangles for each cases.

Now we come to the interesting in which we’ll cover the other corner case. I will use picture to express the combination and briefly describe how to detect them.

Assume that we got a configuration {t1, t2, t3, t4} which is 4 triangles in some specific order and orientation. Note that there is 6 possible orientation of a triangles which is corresponding to 3! way of arranging there sides. Let ti.a, ti.b, ti.c are the sides of the triangle ti and ti.A, ti.B, ti.C are the internal angle of its where ti.A is opposite ti.a and ti.B is opposite ti.b and so on.

sub-case 2

condition

    t1.c = t2.c
    t3.b = t3.b
    t4.a = t4.a

t1.B + t2.A + t3.C = 180
t1.A + t2.B + t4.C = 180
t3.A + t2.C + t4.B = 180

sub-case 3

condition

    t1.c + t2.c = t4.c
    t3.b + t2.b = t1.b
    t4.a + t2.a = t3.a

t1.A + t2.A = 180
t3.C + t2.C = 180
t4.B + t2.B = 180

sub-case 4

condition

    t1.b = t2.b + t3.b
    t2.c = t3.c
    t3.a = t4.a

t1.C + t2.C = 180
t2.A + t3.B + t4.C = 180
t1.A + t4.A = 180

I have implemented a solution but unfortunately it isn’t optimized enough to pass the time limit (even though it’s correct). You can still refer to my TLE solution in tester’s solution. The room to optimize is on the backstracking process of case 3, 4, 5. You should have different method for each case and stop as soon as you can see some condition is invalid. You should take a look at the problem setter’s code too.

AUTHOR'S AND TESTER'S SOLUTIONS:

setter
tester

runtime error (sigfpe)

$
0
0

I'm getting the following error while running this code. I initially used a sieve but that was exceeding time limit. HELP!!! why is it giving me this error?

#include<iostream>
#include<math.h>
#define max 1229
using namespace :: std;
int primes[max]={2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009, 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101, 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209, 5227, 5231, 5233, 5237, 5261, 5273, 5279, 5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443, 5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639, 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783, 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857, 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953, 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073, 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133, 6143, 6151, 6163, 6173, 6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247, 6257, 6263, 6269, 6271, 6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329, 6337, 6343, 6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427, 6449, 6451, 6469, 6473, 6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571, 6577, 6581, 6599, 6607, 6619, 6637, 6653, 6659, 6661, 6673, 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737, 6761, 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833, 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6947, 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997, 7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, 7109, 7121, 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229, 7237, 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333, 7349, 7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487, 7489, 7499, 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561, 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621, 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723, 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829, 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919, 7927, 7933, 7937, 7949, 7951, 7963, 7993, 8009, 8011, 8017, 8039, 8053, 8059, 8069, 8081, 8087, 8089, 8093, 8101, 8111, 8117, 8123, 8147, 8161, 8167, 8171, 8179, 8191, 8209, 8219, 8221, 8231, 8233, 8237, 8243, 8263, 8269, 8273, 8287, 8291, 8293, 8297, 8311, 8317, 8329, 8353, 8363, 8369, 8377, 8387, 8389, 8419, 8423, 8429, 8431, 8443, 8447, 8461, 8467, 8501, 8513, 8521, 8527, 8537, 8539, 8543, 8563, 8573, 8581, 8597, 8599, 8609, 8623, 8627, 8629, 8641, 8647, 8663, 8669, 8677, 8681, 8689, 8693, 8699, 8707, 8713, 8719, 8731, 8737, 8741, 8747, 8753, 8761, 8779, 8783, 8803, 8807, 8819, 8821, 8831, 8837, 8839, 8849, 8861, 8863, 8867, 8887, 8893, 8923, 8929, 8933, 8941, 8951, 8963, 8969, 8971, 8999, 9001, 9007, 9011, 9013, 9029, 9041, 9043, 9049, 9059, 9067, 9091, 9103, 9109, 9127, 9133, 9137, 9151, 9157, 9161, 9173, 9181, 9187, 9199, 9203, 9209, 9221, 9227, 9239, 9241, 9257, 9277, 9281, 9283, 9293, 9311, 9319, 9323, 9337, 9341, 9343, 9349, 9371, 9377, 9391, 9397, 9403, 9413, 9419, 9421, 9431, 9433, 9437, 9439, 9461, 9463, 9467, 9473, 9479, 9491, 9497, 9511, 9521, 9533, 9539, 9547, 9551, 9587, 9601, 9613, 9619, 9623, 9629, 9631, 9643, 9649, 9661, 9677, 9679, 9689, 9697, 9719, 9721, 9733, 9739, 9743, 9749, 9767, 9769, 9781, 9787, 9791, 9803, 9811, 9817, 9829, 9833, 9839, 9851, 9857, 9859, 9871, 9883, 9887, 9901, 9907, 9923, 9929, 9931, 9941, 9949, 9967, 9973};

int ifkprime(int n, int k)
{   int ctr=0;
    int i=0;
    for(;primes[i]<=n;i++)
    {   if(n%primes[i]==0)
                ctr++;
    }
    if(ctr==k)
        return 1;
    else
        return 0;

}
int nk(int a, int b, int k)
{   int ctr=0;
    for(int i=a;i<=b;i++)
    {   if(ifkprime(i,k))
        ctr++;
    }
    return ctr;
}
int main(void)
{// init();
    int a,b,k,t;
    cin>>t;
    int * output=new int[t];
    for(int i=0;i<t;i++)
    {   cin>>a>>b>>k;
        output[i]=nk(a,b,k);
    }
    cout<<"\n";
    for(int i=0;i<t;i++)
    {   cout<<output[i]<<"\n";
    }
    delete output;
    return 0;
}

MCO16101 - Editorial

$
0
0

PROBLEM LINK:

PracticeContest

Author:Zi Song Yeoh

DIFFICULTY:

CAKEWALK

PREREQUISITES:

Brute Force

PROBLEM:

Given two strings of digits denoting two consecutive numbers, with some digits missing. Find all possible numbers that they can possibly denote.

QUICK EXPLANATION:

Iterate through all possible numbers and check whether it works.

EXPLANATION:

Since the given constraints is small, it is possible to iterate through all possible numbers with the given number of digits. It remains to have a function that checks whether each number matches a string of digits.

For subtask $1$, this is fairly easy, since the number only consists of $1$ digit, so we can just check directly.

However, for subtask $2$, the number can have $5$ digits. We need to extract the digits of the number to compare it with the given string of digits. How do we do that? The easiest way is to keep extracting the last digit from the number, and divide it by $10$. If we keep doing this, we can get all the digits.

For example, let's say we want to get the digits of $2346$. We extract the last digit (which can be done by taking its remainder modulo $10$), so in this case we get $6$, and divide the number by $10$ (and taking the integer part of it), so we get $234$. We repeat this process and extract the digit $4$, and divide it by $10$, which gives $23$. Repeating this process two more times gives us the digits $6, 4, 3, 2$. Now, we just have to reverse the order of the digits and we get $2, 3, 4, 6$.

After getting all the digits, we can compare the digits with the given string directly and determine if the number matches the string.

Another way to get all the digits is to use builtin functions in your chosen language. If you're aware of them (or googled them), it will simplify your implementation.

A similar problem which can be solved using this trick can be found here.

There is a small but nasty trap : The pair (99999, 100000) (and similarly (9, 10), (99, 100), ...) are invalid because they have different number of digits. Also, be careful to not print the pair (0, 1). (this was explicitly stated in the problem statement)

Time Complexity : $O(N \cdot \log{N})$, where $N$ is the maximum possible number that can be formed, which is $99999$ in this case.

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.

Tester's solution can be found here.

RELATED PROBLEMS:

MCO16102 - Editorial

$
0
0

PROBLEM LINK:

PracticeContest

Author:Zi Song Yeoh

DIFFICULTY:

SIMPLE

PREREQUISITES:

Dynamic Programming

PROBLEM:

You're given an array $A$ of integers. You may delete some of the elements so that each subsegment formed (that contains no deleted elements) is non-decreasing. Determine the number of ways to do so.

QUICK EXPLANATION:

Let $dp[i]$ denote the number of ways to delete the elements among the first $i$ elements of the array so that the conditions are satisfied. We can get a simple recurrence for $dp[i]$ which can be computed in linear time.

EXPLANATION:

The first subtask has small constraints : $n \le 20$. Thus, we can iterate through all possible subsets of elements to delete and check whether each way is feasible. The complexity of this solution is $O(2^{n} \cdot n)$ and this can pass the first subtask. However, to get the full $100$ points, we need something much faster than that.

At first, this problem seems hard. However, once you have the idea of dp in mind, this problem becomes trivial.

Let $dp[i]$ denote the number of ways to delete a set of integers so that the first $i$ integers satisfies the conditions. Now, the base case $dp[1] = 2$ is trivial. (since we can choose to delete it or keep it) It remains to see how we can calculate $dp[i + 1]$ from $dp[i]$.

There are two cases : We either delete $a[i + 1]$ for keep $a[i + 1]$. If we delete $a[i + 1]$, then the number of ways will be increased by $dp[i]$, since we've already reduced it to solving the same problem on the first $i$ elements. On the other hand, if we choose to keep $a[i + 1]$, then we have two more cases :

  1. If $a[i] \le a[i + 1]$. In this case, we again reduce the problem to solving it on the first $i$ elements. So, we add $dp[i]$ to the answer.
  2. If $a[i] > a[i + 1]$, then we must delete $a[i]$. Now, we only need to solve it on the first $i - 1$ elements, so we add $dp[i - 1]$ to the answer. (we let $dp[-1] = 1$)

The answer can be found as $dp[n]$. This solution takes $O(n)$ time.

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.

Tester's solution can be found here.

RELATED PROBLEMS:


MCO16103 - Editorial

$
0
0

PROBLEM LINK:

PracticeContest

Author:Zi Song Yeoh

DIFFICULTY:

EASY

PREREQUISITES:

Greedy

PROBLEM:

You're given a string $S$. You can perform at most $k$ moves. Each move consists of shifting one of the letters by $1$. Determine the lexicographically smallest palindrome you can achieve.

QUICK EXPLANATION:

This problem is not difficult, but there might be some tricky cases which you have to avoid. The solution is explained in the section below.

EXPLANATION:

For subtask $1$, iterating through all possible string suffices. The complexity for this is $O(26^{|s|} \cdot |s|$).

We'll directly solve subtask $3$. Now, to produce a lexicographical string we want the first letter to be as small as possible, then the second letter should be as small as possible, and so on... However, the problem requires the resulting string to be a palindrome as well, so more work needs to be done.

First, we have to turn $S$ into a palindrome. Let $f(i, j)$ denote the minimum number of seconds needed to turn the $i$-th character and the $(n - 1 - i)$-th character ($n$ is the length of $s$, the characters are $0$-indexed) into letter $j$. This value is easy to calculate for all $i$ and $j$ in $O(1)$. Now, we first convert $S$ into a palindrome using the minimum number of seconds, by choosing the letter $j$ with the minimum value of $f(i, j)$ for each $i$. (and breaking ties by choosing the $j$ that appears first in the alphabet)

Suppose at this point we still have $x$ seconds left. If $x < 0$, there's no solution. Otherwise, we can improve our solution. We start improving the string from the first letter. We try to make the first letter as small as possible, provided we can perform at most $x$ moves. We keep improving the string until $x = 0$. This greedy solution works because to get the lexicographically smallest string we must minimize the first letter, then the second letter and so on. This solution works in $O(|s| \cdot 26)$ time.

There are a few tricky cases for this problem :

  1. 3 yz
  2. 2 x
  3. 1 a
  4. 8 fx
  5. 4 xa

The answers to these cases are :

  1. a
  2. v
  3. a
  4. aa
  5. aa

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.

Tester's solution can be found here.

RELATED PROBLEMS:

MCO16104 - Editorial

$
0
0

PROBLEM LINK:

PracticeContest

Author:Zi Song Yeoh

DIFFICULTY:

EASY

PREREQUISITES:

Graph Theory, DFS/BFS

PROBLEM:

You are given a graph of $n$ vertices. Initially, all pairs of vertices are connected by an edge. However, $m$ pairs of edges were deleted. Some of the vertices are colored by white or black. Determine the number of ways to color the remaining vertices by white or black so that the subgraph formed by vertices of each color is a complete graph.

QUICK EXPLANATION:

The trick is to consider the complement graph, i.e. the graph with $n$ vertices and $m$ edges. The key observation is that the endpoints of an edge in the new graph cannot have the same color. Otherwise, in the original graph, we'll have two same color vertices with no edge between them. Now, we consider each connected component of the new graph seperately. We have to color the vertices so that each edge has distinct-colored endpoints. Consider a connected component. If one of its vertices has been colored, then the colors of the remaining vertices are uniquely determined as well. We can check whether the colors are correct by a single dfs/bfs. If none of the vertices has been colored, and the component is bipartite (which we can check with a dfs/bfs), then there are $2$ ways to color this component. In the end, multiply all the answers for each component.

EXPLANATION:

Subtask $1$ can be solved by enumerating all possibilities of coloring the vertices. However, one has to be careful when checking whether a coloring is feasible. The naive way works in $O(2^{n} \cdot n^{2})$, which might or might not pass the TL. A faster way which makes it possible to check whether a coloring is feasible in $O(m + n)$ comes from the following observation :

If we consider the graph where the missing edges are the edges of the graph, then any two adjacent vertices in the new graph must have different color.

Armed with this fact, we can check whether a coloring works by a single dfs/bfs on the graph.

The solution to subtask $2$ is also based on the above fact. The thing is, we can't afford to try all possible colorings. Let's analyze the graph closely. It is made up of some connected components. Clearly, we can consider each connected component seperately and multiply the results of each component together. It remains to know how to calculate the answer for a single component.

To calculate the answer for each component, we note that once the color of one vertex is determined, the remaining vertices will automatically have its color determined. (and this can be found by a dfs/bfs) So, the solution is simple. For each component select a vertex, and try both possible colors and see if the whole component can be colored correctly. Then, multiply the answer for each component. The whole answer can thus be calculated by a single dfs/bfs, so the complexity is $O(n + m)$, which passes easily.

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.

Tester's solution can be found here.

RELATED PROBLEMS:

MCO16105 - Editorial

$
0
0

PROBLEM LINK:

PracticeContest

Author:Zi Song Yeoh

DIFFICULTY:

EASY

PREREQUISITES:

Sliding Window, Binary Search, Dynamic Programming

PROBLEM:

Given a binary grid, find the number of subrectangles which contains at least $l$ 1s and at most $r$ 1s.

QUICK EXPLANATION:

There are many ways to solve this problem. The key is to note that $m$ is small, so we can consider all pairs of rows $i$ and $j$ so that the upper row of the subrectangle is $i$ and the bottom row of the subrectangle is $j$. It also helps to note that we can split the problem into two parts, calculating the number of subrectangles with at most $r$ 1s and the number of subrectangles with at most $l - 1$ 1s, and subtracting the results.

EXPLANATION:

The first subtask is simple. One can just iterate through all possible subrectangles and for each subrectangle, count the number of 1s in it. Since there are $O(n^{2}m^{2})$ subrectangles and $O(nm)$ time is needed to count the number of 1s in each subrectangle, this solution works in $O(n^{3}m^{3})$ time.

The second subtask is just a slight improvement over the first. We can use a simple trick to count the number of 1s in each subrectangle in $O(1)$. Let $dp[i][j]$ denote the number of 1s in the subrectangle with opposite corners $(1, 1)$ and $(i, j)$. The values of $dp$ can be calculated by $dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + cnt[i][j]$, where $cnt[i][j] = 1$ if there is a 1 in square $(i, j)$. This $dp$ can be calculated in $O(mn)$. Now, the same solution in the first subtask works in $O(n^{2}m^{2})$.

For the last subtask, one have to optimize the solution. Consider all pairs of rows $i$ and $j$ so that the upper row of the subrectangle is $i$ and the bottom row of the subrectangle is $j$. We'll count the number of valid subrectangles in $O(n)$, which will give us a grand total of $O(m^{2}n)$, which is fast enough.

To do so, let's create a new array $A$, containing $n$ elements. Each element $k$ denotes the number of elements in column $k$ from rows $i$ to $j$. Each subrectangle now corresponds to a subsegment of this array. We want to find the number of subsegments with sum at least $l$ and at most $r$. This is a standard problem and can be solved in various ways. One possible way is to iterate through all possible start points from left to right, and keep incrementing the endpoint until the sum becomes larger than $r$. Then, when we go to the next start point, we start from the last endpoint, instead starting over from the beginning. This way, we can count the number of subsegments with sum $\le r$ in $O(n)$. Repeat this to count the number of subsegments with sum $\le l - 1$. The answer is obtained by subtracting these two results. Thus, the whole solution works in $O(m^{2}n)$.

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.

Tester's solution can be found here.

RELATED PROBLEMS:

MCO16106 - Editorial

$
0
0

PROBLEM LINK:

PracticeContest

Author:Zi Song Yeoh

DIFFICULTY:

EASY-MEDIUM

PREREQUISITES:

Dynamic Programming

PROBLEM:

Given a string $S$ which denotes a bracket sequence with brackets () and []. Some of the brackets are missing. Find the number of correct bracket sequences that can be formed.

QUICK EXPLANATION:

Let $dp[l][r]$ be the answer for the substring $[l..r)$. Then, we can compute the answer using a recursive dp.

EXPLANATION:

For the first subtask, there are no question marks. Thus, the task reduces to determining whether the given bracket sequence is valid. This is a simple task that can be achieved using a stack. If the current element is ( or [, we push it to the stack. If it is a ), we check if the character on top of the stack is (. If it is (, we pop it from the stack, otherwise, the sequence is not correct, and we output $0$. We can do a similar check for when the current letter is ]. This will pass subtask $1$.

For the second subtask, $n$ (the length of $S$) is at most $10$. This makes the $O(4^{n} \cdot n)$ solution viable. We can iterate through all possible strings of length $10$ and for each string check whether it's possible. This is sufficient to solve subtask $2$.

For the last subtask, $n \le 500$, so a polynomial-time solution is required. We'll again resort to Dynamic Programming. Let $dp[l][r]$ be the answer for the substring $[l, r)$ (this means we're considering the substring formed from the $l$-th character to the $(r - 1)$-th character). We have $dp[l][l] = 1$ as the base case. Now, suppose we want to compute $dp[l][r]$. Consider the $l$-th letter. We loop through all $l + 1 \le i < r$ and count the number of ways to match the $l$-th bracket with the $i$-th bracket. Then, we're left with two subproblems, the string $[l + 1, i)$ and $[i + 1, r)$, and we can multiply the result by $dp[l+1][i] \cdot dp[i+1][r]$ and sum the result for all $i$. Since we have $O(n^{2})$ states and each state takes $O(n)$ to compute, the complexity is $O(n^3)$, which works in the time limit.

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.

Tester's solution can be found here.

RELATED PROBLEMS:

MCO16201 - Editorial

$
0
0

PROBLEM LINK:

PracticeContest

Author:Zi Song Yeoh

DIFFICULTY:

CAKEWALK

PREREQUISITES:

Brute Force

PROBLEM:

You want to place a rook on a chessboard so that the sum of the values written on the squares it attacks is maximal.

EXPLANATION:

The naive solution is to try placing a rook on every square and find the score for each possibility by looping through all the squares it attacks. This solution works in $O(n^{3})$ time because there are $O(n^2)$ squares to try and for each square we take $O(n)$ time to find the score.

We can improve the solution by noting that there is no need to iterate through all squares that can be attacked by the rook. Let $r_{i}$ and $c_{i}$ denote the sum of values on the squares in the $i$-th row and $i$-th column respectively. Then, the score obtained when the rook is on $(i, j)$ is just $r_{i} + c_{j} - a_{i, j}$.

Thus, with $O(n^2)$ precomputation, the answer can be found in $O(n^2)$ time.

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.

Tester's solution can be found here.

RELATED PROBLEMS:

MCO16202 - Editorial

$
0
0

PROBLEM LINK:

PracticeContest

Author:Zi Song Yeoh

DIFFICULTY:

EASY

PREREQUISITES:

Dynamic Programming

PROBLEM:

Given a binary string with some characters replaced with question marks. Find the number of ways to replace the question marks with 1 or 0 so that the resulting string has exactly $k$ runs for all $1 \le k \le n$.

EXPLANATION:

Firstly, the naive solution is to iterate through all possible binary strings and find the number of runs in each of them. This solution will work in $O(2^{n} \cdot n)$ time.

Now, to get a faster solution, we need dynamic programming. Let $dp[i][j][k]$ denote the number of ways to fill the question marks from digits $1$ to $i$ so that there are exactly $j$ runs and the $i$-th digit is $k$. (so $k = 0$ or $1$)

We can easily update the dp by considering all possible values of the current digit.

If the $i$-th digit can be $1$, then $dp[i][j][1] += dp[i - 1][j - 1][0] + dp[i - 1][j][1]$.

If the $i$-th digit can be $0$, then $dp[i][j][0] += dp[i - 1][j - 1][1] + dp[i - 1][j][0]$.

The time complexity of this solution is $O(n^2)$.

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.

Tester's solution can be found here.

RELATED PROBLEMS:

MCO16203 - Editorial

$
0
0

PROBLEM LINK:

PracticeContest

Author:Zi Song Yeoh

DIFFICULTY:

EASY

PREREQUISITES:

+1, -1 Trick

PROBLEM:

You have a $n \times n$ binary grid. There are $q$ operations, which involves flipping all the bits in a subrectangle. Output the final grid.

EXPLANATION:

For subtask $1$, iterating through all bits in the subrectangle and flipping them naively gives a simple $O(qn^{2})$ solution.

To solve the full task, we need to make the update process faster. First, let's consider the problem in 1D. Suppose we only have a $1 \times n$ grid instead. Let $a_{1}, a_{2}, ..., a_{n}$ be the original bits. We define a new sequence $d_{1}, d_{2}, ..., d_{n + 1}$, so that $d_{i} = a_{i} - a_{i - 1}$ for $2 \le i \le n$, $d_{n + 1} = a_{n}$, $d_{1} = a_{1}$. Then, to flip a range $[l, r]$, we just flip $d_{l}$ and $d_{r + 1}$, and the resulting values of $a_{i}$ can be restored with a simple loop at the end.

The 2D case is similar. First, we define the sequence $d$ for each row, and now we note that when we update a subrectangle, we're effectively performing range $[l, r]$ updates of a subsegment of $d_{i}$ in a column. We can use the same trick again to reduce each query to flipping $4$ corners of a rectangle. Finally, we can restore the grid by taking the prefix sums on rows and columns. You can see the code for the details.

The complexity of the solution is $O(q + n^2)$.

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.

Tester's solution can be found here.

RELATED PROBLEMS:


MCO16204 - Editorial

$
0
0

PROBLEM LINK:

PracticeContest

Author:Zi Song Yeoh

DIFFICULTY:

EASY

PREREQUISITES:

Dynamic Programming

PROBLEM:

There're 2 stacks of coins. Flippy and Fluffy takes coins on top of the stack alternately. What is the difference between Flippy and Fluffy's score at the end of the game?

EXPLANATION:

At first, one might think of the obvious greedy solution, i.e. take the coin that worth the most every time. However, one can come up with an example where the greedy solution fails. (though random cases are strong enough to fail greedy solutions)

The trick here is to note that the game can be defined by two integers, $(i, j)$, the size of the first stack and the size of the second stack. Thus, we can let $dp[i][j]$ be the value of the game when Player 1 moves first and the piles have $i$ and $j$ coins respectively.

Now, the recurrence for the dp is simple. We just have to consider which coin we pick at each state and we can finish the dp in $O(n^2)$ time. See the code for the details of the dp.

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.

Tester's solution can be found here.

RELATED PROBLEMS:

MCO16205 - Editorial

$
0
0

PROBLEM LINK:

PracticeContest

Author:Zi Song Yeoh

DIFFICULTY:

EASY

PREREQUISITES:

Dijkstra's Algorithm, Graph Theory

PROBLEM:

There's a weighted graph with $n$ vertices and $m$ edges. Additionally, each edge has a color. It is known the cost to go from one edge of color $i$ to one edge of color $j$ for all possible $(i, j)$. Find the shortest path from vertex $s$ to all other vertices.

EXPLANATION:

The first subtask can be solved using BFS, since the graph is unweighted and the colors of each edge is the same. Similarly, subtask $2$ can be solved with a direct application of Dijkstra's Algorithm.

The last subtask requires a small trick. We construct a new graph where each vertex is split into $k$ different vertices. In the new graph, the vertex $(i, j)$ denotes that we are at vertex $i$ and the color of the last edge we used is color $j$. Now, we can apply Dijkstra's Algorithm on this new graph and get the correct answer.

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.

Tester's solution can be found here.

RELATED PROBLEMS:

MCO16206 - Editorial

$
0
0

PROBLEM LINK:

PracticeContest

Author:Zi Song Yeoh

DIFFICULTY:

EASY-MEDIUM

PREREQUISITES:

CYCLE-FINDING, BINARY SEARCH, SPARSE TABLE

PROBLEM:

There is a country with $n$ cities arranged in a circle. The length of the path connecting city $i$ and $(i + 1)$ mod $n$ is $l_{i}$. You start at city $1$. Each day, you can run at most $x$ kilometres. However, after each day, you must end up in one of the cities. Determine the minimum number of days before you run at least $y$ kilometres.

EXPLANATION:

This problem can be solved in different ways. Some of the ways which are unoptimized can lead to a solution for the smaller subtasks. I'll describe a full solution here.

Firstly, we can detect if the answer is $-1$ by just going one round and see if we get stuck forever at one town (the length of road for next town is higher than $x$). Thus, from now on, we assume the task is always possible.

Suppose we're at city $i$, which city is the next that we'll visit? We can calculate how many rounds we go from city $i$. Then, we can binary search to find the last town we'll arrive at. We can do this for each city in $O(n\log n)$ total. Now, the key is that if we start from town $1$, and keep moving, eventually after at most $n + 1$ days, we'll end up at one of the towns we've visited before. Thus, we now have a cycle, i.e. after every few days we'll return back to the same position.

Thus, we can divide $y$ by the total length travelled for each cycle and calculate the number of cycles we have to make. Then, we can calculate the number of days needed to finish the last few kilometres of the whole run. This gives us an $O(n \log n)$ solution for this problem. You can see the details for this solution from the tester's solution.

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.

Tester's solution can be found here.

RELATED PROBLEMS:

interview question

$
0
0

1)
You need to prepare food, for that you have a set of ingredients available. Each ingredient has its own health value. You need to write a code which can determine all possible combinations of ingredients whose sum of total health is highest in the end. The number of ingredients in any combination can be 3 ingredients. Given below is the list with ingredient and their health value.
Salt -1
Sugar +1
Onion +2
Tomato +1
Spinach +3
Oreo -2
For example: Spinach (+3), Onion (+2), Sugar(+1) have a total health value of 6 and number of ingredients are 3. Like this your program should find best combinations (print the 3 best ingredients).

2)You need to find smallest positive integer which has certain digits on a condition that if this integer is multiplied by 2,3,4,5 or 6, the answers will still have same digits.

For example: 125874 when multiplied by 2 gives 251748. Both numbers have same digits, i.e. 1, 2, 4, 5, 7 and 8

How does Codechef test whether my solution is correct or not?

$
0
0

When I submit my code for a particular problem on CodeChef, who will test if my solution is correct or not? Can some one tell me how is my solution tested?

Viewing all 39796 articles
Browse latest View live


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