Skip to content

pre coders v8 #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 60 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
e5ff310
Merge pull request #1 from HackersUOP/master
gihanjayatilaka Jun 8, 2016
bffec95
Merge branch 'master' of https://github.com/HackersUOP/codeBase
gihanjayatilaka Jun 9, 2016
1acff41
Merge pull request #2 from HackersUOP/master
gihanjayatilaka Oct 3, 2017
522b5e3
Add files via upload
gihanjayatilaka Feb 15, 2020
54979e9
v7 richest days
gihanjayatilaka Feb 16, 2020
626631f
Create Solution.py
gihanjayatilaka Feb 22, 2020
cfddfd6
Merge pull request #1 from gihanjayatilaka/master
seras3141 Feb 23, 2020
b686195
Inefficient sol gauntlet
seras3141 Feb 23, 2020
08257b9
Inefficient sol gauntlet
seras3141 Feb 23, 2020
9b68758
Solution_optimized.py
seras3141 Feb 23, 2020
903d390
Merge pull request #3 from suren3141/master
gihanjayatilaka Feb 23, 2020
61f9f0c
Update Solution.py
gihanjayatilaka Feb 23, 2020
1a2bdd1
Update Solution_unoptimized.py
gihanjayatilaka Feb 23, 2020
d88d024
Add files via upload
gihanjayatilaka Mar 3, 2020
cdc3575
Add files via upload
gihanjayatilaka Mar 3, 2020
852bc32
Create Readme.md
gihanjayatilaka Mar 3, 2020
d8ad11b
Solution Love-letters
DhanushkiMapitigama Mar 6, 2020
4daa721
Merge pull request #4 from DhanushkiMapitigama/master
gihanjayatilaka Mar 10, 2020
8a8048a
Create Solution.java
gihanjayatilaka Mar 10, 2020
90d1df2
Delete Readme.md
gihanjayatilaka Mar 10, 2020
e419b81
Create Solution2.cpp
gihanjayatilaka Mar 10, 2020
9589b6b
Create sol.py
harshana95 Mar 10, 2020
f4a7025
Create sol.py
harshana95 Mar 10, 2020
7232552
Create sol.py
harshana95 Mar 10, 2020
654aeca
Merge pull request #7 from harshana95/patch-5
gihanjayatilaka Mar 11, 2020
45cc87f
Merge pull request #6 from harshana95/patch-4
gihanjayatilaka Mar 11, 2020
3f40651
Merge pull request #5 from harshana95/patch-3
gihanjayatilaka Mar 11, 2020
9eadcfb
Delete Readme.md
gihanjayatilaka Mar 11, 2020
22873cd
Delete Readme.md
gihanjayatilaka Mar 11, 2020
76e7b4d
Delete Readme.md
gihanjayatilaka Mar 11, 2020
d9b92b2
Delete Readme.md
gihanjayatilaka Mar 11, 2020
e1e113b
Create Solution_dtdinidu7.py
gihanjayatilaka Mar 15, 2020
83127e5
Create Solution_InterGreat_UOP.py
gihanjayatilaka Mar 15, 2020
e82d85d
Update Readme.md
gihanjayatilaka Mar 15, 2020
f545997
Create Solution.cpp
gihanjayatilaka Mar 15, 2020
bfed3be
Update Readme.md
gihanjayatilaka Mar 15, 2020
aba095e
Create Solution.java
gihanjayatilaka Mar 15, 2020
18f5b9d
Update Solution.java
gihanjayatilaka Mar 15, 2020
a969f99
Create Solution.java
gihanjayatilaka Mar 15, 2020
1482217
Create Solution.java
gihanjayatilaka Mar 15, 2020
0feede5
Create Solution.java
gihanjayatilaka Mar 15, 2020
6b3e095
Create Solution.java
gihanjayatilaka Mar 15, 2020
46adb2b
Update README.md
gihanjayatilaka Mar 17, 2020
535e54b
Update sol.py
gihanjayatilaka Mar 18, 2020
3c4dce4
Create Solution.py
gihanjayatilaka Mar 22, 2020
d85bba2
added solution for the workout problem in kickstart 2020 round A
dininduwm Mar 22, 2020
32a97c4
Merge pull request #8 from dtdinidu7/kickstart_Q02
gihanjayatilaka Mar 22, 2020
26c21f5
some changes done to workout problem kickstart 2020 Round A
dininduwm Mar 22, 2020
fdddf15
Merge pull request #9 from dtdinidu7/kickstart_Q02
gihanjayatilaka Mar 22, 2020
11f7430
Solution for kickstart 2020 Round A Allocation added
dininduwm Mar 22, 2020
04db2fc
Merge pull request #10 from dtdinidu7/kickstart_Q01
gihanjayatilaka Mar 22, 2020
26c7385
Create Readme.md
gihanjayatilaka Mar 24, 2020
aea6820
Update Readme.md
gihanjayatilaka Mar 24, 2020
d0da405
Delete New Text Document.txt
gihanjayatilaka Mar 24, 2020
8dbd6dc
Create NCR.java
gihanjayatilaka Apr 4, 2020
a6b22c4
Update NCR.java
gihanjayatilaka Apr 4, 2020
858422d
solution for the Vestigium Problem added
dininduwm Apr 8, 2020
75fb0df
solution for the Nesting Depth problem added
dininduwm Apr 8, 2020
acb8617
solution for the Parent Parenting problem added
dininduwm Apr 8, 2020
caa4be2
Merge pull request #11 from dtdinidu7/CodeJam2020
gihanjayatilaka Apr 8, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[![Code Health](https://landscape.io/github/HackersUOP/codeBase/master/landscape.svg?style=flat)](https://landscape.io/github/HackersUOP/codeBase/master)
# codeBase
This repository is to store useful algorithms which are used frequently and to study them
This repository is to store useful algorithms which are used frequently and to study them.

# Google Group

All information regarding the programming related activities of the deaprtment are archived in [https://groups.google.com/forum/#!forum/cepdnaclk](https://groups.google.com/forum/#!forum/cepdnaclk)
20 changes: 20 additions & 0 deletions codeBase/ACES_Coders/v7.0/coders/RichestDay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'''
gihanjayatilaka[at]eng[dot]pdn[dot]ac[dot]lk 16/02/2020
'''
P=int(input())

diff=[0]*(100000+1)

for _ in range(P):
m,s,e=map(int,input().strip().split())

diff[s]+=m
diff[e+1]-=m

pay=[0]*len(diff)

pay[0]=diff[0]
for i in range(1,len(pay)):
pay[i]=pay[i-1]+diff[i]

print(max(pay))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The solution will be added later. Please contact sanoj.punchihewa[at]eng[dot]pdn[dot]ac[dot]lk for support.
23 changes: 23 additions & 0 deletions codeBase/ACES_Coders/v8.0/coders/A-walk-to-remember/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'''
This solution was submitted by Team: eyeCoders_UOP
during ACES Coders v8 2020
Team lead: Rusiru Thushara thusharakart@gmail.com

The solution runs in O(n)
'''
def getline():return [float(x) for x in input().strip().split(' ')]
c,e,n,s0 = getline()
arr, lst = [], []
m = c/s0
for i in range(int(n)):
x,s = getline()
arr.append((x,s))
if x<c and s>=0:
m = max(m,(c-x)/s)
elif s<0:
lst.append((x,s))
count = 0
for x,s in lst:
if x+s*m <= c:
count+=1
print(round(m),count)
13 changes: 13 additions & 0 deletions codeBase/ACES_Coders/v8.0/coders/Basketball-tryouts/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Basketball Tryouts - Solution

The constraints of the problem are,
N < 500000
D < 1000000

For a solution to work in under 2 sec for C / 4 sec for JAVA or 10 sec for python, the solution ought to be of time complexity O(N log(N)) at worst.

No team scored full marks for this question during the competition. But several users have scored full marks while upsolving the problem after the competition ended. These solutions are of order O(N^2). Apparently, it is possible to get full marks for this question even with an O(N^2) solution with several clever tweaks.

O(N log(N)) Solution: Solution.cpp
O(N^2) solutions : Solution_dtdinidu7.py , Solution_InterGreat_UOP.py

148 changes: 148 additions & 0 deletions codeBase/ACES_Coders/v8.0/coders/Basketball-tryouts/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#include <iostream>
#include <stdio.h>

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


bool DEBUG= false;

vector<pair<int, int> > normalize(vector<pair<int, int> > v) {
vector<pair<int, int> > ret;
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); i++) {
while (ret.size() && ret.back().first <= v[i].first &&
ret.back().second <= v[i].second) {
ret.pop_back();
}
ret.push_back(v[i]);
}
return ret;
}

vector<pair<int, int> > rev(vector<pair<int, int> > v) {
reverse(v.begin(), v.end());
for (int i = 0; i < v.size(); i++) v[i].first = -v[i].first;
for (int i = 0; i < v.size(); i++) v[i].second = -v[i].second;
return v;
}

long long solve(const vector<pair<int, int> >& a, int ai1, int ai2,
const vector<pair<int, int> >& b, int bi1, int bi2,
bool swapped) {
if (ai2-ai1 <= 50 || bi2-bi1 <= 50) {
long long ret = 0;
for (int i = ai1; i < ai2; i++)
for (int j = bi1; j < bi2; j++) {
if (swapped) {
ret = max(ret, (long long)min(0, b[j].first-a[i].first) *
(b[j].second-a[i].second));
} else {
ret = max(ret, (long long)max(0, b[j].first-a[i].first) *
(b[j].second-a[i].second));
}
}
return ret;
}

vector<pair<int, int> > b1, b2;
int i = (ai2+ai1)/2;
for (int j = bi1; j+1 < bi2; j += 2) {
long long v1 = (long long)(b[j].first-a[i].first) *
(b[j].second-a[i].second);
long long v2 = (long long)(b[j+1].first-a[i].first) *
(b[j+1].second-a[i].second);
if (v1 < v2) {
b1.push_back(b[j]); b1.push_back(b[j+1]); b2.push_back(b[j+1]);
} else {
b1.push_back(b[j]); b2.push_back(b[j]); b2.push_back(b[j+1]);
}
}
if ((bi2-bi1)%2) { b1.push_back(b[bi2-1]); b2.push_back(b[bi2-1]); }

return max(solve(b1, 0, b1.size(), a, ai1, i, !swapped),
solve(b2, 0, b2.size(), a, i, ai2, !swapped));
}

int main() {
int N, D;
scanf("%d %d",&N,&D);

// vector<pair<int, int> > A, C;

int ar_st[N];
int ar_en[N];
char ar_type[N];
int ar_score[N];

int countA=0,countB=0,countC=0;

long ansA,ansB=0,ansC=0;

int n=0;
for(n=0;n<N;n++){
int st,en,sc;
char type;
scanf("%d %d %c %d",&ar_st[n],&ar_en[n],&ar_type[n],&ar_score[n]);

if(ar_type[n]=='A')countA++;
if(ar_type[n]=='B')countB++;
if(ar_type[n]=='C')countC++;

}

if(DEBUG) {
for (n = 0; n < N; n++) {
printf("DEBUG: %d %d %c %d\n", ar_st[n], ar_en[n], ar_type[n], ar_score[n]);

}
}


vector<pair<int, int> > A(countA), C(countC);
countA=0,countB=0,countC=0;
for(n=0;n<N;n++){//buyers
if(ar_type[n]=='A'){
A[countA].first=ar_en[n]+1;
A[countA].second=ar_score[n];
ansA+=(ar_en[n]-ar_st[n]+1)*ar_score[n];

countA++;


} else if (ar_type[n]=='B'){
ansB+=(ar_en[n]-ar_st[n]+1)*ar_score[n];
countB++;
}else{//producers
C[countC].first=ar_st[n];
C[countC].second=ar_score[n];
ansC+=(ar_en[n]-ar_st[n]+1)*ar_score[n];
countC++;
}
}
if(DEBUG) {
printf("DEBUG: added.A %d addec.B %d added.C %d\n", countA, countB, countC);
}

// A = rev(normalize(rev(A)));
// C = normalize(C);

C=rev(normalize(rev(C)));
A=normalize(A);

// int ansAC=solve(A, 0, A.size(), C, 0, C.size(), false);

long ansAC=solve(C, 0, C.size(),A, 0, A.size(), false);
if(DEBUG) {
printf("A:%ld B:%ld C:%ld\n", ansA, ansB, ansC);
printf("A+C:%ld\n", ansA + ansC);
printf("ACnew:%ld\n", ansAC);
printf("0LD:%ld NEW:%ld\n", ansA + ansB + ansC, ansA + ansB + ansC + ansAC);
}


printf("%ld %ld\n",ansA + ansB + ansC, ansA + ansB + ansC + ansAC);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from collections import defaultdict

def pair():
maxi = 0
for ele in As:
a = A[ele]
for eleC in Cs:
c = C[eleC]
val = (ele - eleC) * (a - c + 1)
if val > maxi:
maxi = val
return maxi

N, D = list(map(int, input().split()))
A = defaultdict()
C = defaultdict()
sA = 0
sC = 0
sB = 0
numA = -1
numC = -1
for t in range(N):
t1, t2, t3, t4 = input().split()
t1 = int(t1)
t2 = int(t2)
t4 = int(t4)
if t3 == "A":
numA += 1
if (t4 not in A) or A[t4] < t2:
A[t4] = t2
sA += (t4 * t2)
elif t3 == "B":
sB += (t2 - t1 + 1) * t4
else:
numC += 1
if (t4 not in C) or C[t4] > t1:
C[t4] = t1
sC += (t2 - t1 + 1) * t4
unpaired = sA + sB + sC
As = A
Cs = C
# As = sorted(A.keys(), reverse = True)
# Cs = sorted(C.keys())
paired = pair()
print(str(unpaired) + " " + str(unpaired + paired))
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
n, d = [int(i) for i in input().split()]

A = {}
C = {}

shoots = 0

def rangeD(e1, s2, e2):
tmp = min(e1, e2) - s2+1
return(tmp if tmp >= 0 else -1)

for _ in range(n):
s, e, t, num = [i for i in input().split()]
s, e, num = int(s), int(e), int(num)
shoots += (e-s+1)*num
if (t == "A"):
if num in A:
A[num] = max(A[num], e)
else:
A[num] = e
elif (t == "C"):
if num in C:
C[num] = min(C[num], s)
else:
C[num] = s

print(shoots, end = ' ')

maxVal = 0

for a in A:
for c in C:
if (a > c):
tmp = rangeD(A[a], C[c], d)
if not tmp == -1:
maxVal = max(maxVal, tmp*(a-c))

print(shoots+maxVal)
1 change: 1 addition & 0 deletions codeBase/ACES_Coders/v8.0/coders/Dunder-Mifflin/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The solution will be added later. Please contact hishan.indrajith.adikari[at]eng[dot]pdn[dot]ac[dot]lk for support.
87 changes: 87 additions & 0 deletions codeBase/ACES_Coders/v8.0/coders/Essence-scanner/sol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Problem setter's code
# Author : harshana.w@eng.pdn.ac.lk
def f(X,Y,M,_x,_y):
ret = 0
for i in range(len(X)):
d = (X[i]-_x)**2 + (Y[i]-_y)**2
d = d**0.5
ret += d*M[i]
return ret

def geometricMedian(x, y, m):
n = len(x)

# start assumption from Center of Mass
_x,_y = 0, 0
for i in range(n):
_x += x[i] * m[i]
_y += y[i] * m[i]
_x,_y = _x/sum(m),_y/sum(m)
opt_d = f(x,y,m,_x,_y)


for i in range(n):
newd = f(x,y, m, x[i],y[i])
if (newd < opt_d):
opt_d = newd
_x = x[i]
_y = y[i]

test_distance = 10**5

test_x = [-1,0,1,0]
test_y = [0,1,0,-1]
delta = 10**100
while (test_distance > lower_limit and delta > 1e-7):

flag = 0
for i in range(4):
newx = _x + test_distance * test_x[i]
newy = _y + test_distance * test_y[i]
newd = f(x,y,m, newx,newy)
if (newd < opt_d):
delta = opt_d - newd
opt_d = newd
_x = newx
_y = newy
# print("new loc {:.6f} {:.6f} d={:.6f} delta={:8f} testd={:.8f}".format(_x,_y,opt_d,delta,test_distance))
flag = 1
break
if (flag == 0):
test_distance /= 2
return _x,_y

lower_limit = 1e-5
N,M = map(int,input().split())
Xs,Ys = [],[]
ms = []
for i in range(N):
x,y,m = map(int,input().split())
Xs.append(x)
Ys.append(y)
ms.append(m)


xC,yC = geometricMedian(Xs, Ys, ms)

_x,_y = 0, 0
for i in range(len(Xs)):
_x += Xs[i] * ms[i]
_y += Ys[i] * ms[i]
_x,_y = _x/sum(ms),_y/sum(ms)


Xs.append(0)
Ys.append(0)
ms.append(M)
xH,yH = geometricMedian(Xs, Ys, ms)

_x,_y = 0, 0
for i in range(len(Xs)):
_x += Xs[i] * ms[i]
_y += Ys[i] * ms[i]
_x,_y = _x/sum(ms),_y/sum(ms)


print("{} {}".format(round(xC),round(yC)))
print("{} {}".format(round(xH),round(yH)))
1 change: 1 addition & 0 deletions codeBase/ACES_Coders/v8.0/coders/Game-of-life/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The solution will be added later. Please contact suren.sri[at]eng[dot]pdn[dot]ac[dot]lk for support.
Loading