CPHUB Beginner Contest 47 - Editorial
CPHUB Beginner Contest 47 - Editorial
https://vjudge.net/contest/577747
Note:
1. Try the questions ATLEAST ONCE before reading the solutions written
below.
2. Do not be intimidated by the long code, I may have not written the most
neat and ideal code :P
3. If you didn’t get the first 2 questions, work on the fundamentals in your
programming language you’re studying, if you didn’t get the last 4
questions, practice more CP problems.
A - Four tickets-
Note that the cost of EACH ticket is given, that means if the total cost must not exceed 1000
rupees the cost of each ticket must not exceed 250 rupees.
Code- (C++)
#include <bits/stdc++.h>
using namespace std;
int main()
{
int tests;
cin>>tests;
for(int runtests=0;runtests<tests;runtests++)
{
int n;
cin>>n;
if(n>250)
{
cout<<"NO"<<endl;
}
else
{
cout<<"YES"<<endl;
}
}
return 0;
}
B - Translation-
This is a question to check if one string is the reverse of the other, this can be done by checking
if first and last character is equal, second and second last is equal, and so on.
Code- (C++)
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str1;
string str2;
cin>>str1>>str2;
int n=str1.length();
if(str2.length()!=n)
{
cout<<"NO"<<endl;
}
else
{
int flag=0;
for(int i=0;i<n;i++)
{
if(str1[i]!=str2[n-i-1])
{
flag=1;
break;
}
}
if(flag==1)
{
cout<<"NO"<<endl;
}
else
{
cout<<"YES"<<endl;
}
}
}
C - Prime subtraction-
Note that the numbers 2 and 3 are prime, with these two numbers you can make any number
>= 2 by simply repeatedly adding 2 and 3 (Eg: 7 = 2 + 2 + 3), this means that as long as the
difference between x and y is greater than 1, the answer is always “YES”.
Alternative explanation-
Every number can be represented as a product of prime factors OTHER THAN ONE, therefore if
the difference between two numbers is anything other than 1 it can be represented as
p1*p2*...pn => (prime number)*(some other number).
Code- (C++)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
int tests;
cin>>tests;
for(int runtests=0;runtests<tests;runtests++)
{
ll x;
ll y;
cin>>x>>y;
if(y==x-1)
{
cout<<"NO"<<endl;
}
else
{
cout<<"YES"<<endl;
}
}
return 0;
}
D – Contest Start-
Each student will have a fixed maximum number of students that can dissatisfy them, this can
be found by the equation t/x (say k). Lets imagine a ‘window’ of students such that each
student can be dissatisfied by all the people that are in their window, we get the answer by
counting the number of people in each students windows, this can be solving the following
cases-
1) The number of students is less than the maximum window, that means the last person
can be dissatisfied by 0 people, second last by 1 person, third last by 2 people and so on
until the first person.
This can thus be found by doing 1+2+….n-1 ( n*(n-1)/2 )
2) The number of students is greater than the maximum window, that means the answer
to the problem is 1+2+….+k+k+k+k
This can be found by doing (n*k) - (k*(k+1)/2)
Code- (C++)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
int tests;
cin>>tests;
for(int runtests=0;runtests<tests;runtests++)
{
ll n,x,t;
cin>>n>>x>>t;
ll win=t/x;
ll count=(n*win);
ll sub=(win*(win+1))/2;;
if(win>n)
{
cout<<(n)*(n-1)/2<<endl;
}
else
{
cout<<count-sub<<endl;
}
}
return 0;
}
E – String typing-
Note that the operation of copying the string can be done at most ONCE. Since the copying
operation can save us from doing a lot of operations we must make sure that we use it
optimally, this can be done by copying the largest string, this can be done by taking a string of
size k from the first part of the original string and checking if it is the same as the next k letters
(where k goes from 1 -> n/2)
Eg: In abcabca – Clearly abc is the best to copy (abc (3) + abc (1,copy) + a (1) )
In aaaaaaaa – We can copy ( a aa aaa or aaaa, since aaaa is the largest we copy aaaa,
therefore aaaa(4) + aaaa(1,copy) + a(1) )
Therefore the final answer is length of string if its not possible to copy, else we take length of
string-length of copied string+1 (+1 for the copy operation)
Code- (C++)
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
string s;
cin>>n;
cin>>s;
int max=0;
int in=0;
while(in<=n/2)
{
int flag=1;
for(int i=0;i<in;i++)
{
if(s[i]!=s[in+i])
{
flag=0;
break;
}
}
if(flag)
{
max=in;
}
in++;
}
if(max==0)
{
cout<<n<<endl;
}
else
{
cout<<n-max+1<<endl;
}
return 0;
}
F – Candies distribution
The thing to pick up on is that li + ri students get more candies than the ith student, therefore we
can assume that the ith student has n-li-ri candies. The only remaining thing to do is to check if
the li and ri for each student is satisfied by the candy values we found, this can be done by
counting the number of students that have more candies than the i th child to the left and right
of them and then comparing it to li and ri respectively.
Note that the checking mentioned above makes sure than n-li-ri never goes below 0.
Code- (C++)
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
vector<int> l(n);
vector<int> r(n);
for(int i=0;i<n;i++)
{
cin>>l[i];
}
for(int i=0;i<n;i++)
{
cin>>r[i];
}
if(l[0]!=0 || r[n-1]!=0)
{
cout<<"NO"<<endl;
return 0;
}
int maxx=l[0];
for(int i=0;i<n;i++)
{
maxx=max(maxx,l[i]);
}
vector<int> final(n);
for(int i=0;i<n;i++)
{
final[i]=n-l[i]-r[i];
}
int flag=0;
for(int i=0;i<n;i++)
{
int count=0;
for(int j=0;j<i;j++)
{
if(final[j]>final[i])
{
count++;
}
}
if(count!=l[i])
{
flag=1;
break;
}
count=0;
for(int j=i+1;j<n;j++)
{
if(final[j]>final[i])
{
count++;
}
}
if(count!=r[i])
{
flag=1;
break;
}
}
if(flag)
{
cout<<"NO"<<endl;
return 0;
}
cout<<"YES"<<endl;
for(int i=0;i<n;i++)
{
cout<<final[i]<<" ";
}
cout<<endl;
return 0;
}