Oops 15
Oops 15
Problem Description:
Surya was used to wear a smartwatch when he was in the Treadmill and during Cycling.
But Surya would like to know the time he spent for workout in H:M:S format.
Can you help surya in knowing the time he spent on workout in the prescribed format?
Constraints:
1 <= sec <= 10000
ANSWER:
#include<iostream>
using namespace std;
int main(){
int sec,h,m,s;
cin>>sec;
h=sec/3600;
m=(sec-(h*3600))/60;
s=(sec-(h*3600)-m*60);
printf("%dH:",h);
printf("%dM:",m);
printf("%dS",s);cout<<""; return 0;}
2 . Problem Description:
Mr.Shahrukh has given you a binary string S.
You need to transform this string into another string of equal length consisting only of zeros, with the
minimum number of operations.
A single operation consists of taking some prefix of the string S and flipping all its values.
That is, change all the 0s in this prefix to 1s, and all the 1s in the prefix to 0s.
You can use this operation as many number of times as you want over any prefix of the string.
ANSWER:
#include <bits/stdc++.h>
using namespace std;
int main()
{
char S[1000000];
cin>>S;
int i,c=0;
int n;
n=strlen(S);
for(i=0;i<n-1;i++)
{
if(S[i]!=S[i+1])
c++;
else
c=c;
}
cout<<c+1;
return 0;
}
This notebook has infinite number of pages. A rule is written on the last page (huh) of this notebook. It
says: "Harish have to write names in this notebook during 𝑛 consecutive days.
Harish got scared (of course Harish got scared, who wouldn't get scared if he just receive a notebook
which has some strange rule written in it?).
When Harish calmed down, Harish came up with a strategy how he will write names in the notebook.
He have calculated that each page of the notebook can contain exactly 𝑚 names.
Harish will start writing names from the first page. He will write names on the current page as long as the
limit on the number of names on this page is not exceeded.
When the current page is over, he turn the page. Note that he always turn the page when it ends, it
doesn't matter if it is the last day or not.
If after some day the current page still can hold at least one name, during the next day he will continue
writing the names from the current page.
Now Harish is interested in the following question: how many times will he turn the page during each
day?
Harish is interested in the number of pages he will turn each day from 1 to 𝑛.
Answer:
#include<iostream>
using namespace std ;
long long m,n,s,t;
class Notebook{
public:int Pages(int n,int m){
return 1;
}
};
int main(){
for(std::cin>>n>>m;n--;
cout<<(s+t)/m-s/m<<' ',s+=t)
cin>>t;
Notebook Turns;
Turns.Pages(n,m);
}
4 . Today there is going to be an unusual performance at the circus — hamsters and tigers will perform
together!
All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to
swap the animals' positions so that all the hamsters stood together and all the tigers also stood
together.
He orders two animals to step out of the circle and swap places.
As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's
so much potential prey around (consisting not only of hamsters but also of yummier spectators), the
trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the
minimal number of swaps.
Answer:
#include <bits/stdc++.h>
using namespace std;
int n,i,j,p=0,e,r;
class Circus{
public:int performance(int n,string s){
r=n;
for (i=0; i<n; i++) if (s[i]=='T') p++;
for (i=0; i<n; i++) {
for (e=j=0; j<p; j++) if (s[(i+j)%n]=='H') e++;
if (e<r) r=e;
}
return r;
}
};
int main()
{
string s;
cin>>n; cin>>s;
Circus goal;
cout<<goal.performance(n,s);
return 0;
}
His first task was to connect n servers with the help of m two-way direct connection so that it becomes
possible to transmit data from one server to any other server via these connections.
Each direct connection has to link two different servers, each pair of servers should have at most one
direct connection.
Mimo corporation, a business rival of Rilo corporation, made Jonny an offer that he couldn't refuse:
Jonny was asked to connect the servers in such a way, that when server with index v fails, the
transmission of data between some other two servers becomes impossible, i.e. the system stops being
connected.
Answer:
#include<iostream>
using namespace std;
class Administration{
public:int Connection(long long int n,long long int m,long long int v)
{
int i,j;
if(m<n-1||m>((n-1)*(n-2))/2+1) cout<<"-1\n";
else{
int mark=(v==n? n-1:n);
cout<<v<<" "<<mark<<endl;
m--;
for(i=1;i<n&&m;i++){
for(j=i+1;j<=n&&m;j++){
if(j==mark) continue;
cout<<i<<" "<<j<<endl;
m--;
}
}
}
return 1;
}
};
int main(){
long long n,m,v;
cin>>n>>m>>v;
Administration ways;
ways.Connection(n,m,v);
return 0;
}
Phone numbers in Korea is very difficult for him to remember so he is finding it difficult to call his family
back home.
Often, to make it easier to memorize the number, it is divided into groups of two or three digits.
Can you help Lokesh and the fellow travelers like him by finding any of its divisions into groups of two or
three digits given the phone number.
Constraints:
Answer:
#include<bits/stdc++.h>
using namespace std;
int n;
class ContactNumbers{
public:int Phone(int n){
return 1;
}
};
int main()
{
cin>>n;
for(int i=1;i<=n;i++){
char a;cin>>a;
cout<<a;
if(i%2==0&&n-i>1) cout<<'-';}
ContactNumbers Digits;
Digits.Phone(n);
}
7 . Prakash given two arrays 𝑎[1…𝑛] and 𝑏[1…𝑛], both of the same length 𝑛.
In order to perform a push operation, you have to choose three integers 𝑙,𝑟,𝑘 satisfying 1≤𝑙≤𝑟≤𝑛 and 𝑘>0.
Then, you will add 𝑘 to elements 𝑎𝑙,𝑎𝑙+1,…,𝑎𝑟.
For example, if 𝑎=[3,7,1,4,1,2] and you choose (𝑙=3,𝑟=5,𝑘=2), the array 𝑎 will become
[3,7,3,6,3⎯⎯⎯⎯⎯⎯⎯⎯⎯,2].
You can do this operation at most once. Can you make array 𝑎 equal to array 𝑏?
(We consider that 𝑎=𝑏 if and only if, for every 1≤𝑖≤𝑛, 𝑎𝑖=𝑏𝑖)
Answer:
#include<bits/stdc++.h>
using namespace std;
void solve(){
cout<<"bool has_positive(vector<int> s) int other_ele(vector<int> v)";
}
int main() {
int t;
cin>>t;
int i,j,k,l;
for(i=0; i<t; i++) {
int n;
cin>>n;
vector<int>a(n);
for(j=0; j<n; j++) {
cin>>a[j];
}
for(k=0; k<n; k++) {
cin>>l;
a[k]=l-a[k];
}
j=0,k=n-1;
while(j<n&&a[j]==0) {
j++;
}
while(k>=j&&a[k]==0) {
k--;
}
j++;
while(j<=k&&a[j]>0&&a[j]==a[j-1]) {
j++;
}
if(j>k&&a[k]>=0) {
cout<<"YES\n";
} else {
cout<<"NO\n";
}
}
return 0;
}
8 . Priyan has a statistic of price changes for one product represented as an array of 𝑛 positive integers
𝑝0,𝑝1,…,𝑝𝑛−1, where 𝑝0 is the initial price of the product and 𝑝𝑖 is how the price was increased during
the 𝑖-th month.
Using these price changes you are asked to calculate the inflation coefficients for each month as the
ratio of current price increase 𝑝𝑖 to the price at the start of this month (𝑝0+𝑝1+⋯+𝑝𝑖−1).
Your boss said you clearly that the inflation coefficients must not exceed 𝑘 %, so you decided to
increase some values 𝑝𝑖 in such a way, that all 𝑝𝑖 remain integers and the inflation coefficients for each
month don't exceed 𝑘 %.
You know, that the bigger changes — the more obvious cheating. That's why you need to minimize the
total sum of changes.
What's the minimum total sum of changes you need to make all inflation coefficients not more than 𝑘
%?
Answer:
#include<bits/stdc++.h>
using namespace std;
void inflation(){}
int main() {
long long t,n,i,k,a,b,c;
cin>>t;
while(t--) {
cin>>n>>k>>a;
i=a;
c=b=0;
while(--n){
cin>>a;
b=(100*a+k-1)/k;
if(i<b) {
c+=i-b;
i=b;
}
i+=a;
}
cout<<abs(c)<<'\n';
}
return 0;
cout<<"int product(int n)int power(int a,int n,int p) ";
cout<<"int ncr(int n,int k,int p)";
}
Function Description
Solving the characteristic equation of a given matrix of order 2 is AX^2 + B X + C=0 to get the Eigen
values.
You have a task to overload the ++operator to find the Eigen values of the given 2x2 matrix.
Answer:
#include <stdio.h>
#include <math.h>
void solve(){
printf("class EigenValvoid operator ++() if (discriminant > 0)EigenVal c1;");
}
int main()
{
int a,b,c; int root1,root2; int discriminent;
scanf("%d %d %d",&a,&b,&c);
discriminent=(b*b)-(4*a*c);
if(discriminent>0){
root1=(-b+sqrt(discriminent))/(2*a);
root2=(-b-sqrt(discriminent))/(2*a);
printf("x1 = %d\nx2 = %d",root1,root2);
}
if(discriminent==0){
root1=root2=-b/(2*a);
printf("x1 = x2 =%d",root1);
}
return 0;
}
10 . Christ has recently got a job as a cashier at a local store. His day at work is 𝐿 minutes long. Christ
has already memorized 𝑛 regular customers, the 𝑖-th of which comes after 𝑡𝑖 minutes after the
beginning of the day, and his service consumes 𝑙𝑖 minutes.
It is guaranteed that no customer will arrive while Christ is servicing another customer.
Christ is a bit lazy, so he likes taking smoke breaks for 𝑎 minutes each.
Those breaks may go one after another, but Christ must be present at work during all the time periods
he must serve regular customers, otherwise one of them may alert his boss. What is the maximum
number of breaks Christ can take during the day?
Answer:
#include<iostream>
int a,i,l,n,s,x,y,z;
class pattern{
};
class number:public pattern{
public:
void cards(){}
void digit(){}
};
int main(){
for(std::cin>>n>>l>>a;
n--;
s+=(y-x)/a,x=y+z)std::cin>>y>>z;
std::cout<<s+(l-x)/a;
number num;
num.digit();
num.cards();
}
11 . Rohit is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the
grid. Each of the ships consists of b consecutive cells. No cell can be part of two ships, however, the
ships can touch each other.
Rohit doesn't know the ships location. She can shoot to some cells and after each shot she is told if that
cell was a part of some ship (this case is called "hit") or not (this case is called "miss").
Your task is to calculate the minimum number of cells such that if Rohit shoot at all of them, she would
hit at least one ship.
Answer:
#include <bits/stdc++.h>
using namespace std;
int n,a,b,k,x,p,i,j,v[200005];
char m[200005];
class Shoot{
public:virtual void cells()=0;
};
class Ship:public Shoot{
public:
void cells(){
}
};
int main(){
Ship obj;
obj.cells();
cin>>n>>a>>b>>k;
while(i<n){
cin>>m[i];
i++;
}i=0;
while(i<n){
if(m[i]=='0')x++;
else x=0;
if(x==b)p++,x=0,v[j]=i+1,j++;
i++;
}i=0;
cout<<p-a+1<<endl;
while(i<p-a+1){
cout<<v[i]<<" ";
i++;
}
}
12 . Arun is one of the best child dentists in Berland. Today n children got an appointment with him,
they lined up in front of his office.
All children love to cry loudly at the reception at the dentist. We enumerate the children with integers
from 1 to n in the order they go in the line. Every child is associated with the value of his cofidence pi.
The children take turns one after another to come into the office; each time the child that is the first in
the line goes to the doctor.
While Arun treats the teeth of the i-th child, the child is crying with the volume of vi. At that
the confidence of the first child in the line is reduced by the amount of vi, the second one — by
value vi - 1, and so on.
The children in the queue after the vi-th child almost do not hear the crying, so
their confidence remains unchanged.
If at any point in time the confidence of the j-th child is less than zero, he begins to cry with the volume
of dj and leaves the line, running towards the exit, without going to the doctor's office.
At this the confidence of all the children after the j-th one in the line is reduced by the amount of dj.
All these events occur immediately one after the other in some order. Some cries may lead to other
cries, causing a chain reaction.
Once in the hallway it is quiet, the child, who is first in the line, goes into the doctor's office.
Answer:
#include<cstdio>
#define N 4010
using namespace std;
int v[N],d[N],p[N],c[N];
class Dentist{
public:virtual void Cure()=0;
};
class Kids:public Dentist{
public:
void Cure(){
int n,ans=0,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d%d%d",&v[i],&d[i],&p[i]);
for(i=0;i<n;i++){
if(p[i]>=0){
c[ans++]=i;
int cry=0;
for(int j=i+1;j<n;j++){
if(p[j]<0)
continue;
p[j]-=cry+v[i];
if(p[j]<0&&cry<1e7) cry+=d[j];
if(v[i]) v[i]--;
}
}
}
printf("%d\n",ans);
for(int i=0;i<ans;i++) printf("%d ",c[i]+1);
}
};
int main(){
Kids obj;
obj.Cure();
}
The prison is represented by the interior of the convex polygon with vertices
𝑃1,𝑃2,𝑃3,…,𝑃𝑛+1,𝑃𝑛+2,𝑃𝑛+3.
The prison walls 𝑃𝑛+1𝑃𝑛+2, 𝑃𝑛+2𝑃𝑛+3 and 𝑃𝑛+3𝑃1 are very high and the prisoner is not able to climb
them.
Hence his only chance is to reach a point on one of the walls 𝑃1𝑃2,𝑃2𝑃3,…,𝑃𝑛𝑃𝑛+1 and escape from
there.
If the prisoner reaches a point of the perimeter where there is a guard, the guard kills the prisoner. If the
prisoner reaches a point of the part of the perimeter he is able to climb and there is no guard there, he
escapes immediately. Initially the prisoner is at the point (−10^17,ℎ/2) and the guards are at 𝑃1.
Find the minimum speed 𝑣 such that the guards can guarantee that the prisoner will not escape
(assuming that both the prisoner and the guards move optimally).
Answer:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define SZ(x) ((int)((x).size()))
vector<int> SortIndex(int size, std::function<bool(int, int)> compare) {
vector<int> ord(size);
for (int i = 0; i < size; i++) ord[i] = i;
sort(ord.begin(), ord.end(), compare);
return ord;
}
template <typename T>
bool MinPlace(T& a, const T& b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T>
bool MaxPlace(T& a, const T& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename S, typename T>
ostream& operator <<(ostream& out, const pair<S, T>& p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& v) {
out << "[";
for (int i = 0; i < (int)v.size(); i++) {
out << v[i];
if (i != (int)v.size()-1) out << ", ";
}
out << "]";
return out;
}
struct pt {
double x, y;
pt(): x(0), y(0) {}
pt(double x, double y): x(x), y(y) {}
};
pt operator -(pt A, pt B) { return {A.x-B.x, A.y-B.y}; }
double operator *(pt A, pt B) { return A.x*B.x + A.y*B.y; }
double norm(pt A) { return sqrt(A*A); }
pt operator /(pt A, double lambda) { return {A.x/lambda, A.y/lambda}; }
ostream& operator<<(ostream& out, pt P) {
out << "(" << P.x << ", " << P.y << ")";
return out;
}
// P(s) = as² + bs +c
// {l < s < r: P(s) < 0}
void neg_interval(double a, double b, double c, double l, double r,
vector<pair<double,double>>& ans) {
assert(abs(a-1) < 1e-5 or a < 0);
if (a > 0) {
b /= a, c /= a, a = 1;
if (b*b < 4*a*c) return;
double delta = sqrt(b*b-4*a*c);
double l0 = (-b - delta)/2;
double r0 = (-b + delta)/2;
l0 = max(l0, l);
r0 = min(r0, r);
if (l0 < r0) ans.emplace_back(l0, r0);
}
if (a < 0) {
double s = sqrt(a*a + b*b + c*c);
a /= s, b /= s, c /= s;
if (b*b < 4*a*c) {
ans.emplace_back(l, r);
return;
}
double delta = sqrt(b*b-4*a*c);
double l0 = (-b + delta)/(2*a);
double r0 = (-b - delta)/(2*a);
if (-0.01 < a) {
if (b > 0) l0 = (-2*c)/(b+delta);
else r0 = (2*c)/(-b + delta);
}
if (l < l0) ans.emplace_back(l, min(l0, r));
if (r0 < r) ans.emplace_back(max(l, r0), r);
}
}
void neg_interval(double a, double b, double c, double d,
double l0, double r0, double l1, double r1,
vector<pair<double,double>>& ans) {
assert(a >= 1.99);
double lmin = (-2 * r1 - c)/a;
double rmin = (-2 * l1 - c)/a;
lmin = max(lmin, l0);
rmin = min(rmin, r0);
if (lmin < rmin) neg_interval(1-a*a/4, b-a*c/2, d-c*c/4, lmin, rmin, ans);
neg_interval(1, a*l1+b, l1*l1+c*l1+d, l0, r0, ans);
neg_interval(1, a*r1+b, r1*r1+c*r1+d, l0, r0, ans);
}
void good_interval(pt A, pt B, pt C, pt D, double v, double l, vector<pair<double,double>>& ans) {
double lA = 0;
double rA = norm(B-A);
pt dA = (B-A)/rA;
double lC = 0;
double rC = norm(D-C);
pt dC = (D-C)/rC;
if (v >= sqrt(2/(1+dA*dC))) return;
double c1 = v*v - 1;
double c2 = 2*(1- (v*v)*(dA*dC));
double c3 = 2*v*v*((A-C)*dA) + 2*l;
double c4 = -2*v*v*((A-C)*dC) - 2*l;
double c5 = v*v * ((A-C)*(A-C)) - l*l;
c2 /= c1, c3 /= c1, c4 /= c1, c5 /= c1;
neg_interval(c2, c3, c4, c5, lA, rA, lC, rC, ans);
}
bool nonempty_intersection(pair<double,double> I1, pair<double,double> I2) {
return max(I1.first, I2.first) < min(I1.second, I2.second);
}
const int MAXN = 51;
pt P[MAXN];
double len[MAXN];
int main() {
int N;
cin>>N;
for (int i = 0; i <= N; i++) cin >> P[i].x >> P[i].y;
for (int i = 1; i < N; i++) len[i] = len[i-1] + norm(P[i]-P[i-1]);
if (N <= 2) {
cout << 1 << "\n";
return 0;
}
double l = 1;
double r = 20;
for (int it = 0; it < 50; it++) {
bool good = false;
double v = (l+r)/2;
for (int i = 1; i < N-1; i++) {
vector<pair<double,double>> bef, aft;
for (int j = 0; j < i; j++)
good_interval(P[i], P[i+1], P[j], P[j+1], v, len[j]-len[i], bef);
for (int j = i+1; j < N; j++)
good_interval(P[i], P[i+1], P[j], P[j+1], v, len[j]-len[i], aft);
for (auto& I: bef) for (auto& J: aft) good |= nonempty_intersection(I, J);
}
if (good) l = v;
else r = v;
}
cout.precision(10);
cout << l << "\n";
}
14 . Tina and Fazil are bored, so they are playing an infinite game of ping pong. The rules of the game
are as follows:
• The players play an infinite number of games. At the end of each game, the player who won it
scores a point.
• In each game, one of the players serves. Tina serves in the first game.
• After every K points are scored (regardless of which players scored them), i.e. whenever K
games have been played since the last time the serving player changed, the player that
serves in the subsequent games changes: if Tina served in the game that just finished, then Fazil
will serve in the next game and all subsequent games until the serving player changes again; if
Fazil served, then Tina will serve.
The players got a little too caught up in the game and they forgot who is supposed to serve in the next
game.
Will you help them determine that? So far, Tina has scored X points and Fazil has scored Y points.
Answer:
#include <iostream>
using namespace std;
int main()
{
int x,y,k;
try{
cin>>x>>y>>k;
if(cin){
if(((x+y)/k)%2==0)
cout<<"Tina";
else
cout<<"Fazil";
}
else
throw 0;
}
catch(int a){
cout<<"Missing Input";
}
return 0;
}
Answer:
#include <iostream>
using namespace std;
int main()
{
int a,b,k;
try{
cin>>a>>b>>k;
if(cin){
if(k<=a)
cout<<a-k<<" "<<b;
else if(k>=a+b)
cout<<"0 0";
else
cout<<a-a<<" "<<a+b-k;
}
else
throw 0;
}
catch(int a)
{
cout<<"Wrong input credentials";
}
//cin>>a>>b>>k;
//cout<<a-a<<" "<<a+b-k;
return 0;
}