Skip to content

Commit b51f50c

Browse files
committed
first commit
0 parents  commit b51f50c

File tree

35 files changed

+1687
-0
lines changed

35 files changed

+1687
-0
lines changed

Apartments/main.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
There are n applicants and m free apartments. Your task is to distribute the apartments so that as many applicants as possible will get an apartment.
3+
4+
Each applicant has a desired apartment size, and they will accept any apartment whose size is close enough to the desired size.
5+
6+
Input
7+
8+
The first input line has three integers n, m, and k: the number of applicants, the number of apartments, and the maximum allowed difference.
9+
10+
The next line contains n integers a1,a2,…,an: the desired apartment size of each applicant. If the desired size of an applicant is x, he or she will accept any apartment whose size is between x−k and x+k.
11+
12+
The last line contains m integers b1,b2,…,bm: the size of each apartment.
13+
14+
Output
15+
16+
Print one integer: the number of applicants who will get an apartment.
17+
18+
Constraints
19+
1≤n,m≤2⋅105
20+
0≤k≤109
21+
1≤ai,bi≤109
22+
Example
23+
24+
Input:
25+
4 3 5
26+
60 45 80 60
27+
30 60 75
28+
29+
Output:
30+
2
31+
"""
32+
33+
import collections
34+
35+
if __name__ == '__main__':
36+
people_cnt, apt, diff = list(map(int, input().split()))
37+
prefer_size = collections.deque(list(sorted(map(int, input().split()))))
38+
actual_size = list(sorted(map(int, input().split())))
39+
40+
res = 0
41+
42+
for i in range(len(actual_size)):
43+
while prefer_size and prefer_size[0] + diff < actual_size[i]:
44+
prefer_size.popleft()
45+
46+
if not prefer_size:
47+
break
48+
49+
if prefer_size[0] - diff <= actual_size[i] <= prefer_size[0] + diff:
50+
prefer_size.popleft()
51+
res += 1
52+
53+
print(res)

Array Division/main.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
You are given an array containing n positive integers.
3+
4+
Your task is to divide the array into k subarrays so that the maximum sum in a subarray is as small as possible.
5+
6+
Input
7+
8+
The first input line contains two integers n and k: the size of the array and the number of subarrays in the division.
9+
10+
The next line contains n integers x1,x2,…,xn: the contents of the array.
11+
12+
Output
13+
14+
Print one integer: the maximum sum in a subarray in the optimal division.
15+
16+
Constraints
17+
1≤n≤2⋅105
18+
1≤k≤n
19+
1≤xi≤109
20+
Example
21+
22+
Input:
23+
5 3
24+
2 4 7 3 5
25+
26+
Output:
27+
8
28+
29+
Explanation: An optimal division is [2,4],[7],[3,5] where the sums of the subarrays are 6,7,8. The largest sum is the last sum 8.
30+
"""
31+
32+
if __name__ == '__main__':
33+
n, k = list(map(int, input().split()))
34+
A = list(map(int, input().split()))
35+
36+
lo, hi = max(A), sum(A) + 1
37+
38+
while lo < hi:
39+
mid = lo + ((hi - lo) >> 1)
40+
41+
cnt_sub = 1
42+
prefix = 0
43+
44+
for i in range(len(A)):
45+
if prefix + A[i] <= mid:
46+
prefix += A[i]
47+
else:
48+
prefix = A[i]
49+
cnt_sub += 1
50+
51+
if cnt_sub <= k:
52+
hi = mid
53+
else:
54+
lo = mid + 1
55+
56+
print(lo)
57+
58+
59+
60+

Collecting Numbers II/main.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
You are given an array that contains each number between 1…n exactly once. Your task is to collect the numbers from 1 to n in increasing order.
3+
4+
On each round, you go through the array from left to right and collect as many numbers as possible.
5+
6+
Given m operations that swap two numbers in the array, your task is to report the number of rounds after each operation.
7+
8+
Input
9+
10+
The first line has two integers n and m: the array size and the number of operations.
11+
12+
The next line has n integers x1,x2,…,xn: the numbers in the array.
13+
14+
Finally, there are m lines that describe the operations. Each line has two integers a and b: the numbers at positions a and b are swapped.
15+
16+
Output
17+
18+
Print m integers: the number of rounds after each swap.
19+
20+
Constraints
21+
1≤n,m≤2⋅105
22+
1≤a,b≤n
23+
Example
24+
25+
Input:
26+
5 3
27+
4 2 1 5 3
28+
2 3
29+
1 5
30+
2 3
31+
32+
Output:
33+
2
34+
3
35+
4
36+
"""
37+

Collecting Numbers/main.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
Time limit: 1.00 s Memory limit: 512 MB
3+
You are given an array that contains each number between 1…n exactly once. Your task is to collect the numbers from 1 to n in increasing order.
4+
5+
On each round, you go through the array from left to right and collect as many numbers as possible. What will be the total number of rounds?
6+
7+
Input
8+
9+
The first line has an integer n: the array size.
10+
11+
The next line has n integers x1,x2,…,xn: the numbers in the array.
12+
13+
Output
14+
15+
Print one integer: the number of rounds.
16+
17+
Constraints
18+
1≤n≤2⋅105
19+
Example
20+
21+
Input:
22+
5
23+
4 2 1 5 3
24+
25+
Output:
26+
3
27+
"""
28+
29+
import bisect
30+
from typing import List
31+
32+
if __name__ == '__main__':
33+
n = int(input())
34+
A = list(map(int, input().split()))
35+
seen = set()
36+
res = 1
37+
38+
for x in A:
39+
if x + 1 in seen:
40+
res += 1
41+
seen.add(x)
42+
43+
print(res)

Concert Tickets/consert_tickets.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <iostream>
2+
#include <set>
3+
#include <vector>
4+
5+
using namespace std;
6+
7+
int main() {
8+
int n, m, x;
9+
cin >> n >> m;
10+
multiset<int, greater<int>> h;
11+
while (n--) {
12+
cin >> x;
13+
h.insert(x);
14+
}
15+
while (m--) {
16+
cin >> x;
17+
auto it = h.lower_bound(x);
18+
if (it == h.end()) cout << "-1\n";
19+
else {
20+
cout << *it << endl;
21+
h.erase(it);
22+
}
23+
}
24+
}

Concert Tickets/main.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import bisect
2+
3+
if __name__ == '__main__':
4+
n, m = list(map(int, input().split()))
5+
ticket_prices = list(sorted(map(int, input().split())))
6+
user_price = list(map(int, input().split()))
7+
8+
for x in user_price:
9+
if not ticket_prices:
10+
print(-1)
11+
else:
12+
idx = bisect.bisect_left(ticket_prices, x)
13+
if (idx == 0 and ticket_prices[idx] > x):
14+
print(-1)
15+
else:
16+
if idx < len(ticket_prices) and ticket_prices[idx] == x:
17+
print(ticket_prices[idx])
18+
del ticket_prices[idx]
19+
else:
20+
print(ticket_prices[idx - 1])
21+
del ticket_prices[idx - 1]

Distinct Numbers/main.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
You are given a list of n integers, and your task is to calculate the number of distinct values in the list.
3+
4+
Input
5+
6+
The first input line has an integer n: the number of values.
7+
8+
The second line has n integers x1,x2,…,xn.
9+
10+
Output
11+
12+
Print one integers: the number of distinct values.
13+
14+
Constraints
15+
1≤n≤2⋅105
16+
1≤xi≤109
17+
Example
18+
19+
Input:
20+
5
21+
2 3 2 2 3
22+
23+
Output:
24+
2
25+
"""
26+
27+
from typing import List
28+
29+
30+
def test(n: int, A: List[int]):
31+
return len(set(A))
32+
33+
34+
if __name__ == '__main__':
35+
x = input().split()
36+
y = list(map(int, input().split()))
37+
print(test(int(x[0]), y))

Factory Machines/main.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from utils.read_write_io import IOWrapper
2+
import sys
3+
4+
if __name__ == '__main__':
5+
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
6+
7+
input = lambda: list(map(int, sys.stdin.readline().rstrip("\n").split(" ")))
8+
9+
n, t = input()
10+
A = input()
11+
12+
lo, hi = 1, int(1e18)
13+
res = float('inf')
14+
15+
while lo < hi:
16+
mid = lo + ((hi - lo) >> 1)
17+
18+
cur_time = 0
19+
20+
for x in A:
21+
cur_time += mid // x
22+
if cur_time >= t:
23+
break
24+
25+
if cur_time < t:
26+
lo = mid + 1
27+
else:
28+
hi = mid
29+
30+
print(lo)

Ferris Wheel/main.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
There are n children who want to go to a Ferris wheel, and your task is to find a gondola for each child.
3+
4+
Each gondola may have one or two children in it, and in addition, the total weight in a gondola may not exceed x.
5+
6+
You know the weight of every child.
7+
8+
What is the minimum number of gondolas needed for the children?
9+
10+
Input
11+
12+
The first input line contains two integers n and x: the number of children and the maximum allowed weight.
13+
14+
The next line contains n integers p1,p2,…,pn: the weight of each child.
15+
16+
Output
17+
18+
Print one integer: the minimum number of gondolas.
19+
20+
Constraints
21+
1≤n≤2⋅105
22+
1≤x≤109
23+
1≤pi≤x
24+
Example
25+
26+
Input:
27+
4 10
28+
7 2 3 9
29+
30+
Output:
31+
3
32+
"""
33+
34+
35+
if __name__ == '__main__':
36+
n, max_weight = list(map(int, input().split()))
37+
A = list(map(int, input().split()))
38+
39+
A.sort()
40+
i, j = 0, len(A) - 1
41+
res = 0
42+
pairs = [False] * n
43+
44+
while i < j:
45+
if i != j and A[i] + A[j] <= max_weight:
46+
pairs[i] = pairs[j] = True
47+
res += 1
48+
i, j = i + 1, j - 1
49+
else:
50+
j -= 1
51+
52+
print(res + sum([1 for x in pairs if not x]))
53+
54+

0 commit comments

Comments
 (0)