Coding Test 1 1: Array Game
Coding Test 1 1: Array Game
1 Array Game
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m; cin>>n>>m; ll a[n],b[m],ans=0;
for(int i=0; i<n; i++) cin>>a[i];
for(int i=0; i<m; i++) cin>>b[i];
sort(a,a+n);
sort(b,b+m);
for(int i=0,j=m-1; i<n && j>=0; i++,j--){
if(a[i]<b[j]){
ans+=abs(a[i]-b[j]);
}
}
cout<<ans;
return 0;
}
2 Tourist
import java.util.*;
import java.io.*;
SegmentTree(int n) {
st = new int[4*n];
}
for(int v: g[u])
if(b != v && v != p)
trick(v,u,false);
if(mx != -1){
big[b] = 1;
trick(b,u,true);
}
add(u, p, 1);
if(u < K) {
val[u] = u == 0? 0: seg.get(0, n - 1, 0, u - 1, 1);
val[u]++;
}
else if(u > K) {
val[u] = u == n - 1? 0: seg.get(0, n - 1, u + 1, n - 1, 1);
val[u]++;
}
//debug(u,val[u]);
seg.update(0, n - 1, u, u, val[u], 1);
if(p == K) {
int x = (K == 0)? 0: seg.get(0, n - 1, 0, K - 1, 1);
int y = (K == n - 1)? 0: seg.get(0, n - 1, K + 1, n - 1, 1);
ans = Math.max(ans, maxX + y);
ans = Math.max(ans, maxY + x);
maxX = Math.max(maxX, x);
maxY = Math.max(maxY, y);
}
n = in.nextInt();
K = in.nextInt() - 1;
sub = new int[n];
big = new int[n];
val = new int[n];
int[] u = new int[n - 1];
int[] v = new int[n - 1];
seg = new SegmentTree(n);
out.close();
}
int x,y;
int l,r;
@Override
public String toString()
{
return x + " "+ y + " "+l + " " + r;
}
}
}
Coding test 2
1. Acche Din
import java.io.*;
import java.util.*;
CODING TEST 3
1 RECTANGULAR AREA
import java.util.Stack;
// If this bar is lower than top of stack, then calculate area of rectangle
// with stack top as the smallest (or minimum height) bar. 'i' is
// 'right index' for the top and element before top in stack is 'left index'
else
{
tp = s.peek(); // store the top index
s.pop(); // pop the top
// Calculate the area with hist[tp] stack as smallest bar
area_with_top = hist[tp] * (s.empty() ? i : i - s.peek() - 1);
// Now pop the remaining bars from stack and calculate area with every
// popped bar as the smallest bar
while (s.empty() == false)
{
tp = s.peek();
s.pop();
area_with_top = hist[tp] * (s.empty() ? i : i - s.peek() - 1);
return max_area;
#include<stdio.h>
int gcd(int a,int b)
{
if(a==0)
{
return b;
}
return gcd(b%a,a);
}
int main()
{
int t,i,n,x,y,z,arr[100000],j,a;
scanf("%d",&t);
for(i=0;i<=t-1;i++)
{int count=0;
scanf("%d %d %d %d",&n,&x,&y,&z);
for(j=0;j<=n-1;j++)
{
scanf("%d",&arr[j]);
if(j==0)
{
a=arr[j];
}
else
a=gcd(a,arr[j]);
}
for(j=0;j<=n-1;j++)
{arr[j]=arr[j]/a;
while(arr[j]%x==0)
{
arr[j]=arr[j]/x;
}
while(arr[j]%y==0)
{
arr[j]=arr[j]/y;
}
while(arr[j]%z==0)
{
arr[j]=arr[j]/z;
}
if(arr[j]>1)
{
count++;
break;
}
}
if(count==0)
{
printf("She can\n");
}
else
{
printf("She can't\n");
}
}
return 0;
}
3. RANK LIST
import java.io.BufferedReader;
import java.io.InputStreamReader;
if(c==0) {
c = s1.name.compareTo(s2.name);
}
if(c==0) {
c= s1.scholar-(s2.scholar);
}
return c;
}
});
for(Student s: list)
System.out.println(s);
}
}
class Student {
String name;
int scholar;
int marks;
public Student(String name, int scholar, int marks){
this.name = name;
this.scholar = scholar;
this.marks = marks;
}
@Override
public boolean equals(Object o) {
if(this==o)
return true;
return false;
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * scholar;
result = prime * (name.hashCode()) + result;
return result;
}
}
CODING TEST 4
1. Capital of hills
#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define fr(i,n) for(ll i = 0; i < n; ++i)
#define frs(i,s,n) for(ll i = s; i < n; ++i)
#define frb(i,s,e) for(ll i = s; i <= e; ++i)
#define rfr(i,n) for(ll i = n-1; i >= 0; i--)
#define frbr(i,e,s) for(ll i = e; i >= s; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
string to_string(bool b)
{
return (b ? "true" : "false");
}
string to_string(vector<bool> v)
{
bool first = true;
string res = "{";
for(int i = 0; i < static_cast<int>(v.size()); i++)
{
if(!first)
res += ", ";
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
#ifdef LOCAL
#define dbg(...) {cerr << "[" << #__VA_ARGS__ << "] = [", debugOut(__VA_ARGS__);}
#define dbg_arr(a,n) {cerr << "[" << #a << "] = [ "; fr(i,n) cerr << a[i] << " "; cerr << "]\n";}
#define dbg_mat(m,r,c) {cerr << "[" << #m << "]:\n"; fr(i,r) {cerr << "[ "; fr(j,c) cerr << m[i][j]
<< " "; cerr << "]\n";}}
#define dbg_time() {cerr << "\n" << "Time elapsed: " << getCurrentTime() << "\n\n\n";}
#else
#define dbg(x...) {}
#define dbg_arr(a,n) {}
#define dbg_mat(m,r,c) {}
#define dbg_time() {}
#endif
const ld PI = acos(-1);
const ld EPS = 1e-9;
const ll INF = 1e18;
const ll SINF = 1e9;
const ll MOD = 1e9+7; // 998244353;
const ll MAX = 100100;
void pre()
{
// factorizer::sieve();
// prepare_factorials();
}
void solve()
{
ll n; cin >> n;
ll arr[n];
fr(i, n)
cin>>arr[i];
vl temp(n, 0);
ll ans = 0;
stack < ll > st; // stictly decreasing
fr(i, n)
{
if(st.empty())
st.push(i);
else
{
while(!st.empty() && (arr[i] >= arr[st.top()]))
{
if(arr[i] == arr[st.top()]) // equal towards left
{
temp[i] = temp[st.top()] + 1;
ans += temp[i];
}
else // less towards left
ans += temp[st.top()] + 1;
st.pop();
}
// high towards left
if(!st.empty())
ans++;
st.push(i);
}
}
cout << ans << "\n";
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
pre();
ll t = 1;
// cin >> t;
frb(CASE, 1, t)
{
// cout << "Case #" << CASE << ": ";
// dbg(CASE);
solve();
}
dbg_time();
return 0;
}
2. MAGIC SQUARE
#include <bits/stdc++.h>
return ans;
}
int main()
{
vector<vector<int>> s(3);
for (int i = 0; i < 3; i++) {
s[i].resize(3);
return 0;
}
import sys
st = [n - 1]
for i in range(n - 2, -1, -1):
while st and arr[st[-1]] < arr[i]:
st.pop()
if st:
rights[i] = st[-1]
st.append(i)
st =[0]
if st:
lefts[i] = st[-1]
st.append(i)
res = [0] * n
for i in range(n):
if rights[i] != -1:
res[rights[i] - i] = max([res[rights[i] - i], i - lefts[i]])
return sum(res)
n = int(input())
arr = list(map(int, sys.stdin.readline().split()))
if n <= 1:
print(0)
else:
count = numFab(arr, n)
print(count)
CODING TEST 5
1. AKSHI_PROBLEM
T = int(input())
for p in range (T):
N = int(input())
A = [int (x) for x in reversed(list(map(int, input().split())))]
for i in range (N):
print(A[i], end=' ')
print()
2. VOWEL RECOGNITION
#include<iostream>
#include<string>
int32_t main()
{
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
int dp[s.length()];
int sum = 0;
for(int i = 0; i<s.length(); i++)
{
if(i==0)
dp[i]=s.length();
else
dp[i] = dp[i-1]+s.length()-(2*i);
cout<<sum<<"\n";
}
return 0;
}
CODING TEST 6
1. BALANCED STRINGS
def balanced_strings():
for _ in range(int(stdin.readline())):
string = stdin.readline().strip()
xor = 0
A = [0]
for i in string:
xor = xor ^ (1<<(ord(i)-97))
A.append(xor)
counter = Counter(A)
yield sum(map(lambda n: n*(n-1)//2,counter.values()))
def main():
print(*balanced_strings(),sep = "\n")
if __name__ == "__main__":
main()
2. SORT NUMBERS
import java.util.*;
import java.util.Scanner;
public class ArrayListDescendingSort {
public static void main(String args[]) {
int n,t;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
ArrayList<Integer> arraylist = new ArrayList<Integer>();
for(int i=0;i<n;i++)
{
t=sc.nextInt();
arraylist.add(t);
}
CODING TEST 7
int b=0;
int flag=1;
for(int i=0; i<n; i++)
{
int a;
cin>>a;
while(a%x==0) a/=x;
while(a%y==0) a/=y;
while(a%z==0) a/=z;
if(i>1&&a!=b)
{
flag=0;
// break;
}
b=a;
}
if(flag)
cout<<"She can"<<endl;
else
cout<<"She can't"<<endl;
return 0;
}
CODING TEST 8
1. LIFE MATRIX
'''
# Sample code to perform I/O:
name = input() # Reading input from STDIN
print('Hi, %s.' % name) # Writing output to STDOUT
# Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
'''
from collections import deque
def counter(matrix,next_to_visit,list_1,N,M):
counter = 0
waiting = deque([1])
while waiting:
counter += 1
waiting = deque()
while next_to_visit:
i,j = next_to_visit.popleft()
for m,n in [(i+1,j),(i,j-1),(i,j+1),(i-1,j)]:
if 0<=m<N and 0<=n<M:
if matrix[m][n] == 1:
matrix[m][n] = 2
waiting.append((m,n))
list_1 -= 1
next_to_visit = waiting
if list_1:
return -1
return counter-1
N,M = map(int,input().split())
matrix = list()
next_to_visit = deque()
list_1 = 0
for i in range(N):
try:
matrix.append(list(map(int,input().split())))
except EOFError:
continue
for i in range(0,N):
for j in range(0,M):
try:
if matrix[i][j] == 2:
next_to_visit.append((i,j))
elif matrix[i][j] == 1:
list_1 += 1
except IndexError:
pass
try:
print(counter(matrix,next_to_visit,list_1,N,M))
except IndexError:
print(999)
2. CAPITAL OF HILLS
'''
# Sample code to perform I/O:
# Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
'''
if stack:
if stack[-1][0] == num:
count += stack[-1][1]
stack[-1][1] += 1
if len(stack)>1:
count += 1
else:
count += 1
stack.append([num,1])
else:
stack.append([num,1])
total += count
#print(stack)
print(total)
CODING TEST 9
'''
# Sample code to perform I/O:
# Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
'''
print(find(l, n))
CODING TEST 10
1. WRONG BALL
import java.util.*;
import java.lang.*;
import java.io.*;
def balanced_strings():
for _ in range(int(stdin.readline())):
string = stdin.readline().strip()
xor = 0
A = [0]
for i in string:
xor = xor ^ (1<<(ord(i)-97))
A.append(xor)
counter = Counter(A)
yield sum(map(lambda n: n*(n-1)//2,counter.values()))
def main():
print(*balanced_strings(),sep = "\n")
if __name__ == "__main__":
main()