304 North Cardinal St.
Dorchester Center, MA 02124

# Restaurant Rating CodeChef Solution

## Restaurant Rating CodeChef Solution in C++14

``````#include <bits/stdc++.h>

#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("Ofast")//Comment optimisations for interactive problems (use endl)
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")

using namespace std;

struct PairHash {inline std::size_t operator()(const std::pair<int, int> &v) const { return v.first * 31 + v.second; }};

// speed
#define Code ios_base::sync_with_stdio(false);
#define By ios::sync_with_stdio(0);
#define Sumfi cout.tie(NULL);

// alias
using ll = long long;
using ld = long double;
using ull = unsigned long long;

// constants
const ld PI = 3.14159265358979323846;  /* pi */
const ll INF = 1e18;
const ld EPS = 1e-9;
const ll MAX_N = 101010;
const ll mod = 998244353;

// typedef
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef array<ll,3> all3;
typedef array<ll,5> all5;
typedef vector<all3> vall3;
typedef vector<all5> vall5;
typedef vector<ld> vld;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int> vi;
typedef deque<ll> dqll;
typedef deque<pll> dqpll;
typedef pair<string, string> pss;
typedef vector<pss> vpss;
typedef vector<string> vs;
typedef vector<vs> vvs;
typedef unordered_set<ll> usll;
typedef unordered_set<pll, PairHash> uspll;
typedef unordered_map<ll, ll> umll;
typedef unordered_map<pll, ll, PairHash> umpll;

// macros
#define rep(i,m,n) for(ll i=m;i<n;i++)
#define rrep(i,m,n) for(ll i=n;i>=m;i--)
#define all(a) begin(a), end(a)
#define rall(a) rbegin(a), rend(a)
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define INF(a) memset(a,0x3f3f3f3f3f3f3f3fLL,sizeof(a))
#define ASCEND(a) iota(all(a),0)
#define sz(x) ll((x).size())
#define BIT(a,i) (a & (1ll<<i))
#define BITSHIFT(a,i,n) (((a<<i) & ((1ll<<n) - 1)) | (a>>(n-i)))
#define pyes cout<<"YES\n";
#define pno cout<<"NO\n";
#define endl "\n"
#define pneg1 cout<<"-1\n";
#define ppossible cout<<"Possible\n";
#define pimpossible cout<<"Impossible\n";
#define TC(x) cout<<"Case #"<<x<<": ";
#define X first
#define Y second

// utility functions
template <typename T>
void print(T &&t)  { cout << t << "\n"; }
template<typename T>
void printv(vector<T>v){ll n=v.size();rep(i,0,n){cout<<v[i];if(i+1!=n)cout<<' ';}cout<<endl;}
template<typename T>
void printvln(vector<T>v){ll n=v.size();rep(i,0,n)cout<<v[i]<<endl;}
void fileIO(string in = "input.txt", string out = "output.txt") {freopen(in.c_str(),"r",stdin); freopen(out.c_str(),"w",stdout);}
template<typename T>
template<typename T, typename U>
template<typename T, typename U>

struct Combination {
vll fac, inv;
ll n, MOD;

ll modpow(ll n, ll x, ll MOD = mod) { if(!x) return 1; ll res = modpow(n,x>>1,MOD); res = (res * res) % MOD; if(x&1) res = (res * n) % MOD; return res; }

Combination(ll _n, ll MOD = mod): n(_n + 1), MOD(MOD) {
inv = fac = vll(n,1);
rep(i,1,n) fac[i] = fac[i-1] * i % MOD;
inv[n - 1] = modpow(fac[n - 1], MOD - 2, MOD);
rrep(i,1,n - 2) inv[i] = inv[i + 1] * (i + 1) % MOD;
}

ll fact(ll n) {return fac[n];}
ll nCr(ll n, ll r) {
if(n < r or n < 0 or r < 0) return 0;
return fac[n] * inv[r] % MOD * inv[n-r] % MOD;
}
};

struct Matrix {
ll r,c;
vvll matrix;
Matrix(ll r, ll c, ll v = 0): r(r), c(c), matrix(vvll(r,vll(c,v))) {}

Matrix operator*(const Matrix& B) const {
Matrix res(r, B.c);
rep(i,0,r) rep(j,0,B.c) rep(k,0,B.r) {
res.matrix[i][j] = (res.matrix[i][j] + matrix[i][k] * B.matrix[k][j] % mod) % mod;
}
return res;
}

Matrix copy() {
Matrix copy(r,c);
copy.matrix = matrix;
return copy;
}

Matrix pow(ll n) {
assert(r == c);
Matrix res(r,r);
Matrix now = copy();
rep(i,0,r) res.matrix[i][i] = 1;
while(n) {
if(n & 1) res = res * now;
now = now * now;
n /= 2;
}
return res;
}
};

// geometry data structures
template <typename T>
struct Point {
T y,x;
Point(T y, T x) : y(y), x(x) {}
Point(pair<T,T> p) : y(p.first), x(p.second) {}
Point() {}
void input() {cin>>y>>x;}
friend ostream& operator<<(ostream& os, const Point<T>& p) { os<<p.y<<' '<<p.x<<'\n'; return os;}
Point<T> operator+(Point<T>& p) {return Point<T>(y + p.y, x + p.x);}
Point<T> operator-(Point<T>& p) {return Point<T>(y - p.y, x - p.x);}
Point<T> operator*(ll n) {return Point<T>(y*n,x*n); }
Point<T> operator/(ll n) {return Point<T>(y/n,x/n); }
bool operator<(const Point &other) const {if (x == other.x) return y < other.y;return x < other.x;}
Point<T> rotate(Point<T> center, ld angle) {
ld si = sin(angle * PI / 180.), co = cos(angle * PI / 180.);
ld y = this->y - center.y;
ld x = this->x - center.x;

return Point<T>(y * co - x * si + center.y, y * si + x * co + center.x);
}
ld distance(Point<T> other) {
T dy = abs(this->y - other.y);
T dx = abs(this->x - other.x);
return sqrt(dy * dy + dx * dx);
}

T norm() { return x * x + y * y; }
};

template<typename T>
struct Line {
Point<T> A, B;
Line(Point<T> A, Point<T> B) : A(A), B(B) {}
Line() {}

void input() {
A = Point<T>();
B = Point<T>();
A.input();
B.input();
}

T ccw(Point<T> &a, Point<T> &b, Point<T> &c) {
T res = a.x * b.y + b.x * c.y + c.x * a.y;
res -= (a.x * c.y + b.x * a.y + c.x * b.y);
return res;
}

bool isIntersect(Line<T> o) {
T p1p2 = ccw(A,B,o.A) * ccw(A,B,o.B);
T p3p4 = ccw(o.A,o.B,A) * ccw(o.A,o.B,B);
if (p1p2 == 0 && p3p4 == 0) {
pair<T,T> p1(A.y, A.x), p2(B.y,B.x), p3(o.A.y, o.A.x), p4(o.B.y, o.B.x);
if (p1 > p2) swap(p2, p1);
if (p3 > p4) swap(p3, p4);
return p3 <= p2 && p1 <= p4;
}
return p1p2 <= 0 && p3p4 <= 0;
}

pair<bool,Point<ld>> intersection(Line<T> o) {
if(!this->intersection(o)) return {false, {}};
ld det = 1. * (o.B.y-o.A.y)*(B.x-A.x) - 1.*(o.B.x-o.A.x)*(B.y-A.y);
ld t = ((o.B.x-o.A.x)*(A.y-o.A.y) - (o.B.y-o.A.y)*(A.x-o.A.x)) / det;
return {true, {A.y + 1. * t * (B.y - A.y), B.x + 1. * t * (B.x - A.x)}};
}

//@formula for : y = ax + b
//@return {a,b};
pair<ld, ld> formula() {
T y1 = A.y, y2 = B.y;
T x1 = A.x, x2 = B.x;
if(y1 == y2) return {1e9, 0};
if(x1 == x2) return {0, 1e9};
ld a = 1. * (y2 - y1) / (x2 - x1);
ld b = -x1 * a + y1;
return {a, b};
}
};

template<typename T>
struct Circle {
Point<T> center;
Circle() {}

void input() {
center = Point<T>();
center.input();
}

bool circumference(Point<T> p) {
return (center.x - p.x) * (center.x - p.x) + (center.y - p.y) * (center.y - p.y) == radius * radius;
}

bool intersect(Circle<T> c) {
T d = (center.x - c.center.x) * (center.x - c.center.x) + (center.y - c.center.y) * (center.y - c.center.y);
}

bool include(Circle<T> c) {
T d = (center.x - c.center.x) * (center.x - c.center.x) + (center.y - c.center.y) * (center.y - c.center.y);
}
};

ll __gcd(ll x, ll y) { return !y ? x : __gcd(y, x % y); }
all3 __exgcd(ll x, ll y) { if(!y) return {x,1,0}; auto [g,x1,y1] = __exgcd(y, x % y); return {g, y1, x1 - (x/y) * y1}; }
ll __lcm(ll x, ll y) { return x / __gcd(x,y) * y; }
ll modpow(ll n, ll x, ll MOD = mod) { n%=MOD; if(!x) return 1; ll res = modpow(n,x>>1,MOD); res = (res * res) % MOD; if(x&1) res = (res * n) % MOD; return res; }

ll op, x;
cin>>op;
if(op == 1) cin>>x;
return {op, x};
}

vs solve(ll n) {
priority_queue<ll> worst;
priority_queue<ll, vll, greater<ll>> good;
vs res;
rep(i,0,n) {
if(op == 1) {
ll review = (sz(worst) + sz(good) + 1) / 3;
good.push(x);
while(sz(good) > review) {
worst.push(good.top());
good.pop();
}
while(sz(good) and sz(worst) and good.top() < worst.top()) {
ll g = good.top(); good.pop();
ll w = worst.top(); worst.pop();
good.push(w);
worst.push(g);
}
} else {
if(sz(worst) + sz(good) < 3) res.push_back("No reviews yet");
else res.push_back(to_string(good.top()));
}
}
return res;
}
int main() {
Code By Sumfi
cout.precision(12);
ll tc = 1;
//cin>>tc;
rep(i,1,tc+1) {
ll n;
cin>>n;
printvln(solve(n));
}
return 0;
}``````

## Restaurant Rating CodeChef Solution in PYTH 3

``````import heapq
import sys

def FILE_IO():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')

def solve():
N = int(input())
no_reviews = 0
min_heap = []
max_heap = []

for _ in range(N):
line = input()

if line[0] == "1":
x = int(line[2:])
no_reviews += 1

if len(min_heap) != 0 and min_heap[0] < x:
heapq.heappush(min_heap, x)
heapq.heappush(max_heap, -heapq.heappop(min_heap))
else:
heapq.heappush(max_heap, -x)

if no_reviews % 3 == 0:
heapq.heappush(min_heap, -heapq.heappop(max_heap))

else:
if len(min_heap) == 0:
print("No reviews yet")
else:
print(min_heap[0])

# FILE_IO()
solve()``````

## Restaurant Rating CodeChef Solution in C

``````#include <stdio.h>
#include <stdlib.h>

#define MAX_HEAP 1
#define MIN_HEAP 2

struct Heap{
unsigned int *data;
int cap, size;
int type; // MAX_HEAP || MIN_HEAP
};

struct Heap* createHeap(int cap, int MAX_MIN){
struct Heap *heap = malloc(sizeof(struct Heap));
heap->cap = cap;
heap->size =0;
heap->data = malloc(sizeof(unsigned int)*cap);
heap->type = MAX_MIN;
return heap;
};

void deleteHeap(struct Heap *heap){
free(heap->data);
free(heap);
}

int isEmpty(struct Heap *heap){
return heap->size == 0;
}

int isFull(struct Heap *heap){
return heap->size == heap->cap;
}

void insertHeap(struct Heap *h, unsigned int elem){
if(isFull(h))
exit(-1);

int pos = h->size;
h->data[h->size++] = elem;

while(pos > 0){
int parent = (pos-1)/2;
if((h->type == MAX_HEAP && h->data[parent] < h->data[pos])
|| (h->type == MIN_HEAP && h->data[parent] > h->data[pos])){
unsigned int tmp = h->data[parent];
h->data[parent] = h->data[pos];
h->data[pos] = tmp;
pos = parent;
}else
break;
}
}

unsigned int peekHeap(struct Heap *h){
if(isEmpty(h))
exit(1);
return h->data[0];
}

unsigned int pollHeap(struct Heap *h){
if(isEmpty(h))
exit(-1);

unsigned int rvalue = h->data[0];
h->data[0] = h->data[--h->size];
int pos = 0;
while(pos < h->size){
int lchild = 2*pos+1, rchild = 2*pos+2, bchild = lchild;

if((h->type == MAX_HEAP && rchild < h->size && h->data[rchild] > h->data[lchild])
|| (h->type == MIN_HEAP && rchild < h->size && h->data[rchild] < h->data[lchild]))
bchild = rchild;

if((h->type == MAX_HEAP && bchild <h->size && h->data[bchild] > h->data[pos])
|| (h->type == MIN_HEAP && bchild <h->size && h->data[bchild] < h->data[pos])){
unsigned int tmp = h->data[bchild];
h->data[bchild] = h->data[pos];
h->data[pos] = tmp;
pos = bchild;
}else
break;
}
return rvalue;
}

int main(){

int n;
scanf("%d", &n);
struct Heap *max = createHeap((n*2)/3, MAX_HEAP);
struct Heap *min = createHeap(n/3, MIN_HEAP);
int cnt =0;

while(n-- > 0){
int m;
scanf("%d", &m);
if(m == 1){
int rev;
scanf("%d", &rev);
insertHeap(max, rev);
cnt++;

if(cnt/3 == 0)
continue;
if(cnt % 3 == 0)
insertHeap(min, pollHeap(max));
else if(peekHeap(min) < peekHeap(max)){
insertHeap(min, pollHeap(max));
insertHeap(max, pollHeap(min));
}

}else if(m == 2){
if(cnt/3 == 0)
printf("No reviews yet\n");
else
printf("%u\n", peekHeap(min));
}else
break;
}

return 0;
}``````

## Restaurant Rating CodeChef Solution in JAVA

``````/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
static PrintWriter out=new PrintWriter((System.out));
public static void main(String args[])throws IOException
{
int t=sc.nextInt();
int tr=0;
PriorityQueue<Integer> displayed=new PriorityQueue<>();
PriorityQueue<Integer> other=new PriorityQueue<>();

while(t-->0)
{
int type=sc.nextInt();
if(type==1){
int review=sc.nextInt();
tr++;
if(tr%3==0){
int nt = -other.remove();
}
if(displayed.size()>0){
int smallest=displayed.peek(),largest=-other.peek();
if(smallest<largest){
displayed.remove();
other.remove();
}
}
}else{
if(displayed.size()==0){
out.println("No reviews yet");
}else{
out.println(displayed.peek());
}

}

}
out.close();
}

{
StringTokenizer st=new StringTokenizer("");
public String next()
{
while(!st.hasMoreTokens())
{
try
{
}
catch(Exception e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt()
{
return Integer.parseInt(next());
}
public long nextLong()
{
return Long.parseLong(next());
}
public double nextDouble()
{
return Double.parseDouble(next());
}
public String nextLine()
{
try
{
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
public boolean hasNext()
{
String next=null;
try
{
}
catch(Exception e)
{
}
if(next==null)
{
return false;
}
st=new StringTokenizer(next);
return true;
}
}
}``````

## Restaurant Rating CodeChef Solution in PYTH

``````# maintain 2/3 low max-heap, 1/3 high min-heap
import heapq

N = int(raw_input())
maxH = []
minH = []
i = 0
for _ in range(N):
inp = map(int, raw_input().split())
if inp[0] == 1:
review = inp[1]
i += 1
if i % 3 == 0:
if review > -maxH[0]:
heapq.heappush(minH, review)
else:
heapq.heappush(maxH, -review)
heapq.heappush(minH, -heapq.heappop(maxH))
else:
if minH and review > minH[0]:
swap = heapq.heappop(minH)
heapq.heappush(minH, review)
heapq.heappush(maxH, -swap)
else:
heapq.heappush(maxH, -review)
else:
if minH:
print(minH[0])
else:
print('No reviews yet')``````

## Restaurant Rating CodeChef Solution in C#

``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace RRATING
{
class MinHeap
{
public int[] Heap;
public int Count;

public MinHeap()
{
Heap = new int[250000];
Count = 0;
}

public void Insert(int n)
{
Heap[Count] = n; Count++;
int curPos = Count - 1;
int parent = (curPos % 2 == 0) ? (curPos - 2) / 2 : (curPos - 1) / 2;

while (curPos > 0 && parent >= 0 && Heap[parent] > Heap[curPos])
{
int t = Heap[parent];
Heap[parent] = Heap[curPos];
Heap[curPos] = t;
curPos = parent;
parent = (curPos % 2 == 0) ? (curPos - 2) / 2 : (curPos - 1) / 2;
}
}

public int RemoveMin()
{
int res = Heap[0];
Heap[0] = Heap[Count - 1];
Count--;
int curPos = 0;
int leftChild = 2 * curPos + 1;
int rightChild = leftChild + 1;

while (curPos < Count && leftChild < Count)
{
if (rightChild < Count && Heap[rightChild] < Heap[leftChild] && Heap[rightChild] < Heap[curPos])
{
int t = Heap[curPos];
Heap[curPos] = Heap[rightChild];
Heap[rightChild] = t;
curPos = rightChild;
}
else if (Heap[leftChild] < Heap[curPos])
{
int t = Heap[curPos];
Heap[curPos] = Heap[leftChild];
Heap[leftChild] = t;
curPos = leftChild;
}
else break;
leftChild = 2 * curPos + 1;
rightChild = leftChild + 1;
}

return res;
}
}

class MaxHeap
{
public int[] Heap;
public int Count;

public MaxHeap()
{
Heap = new int[250000];
}

public void Insert(int n)
{
Heap[Count] = n; Count++;
int curPos = Count - 1;
int parent = (curPos % 2 == 0) ? (curPos - 2) / 2 : (curPos - 1) / 2;

while (curPos > 0 && parent >= 0 && Heap[parent] < Heap[curPos])
{
int t = Heap[parent];
Heap[parent] = Heap[curPos];
Heap[curPos] = t;
curPos = parent;
parent = (curPos % 2 == 0) ? (curPos - 2) / 2 : (curPos - 1) / 2;
}
}

public int RemoveMax()
{
int res = Heap[0];
Heap[0] = Heap[Count - 1];
Count--;
int curPos = 0;
int leftChild = 2 * curPos + 1;
int rightChild = leftChild + 1;

while (curPos < Count && leftChild < Count)
{
if (rightChild < Count && Heap[rightChild] > Heap[leftChild] && Heap[rightChild] > Heap[curPos])
{
int t = Heap[curPos];
Heap[curPos] = Heap[rightChild];
Heap[rightChild] = t;
curPos = rightChild;
}
else if (Heap[leftChild] > Heap[curPos])
{
int t = Heap[curPos];
Heap[curPos] = Heap[leftChild];
Heap[leftChild] = t;
curPos = leftChild;
}
else break;
leftChild = 2 * curPos + 1;
rightChild = leftChild + 1;
}

return res;
}
}

class RRATING
{
static byte[] Buffer;
public static void Initialze()
{
Size = 1 << 22;
Buffer = new byte[Size];
}
{
//For trimming
do
{
}
bool neg = false;
//Checking sign of number
{
neg = true;
}
int m = _ReadByte - '0';
while (true)
{
m = m * 10 + (_ReadByte - '0');
}
if (neg) return -m;
return m;
}
{
Idx = 0;
if (BytesRead <= 0) Buffer[0] = 32;
}
{
return Buffer[Idx++];
}
public static void Dispose()
{
}

static void Main()
{
Initialze();
byte[] res = new byte[1 << 22];
int rc = 0;
MaxHeap Maxrate = new MaxHeap(); // size = 2/3 of the total count
MinHeap Minrate = new MinHeap(); // size =  1/3 of total count
int totalCount = 0;

while (n-- > 0)
{

if (input == 1)
{
totalCount++;

if (Maxrate.Count == 0)
{
Maxrate.Insert(x);
}
else
{
if (x <= Maxrate.Heap[0]) Maxrate.Insert(x);
else Minrate.Insert(x);

if (Maxrate.Count < 2 * Minrate.Count)
{
Maxrate.Insert(Minrate.RemoveMin());
}
else if (Maxrate.Count > 2 * (Minrate.Count + 1))
{
Minrate.Insert(Maxrate.RemoveMax());
}
}
}
else if (input == 2)
{
if (totalCount / 3 == 0)
{
byte[] _res = Encoding.ASCII.GetBytes("No reviews yet");
_res.CopyTo(res, rc);
rc += _res.Length;
res[rc] = 0xA;
rc++;
}
else
{
byte[] _res = Encoding.ASCII.GetBytes(Minrate.Heap[0].ToString());
_res.CopyTo(res, rc);
rc += _res.Length;
res[rc] = 0xA;
rc++;
}
}
}
Dispose();
Console.OpenStandardOutput().Write(res, 0, rc);
}
}
}``````
##### Restaurant Rating CodeChef Solution Review:

In our experience, we suggest you solve this Restaurant Rating CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Restaurant Rating CodeChef Solution.

Find on CodeChef

##### Conclusion:

I hope this Restaurant Rating CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!