description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity!
A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system.
There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly.
We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment.
Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing.
Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum.
-----Input-----
The first line of the input contains integer n (1 β€ n β€ 1000) β the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 β€ c_{i}, p_{i} β€ 1000) β the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly.
The next line contains integer k (1 β€ k β€ 1000) β the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 β€ r_{i} β€ 1000) β the maximum number of people that can sit at each table.
-----Output-----
In the first line print two integers: m, s β the number of accepted requests and the total money you get from these requests, correspondingly.
Then print m lines β each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input.
If there are multiple optimal answers, print any of them.
-----Examples-----
Input
3
10 50
2 100
5 30
3
4 6 9
Output
2 130
2 1
3 2
|
n = int(input())
x = []
for i in range(n):
c, p = map(int, input().split())
x += [(p, c, i)]
k = int(input())
r = list(map(int, input().split()))
sm = 0
ans = []
x.sort(reverse=True)
for i in range(n):
count = x[i][0]
cost = x[i][1]
uk = x[i][2]
tm1 = -1
tm2 = 100000
for j in range(k):
if cost <= r[j] < tm2:
tm1 = j
tm2 = r[j]
if tm1 > -1:
r[tm1] = 0
ans += [(uk, tm1)]
sm += count
print(len(ans), sm)
for i, j in ans:
print(i + 1, j + 1)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
|
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity!
A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system.
There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly.
We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment.
Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing.
Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum.
-----Input-----
The first line of the input contains integer n (1 β€ n β€ 1000) β the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 β€ c_{i}, p_{i} β€ 1000) β the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly.
The next line contains integer k (1 β€ k β€ 1000) β the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 β€ r_{i} β€ 1000) β the maximum number of people that can sit at each table.
-----Output-----
In the first line print two integers: m, s β the number of accepted requests and the total money you get from these requests, correspondingly.
Then print m lines β each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input.
If there are multiple optimal answers, print any of them.
-----Examples-----
Input
3
10 50
2 100
5 30
3
4 6 9
Output
2 130
2 1
3 2
|
import sys
def bin_search(c, k, rs):
l, r = 0, k
while l < r:
mid = (l + r) // 2
if c == rs[mid][0]:
return mid
if c < rs[mid][0]:
r = mid
else:
l = mid + 1
return l
def solve(n, groups, k, rs):
groups.sort(key=lambda x: (-x[2], x[1]))
seated = [(False) for _ in rs]
rs = sorted([(r, i + 1) for i, r in enumerate(rs)], key=lambda x: x[0])
res = []
total = 0
for m, c, p in groups:
index = bin_search(c, k, rs)
while 0 < index and index < k and rs[index - 1][0] == rs[index][0]:
index -= 1
while index < k and seated[index]:
index += 1
if index < k:
res.append((m, rs[index][1]))
total += p
seated[index] = True
print("{} {}".format(len(res), total))
for m, i in res:
print("{} {}".format(m, i))
def main():
n = int(input())
groups = []
for i in range(n):
c, p = map(int, input().split())
groups.append((i + 1, c, p))
k = int(input())
rs = list(map(int, input().split()))
solve(n, groups, k, rs)
main()
|
IMPORT FUNC_DEF ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR WHILE NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR
|
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity!
A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system.
There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly.
We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment.
Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing.
Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum.
-----Input-----
The first line of the input contains integer n (1 β€ n β€ 1000) β the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 β€ c_{i}, p_{i} β€ 1000) β the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly.
The next line contains integer k (1 β€ k β€ 1000) β the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 β€ r_{i} β€ 1000) β the maximum number of people that can sit at each table.
-----Output-----
In the first line print two integers: m, s β the number of accepted requests and the total money you get from these requests, correspondingly.
Then print m lines β each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input.
If there are multiple optimal answers, print any of them.
-----Examples-----
Input
3
10 50
2 100
5 30
3
4 6 9
Output
2 130
2 1
3 2
|
n = int(input())
book = [None] * n
for i in range(n):
t = [int(i) for i in input().split(" ")]
book[i] = i, t[0], t[1]
k = int(input())
cap = [(i, int(c)) for i, c in enumerate(input().split(" "))]
cap.sort(key=lambda x: x[1])
book.sort(key=lambda x: x[1])
m = 0
s = 0
r = []
cand = []
j = 0
for i in range(k):
while j < len(book) and cap[i][1] >= book[j][1]:
cand.append((book[j][0], book[j][2]))
j += 1
if len(cand) == 0:
continue
cand.sort(key=lambda x: x[1])
m += 1
s += cand[-1][1]
r.append((cand.pop()[0] + 1, cap[i][0] + 1))
print(m, s)
for i in range(m):
print(r[i][0], r[i][1])
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER
|
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity!
A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system.
There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly.
We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment.
Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing.
Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum.
-----Input-----
The first line of the input contains integer n (1 β€ n β€ 1000) β the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 β€ c_{i}, p_{i} β€ 1000) β the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly.
The next line contains integer k (1 β€ k β€ 1000) β the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 β€ r_{i} β€ 1000) β the maximum number of people that can sit at each table.
-----Output-----
In the first line print two integers: m, s β the number of accepted requests and the total money you get from these requests, correspondingly.
Then print m lines β each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input.
If there are multiple optimal answers, print any of them.
-----Examples-----
Input
3
10 50
2 100
5 30
3
4 6 9
Output
2 130
2 1
3 2
|
def findSeat(seats, cap):
for i in range(len(seats)):
if cap <= seats[i][0]:
return i
return -1
n = int(input())
visitors = []
for i in range(n):
c, p = map(int, input().split(" "))
visitors.append([c, p, i])
visitors.sort(key=lambda x: x[1], reverse=True)
k = int(input())
seats = list(map(int, input().split(" ")))
seats_new = []
for i in range(len(seats)):
seats_new.append([seats[i], i])
seats = seats_new
seats.sort(key=lambda x: x[0])
ans = 0
allotment = []
for i in range(len(visitors)):
cap, price, entry = visitors[i]
idx = findSeat(seats, cap)
if idx == -1:
continue
else:
seats[idx][0] = -1
ans += price
allotment.append([entry + 1, seats[idx][1] + 1])
print(len(allotment), ans)
for allots in allotment:
print(allots[0], allots[1])
|
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER RETURN VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
|
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity!
A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system.
There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly.
We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment.
Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing.
Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum.
-----Input-----
The first line of the input contains integer n (1 β€ n β€ 1000) β the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 β€ c_{i}, p_{i} β€ 1000) β the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly.
The next line contains integer k (1 β€ k β€ 1000) β the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 β€ r_{i} β€ 1000) β the maximum number of people that can sit at each table.
-----Output-----
In the first line print two integers: m, s β the number of accepted requests and the total money you get from these requests, correspondingly.
Then print m lines β each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input.
If there are multiple optimal answers, print any of them.
-----Examples-----
Input
3
10 50
2 100
5 30
3
4 6 9
Output
2 130
2 1
3 2
|
def main():
F = lambda: map(int, input().split())
n = int(input())
groups = [(list(F())[::-1] + [i]) for i in range(n)]
k = int(input())
tables = list(F())
tables = [[tables[i], i] for i in range(k)]
tables = sorted(tables)
groups = sorted(groups)
count = 0
SUM = 0
res = []
for i in range(n - 1, -1, -1):
for j in range(len(tables)):
if tables:
if groups[i][1] <= tables[j][0]:
res.append([groups[i][2], tables[j][1]])
count += 1
SUM += groups[i][0]
tables.pop(j)
break
else:
return [count, SUM, res]
return [count, SUM, res]
answer = main()
print(answer[0], answer[1])
for x in answer[2]:
print(x[0] + 1, x[1] + 1)
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER LIST VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN LIST VAR VAR VAR RETURN LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER
|
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity!
A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system.
There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly.
We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment.
Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing.
Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum.
-----Input-----
The first line of the input contains integer n (1 β€ n β€ 1000) β the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 β€ c_{i}, p_{i} β€ 1000) β the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly.
The next line contains integer k (1 β€ k β€ 1000) β the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 β€ r_{i} β€ 1000) β the maximum number of people that can sit at each table.
-----Output-----
In the first line print two integers: m, s β the number of accepted requests and the total money you get from these requests, correspondingly.
Then print m lines β each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input.
If there are multiple optimal answers, print any of them.
-----Examples-----
Input
3
10 50
2 100
5 30
3
4 6 9
Output
2 130
2 1
3 2
|
n = int(input())
s = []
for i in range(n):
c, p = map(int, input().split())
s.append((p, c, i))
z = s[:]
k = int(input())
m = list(map(int, input().split()))
s.sort()
s.reverse()
answ = 0
summ = 0
sisok = []
f = "lopata"
for i in range(n):
price = s[i][0]
col = s[i][1]
index = s[i][2]
zn = 100000001
uk = -1
for j in range(k):
if col <= m[j] < zn:
uk = j
zn = m[j]
if uk != -1:
m[uk] = 0
sisok.append((index + 1, uk + 1))
summ += price
print(len(sisok), summ)
for x in sisok:
print(*x)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
|
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity!
A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system.
There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly.
We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment.
Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing.
Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum.
-----Input-----
The first line of the input contains integer n (1 β€ n β€ 1000) β the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 β€ c_{i}, p_{i} β€ 1000) β the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly.
The next line contains integer k (1 β€ k β€ 1000) β the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 β€ r_{i} β€ 1000) β the maximum number of people that can sit at each table.
-----Output-----
In the first line print two integers: m, s β the number of accepted requests and the total money you get from these requests, correspondingly.
Then print m lines β each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input.
If there are multiple optimal answers, print any of them.
-----Examples-----
Input
3
10 50
2 100
5 30
3
4 6 9
Output
2 130
2 1
3 2
|
class er:
def __init__(self, ci, pi, id):
self.c = ci
self.p = pi
self.id = id
def __lt__(self, other):
return self.p < other.p
def __str__(self):
return str(self.c) + "," + str(self.p) + "," + str(self.id)
n = int(input())
a = []
for i in range(n):
ci, pi = map(int, input().split())
a.append(er(ci, pi, i + 1))
k = int(input())
t = list(map(int, input().split()))
tb = [0] * k
a.sort(reverse=True)
m, s = 0, 0
ans = []
for i in a:
ind = -1
for j in range(k):
if i.c <= t[j] and tb[j] == 0:
if ind == -1:
ind = j
elif t[j] < t[ind]:
ind = j
if ind != -1:
m += 1
s += i.p
ans.append([i.id, ind + 1])
tb[ind] = 1
print(m, s)
for i in ans:
print(i[0], i[1])
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
|
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity!
A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system.
There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly.
We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment.
Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing.
Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum.
-----Input-----
The first line of the input contains integer n (1 β€ n β€ 1000) β the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 β€ c_{i}, p_{i} β€ 1000) β the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly.
The next line contains integer k (1 β€ k β€ 1000) β the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 β€ r_{i} β€ 1000) β the maximum number of people that can sit at each table.
-----Output-----
In the first line print two integers: m, s β the number of accepted requests and the total money you get from these requests, correspondingly.
Then print m lines β each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input.
If there are multiple optimal answers, print any of them.
-----Examples-----
Input
3
10 50
2 100
5 30
3
4 6 9
Output
2 130
2 1
3 2
|
n = int(input())
arr = []
for i in range(n):
arr.append(list(map(int, input().strip().split())) + [i + 1])
k = int(input())
tables = list(map(int, input().strip().split()))
arr.sort(key=lambda x: -x[1])
ans = []
vis = [(0) for i in range(k)]
for i in arr:
mini = float("inf")
temp = None
for j in range(k):
if vis[j] == 0 and tables[j] >= i[0] and tables[j] < mini:
temp = i + [j + 1]
mini = tables[j]
if temp:
vis[temp[-1] - 1] = 1
ans.append(temp)
tot = 0
for i in ans:
tot += i[1]
print(len(ans), tot)
for i in ans:
print(*i[-2:])
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR LIST BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR LIST BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
|
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity!
A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system.
There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly.
We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment.
Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing.
Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum.
-----Input-----
The first line of the input contains integer n (1 β€ n β€ 1000) β the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 β€ c_{i}, p_{i} β€ 1000) β the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly.
The next line contains integer k (1 β€ k β€ 1000) β the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 β€ r_{i} β€ 1000) β the maximum number of people that can sit at each table.
-----Output-----
In the first line print two integers: m, s β the number of accepted requests and the total money you get from these requests, correspondingly.
Then print m lines β each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input.
If there are multiple optimal answers, print any of them.
-----Examples-----
Input
3
10 50
2 100
5 30
3
4 6 9
Output
2 130
2 1
3 2
|
from sys import stdin
input = stdin.readline
class Booking:
def __init__(self, number, people, cost):
self.number = number
self.people = people
self.cost = cost
self.table = None
def __str__(self):
return str(self.__dict__)
class Table:
def __init__(self, number, capacity):
self.number = number
self.capacity = capacity
self.occupied = False
def __str__(self):
return str(self.__dict__)
b = int(input())
requests = [[int(item) for item in input().split(" ")] for i in range(b)]
bookings = []
for i in range(b):
bookings.append(Booking(i + 1, requests[i][0], requests[i][1]))
n = int(input())
capacity = [int(item) for item in input().split(" ")]
tables = []
for i in range(n):
tables.append(Table(i + 1, capacity[i]))
st = [table for table in sorted(tables, key=lambda x: x.capacity)]
granted = 0
total = 0
for booking in sorted(bookings, key=lambda bo: -1 * bo.cost):
if granted == len(st):
break
for table in st:
if table.occupied:
continue
if table.capacity >= booking.people:
booking.table = table.number
total += booking.cost
table.occupied = True
granted += 1
break
print(granted, total)
for booking in bookings:
if booking.table:
print(booking.number, booking.table)
|
ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE FUNC_DEF RETURN FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR
|
You are on the island which can be represented as a $n \times m$ table. The rows are numbered from $1$ to $n$ and the columns are numbered from $1$ to $m$. There are $k$ treasures on the island, the $i$-th of them is located at the position $(r_i, c_i)$.
Initially you stand at the lower left corner of the island, at the position $(1, 1)$. If at any moment you are at the cell with a treasure, you can pick it up without any extra time. In one move you can move up (from $(r, c)$ to $(r+1, c)$), left (from $(r, c)$ to $(r, c-1)$), or right (from position $(r, c)$ to $(r, c+1)$). Because of the traps, you can't move down.
However, moving up is also risky. You can move up only if you are in a safe column. There are $q$ safe columns: $b_1, b_2, \ldots, b_q$. You want to collect all the treasures as fast as possible. Count the minimum number of moves required to collect all the treasures.
-----Input-----
The first line contains integers $n$, $m$, $k$ and $q$ ($2 \le n, \, m, \, k, \, q \le 2 \cdot 10^5$, $q \le m$)Β β the number of rows, the number of columns, the number of treasures in the island and the number of safe columns.
Each of the next $k$ lines contains two integers $r_i, c_i$, ($1 \le r_i \le n$, $1 \le c_i \le m$)Β β the coordinates of the cell with a treasure. All treasures are located in distinct cells.
The last line contains $q$ distinct integers $b_1, b_2, \ldots, b_q$ ($1 \le b_i \le m$) β the indices of safe columns.
-----Output-----
Print the minimum number of moves required to collect all the treasures.
-----Examples-----
Input
3 3 3 2
1 1
2 1
3 1
2 3
Output
6
Input
3 5 3 2
1 2
2 3
3 1
1 5
Output
8
Input
3 6 3 2
1 6
2 2
3 4
1 6
Output
15
-----Note-----
In the first example you should use the second column to go up, collecting in each row treasures from the first column. [Image]
In the second example, it is optimal to use the first column to go up. [Image]
In the third example, it is optimal to collect the treasure at cell $(1;6)$, go up to row $2$ at column $6$, then collect the treasure at cell $(2;2)$, go up to the top row at column $1$ and collect the last treasure at cell $(3;4)$. That's a total of $15$ moves. [Image]
|
n, m, k, q = map(int, input().split())
tr_min = [None for _ in range(n)]
tr_max = [None for _ in range(n)]
for _ in range(k):
row, col = map(int, input().split())
row = n - row
col -= 1
if tr_min[row] == None or col < tr_min[row]:
tr_min[row] = col
if tr_max[row] == None or col > tr_max[row]:
tr_max[row] = col
tr_min[-1] = 0
tr_max[-1] = tr_max[-1] or 0
savecols = sorted(map(lambda t: int(t) - 1, input().split()))
def binsearch(arr, val):
l, r = 0, len(arr) - 1
while l <= r:
mid = l + (r - l) // 2
if arr[mid] < val:
l = mid + 1
elif arr[mid] > val:
r = mid - 1
else:
return mid
return r
def find_short_descent(A, B):
if A > B:
return find_short_descent(B, A)
idx1 = binsearch(savecols, A)
idx2 = idx1 + 1
minval = m * m
if idx2 < len(savecols):
if savecols[idx2] < B:
return B - A
else:
minval = min(minval, (savecols[idx2] << 1) - A - B)
if idx1 >= 0:
minval = min(minval, A + B - (savecols[idx1] << 1))
return minval
l, r = 0, 0
found_valid = False
last_valid = None
for row in range(0, n):
if found_valid == False:
if tr_min[row] != None:
found_valid = True
last_valid = row
l = tr_max[row] - tr_min[row]
r = tr_max[row] - tr_min[row]
continue
continue
if tr_min[row] == None:
l += 1
r += 1
continue
ll = find_short_descent(tr_min[last_valid], tr_min[row])
lr = find_short_descent(tr_min[last_valid], tr_max[row])
rl = find_short_descent(tr_max[last_valid], tr_min[row])
rr = find_short_descent(tr_max[last_valid], tr_max[row])
new_l = min(l + lr, r + rr) + 1 + (tr_max[row] - tr_min[row])
new_r = min(l + ll, r + rl) + 1 + (tr_max[row] - tr_min[row])
l, r = new_l, new_r
last_valid = row
answ = l + tr_min[last_valid]
print(answ)
|
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR NONE VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NONE VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER IF VAR VAR NONE ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR NONE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
You are on the island which can be represented as a $n \times m$ table. The rows are numbered from $1$ to $n$ and the columns are numbered from $1$ to $m$. There are $k$ treasures on the island, the $i$-th of them is located at the position $(r_i, c_i)$.
Initially you stand at the lower left corner of the island, at the position $(1, 1)$. If at any moment you are at the cell with a treasure, you can pick it up without any extra time. In one move you can move up (from $(r, c)$ to $(r+1, c)$), left (from $(r, c)$ to $(r, c-1)$), or right (from position $(r, c)$ to $(r, c+1)$). Because of the traps, you can't move down.
However, moving up is also risky. You can move up only if you are in a safe column. There are $q$ safe columns: $b_1, b_2, \ldots, b_q$. You want to collect all the treasures as fast as possible. Count the minimum number of moves required to collect all the treasures.
-----Input-----
The first line contains integers $n$, $m$, $k$ and $q$ ($2 \le n, \, m, \, k, \, q \le 2 \cdot 10^5$, $q \le m$)Β β the number of rows, the number of columns, the number of treasures in the island and the number of safe columns.
Each of the next $k$ lines contains two integers $r_i, c_i$, ($1 \le r_i \le n$, $1 \le c_i \le m$)Β β the coordinates of the cell with a treasure. All treasures are located in distinct cells.
The last line contains $q$ distinct integers $b_1, b_2, \ldots, b_q$ ($1 \le b_i \le m$) β the indices of safe columns.
-----Output-----
Print the minimum number of moves required to collect all the treasures.
-----Examples-----
Input
3 3 3 2
1 1
2 1
3 1
2 3
Output
6
Input
3 5 3 2
1 2
2 3
3 1
1 5
Output
8
Input
3 6 3 2
1 6
2 2
3 4
1 6
Output
15
-----Note-----
In the first example you should use the second column to go up, collecting in each row treasures from the first column. [Image]
In the second example, it is optimal to use the first column to go up. [Image]
In the third example, it is optimal to collect the treasure at cell $(1;6)$, go up to row $2$ at column $6$, then collect the treasure at cell $(2;2)$, go up to the top row at column $1$ and collect the last treasure at cell $(3;4)$. That's a total of $15$ moves. [Image]
|
import sys
n, m, k, q = [int(i) for i in sys.stdin.readline().split()]
indata = sys.stdin.readlines()
data = []
inf = 1 << 50
for i in range(k):
data.append(tuple([int(f) for f in indata[i].split()]))
data.sort()
chutes = [int(i) for i in indata[-1].split()]
chutes.sort()
nearch = [(0, 0)] * (m + 1)
for i in range(1, chutes[0] + 1):
nearch[i] = chutes[0], chutes[0]
for i in range(q - 1):
a = chutes[i]
b = chutes[i + 1]
for j in range(a + 1, b):
nearch[j] = a, b
nearch[b] = b, b
for i in range(chutes[-1], m + 1):
nearch[i] = chutes[-1], chutes[-1]
lastr = -1
rows = []
rowdata = {}
for d in data:
if d[0] != lastr:
lastr = d[0]
rows.append(d[0])
rowdata[d[0]] = []
rowdata[d[0]].append(d[1])
dp = [[(1, 0), (1, 0)]]
start = 0
if rows[0] == 1:
dp[0] = [(rowdata[1][-1], rowdata[1][-1] - 1), (1, inf)]
start = 1
lnr = len(rows)
for i in range(start, lnr):
row = rowdata[rows[i]]
LR = 0, inf
for f in range(4):
ldp = dp[-1][f // 2]
am = ldp[1]
d = nearch[ldp[0]][f % 2]
am += abs(ldp[0] - d)
am += abs(d - row[0])
am += row[-1] - row[0]
if am < LR[1]:
LR = row[-1], am
RL = 0, inf
for f in range(4):
ldp = dp[-1][f // 2]
am = ldp[1]
d = nearch[ldp[0]][f % 2]
am += abs(ldp[0] - d)
am += abs(d - row[-1])
am += row[-1] - row[0]
if am < RL[1]:
RL = row[0], am
dp.append([LR, RL])
print(min(dp[-1][0][1], dp[-1][1][1]) + rows[-1] - 1)
|
IMPORT ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER LIST EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER LIST VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER
|
You are on the island which can be represented as a $n \times m$ table. The rows are numbered from $1$ to $n$ and the columns are numbered from $1$ to $m$. There are $k$ treasures on the island, the $i$-th of them is located at the position $(r_i, c_i)$.
Initially you stand at the lower left corner of the island, at the position $(1, 1)$. If at any moment you are at the cell with a treasure, you can pick it up without any extra time. In one move you can move up (from $(r, c)$ to $(r+1, c)$), left (from $(r, c)$ to $(r, c-1)$), or right (from position $(r, c)$ to $(r, c+1)$). Because of the traps, you can't move down.
However, moving up is also risky. You can move up only if you are in a safe column. There are $q$ safe columns: $b_1, b_2, \ldots, b_q$. You want to collect all the treasures as fast as possible. Count the minimum number of moves required to collect all the treasures.
-----Input-----
The first line contains integers $n$, $m$, $k$ and $q$ ($2 \le n, \, m, \, k, \, q \le 2 \cdot 10^5$, $q \le m$)Β β the number of rows, the number of columns, the number of treasures in the island and the number of safe columns.
Each of the next $k$ lines contains two integers $r_i, c_i$, ($1 \le r_i \le n$, $1 \le c_i \le m$)Β β the coordinates of the cell with a treasure. All treasures are located in distinct cells.
The last line contains $q$ distinct integers $b_1, b_2, \ldots, b_q$ ($1 \le b_i \le m$) β the indices of safe columns.
-----Output-----
Print the minimum number of moves required to collect all the treasures.
-----Examples-----
Input
3 3 3 2
1 1
2 1
3 1
2 3
Output
6
Input
3 5 3 2
1 2
2 3
3 1
1 5
Output
8
Input
3 6 3 2
1 6
2 2
3 4
1 6
Output
15
-----Note-----
In the first example you should use the second column to go up, collecting in each row treasures from the first column. [Image]
In the second example, it is optimal to use the first column to go up. [Image]
In the third example, it is optimal to collect the treasure at cell $(1;6)$, go up to row $2$ at column $6$, then collect the treasure at cell $(2;2)$, go up to the top row at column $1$ and collect the last treasure at cell $(3;4)$. That's a total of $15$ moves. [Image]
|
import sys
n, m, k, q = list(map(int, sys.stdin.readline().strip().split()))
T = [0] * k
for i in range(0, k):
T[i] = list(map(int, sys.stdin.readline().strip().split()))
T[i][1] = T[i][1] - 1
T[i][0] = T[i][0] - 1
T.sort()
b = list(map(int, sys.stdin.readline().strip().split()))
b.sort()
for i in range(0, q):
b[i] = b[i] - 1
L = [b[0]] * m
R = [b[-1]] * m
R1 = [10**6] * n
R2 = [-1] * n
n1 = 0
n2 = 0
for i in range(0, k):
R1[T[i][0]] = min(R1[T[i][0]], T[i][1])
R2[T[i][0]] = max(R2[T[i][0]], T[i][1])
for i in range(0, q):
L[b[i]] = b[i]
R[b[i]] = b[i]
for i in range(1, m):
L[i] = max([L[i - 1], L[i]])
R[m - i - 1] = min([R[m - i], R[m - i - 1]])
r = 0
c1 = 0
c2 = 0
n1 = 0
n2 = 0
if R2[0] != -1:
c1 = R2[0]
c2 = R2[0]
n1 = R2[0]
n2 = R2[0]
for i in range(1, n):
if R2[i] != -1:
r, c1, c2, n2, n1 = (
i,
R1[i],
R2[i],
i
- r
+ abs(R1[i] - R2[i])
+ min(
[
n1 + abs(c1 - L[c1]) + abs(R1[i] - L[c1]),
n2 + abs(c2 - L[c2]) + abs(R1[i] - L[c2]),
n1 + abs(c1 - R[c1]) + abs(R1[i] - R[c1]),
n2 + abs(c2 - R[c2]) + abs(R1[i] - R[c2]),
]
),
i
- r
+ abs(R1[i] - R2[i])
+ min(
[
n1 + abs(c1 - L[c1]) + abs(R2[i] - L[c1]),
n2 + abs(c2 - L[c2]) + abs(R2[i] - L[c2]),
n1 + abs(c1 - R[c1]) + abs(R2[i] - R[c1]),
n2 + abs(c2 - R[c2]) + abs(R2[i] - R[c2]),
]
),
)
print(min([n1, n2]))
|
IMPORT ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR LIST VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR
|
You are on the island which can be represented as a $n \times m$ table. The rows are numbered from $1$ to $n$ and the columns are numbered from $1$ to $m$. There are $k$ treasures on the island, the $i$-th of them is located at the position $(r_i, c_i)$.
Initially you stand at the lower left corner of the island, at the position $(1, 1)$. If at any moment you are at the cell with a treasure, you can pick it up without any extra time. In one move you can move up (from $(r, c)$ to $(r+1, c)$), left (from $(r, c)$ to $(r, c-1)$), or right (from position $(r, c)$ to $(r, c+1)$). Because of the traps, you can't move down.
However, moving up is also risky. You can move up only if you are in a safe column. There are $q$ safe columns: $b_1, b_2, \ldots, b_q$. You want to collect all the treasures as fast as possible. Count the minimum number of moves required to collect all the treasures.
-----Input-----
The first line contains integers $n$, $m$, $k$ and $q$ ($2 \le n, \, m, \, k, \, q \le 2 \cdot 10^5$, $q \le m$)Β β the number of rows, the number of columns, the number of treasures in the island and the number of safe columns.
Each of the next $k$ lines contains two integers $r_i, c_i$, ($1 \le r_i \le n$, $1 \le c_i \le m$)Β β the coordinates of the cell with a treasure. All treasures are located in distinct cells.
The last line contains $q$ distinct integers $b_1, b_2, \ldots, b_q$ ($1 \le b_i \le m$) β the indices of safe columns.
-----Output-----
Print the minimum number of moves required to collect all the treasures.
-----Examples-----
Input
3 3 3 2
1 1
2 1
3 1
2 3
Output
6
Input
3 5 3 2
1 2
2 3
3 1
1 5
Output
8
Input
3 6 3 2
1 6
2 2
3 4
1 6
Output
15
-----Note-----
In the first example you should use the second column to go up, collecting in each row treasures from the first column. [Image]
In the second example, it is optimal to use the first column to go up. [Image]
In the third example, it is optimal to collect the treasure at cell $(1;6)$, go up to row $2$ at column $6$, then collect the treasure at cell $(2;2)$, go up to the top row at column $1$ and collect the last treasure at cell $(3;4)$. That's a total of $15$ moves. [Image]
|
bn = 0
ar = []
def binSearchMax(number):
last = bn
first = 0
mid = (last + first) // 2
while last >= first:
if ar[mid] < number:
first = mid + 1
elif ar[mid] > number:
last = mid - 1
else:
return mid
mid = (last + first) // 2
if ar[last] == number:
return last
return first
n, m, k, q = list(map(int, input().split()))
mmn = [[m, -1] for i in range(n)]
maxn = 0
for i in range(k):
r, c = list(map(int, input().split()))
r -= 1
mmn[r][0] = min(mmn[r][0], c)
mmn[r][1] = max(mmn[r][1], c)
maxn = max(maxn, r)
if mmn[0][1] == -1:
mmn[0][1] = 1
mmn[0][0] = 1
bi = list(map(int, input().split()))
bi += [-1000000000, 10000000000, -1000000000, 10000000000, 10000000000]
bi.sort()
ar = bi
bn = q + 3
dp = [[0, 0] for i in range(n)]
dp[0][1] = mmn[0][1] - mmn[0][0]
dp[0][0] = dp[0][1] * 2
z = 0
for i in range(1, n):
if mmn[i][1] == -1:
dp[i][0] = dp[i - 1][0]
dp[i][1] = dp[i - 1][1]
continue
p1 = binSearchMax(mmn[z][0])
p2 = bi[p1 - 1]
p1 = bi[p1]
p3 = binSearchMax(mmn[z][1])
p4 = bi[p3 - 1]
p3 = bi[p3]
num1 = abs(p1 - mmn[i][1]) + abs(p1 - mmn[z][0]) + dp[z][0]
num2 = abs(p2 - mmn[i][1]) + abs(p2 - mmn[z][0]) + dp[z][0]
num3 = abs(p3 - mmn[i][1]) + abs(p3 - mmn[z][1]) + dp[z][1]
num4 = abs(p4 - mmn[i][1]) + abs(p4 - mmn[z][1]) + dp[z][1]
dp[i][0] = min(min(num1, num2), min(num3, num4)) + mmn[i][1] - mmn[i][0]
num1 = abs(p1 - mmn[i][0]) + abs(p1 - mmn[z][0]) + dp[z][0]
num2 = abs(p2 - mmn[i][0]) + abs(p2 - mmn[z][0]) + dp[z][0]
num3 = abs(p3 - mmn[i][0]) + abs(p3 - mmn[z][1]) + dp[z][1]
num4 = abs(p4 - mmn[i][0]) + abs(p4 - mmn[z][1]) + dp[z][1]
dp[i][1] = min(min(num1, num2), min(num3, num4)) + mmn[i][1] - mmn[i][0]
z = i
print(min(dp[-1]) + maxn)
|
ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR RETURN VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR
|
You are on the island which can be represented as a $n \times m$ table. The rows are numbered from $1$ to $n$ and the columns are numbered from $1$ to $m$. There are $k$ treasures on the island, the $i$-th of them is located at the position $(r_i, c_i)$.
Initially you stand at the lower left corner of the island, at the position $(1, 1)$. If at any moment you are at the cell with a treasure, you can pick it up without any extra time. In one move you can move up (from $(r, c)$ to $(r+1, c)$), left (from $(r, c)$ to $(r, c-1)$), or right (from position $(r, c)$ to $(r, c+1)$). Because of the traps, you can't move down.
However, moving up is also risky. You can move up only if you are in a safe column. There are $q$ safe columns: $b_1, b_2, \ldots, b_q$. You want to collect all the treasures as fast as possible. Count the minimum number of moves required to collect all the treasures.
-----Input-----
The first line contains integers $n$, $m$, $k$ and $q$ ($2 \le n, \, m, \, k, \, q \le 2 \cdot 10^5$, $q \le m$)Β β the number of rows, the number of columns, the number of treasures in the island and the number of safe columns.
Each of the next $k$ lines contains two integers $r_i, c_i$, ($1 \le r_i \le n$, $1 \le c_i \le m$)Β β the coordinates of the cell with a treasure. All treasures are located in distinct cells.
The last line contains $q$ distinct integers $b_1, b_2, \ldots, b_q$ ($1 \le b_i \le m$) β the indices of safe columns.
-----Output-----
Print the minimum number of moves required to collect all the treasures.
-----Examples-----
Input
3 3 3 2
1 1
2 1
3 1
2 3
Output
6
Input
3 5 3 2
1 2
2 3
3 1
1 5
Output
8
Input
3 6 3 2
1 6
2 2
3 4
1 6
Output
15
-----Note-----
In the first example you should use the second column to go up, collecting in each row treasures from the first column. [Image]
In the second example, it is optimal to use the first column to go up. [Image]
In the third example, it is optimal to collect the treasure at cell $(1;6)$, go up to row $2$ at column $6$, then collect the treasure at cell $(2;2)$, go up to the top row at column $1$ and collect the last treasure at cell $(3;4)$. That's a total of $15$ moves. [Image]
|
[n, m, k, q] = [int(w) for w in input().split()]
trs = [[(int(w) - 1) for w in input().split()] for _ in range(k)]
b = [(int(w) - 1) for w in input().split()]
b.sort()
b.append(b[-1])
br = bl = 0
er = []
el = []
for i in range(m):
if br < q and i > b[br]:
bl = br
br += 1
el.append(b[bl])
er.append(b[br])
for x in b:
el[x] = er[x] = x
g = [None for _ in range(n)]
dy = 0
for r, c in trs:
if g[r] is None:
g[r] = c, c
else:
a, b = g[r]
if c < a:
g[r] = c, b
elif c > b:
g[r] = a, c
if r > dy:
dy = r
if g[0] is not None:
_, xr = g[0]
else:
xr = 0
dxl = dxr = xl = xr
for i in range(1, n):
if g[i] is None:
continue
gl, gr = g[i]
d = gr - gl
dxr, dxl = d + min(
dxl + abs(gl - el[xl]) + abs(xl - el[xl]),
dxl + abs(gl - er[xl]) + abs(xl - er[xl]),
dxr + abs(gl - el[xr]) + abs(xr - el[xr]),
dxr + abs(gl - er[xr]) + abs(xr - er[xr]),
), d + min(
dxl + abs(gr - el[xl]) + abs(xl - el[xl]),
dxl + abs(gr - er[xl]) + abs(xl - er[xl]),
dxr + abs(gr - el[xr]) + abs(xr - el[xr]),
dxr + abs(gr - er[xr]) + abs(xr - er[xr]),
)
xl, xr = gl, gr
print(min(dxl, dxr) + dy)
|
ASSIGN LIST VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR NONE ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER NONE ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NONE ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR
|
You are on the island which can be represented as a $n \times m$ table. The rows are numbered from $1$ to $n$ and the columns are numbered from $1$ to $m$. There are $k$ treasures on the island, the $i$-th of them is located at the position $(r_i, c_i)$.
Initially you stand at the lower left corner of the island, at the position $(1, 1)$. If at any moment you are at the cell with a treasure, you can pick it up without any extra time. In one move you can move up (from $(r, c)$ to $(r+1, c)$), left (from $(r, c)$ to $(r, c-1)$), or right (from position $(r, c)$ to $(r, c+1)$). Because of the traps, you can't move down.
However, moving up is also risky. You can move up only if you are in a safe column. There are $q$ safe columns: $b_1, b_2, \ldots, b_q$. You want to collect all the treasures as fast as possible. Count the minimum number of moves required to collect all the treasures.
-----Input-----
The first line contains integers $n$, $m$, $k$ and $q$ ($2 \le n, \, m, \, k, \, q \le 2 \cdot 10^5$, $q \le m$)Β β the number of rows, the number of columns, the number of treasures in the island and the number of safe columns.
Each of the next $k$ lines contains two integers $r_i, c_i$, ($1 \le r_i \le n$, $1 \le c_i \le m$)Β β the coordinates of the cell with a treasure. All treasures are located in distinct cells.
The last line contains $q$ distinct integers $b_1, b_2, \ldots, b_q$ ($1 \le b_i \le m$) β the indices of safe columns.
-----Output-----
Print the minimum number of moves required to collect all the treasures.
-----Examples-----
Input
3 3 3 2
1 1
2 1
3 1
2 3
Output
6
Input
3 5 3 2
1 2
2 3
3 1
1 5
Output
8
Input
3 6 3 2
1 6
2 2
3 4
1 6
Output
15
-----Note-----
In the first example you should use the second column to go up, collecting in each row treasures from the first column. [Image]
In the second example, it is optimal to use the first column to go up. [Image]
In the third example, it is optimal to collect the treasure at cell $(1;6)$, go up to row $2$ at column $6$, then collect the treasure at cell $(2;2)$, go up to the top row at column $1$ and collect the last treasure at cell $(3;4)$. That's a total of $15$ moves. [Image]
|
def lsearch(arr, v):
if arr[0] > v:
return None
l = 0
r = len(arr) - 1
while r - l > 1:
m = (l + r) // 2
if arr[m] > v:
r = m
else:
l = m
if arr[r] <= v:
return arr[r]
return arr[l]
def rsearch(arr, v):
if arr[-1] < v:
return None
l = 0
r = len(arr) - 1
while r - l > 1:
m = (l + r) // 2
if arr[m] >= v:
r = m
else:
l = m
if arr[l] >= v:
return arr[l]
return arr[r]
def lcost(start, s, b):
l = lsearch(b, start)
r = rsearch(b, start)
if l is None:
l = r
if r is None:
r = l
vl = abs(start - l) + abs(s[-1] - l) + abs(s[0] - s[-1])
vr = abs(start - r) + abs(s[-1] - r) + abs(s[0] - s[-1])
return min(vl, vr)
def rcost(start, s, b):
l = lsearch(b, start)
r = rsearch(b, start)
if l is None:
l = r
if r is None:
r = l
vl = abs(start - l) + abs(s[0] - l) + abs(s[0] - s[-1])
vr = abs(start - r) + abs(s[0] - r) + abs(s[0] - s[-1])
return min(vl, vr)
n, m, k, q = map(int, input().split())
ss = [[] for _ in range(n)]
for _ in range(k):
r, c = map(int, input().split())
ss[r - 1].append(c - 1)
bs = list(map(lambda x: int(x) - 1, input().split()))
bs.sort()
for row in ss:
row.sort()
lc = 0
rc = 0
l = 0
r = 0
if ss[0]:
l = ss[0][-1]
r = ss[0][-1]
lc = rc = l
top = 0
for ind, s in enumerate(ss[1:]):
if s:
nlc = min(lcost(l, s, bs) + lc, lcost(r, s, bs) + rc)
nrc = min(rcost(l, s, bs) + lc, rcost(r, s, bs) + rc)
lc = nlc
rc = nrc
l = s[0]
r = s[-1]
top = ind + 1
res = min(lc, rc) + top
print(res)
|
FUNC_DEF IF VAR NUMBER VAR RETURN NONE ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR RETURN VAR VAR RETURN VAR VAR FUNC_DEF IF VAR NUMBER VAR RETURN NONE ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR RETURN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
You are on the island which can be represented as a $n \times m$ table. The rows are numbered from $1$ to $n$ and the columns are numbered from $1$ to $m$. There are $k$ treasures on the island, the $i$-th of them is located at the position $(r_i, c_i)$.
Initially you stand at the lower left corner of the island, at the position $(1, 1)$. If at any moment you are at the cell with a treasure, you can pick it up without any extra time. In one move you can move up (from $(r, c)$ to $(r+1, c)$), left (from $(r, c)$ to $(r, c-1)$), or right (from position $(r, c)$ to $(r, c+1)$). Because of the traps, you can't move down.
However, moving up is also risky. You can move up only if you are in a safe column. There are $q$ safe columns: $b_1, b_2, \ldots, b_q$. You want to collect all the treasures as fast as possible. Count the minimum number of moves required to collect all the treasures.
-----Input-----
The first line contains integers $n$, $m$, $k$ and $q$ ($2 \le n, \, m, \, k, \, q \le 2 \cdot 10^5$, $q \le m$)Β β the number of rows, the number of columns, the number of treasures in the island and the number of safe columns.
Each of the next $k$ lines contains two integers $r_i, c_i$, ($1 \le r_i \le n$, $1 \le c_i \le m$)Β β the coordinates of the cell with a treasure. All treasures are located in distinct cells.
The last line contains $q$ distinct integers $b_1, b_2, \ldots, b_q$ ($1 \le b_i \le m$) β the indices of safe columns.
-----Output-----
Print the minimum number of moves required to collect all the treasures.
-----Examples-----
Input
3 3 3 2
1 1
2 1
3 1
2 3
Output
6
Input
3 5 3 2
1 2
2 3
3 1
1 5
Output
8
Input
3 6 3 2
1 6
2 2
3 4
1 6
Output
15
-----Note-----
In the first example you should use the second column to go up, collecting in each row treasures from the first column. [Image]
In the second example, it is optimal to use the first column to go up. [Image]
In the third example, it is optimal to collect the treasure at cell $(1;6)$, go up to row $2$ at column $6$, then collect the treasure at cell $(2;2)$, go up to the top row at column $1$ and collect the last treasure at cell $(3;4)$. That's a total of $15$ moves. [Image]
|
def nearest(point):
l = 0
r = q
while r - l > 1:
mid = int((r + l) / 2)
if safe[mid] <= point:
l = mid
else:
r = mid
if safe[l] > point:
return [safe[l]]
elif l + 1 == q:
return [safe[l]]
else:
return [safe[l], safe[l + 1]]
def way(come, row, side=True):
between = abs(right[row] - left[row])
if side:
return abs(come - left[row]) + between
else:
return abs(come - right[row]) + between
n, m, k, q = [int(x) for x in input().split()]
right = [-1] * n
left = [-1] * n
for i in range(k):
a, b = [(int(x) - 1) for x in input().split()]
if right[a] == -1:
right[a] = left[a] = b
else:
right[a] = max(right[a], b)
left[a] = min(left[a], b)
safe = [(int(x) - 1) for x in input().split()]
safe.sort()
left[0] = right[0]
if right[0] == -1:
L = R = min(nearest(0))
right[0] = left[0] = L
else:
L = R = right[0]
while right[n - 1] == -1:
n -= 1
last = 0
for i in range(1, n):
L += 1
R += 1
if right[i] == -1:
continue
left_way = 100000000000
right_way = 100000000000
lcolumns = nearest(left[last])
rcolumns = nearest(right[last])
for c in lcolumns:
left_way = min(left_way, L + way(c, i, False) + abs(c - left[last]))
right_way = min(right_way, L + way(c, i, True) + abs(c - left[last]))
for c in rcolumns:
left_way = min(left_way, R + way(c, i, False) + abs(c - right[last]))
right_way = min(right_way, R + way(c, i, True) + abs(c - right[last]))
L = left_way
R = right_way
last = i
print(min(L, R))
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR RETURN LIST VAR VAR IF BIN_OP VAR NUMBER VAR RETURN LIST VAR VAR RETURN LIST VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length $0$ are not allowed).
Let's consider empty cells are denoted by '.', then the following figures are stars:
[Image] The leftmost figure is a star of size $1$, the middle figure is a star of size $2$ and the rightmost figure is a star of size $3$.
You are given a rectangular grid of size $n \times m$ consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from $1$ to $n$, columns are numbered from $1$ to $m$. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can't exceed $n \cdot m$. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes.
In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most $n \cdot m$ stars.
-----Input-----
The first line of the input contains two integers $n$ and $m$ ($3 \le n, m \le 1000$) β the sizes of the given grid.
The next $n$ lines contains $m$ characters each, the $i$-th line describes the $i$-th row of the grid. It is guaranteed that grid consists of characters '*' and '.' only.
-----Output-----
If it is impossible to draw the given grid using stars only, print "-1".
Otherwise in the first line print one integer $k$ ($0 \le k \le n \cdot m$) β the number of stars needed to draw the given grid. The next $k$ lines should contain three integers each β $x_j$, $y_j$ and $s_j$, where $x_j$ is the row index of the central star character, $y_j$ is the column index of the central star character and $s_j$ is the size of the star. Each star should be completely inside the grid.
-----Examples-----
Input
6 8
....*...
...**...
..*****.
...**...
....*...
........
Output
3
3 4 1
3 5 2
3 5 1
Input
5 5
.*...
****.
.****
..**.
.....
Output
3
2 2 1
3 3 1
3 4 1
Input
5 5
.*...
***..
.*...
.*...
.....
Output
-1
Input
3 3
*.*
.*.
*.*
Output
-1
-----Note-----
In the first example the output 2
3 4 1
3 5 2
is also correct.
|
def main():
n, m = map(int, input().split())
w = [(c == "*") for i in range(n) for c in input()]
nm = n * m
q = [
*[range(i, i + m) for i in range(0, nm, m)],
*[range(i, nm, m) for i in range(m)],
]
e = [1000] * nm
for f in (True, False):
for r in q:
v = 0
for i in r:
if w[i]:
v += 1
if e[i] > v:
e[i] = v
else:
v = e[i] = 0
if f:
w.reverse()
e.reverse()
e = [(c if c != 1 else 0) for c in e]
for f in (True, False):
for r in q:
v = 0
for i in r:
if v > e[i]:
v -= 1
else:
v = e[i]
if v:
w[i] = False
if f:
w.reverse()
e.reverse()
if any(w):
print(-1)
else:
r = []
for i, c in enumerate(e):
if c:
r.append(f"{i // m + 1} {i % m + 1} {c - 1}")
print(len(r), "\n".join(r), sep="\n")
main()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR FOR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR ASSIGN VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER STRING BIN_OP BIN_OP VAR VAR NUMBER STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL STRING VAR STRING EXPR FUNC_CALL VAR
|
A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length $0$ are not allowed).
Let's consider empty cells are denoted by '.', then the following figures are stars:
[Image] The leftmost figure is a star of size $1$, the middle figure is a star of size $2$ and the rightmost figure is a star of size $3$.
You are given a rectangular grid of size $n \times m$ consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from $1$ to $n$, columns are numbered from $1$ to $m$. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can't exceed $n \cdot m$. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes.
In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most $n \cdot m$ stars.
-----Input-----
The first line of the input contains two integers $n$ and $m$ ($3 \le n, m \le 1000$) β the sizes of the given grid.
The next $n$ lines contains $m$ characters each, the $i$-th line describes the $i$-th row of the grid. It is guaranteed that grid consists of characters '*' and '.' only.
-----Output-----
If it is impossible to draw the given grid using stars only, print "-1".
Otherwise in the first line print one integer $k$ ($0 \le k \le n \cdot m$) β the number of stars needed to draw the given grid. The next $k$ lines should contain three integers each β $x_j$, $y_j$ and $s_j$, where $x_j$ is the row index of the central star character, $y_j$ is the column index of the central star character and $s_j$ is the size of the star. Each star should be completely inside the grid.
-----Examples-----
Input
6 8
....*...
...**...
..*****.
...**...
....*...
........
Output
3
3 4 1
3 5 2
3 5 1
Input
5 5
.*...
****.
.****
..**.
.....
Output
3
2 2 1
3 3 1
3 4 1
Input
5 5
.*...
***..
.*...
.*...
.....
Output
-1
Input
3 3
*.*
.*.
*.*
Output
-1
-----Note-----
In the first example the output 2
3 4 1
3 5 2
is also correct.
|
import sys
n, m = list(map(int, input().split()))
s = [list(input()) for i in range(n)]
u = [[(-1) for i in range(m)] for j in range(n)]
d = [[(-1) for i in range(m)] for j in range(n)]
l = [[(-1) for i in range(m)] for j in range(n)]
r = [[(-1) for i in range(m)] for j in range(n)]
for i in range(m):
acum = 0
for j in range(n):
if s[j][i] == ".":
acum = 0
else:
acum += 1
u[j][i] = acum
for i in range(m):
acum = 0
for j in range(n - 1, -1, -1):
if s[j][i] == ".":
acum = 0
else:
acum += 1
d[j][i] = acum
for i in range(n):
acum = 0
for j in range(m):
if s[i][j] == ".":
acum = 0
else:
acum += 1
l[i][j] = acum
for i in range(n):
acum = 0
for j in range(m - 1, -1, -1):
if s[i][j] == ".":
acum = 0
else:
acum += 1
r[i][j] = acum
ans = []
t1 = [[(0) for i in range(m)] for j in range(n)]
t2 = [[(0) for i in range(m)] for j in range(n)]
for i in range(n):
for j in range(m):
d1 = min(l[i][j], r[i][j], u[i][j], d[i][j]) - 1
if d1 > 0:
ans.append([i + 1, j + 1, d1])
t1[i + d1][j] += 1
t1[i - d1][j] -= 1
t2[i][j - d1] += 1
t2[i][j + d1] -= 1
dp = [["." for i in range(m)] for j in range(n)]
for i in range(n):
acum = 0
for j in range(m):
acum += t2[i][j]
if acum != 0 or t2[i][j] != 0:
dp[i][j] = "*"
for i in range(m):
acum = 0
for j in range(n):
acum += t1[j][i]
if acum != 0 or t1[j][i] != 0:
dp[j][i] = "*"
if dp != s:
print(-1)
return
print(len(ans))
for i in ans:
print(*i)
|
IMPORT ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
|
A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length $0$ are not allowed).
Let's consider empty cells are denoted by '.', then the following figures are stars:
[Image] The leftmost figure is a star of size $1$, the middle figure is a star of size $2$ and the rightmost figure is a star of size $3$.
You are given a rectangular grid of size $n \times m$ consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from $1$ to $n$, columns are numbered from $1$ to $m$. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can't exceed $n \cdot m$. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes.
In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most $n \cdot m$ stars.
-----Input-----
The first line of the input contains two integers $n$ and $m$ ($3 \le n, m \le 1000$) β the sizes of the given grid.
The next $n$ lines contains $m$ characters each, the $i$-th line describes the $i$-th row of the grid. It is guaranteed that grid consists of characters '*' and '.' only.
-----Output-----
If it is impossible to draw the given grid using stars only, print "-1".
Otherwise in the first line print one integer $k$ ($0 \le k \le n \cdot m$) β the number of stars needed to draw the given grid. The next $k$ lines should contain three integers each β $x_j$, $y_j$ and $s_j$, where $x_j$ is the row index of the central star character, $y_j$ is the column index of the central star character and $s_j$ is the size of the star. Each star should be completely inside the grid.
-----Examples-----
Input
6 8
....*...
...**...
..*****.
...**...
....*...
........
Output
3
3 4 1
3 5 2
3 5 1
Input
5 5
.*...
****.
.****
..**.
.....
Output
3
2 2 1
3 3 1
3 4 1
Input
5 5
.*...
***..
.*...
.*...
.....
Output
-1
Input
3 3
*.*
.*.
*.*
Output
-1
-----Note-----
In the first example the output 2
3 4 1
3 5 2
is also correct.
|
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
s = [list(input().rstrip()) for _ in range(n)]
t = [([1000] * m) for _ in range(n)]
ok1 = [([0] * m) for _ in range(n)]
ok2 = [([0] * m) for _ in range(n)]
for i in range(n):
si = s[i]
c = 0
for j in range(m):
if si[j] == "*":
c += 1
else:
c = 0
t[i][j] = min(t[i][j], c)
c = 0
for j in range(m - 1, -1, -1):
if si[j] == "*":
c += 1
else:
c = 0
t[i][j] = min(t[i][j], c)
for j in range(m):
c = 0
for i in range(n):
if s[i][j] == "*":
c += 1
else:
c = 0
t[i][j] = min(t[i][j], c)
c = 0
for i in range(n - 1, -1, -1):
if s[i][j] == "*":
c += 1
else:
c = 0
t[i][j] = min(t[i][j], c)
ans = []
for i in range(n):
for j in range(m):
tij = t[i][j] - 1
if tij >= 1:
ans.append((i + 1, j + 1, tij))
ok1[max(0, i - tij)][j] += 1
if i + tij + 1 < n:
ok1[i + tij + 1][j] -= 1
ok2[i][max(0, j - tij)] += 1
if j + tij + 1 < m:
ok2[i][j + tij + 1] -= 1
for i in range(1, n):
for j in range(1, m):
ok1[i][j] += ok1[i - 1][j]
ok2[i][j] += ok2[i][j - 1]
for i in range(n):
for j in range(m):
if s[i][j] == "*":
if not (ok1[i][j] or ok2[i][j]):
ans = -1
print(ans)
exit()
k = len(ans)
print(k)
for ans0 in ans:
print(*ans0)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
|
A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length $0$ are not allowed).
Let's consider empty cells are denoted by '.', then the following figures are stars:
[Image] The leftmost figure is a star of size $1$, the middle figure is a star of size $2$ and the rightmost figure is a star of size $3$.
You are given a rectangular grid of size $n \times m$ consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from $1$ to $n$, columns are numbered from $1$ to $m$. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can't exceed $n \cdot m$. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes.
In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most $n \cdot m$ stars.
-----Input-----
The first line of the input contains two integers $n$ and $m$ ($3 \le n, m \le 1000$) β the sizes of the given grid.
The next $n$ lines contains $m$ characters each, the $i$-th line describes the $i$-th row of the grid. It is guaranteed that grid consists of characters '*' and '.' only.
-----Output-----
If it is impossible to draw the given grid using stars only, print "-1".
Otherwise in the first line print one integer $k$ ($0 \le k \le n \cdot m$) β the number of stars needed to draw the given grid. The next $k$ lines should contain three integers each β $x_j$, $y_j$ and $s_j$, where $x_j$ is the row index of the central star character, $y_j$ is the column index of the central star character and $s_j$ is the size of the star. Each star should be completely inside the grid.
-----Examples-----
Input
6 8
....*...
...**...
..*****.
...**...
....*...
........
Output
3
3 4 1
3 5 2
3 5 1
Input
5 5
.*...
****.
.****
..**.
.....
Output
3
2 2 1
3 3 1
3 4 1
Input
5 5
.*...
***..
.*...
.*...
.....
Output
-1
Input
3 3
*.*
.*.
*.*
Output
-1
-----Note-----
In the first example the output 2
3 4 1
3 5 2
is also correct.
|
from itertools import accumulate
def main():
n, m = map(int, input().split())
S = [list(str(input())) for _ in range(n)]
L = [([0] * m) for _ in range(n)]
R = [([0] * m) for _ in range(n)]
U = [([0] * m) for _ in range(n)]
D = [([0] * m) for _ in range(n)]
for i in range(n):
cnt = 0
for j in range(m):
if S[i][j] == ".":
cnt = 0
else:
cnt += 1
L[i][j] = cnt
cnt = 0
for j in reversed(range(m)):
if S[i][j] == ".":
cnt = 0
else:
cnt += 1
R[i][j] = cnt
for j in range(m):
cnt = 0
for i in range(n):
if S[i][j] == ".":
cnt = 0
else:
cnt += 1
U[i][j] = cnt
cnt = 0
for i in reversed(range(n)):
if S[i][j] == ".":
cnt = 0
else:
cnt += 1
D[i][j] = cnt
imosH = [([0] * (m + 1)) for _ in range(n)]
imosV = [([0] * m) for _ in range(n + 1)]
ans = []
for i in range(1, n - 1):
for j in range(1, m - 1):
if S[i][j] == ".":
continue
l = L[i][j] - 1
r = R[i][j] - 1
u = U[i][j] - 1
d = D[i][j] - 1
s = min([l, r, u, d])
if s == 0:
continue
ans.append((i + 1, j + 1, s))
imosV[i - s][j] += 1
imosV[i + s + 1][j] -= 1
imosH[i][j - s] += 1
imosH[i][j + s + 1] -= 1
from itertools import accumulate
for i in range(n):
imosH[i] = list(accumulate(imosH[i]))
for j in range(m):
for i in range(1, n + 1):
imosV[i][j] += imosV[i - 1][j]
for i in range(n):
for j in range(m):
if S[i][j] == "*":
if imosH[i][j] <= 0 and imosV[i][j] <= 0:
print(-1)
exit()
else:
print(len(ans))
for i in range(len(ans)):
print(*ans[i])
main()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
|
A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length $0$ are not allowed).
Let's consider empty cells are denoted by '.', then the following figures are stars:
[Image] The leftmost figure is a star of size $1$, the middle figure is a star of size $2$ and the rightmost figure is a star of size $3$.
You are given a rectangular grid of size $n \times m$ consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from $1$ to $n$, columns are numbered from $1$ to $m$. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can't exceed $n \cdot m$. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes.
In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most $n \cdot m$ stars.
-----Input-----
The first line of the input contains two integers $n$ and $m$ ($3 \le n, m \le 1000$) β the sizes of the given grid.
The next $n$ lines contains $m$ characters each, the $i$-th line describes the $i$-th row of the grid. It is guaranteed that grid consists of characters '*' and '.' only.
-----Output-----
If it is impossible to draw the given grid using stars only, print "-1".
Otherwise in the first line print one integer $k$ ($0 \le k \le n \cdot m$) β the number of stars needed to draw the given grid. The next $k$ lines should contain three integers each β $x_j$, $y_j$ and $s_j$, where $x_j$ is the row index of the central star character, $y_j$ is the column index of the central star character and $s_j$ is the size of the star. Each star should be completely inside the grid.
-----Examples-----
Input
6 8
....*...
...**...
..*****.
...**...
....*...
........
Output
3
3 4 1
3 5 2
3 5 1
Input
5 5
.*...
****.
.****
..**.
.....
Output
3
2 2 1
3 3 1
3 4 1
Input
5 5
.*...
***..
.*...
.*...
.....
Output
-1
Input
3 3
*.*
.*.
*.*
Output
-1
-----Note-----
In the first example the output 2
3 4 1
3 5 2
is also correct.
|
n, m = list(map(int, input().split()))
c = []
for j in range(n):
d = []
s = input()
for i in s:
d.append(i)
c.append(d)
a = []
b = []
e = []
g = []
for j in range(n):
k = [0] * m
e.append(k)
for j in range(n):
k = [0] * m
g.append(k)
dpu = []
for j in range(n):
k = [0] * m
dpu.append(k)
dpd = []
for j in range(n):
k = [0] * m
dpd.append(k)
dpl = []
for j in range(n):
k = [0] * m
dpl.append(k)
dpr = []
for j in range(n):
k = [0] * m
dpr.append(k)
for i in range(n):
for j in range(m):
if c[i][j] == "*":
if i > 0:
dpu[i][j] += dpu[i - 1][j] + 1
else:
dpu[i][j] = 1
if j > 0:
dpl[i][j] = dpl[i][j - 1] + 1
else:
dpl[i][j] = 1
i = n - 1
while i >= 0:
j = m - 1
while j >= 0:
if c[i][j] == "*":
if i < n - 1:
dpd[i][j] += dpd[i + 1][j] + 1
else:
dpd[i][j] = 1
if j < m - 1:
dpr[i][j] = dpr[i][j + 1] + 1
else:
dpr[i][j] = 1
j += -1
i += -1
for i in range(1, n - 1):
for j in range(1, m - 1):
if c[i][j] == "*":
k = min(dpd[i][j] - 1, dpu[i][j] - 1, dpr[i][j] - 1, dpl[i][j] - 1)
if k == 0:
pass
elif k > 0:
a.append([i + 1, j + 1, k])
e[i - k][j] += 1
if i + k < n - 1:
e[i + k + 1][j] += -1
g[i][j - k] += 1
if j + k < m - 1:
g[i][j + k + 1] += -1
for i in range(m):
for j in range(1, n):
if c[j - 1][i] == "*":
e[j][i] += e[j - 1][i]
for i in range(n):
for j in range(1, m):
if c[i][j - 1] == "*":
g[i][j] += g[i][j - 1]
f = 0
for i in range(n):
for j in range(m):
if c[i][j] == "*" and e[i][j] <= 0 and g[i][j] <= 0:
f = 1
break
if f == 1:
print(-1)
else:
print(len(a))
for j in a:
print(*j)
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING IF VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR STRING IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR STRING VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER STRING VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
|
There is one apple tree in Arkady's garden. It can be represented as a set of junctions connected with branches so that there is only one way to reach any junctions from any other one using branches. The junctions are enumerated from $1$ to $n$, the junction $1$ is called the root.
A subtree of a junction $v$ is a set of junctions $u$ such that the path from $u$ to the root must pass through $v$. Note that $v$ itself is included in a subtree of $v$.
A leaf is such a junction that its subtree contains exactly one junction.
The New Year is coming, so Arkady wants to decorate the tree. He will put a light bulb of some color on each leaf junction and then count the number happy junctions. A happy junction is such a junction $t$ that all light bulbs in the subtree of $t$ have different colors.
Arkady is interested in the following question: for each $k$ from $1$ to $n$, what is the minimum number of different colors needed to make the number of happy junctions be greater than or equal to $k$?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of junctions in the tree.
The second line contains $n - 1$ integers $p_2$, $p_3$, ..., $p_n$ ($1 \le p_i < i$), where $p_i$ means there is a branch between junctions $i$ and $p_i$. It is guaranteed that this set of branches forms a tree.
-----Output-----
Output $n$ integers. The $i$-th of them should be the minimum number of colors needed to make the number of happy junctions be at least $i$.
-----Examples-----
Input
3
1 1
Output
1 1 2
Input
5
1 1 3 3
Output
1 1 1 2 3
-----Note-----
In the first example for $k = 1$ and $k = 2$ we can use only one color: the junctions $2$ and $3$ will be happy. For $k = 3$ you have to put the bulbs of different colors to make all the junctions happy.
In the second example for $k = 4$ you can, for example, put the bulbs of color $1$ in junctions $2$ and $4$, and a bulb of color $2$ into junction $5$. The happy junctions are the ones with indices $2$, $3$, $4$ and $5$ then.
|
n = int(input())
p = [-1] + list(map(int, input().split()))
g = [[] for i in range(n)]
for i in range(n - 1):
g[i + 1].append(p[i + 1] - 1)
g[p[i + 1] - 1].append(i + 1)
cnt = [0] * n
stack = [(-1, 0)]
stack2 = [(-1, 0)]
while stack:
par, ver = stack.pop()
for to in g[ver]:
if par != to:
stack.append((ver, to))
stack2.append((ver, to))
while stack2:
par, ver = stack2.pop()
if len(g[ver]) == 1 and ver != 0:
cnt[ver] = 1
continue
s = 0
for to in g[ver]:
if par == to:
continue
s += cnt[to]
cnt[ver] = s
if n == 1:
print(1)
else:
print(*sorted(cnt))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
There is one apple tree in Arkady's garden. It can be represented as a set of junctions connected with branches so that there is only one way to reach any junctions from any other one using branches. The junctions are enumerated from $1$ to $n$, the junction $1$ is called the root.
A subtree of a junction $v$ is a set of junctions $u$ such that the path from $u$ to the root must pass through $v$. Note that $v$ itself is included in a subtree of $v$.
A leaf is such a junction that its subtree contains exactly one junction.
The New Year is coming, so Arkady wants to decorate the tree. He will put a light bulb of some color on each leaf junction and then count the number happy junctions. A happy junction is such a junction $t$ that all light bulbs in the subtree of $t$ have different colors.
Arkady is interested in the following question: for each $k$ from $1$ to $n$, what is the minimum number of different colors needed to make the number of happy junctions be greater than or equal to $k$?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of junctions in the tree.
The second line contains $n - 1$ integers $p_2$, $p_3$, ..., $p_n$ ($1 \le p_i < i$), where $p_i$ means there is a branch between junctions $i$ and $p_i$. It is guaranteed that this set of branches forms a tree.
-----Output-----
Output $n$ integers. The $i$-th of them should be the minimum number of colors needed to make the number of happy junctions be at least $i$.
-----Examples-----
Input
3
1 1
Output
1 1 2
Input
5
1 1 3 3
Output
1 1 1 2 3
-----Note-----
In the first example for $k = 1$ and $k = 2$ we can use only one color: the junctions $2$ and $3$ will be happy. For $k = 3$ you have to put the bulbs of different colors to make all the junctions happy.
In the second example for $k = 4$ you can, for example, put the bulbs of color $1$ in junctions $2$ and $4$, and a bulb of color $2$ into junction $5$. The happy junctions are the ones with indices $2$, $3$, $4$ and $5$ then.
|
class DFS:
def __init__(self):
self.G = []
self.leave_tree = []
def take_input(self):
k = int(input())
if k > 1:
graph = [(int(node) - 1) for node in input().split(" ")]
self.G = [[] for _ in range(len(graph) + 1)]
for i in range(len(graph)):
self.G[graph[i]].append(i + 1)
self.visited = [0] * k
self.leave_tree = [0] * k
for i in range(k - 1, -1, -1):
if len(self.G[i]) == 0:
self.leave_tree[i] = 1
else:
for j in self.G[i]:
self.leave_tree[i] += self.leave_tree[j]
self.leave_tree.sort()
print(*self.leave_tree)
else:
print(k)
x = DFS()
x.take_input()
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
There is one apple tree in Arkady's garden. It can be represented as a set of junctions connected with branches so that there is only one way to reach any junctions from any other one using branches. The junctions are enumerated from $1$ to $n$, the junction $1$ is called the root.
A subtree of a junction $v$ is a set of junctions $u$ such that the path from $u$ to the root must pass through $v$. Note that $v$ itself is included in a subtree of $v$.
A leaf is such a junction that its subtree contains exactly one junction.
The New Year is coming, so Arkady wants to decorate the tree. He will put a light bulb of some color on each leaf junction and then count the number happy junctions. A happy junction is such a junction $t$ that all light bulbs in the subtree of $t$ have different colors.
Arkady is interested in the following question: for each $k$ from $1$ to $n$, what is the minimum number of different colors needed to make the number of happy junctions be greater than or equal to $k$?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of junctions in the tree.
The second line contains $n - 1$ integers $p_2$, $p_3$, ..., $p_n$ ($1 \le p_i < i$), where $p_i$ means there is a branch between junctions $i$ and $p_i$. It is guaranteed that this set of branches forms a tree.
-----Output-----
Output $n$ integers. The $i$-th of them should be the minimum number of colors needed to make the number of happy junctions be at least $i$.
-----Examples-----
Input
3
1 1
Output
1 1 2
Input
5
1 1 3 3
Output
1 1 1 2 3
-----Note-----
In the first example for $k = 1$ and $k = 2$ we can use only one color: the junctions $2$ and $3$ will be happy. For $k = 3$ you have to put the bulbs of different colors to make all the junctions happy.
In the second example for $k = 4$ you can, for example, put the bulbs of color $1$ in junctions $2$ and $4$, and a bulb of color $2$ into junction $5$. The happy junctions are the ones with indices $2$, $3$, $4$ and $5$ then.
|
n = int(input())
a = input().split()
A = [int(x) for x in a]
B = [[x, []] for x in range(1, n + 1)]
i = 0
for i in range(len(A)):
B[A[i] - 1][1].append(i + 2)
def merge(arr, l, m, r):
n1 = m - l + 1
n2 = r - m
L = [0] * n1
R = [0] * n2
for i in range(0, n1):
L[i] = arr[l + i]
for j in range(0, n2):
R[j] = arr[m + 1 + j]
i = 0
j = 0
k = l
while i < n1 and j < n2:
if L[i] <= R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
while i < n1:
arr[k] = L[i]
i += 1
k += 1
while j < n2:
arr[k] = R[j]
j += 1
k += 1
def mergeSort(arr, l, r):
if l < r:
m = (l + (r - 1)) // 2
mergeSort(arr, l, m)
mergeSort(arr, m + 1, r)
merge(arr, l, m, r)
for j in B:
mergeSort(j[1], 0, len(j[1]) - 1)
for i in range(len(B)):
if B[n - i - 1][1] == []:
B[n - i - 1][1] = 1
else:
ans = 0
for j in B[n - i - 1][1]:
ans += B[j - 1][1]
B[n - i - 1][1] = ans
C = [(0) for x in range(0, n + 1)]
for i in B:
C[i[1]] += 1
for i in range(len(C)):
for j in range(C[i]):
print(i, end=" ")
print("")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST VAR LIST VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER LIST ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
|
There is one apple tree in Arkady's garden. It can be represented as a set of junctions connected with branches so that there is only one way to reach any junctions from any other one using branches. The junctions are enumerated from $1$ to $n$, the junction $1$ is called the root.
A subtree of a junction $v$ is a set of junctions $u$ such that the path from $u$ to the root must pass through $v$. Note that $v$ itself is included in a subtree of $v$.
A leaf is such a junction that its subtree contains exactly one junction.
The New Year is coming, so Arkady wants to decorate the tree. He will put a light bulb of some color on each leaf junction and then count the number happy junctions. A happy junction is such a junction $t$ that all light bulbs in the subtree of $t$ have different colors.
Arkady is interested in the following question: for each $k$ from $1$ to $n$, what is the minimum number of different colors needed to make the number of happy junctions be greater than or equal to $k$?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of junctions in the tree.
The second line contains $n - 1$ integers $p_2$, $p_3$, ..., $p_n$ ($1 \le p_i < i$), where $p_i$ means there is a branch between junctions $i$ and $p_i$. It is guaranteed that this set of branches forms a tree.
-----Output-----
Output $n$ integers. The $i$-th of them should be the minimum number of colors needed to make the number of happy junctions be at least $i$.
-----Examples-----
Input
3
1 1
Output
1 1 2
Input
5
1 1 3 3
Output
1 1 1 2 3
-----Note-----
In the first example for $k = 1$ and $k = 2$ we can use only one color: the junctions $2$ and $3$ will be happy. For $k = 3$ you have to put the bulbs of different colors to make all the junctions happy.
In the second example for $k = 4$ you can, for example, put the bulbs of color $1$ in junctions $2$ and $4$, and a bulb of color $2$ into junction $5$. The happy junctions are the ones with indices $2$, $3$, $4$ and $5$ then.
|
n = int(input())
p = list(map(int, input().split()))
gr = [[] for i in range(n)]
for i in range(n - 1):
gr[p[i] - 1].append(i + 1)
q = [0]
after = []
i = 0
s = [(0) for i in range(n)]
used = set()
used.add(0)
while q:
cur = q.pop()
after.append(cur)
for el in gr[cur]:
if el not in used:
used.add(el)
q.append(el)
i += 1
q = after
for j in range(i, -1, -1):
if len(gr[q[j]]) == 0:
s[q[j]] = 1
else:
ans = 0
for c in gr[q[j]]:
ans += s[c]
s[q[j]] = ans
s.sort()
print(" ".join(list(map(str, s))))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
There is one apple tree in Arkady's garden. It can be represented as a set of junctions connected with branches so that there is only one way to reach any junctions from any other one using branches. The junctions are enumerated from $1$ to $n$, the junction $1$ is called the root.
A subtree of a junction $v$ is a set of junctions $u$ such that the path from $u$ to the root must pass through $v$. Note that $v$ itself is included in a subtree of $v$.
A leaf is such a junction that its subtree contains exactly one junction.
The New Year is coming, so Arkady wants to decorate the tree. He will put a light bulb of some color on each leaf junction and then count the number happy junctions. A happy junction is such a junction $t$ that all light bulbs in the subtree of $t$ have different colors.
Arkady is interested in the following question: for each $k$ from $1$ to $n$, what is the minimum number of different colors needed to make the number of happy junctions be greater than or equal to $k$?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of junctions in the tree.
The second line contains $n - 1$ integers $p_2$, $p_3$, ..., $p_n$ ($1 \le p_i < i$), where $p_i$ means there is a branch between junctions $i$ and $p_i$. It is guaranteed that this set of branches forms a tree.
-----Output-----
Output $n$ integers. The $i$-th of them should be the minimum number of colors needed to make the number of happy junctions be at least $i$.
-----Examples-----
Input
3
1 1
Output
1 1 2
Input
5
1 1 3 3
Output
1 1 1 2 3
-----Note-----
In the first example for $k = 1$ and $k = 2$ we can use only one color: the junctions $2$ and $3$ will be happy. For $k = 3$ you have to put the bulbs of different colors to make all the junctions happy.
In the second example for $k = 4$ you can, for example, put the bulbs of color $1$ in junctions $2$ and $4$, and a bulb of color $2$ into junction $5$. The happy junctions are the ones with indices $2$, $3$, $4$ and $5$ then.
|
n = int(input())
if n == 1:
print(1)
else:
p = list(map(int, input().split()))
children = []
for i in range(n):
children.append([])
for i in range(n - 1):
children[p[i] - 1].append(i + 1)
layers = [1] + [0] * (n - 1)
layer = [0]
num = 2
bylayer = []
while len(layer) > 0:
bylayer.append(layer)
newlayer = []
for vert in layer:
for child in children[vert]:
layers[child] = num
newlayer.append(child)
layer = newlayer
num += 1
bylayer = bylayer[::-1]
count = [0] * n
for layer in bylayer:
for vert in layer:
if children[vert] == []:
count[vert] = 1
else:
count[vert] = sum(count[v] for v in children[vert])
count.sort()
out = ""
for guy in count:
out += str(guy) + " "
print(out)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FOR VAR VAR IF VAR VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
|
There is one apple tree in Arkady's garden. It can be represented as a set of junctions connected with branches so that there is only one way to reach any junctions from any other one using branches. The junctions are enumerated from $1$ to $n$, the junction $1$ is called the root.
A subtree of a junction $v$ is a set of junctions $u$ such that the path from $u$ to the root must pass through $v$. Note that $v$ itself is included in a subtree of $v$.
A leaf is such a junction that its subtree contains exactly one junction.
The New Year is coming, so Arkady wants to decorate the tree. He will put a light bulb of some color on each leaf junction and then count the number happy junctions. A happy junction is such a junction $t$ that all light bulbs in the subtree of $t$ have different colors.
Arkady is interested in the following question: for each $k$ from $1$ to $n$, what is the minimum number of different colors needed to make the number of happy junctions be greater than or equal to $k$?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of junctions in the tree.
The second line contains $n - 1$ integers $p_2$, $p_3$, ..., $p_n$ ($1 \le p_i < i$), where $p_i$ means there is a branch between junctions $i$ and $p_i$. It is guaranteed that this set of branches forms a tree.
-----Output-----
Output $n$ integers. The $i$-th of them should be the minimum number of colors needed to make the number of happy junctions be at least $i$.
-----Examples-----
Input
3
1 1
Output
1 1 2
Input
5
1 1 3 3
Output
1 1 1 2 3
-----Note-----
In the first example for $k = 1$ and $k = 2$ we can use only one color: the junctions $2$ and $3$ will be happy. For $k = 3$ you have to put the bulbs of different colors to make all the junctions happy.
In the second example for $k = 4$ you can, for example, put the bulbs of color $1$ in junctions $2$ and $4$, and a bulb of color $2$ into junction $5$. The happy junctions are the ones with indices $2$, $3$, $4$ and $5$ then.
|
n = int(input())
if n == 1:
print(1)
else:
adj = [[] for i in range(n + 10)]
s = input().split()
for i in range(2, n + 1):
pi = int(s[i - 2])
adj[i].append(pi)
adj[pi].append(i)
num = 1
curr = [1]
nextcurr = []
disco = [1]
visited = {(1): True}
while num < n:
for v in curr:
for w in adj[v]:
if w not in visited:
nextcurr.append(w)
visited[w] = True
disco.append(w)
num += 1
curr = nextcurr
nextcurr = []
nl = {}
nlvals = {}
for v in disco[::-1]:
nl[v] = max(sum(nl.get(w, 0) for w in adj[v]), 1)
nlvals[nl[v]] = nlvals.get(nl[v], 0) + 1
colors = {}
leaves = nlvals[1]
colors[1] = leaves
for c in range(2, leaves + 1):
colors[c] = colors[c - 1] + nlvals.get(c, 0)
ans = ""
j = 1
for i in range(1, n + 1):
while colors[j] < i:
j += 1
ans += str(j) + " "
print(ans.strip())
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST ASSIGN VAR LIST NUMBER ASSIGN VAR DICT NUMBER NUMBER WHILE VAR VAR FOR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR
|
There is one apple tree in Arkady's garden. It can be represented as a set of junctions connected with branches so that there is only one way to reach any junctions from any other one using branches. The junctions are enumerated from $1$ to $n$, the junction $1$ is called the root.
A subtree of a junction $v$ is a set of junctions $u$ such that the path from $u$ to the root must pass through $v$. Note that $v$ itself is included in a subtree of $v$.
A leaf is such a junction that its subtree contains exactly one junction.
The New Year is coming, so Arkady wants to decorate the tree. He will put a light bulb of some color on each leaf junction and then count the number happy junctions. A happy junction is such a junction $t$ that all light bulbs in the subtree of $t$ have different colors.
Arkady is interested in the following question: for each $k$ from $1$ to $n$, what is the minimum number of different colors needed to make the number of happy junctions be greater than or equal to $k$?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of junctions in the tree.
The second line contains $n - 1$ integers $p_2$, $p_3$, ..., $p_n$ ($1 \le p_i < i$), where $p_i$ means there is a branch between junctions $i$ and $p_i$. It is guaranteed that this set of branches forms a tree.
-----Output-----
Output $n$ integers. The $i$-th of them should be the minimum number of colors needed to make the number of happy junctions be at least $i$.
-----Examples-----
Input
3
1 1
Output
1 1 2
Input
5
1 1 3 3
Output
1 1 1 2 3
-----Note-----
In the first example for $k = 1$ and $k = 2$ we can use only one color: the junctions $2$ and $3$ will be happy. For $k = 3$ you have to put the bulbs of different colors to make all the junctions happy.
In the second example for $k = 4$ you can, for example, put the bulbs of color $1$ in junctions $2$ and $4$, and a bulb of color $2$ into junction $5$. The happy junctions are the ones with indices $2$, $3$, $4$ and $5$ then.
|
il_rozgalezien = int(input())
d = [0, 0] + [int(x) for x in input().split()]
slownik = {}
for i in range(2, il_rozgalezien + 1):
if d[i] in slownik:
slownik[d[i]].append(i)
else:
slownik[d[i]] = [i]
il_lisci = [0] * (il_rozgalezien + 1)
for i in range(il_rozgalezien, 1, -1):
if i not in slownik:
il_lisci[i] = 1
il_lisci[d[i]] += 1
else:
il_lisci[d[i]] += il_lisci[i]
il_lisci.sort()
if il_rozgalezien == 1:
print(1)
else:
print(*il_lisci[1:])
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
|
There is one apple tree in Arkady's garden. It can be represented as a set of junctions connected with branches so that there is only one way to reach any junctions from any other one using branches. The junctions are enumerated from $1$ to $n$, the junction $1$ is called the root.
A subtree of a junction $v$ is a set of junctions $u$ such that the path from $u$ to the root must pass through $v$. Note that $v$ itself is included in a subtree of $v$.
A leaf is such a junction that its subtree contains exactly one junction.
The New Year is coming, so Arkady wants to decorate the tree. He will put a light bulb of some color on each leaf junction and then count the number happy junctions. A happy junction is such a junction $t$ that all light bulbs in the subtree of $t$ have different colors.
Arkady is interested in the following question: for each $k$ from $1$ to $n$, what is the minimum number of different colors needed to make the number of happy junctions be greater than or equal to $k$?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of junctions in the tree.
The second line contains $n - 1$ integers $p_2$, $p_3$, ..., $p_n$ ($1 \le p_i < i$), where $p_i$ means there is a branch between junctions $i$ and $p_i$. It is guaranteed that this set of branches forms a tree.
-----Output-----
Output $n$ integers. The $i$-th of them should be the minimum number of colors needed to make the number of happy junctions be at least $i$.
-----Examples-----
Input
3
1 1
Output
1 1 2
Input
5
1 1 3 3
Output
1 1 1 2 3
-----Note-----
In the first example for $k = 1$ and $k = 2$ we can use only one color: the junctions $2$ and $3$ will be happy. For $k = 3$ you have to put the bulbs of different colors to make all the junctions happy.
In the second example for $k = 4$ you can, for example, put the bulbs of color $1$ in junctions $2$ and $4$, and a bulb of color $2$ into junction $5$. The happy junctions are the ones with indices $2$, $3$, $4$ and $5$ then.
|
n = int(input())
tr = {}
p = [int(s) for s in input().split()]
for i in range(n - 1):
if not tr.get(p[i] - 1):
tr[p[i] - 1] = []
tr[p[i] - 1].append(i + 1)
lc = [(-1) for i in range(n)]
def get_lc(i):
if lc[i] == -1:
if tr.get(i):
lc[i] = 0
for j in tr[i]:
lc[i] += get_lc(j)
else:
lc[i] = 1
return lc[i]
for i in range(n - 1, -1, -1):
get_lc(i)
print(*sorted(lc))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER LIST EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
There is one apple tree in Arkady's garden. It can be represented as a set of junctions connected with branches so that there is only one way to reach any junctions from any other one using branches. The junctions are enumerated from $1$ to $n$, the junction $1$ is called the root.
A subtree of a junction $v$ is a set of junctions $u$ such that the path from $u$ to the root must pass through $v$. Note that $v$ itself is included in a subtree of $v$.
A leaf is such a junction that its subtree contains exactly one junction.
The New Year is coming, so Arkady wants to decorate the tree. He will put a light bulb of some color on each leaf junction and then count the number happy junctions. A happy junction is such a junction $t$ that all light bulbs in the subtree of $t$ have different colors.
Arkady is interested in the following question: for each $k$ from $1$ to $n$, what is the minimum number of different colors needed to make the number of happy junctions be greater than or equal to $k$?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of junctions in the tree.
The second line contains $n - 1$ integers $p_2$, $p_3$, ..., $p_n$ ($1 \le p_i < i$), where $p_i$ means there is a branch between junctions $i$ and $p_i$. It is guaranteed that this set of branches forms a tree.
-----Output-----
Output $n$ integers. The $i$-th of them should be the minimum number of colors needed to make the number of happy junctions be at least $i$.
-----Examples-----
Input
3
1 1
Output
1 1 2
Input
5
1 1 3 3
Output
1 1 1 2 3
-----Note-----
In the first example for $k = 1$ and $k = 2$ we can use only one color: the junctions $2$ and $3$ will be happy. For $k = 3$ you have to put the bulbs of different colors to make all the junctions happy.
In the second example for $k = 4$ you can, for example, put the bulbs of color $1$ in junctions $2$ and $4$, and a bulb of color $2$ into junction $5$. The happy junctions are the ones with indices $2$, $3$, $4$ and $5$ then.
|
s = input().split()
n = int(s[0])
arr = list(map(int, input().split()))
children = [[] for i in range(n + 1)]
for i, j in enumerate(arr):
if 1 < i + 2 <= n:
children[j].append(i + 2)
leaves = [0] * (n + 1)
for i in range(n, 0, -1):
if not children[i]:
leaves[i] = 1
else:
leaves[i] = sum(leaves[j] for j in children[i])
print(" ".join(map(str, sorted(leaves[1:]))))
|
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER
|
There is one apple tree in Arkady's garden. It can be represented as a set of junctions connected with branches so that there is only one way to reach any junctions from any other one using branches. The junctions are enumerated from $1$ to $n$, the junction $1$ is called the root.
A subtree of a junction $v$ is a set of junctions $u$ such that the path from $u$ to the root must pass through $v$. Note that $v$ itself is included in a subtree of $v$.
A leaf is such a junction that its subtree contains exactly one junction.
The New Year is coming, so Arkady wants to decorate the tree. He will put a light bulb of some color on each leaf junction and then count the number happy junctions. A happy junction is such a junction $t$ that all light bulbs in the subtree of $t$ have different colors.
Arkady is interested in the following question: for each $k$ from $1$ to $n$, what is the minimum number of different colors needed to make the number of happy junctions be greater than or equal to $k$?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of junctions in the tree.
The second line contains $n - 1$ integers $p_2$, $p_3$, ..., $p_n$ ($1 \le p_i < i$), where $p_i$ means there is a branch between junctions $i$ and $p_i$. It is guaranteed that this set of branches forms a tree.
-----Output-----
Output $n$ integers. The $i$-th of them should be the minimum number of colors needed to make the number of happy junctions be at least $i$.
-----Examples-----
Input
3
1 1
Output
1 1 2
Input
5
1 1 3 3
Output
1 1 1 2 3
-----Note-----
In the first example for $k = 1$ and $k = 2$ we can use only one color: the junctions $2$ and $3$ will be happy. For $k = 3$ you have to put the bulbs of different colors to make all the junctions happy.
In the second example for $k = 4$ you can, for example, put the bulbs of color $1$ in junctions $2$ and $4$, and a bulb of color $2$ into junction $5$. The happy junctions are the ones with indices $2$, $3$, $4$ and $5$ then.
|
n = int(input())
fa = [0, 0] + list(map(int, input().split()))
delta = [0] * (n + 1)
suml = [0] * (n + 1)
for i in range(n, 0, -1):
if suml[i] == 0:
suml[i] = 1
delta[suml[i]] += 1
suml[fa[i]] += suml[i]
for i in range(1, n + 1):
delta[i] += delta[i - 1]
ans = 0
for i in range(1, n + 1):
while delta[ans] < i:
ans += 1
print("%d " % ans, end="")
print("\n")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR STRING EXPR FUNC_CALL VAR STRING
|
There is one apple tree in Arkady's garden. It can be represented as a set of junctions connected with branches so that there is only one way to reach any junctions from any other one using branches. The junctions are enumerated from $1$ to $n$, the junction $1$ is called the root.
A subtree of a junction $v$ is a set of junctions $u$ such that the path from $u$ to the root must pass through $v$. Note that $v$ itself is included in a subtree of $v$.
A leaf is such a junction that its subtree contains exactly one junction.
The New Year is coming, so Arkady wants to decorate the tree. He will put a light bulb of some color on each leaf junction and then count the number happy junctions. A happy junction is such a junction $t$ that all light bulbs in the subtree of $t$ have different colors.
Arkady is interested in the following question: for each $k$ from $1$ to $n$, what is the minimum number of different colors needed to make the number of happy junctions be greater than or equal to $k$?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of junctions in the tree.
The second line contains $n - 1$ integers $p_2$, $p_3$, ..., $p_n$ ($1 \le p_i < i$), where $p_i$ means there is a branch between junctions $i$ and $p_i$. It is guaranteed that this set of branches forms a tree.
-----Output-----
Output $n$ integers. The $i$-th of them should be the minimum number of colors needed to make the number of happy junctions be at least $i$.
-----Examples-----
Input
3
1 1
Output
1 1 2
Input
5
1 1 3 3
Output
1 1 1 2 3
-----Note-----
In the first example for $k = 1$ and $k = 2$ we can use only one color: the junctions $2$ and $3$ will be happy. For $k = 3$ you have to put the bulbs of different colors to make all the junctions happy.
In the second example for $k = 4$ you can, for example, put the bulbs of color $1$ in junctions $2$ and $4$, and a bulb of color $2$ into junction $5$. The happy junctions are the ones with indices $2$, $3$, $4$ and $5$ then.
|
n = int(input())
p = [0, 0] + [int(w) for w in input().split()]
d = [0] * (n + 1)
for i in range(n, 1, -1):
if d[i] == 0:
d[i] = 1
d[p[i]] += d[i]
if n == 1:
d[1] = 1
d = d[1:]
d.sort()
for i in range(n):
print(d[i], end=" ")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
|
There is one apple tree in Arkady's garden. It can be represented as a set of junctions connected with branches so that there is only one way to reach any junctions from any other one using branches. The junctions are enumerated from $1$ to $n$, the junction $1$ is called the root.
A subtree of a junction $v$ is a set of junctions $u$ such that the path from $u$ to the root must pass through $v$. Note that $v$ itself is included in a subtree of $v$.
A leaf is such a junction that its subtree contains exactly one junction.
The New Year is coming, so Arkady wants to decorate the tree. He will put a light bulb of some color on each leaf junction and then count the number happy junctions. A happy junction is such a junction $t$ that all light bulbs in the subtree of $t$ have different colors.
Arkady is interested in the following question: for each $k$ from $1$ to $n$, what is the minimum number of different colors needed to make the number of happy junctions be greater than or equal to $k$?
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of junctions in the tree.
The second line contains $n - 1$ integers $p_2$, $p_3$, ..., $p_n$ ($1 \le p_i < i$), where $p_i$ means there is a branch between junctions $i$ and $p_i$. It is guaranteed that this set of branches forms a tree.
-----Output-----
Output $n$ integers. The $i$-th of them should be the minimum number of colors needed to make the number of happy junctions be at least $i$.
-----Examples-----
Input
3
1 1
Output
1 1 2
Input
5
1 1 3 3
Output
1 1 1 2 3
-----Note-----
In the first example for $k = 1$ and $k = 2$ we can use only one color: the junctions $2$ and $3$ will be happy. For $k = 3$ you have to put the bulbs of different colors to make all the junctions happy.
In the second example for $k = 4$ you can, for example, put the bulbs of color $1$ in junctions $2$ and $4$, and a bulb of color $2$ into junction $5$. The happy junctions are the ones with indices $2$, $3$, $4$ and $5$ then.
|
N = int(input())
if N == 1:
print(1)
exit()
temp = [int(x) for x in input().split()]
temp.insert(0, 0)
temp.insert(0, 0)
visited = [0] * (N + 1)
for i in range(N, 1, -1):
if not visited[i]:
visited[i] = 1
visited[temp[i]] += visited[i]
print(*sorted(visited[1:]))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
import sys
input = sys.stdin.readline
n = int(input())
moves = []
for i in range(n):
a, b = map(int, input().split())
moves.append((b, a))
moves.sort()
user = 0
no = 0
for i in range(n):
m = moves[i]
if user < m[1]:
user = m[0]
no += 1
print(no)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
from sys import stdin
n = int(input())
lr = [list(map(int, stdin.readline().split())) for i in range(n)]
lr.sort(key=lambda x: x[1])
cnt = 1
idx = 1
l, r = lr[0]
while idx < n:
ll, rr = lr[idx]
if r < ll:
cnt += 1
r = rr
idx += 1
print(cnt)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
n = int(input())
a = []
for _ in range(n):
x, y = map(int, input().split())
a.append((x, y))
a = sorted(a, key=lambda y: (y[0], y[1] - y[0]))
prev = 0, 0
orders = 0
for order in a:
s, e = order
if s > prev[1]:
prev = order
orders += 1
elif e < prev[1]:
prev = order
print(orders)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
a = int(input())
b = []
total = 0
for i in range(a):
x, y = list(map(int, input().split()))
b.append([x, y])
b.sort(key=lambda x: x[1])
ending = 0
for i in b:
if i[0] > ending:
total += 1
ending = i[1]
print(total)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
n = int(input())
L = []
r = []
m = []
for i in range(n):
ch = input()
L = [int(i) for i in ch.split()]
l = L[0]
r = L[1]
m.append((r, l))
m.sort()
x = m[0][0]
nb = 1
for i in range(1, n):
if m[i][1] > x:
x = m[i][0]
nb += 1
print(nb)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
orders = []
for _ in range(int(input())):
l, r = map(int, input().split())
orders.append([l, r])
orders.sort()
count = 0
while len(orders) > 1:
if orders[-2][0] == orders[-1][0]:
orders.pop(-2) if orders[-2][1] > orders[-1][1] else orders.pop(-1)
elif orders[-2][1] >= orders[-1][0]:
orders.pop(-2)
else:
orders.pop(-1)
count += 1
if len(orders) == 1:
count += 1
print(count)
|
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
def __starting_point():
n = int(input())
orders = []
for _ in range(n):
a, b = list(map(int, input().split()))
orders.append((b, a))
sorders = sorted(orders)
cnt = 0
now = 1
for i in range(0, len(sorders)):
if sorders[i][1] > now or i == 0:
cnt += 1
now = sorders[i][0]
print(cnt)
__starting_point()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
def key_tri(argument):
return argument[1]
n = int(input())
L = [list(map(int, input().split())) for _ in range(n)]
L.sort(key=key_tri)
r = 1
t = L[0][1]
for k in range(1, n):
if L[k][0] > t:
r += 1
t = L[k][1]
print(r)
|
FUNC_DEF RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
def f(x):
return x[1]
n = int(input())
L = []
for i in range(n):
order = [int(s) for s in input().split()]
L.append(order)
L = sorted(L, key=f)
last = [L[0][1]]
item = 1
while item < n:
while item < n - 1 and (L[item][1] <= last[-1] or L[item][0] <= last[-1]):
item += 1
if L[item][1] > last[-1] and L[item][0] > last[-1]:
last.append(L[item][1])
item += 1
print(len(last))
|
FUNC_DEF RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
n = int(input())
l = []
for i in range(n):
s = input()
temp = s.split()
temp1 = [int(i) for i in temp]
l.append(temp1)
l = sorted(l, key=lambda x: x[1])
cnt = 1
prev = l[0][1]
for i in range(1, len(l)):
if l[i][0] > prev:
cnt = cnt + 1
prev = l[i][1]
print(cnt)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
import sys
from sys import stdin, stdout
def R():
return map(int, stdin.readline().strip().split())
arr = []
for h in range(int(stdin.readline().strip())):
arr.append(list(R()))
arr.sort(key=lambda x: x[1])
s, d = 0, 0
for i in arr:
if i[0] > d:
s += 1
d = i[1]
stdout.write(str(s))
|
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
n = int(input())
a = []
for i in range(n):
x, y = list(map(int, input().split()))
a.append((y, x))
a.sort()
cnt = 0
cur = -10
i = 0
while i < n:
if cur < a[i][1]:
cur = a[i][0]
cnt += 1
i += 1
print(cnt)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
ints = lambda: list(map(int, input().split()))
rd = lambda: input()
n = ints()[0]
A = []
for i in range(n):
l, r = ints()
A.append((r, r - l))
A.sort()
cnt, ct = 0, 0
for p in A:
if ct + p[1] > p[0]:
continue
else:
ct = p[0] + 1
cnt += 1
print(cnt)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
n = int(input())
data = []
for num in range(n):
t = input()
l, r = t.split(" ")
l = int(l)
r = int(r)
data.append((l, r))
data = sorted(data, key=lambda s: s[1])
num = 1
ci = data[0]
for item in data:
il, ir = item
cl, cr = ci
if il > cr:
ci = item
num += 1
print(num)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
n = int(input())
a = [tuple(map(int, input().split())) for _ in range(n)]
a.sort(key=lambda x: x[1])
max_r = -(10**10)
ans = 0
for l, r in a:
if l > max_r:
ans += 1
max_r = r
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
def solve(tasks):
if not tasks:
return 0
tasks.sort(key=lambda x: x[1])
curr = tasks[0][1]
count = 1
for i in range(1, len(tasks)):
if tasks[i][0] > curr:
curr = tasks[i][1]
count += 1
return count
n = int(input())
tasks = []
for _ in range(n):
task = list(map(int, input().split()))
tasks.append(task)
print(solve(tasks))
|
FUNC_DEF IF VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
n = int(input())
lis = []
for i in range(n):
a, b = map(int, input().split())
lis.append([b, a])
lis.sort()
ans = 1
l = lis[0][1]
h = lis[0][0]
for i in range(1, n):
if lis[i][1] > h:
h = lis[i][0]
ans += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
n = int(input())
end = []
while n != 0:
l, r = map(int, input().split())
end += [[l, r]]
n -= 1
def Second(i):
return i[1]
end.sort(key=Second)
count = 1
k = 1
last = end[0][1]
while k != len(end):
if end[k][0] > last:
count += 1
last = end[k][1]
k += 1
print(count)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR LIST LIST VAR VAR VAR NUMBER FUNC_DEF RETURN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
n = int(input())
q = []
for i in range(n):
l, r = [int(x) for x in input().split()]
q.append([r, l])
q.sort()
prev, ans = 0, 0
for i in range(n):
start = q[i][1]
end = q[i][0]
if start > prev:
ans += 1
prev = end
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
def main():
res = t = 0
for r, l in sorted(
tuple(map(int, reversed(input().split()))) for _ in range(int(input()))
):
if t < l:
t = r
res += 1
print(res)
def __starting_point():
main()
__starting_point()
|
FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
n = int(input())
A = [0] * n
for i in range(n):
A[i] = tuple(map(int, input().split()))
A.sort(key=lambda x: x[1])
now, res = 0, 0
for i in range(n):
if A[i][0] > now:
now = A[i][1]
res += 1
print(res)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
n = int(input())
array = []
for k in range(n):
a = list(map(int, input().split()))
array.append((a[1], a[0]))
array.sort()
c = 1
end = array[0][0]
for k in range(1, n):
if array[k][1] > end:
c += 1
end = array[k][0]
print(c)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
n = int(input())
a = []
for i in range(n):
a.append(tuple(reversed(list(map(int, input().split())))))
a.sort()
c = 0
ans = 0
for i in range(n):
if c < a[i][1]:
c = a[i][0]
ans += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
times = []
for _ in range(int(input())):
a, b = list(map(int, input().split()))
times.append([a, b])
times.sort()
times = times[-1::-1]
a, b = times[0][0], times[0][1]
cnt = 1
for i in range(1, len(times)):
cur = times[i]
start, end = cur[0], cur[1]
if start < a and end < a:
cnt += 1
a = start
b = end
print(cnt)
|
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
def snd(lst):
return lst[1]
n = int(input())
l = []
for i in range(0, n):
l.append([int(i) for i in input().split()])
l.sort(key=snd)
e = 0
ans = 0
for p in l:
if p[0] > e:
ans += 1
e = p[1]
print(ans)
|
FUNC_DEF RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
n = int(input())
l = []
for i in range(n):
a, b = list(map(int, input().split()))
l.append([a, b])
l.sort(key=lambda x: x[1])
c = t = 0
for i in range(len(l)):
if t < int(l[i][0]):
t = int(l[i][1])
c += 1
print(c)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
r = lambda: map(int, input().split())
(n,) = r()
a = sorted(tuple(r()) for _ in range(n))[::-1]
ret, left = 0, 10**9 + 1
for l, r in a:
if r < left:
ret += 1
left = l
print(ret)
|
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
n = int(input())
rg = []
for i in range(n):
a, b = map(int, input().split())
rg.append((a, b))
rg.sort()
last = rg[0][1]
ans = 1
for i in rg[1:]:
if i[0] > last:
last = i[1]
ans += 1
elif i[1] < last:
last = i[1]
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
cin = lambda: map(int, input().split())
(n,) = cin()
a = []
count = 1
for i in range(0, n):
a.append(tuple(list(cin())[::-1]))
a.sort()
ref = a[0][0]
for i in range(1, len(a)):
if a[i][1] > ref:
count += 1
ref = a[i][0]
print(count)
|
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
n = int(input())
lst = [[0, 0] for i in range(n)]
for i in range(n):
lst[i][1], lst[i][0] = map(int, input().split())
lst.sort()
last = 0
cnt = 0
for k in lst:
if k[1] > last:
last = k[0]
cnt += 1
print(cnt)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
import sys
input = sys.stdin.buffer.readline
n = int(input())
arr = []
for i in range(n):
l, r = map(int, input().split())
arr.append([l, r])
arr.sort()
count = -1
arr.append([1000000000000, 100000000000000])
mini = -1
for i in range(n + 1):
l, r = arr[i]
if l > mini:
count += 1
mini = r
continue
mini = min(mini, r)
print(count)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
n = int(input())
main = []
for i in range(n):
s, e = map(int, input().split())
main.append((s, e))
main.sort(key=lambda x: x[1])
cs, ce = main[0][0], main[0][1]
count = 1
for s, e in main[1:]:
if ce < s:
count += 1
cs = s
ce = e
print(count)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
s = d = 0
t = [list(map(int, input().split())) for i in range(int(input()))]
for l, r in sorted(t, key=lambda q: q[1]):
if l > d:
s, d = s + 1, r
print(s)
|
ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
import sys
t = int(sys.stdin.readline())
l = []
for i in range(t):
l.append(list(map(int, sys.stdin.readline().rsplit())))
lst = sorted(l, key=lambda x: x[1])
a = -1
count = 0
for i in lst:
if i[0] > a:
a = i[1]
count += 1
print(count)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
from sys import stdin
n = int(input())
a = lambda: stdin.readline().split()
lst = sorted([[*map(int, input().split())] for _ in range(n)], key=lambda x: x[1])
res, last = 0, 0
for i, (x, y) in enumerate(lst):
if last < x:
last = y
res += 1
print(res)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
import sys
k = 0
s = []
for line in sys.stdin:
if k == 0:
n = int(line)
else:
l, r = map(int, line.split())
s.append((l, r))
k += 1
s.sort(key=lambda x: x[1])
max_r = 0
r = 0
for i in s:
if i[0] > max_r:
r += 1
max_r = i[1]
print(r)
|
IMPORT ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
n = int(input())
t = [list(map(int, input().split(" "))) for i in range(n)]
last = 0
count = 0
for i in sorted(t, key=lambda x: x[1]):
if last < i[0]:
last = i[1]
count += 1
print(count)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values β the start time l_{i} and the finish time r_{i} (l_{i} β€ r_{i}).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
-----Input-----
The first line contains integer number n (1 β€ n β€ 5Β·10^5) β number of orders. The following n lines contain integer values l_{i} and r_{i} each (1 β€ l_{i} β€ r_{i} β€ 10^9).
-----Output-----
Print the maximal number of orders that can be accepted.
-----Examples-----
Input
2
7 11
4 7
Output
1
Input
5
1 2
2 3
3 4
4 5
5 6
Output
3
Input
6
4 8
1 5
4 7
2 5
1 3
6 8
Output
2
|
def main():
tt = list(tuple(map(int, input().split())) for _ in range(int(input())))
tt.sort(key=lambda e: e.__getitem__(1))
res = t = 0
for l, r in tt:
if t < l:
t = r
res += 1
print(res)
def __starting_point():
main()
__starting_point()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
There are $n$ students at your university. The programming skill of the $i$-th student is $a_i$. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has $2 \cdot 10^5$ students ready for the finals!
Each team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of $k$ students with programming skills $a[i_1], a[i_2], \dots, a[i_k]$, then the diversity of this team is $\max\limits_{j=1}^{k} a[i_j] - \min\limits_{j=1}^{k} a[i_j]$).
The total diversity is the sum of diversities of all teams formed.
Your task is to minimize the total diversity of the division of students and find the optimal way to divide the students.
-----Input-----
The first line of the input contains one integer $n$ ($3 \le n \le 2 \cdot 10^5$) β the number of students.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the programming skill of the $i$-th student.
-----Output-----
In the first line print two integers $res$ and $k$ β the minimum total diversity of the division of students and the number of teams in your division, correspondingly.
In the second line print $n$ integers $t_1, t_2, \dots, t_n$ ($1 \le t_i \le k$), where $t_i$ is the number of team to which the $i$-th student belong.
If there are multiple answers, you can print any. Note that you don't need to minimize the number of teams. Each team should consist of at least three students.
-----Examples-----
Input
5
1 1 3 4 2
Output
3 1
1 1 1 1 1
Input
6
1 5 12 13 2 15
Output
7 2
2 2 1 1 2 1
Input
10
1 2 5 129 185 581 1041 1909 1580 8150
Output
7486 3
3 3 3 2 2 2 2 1 1 1
-----Note-----
In the first example, there is only one team with skills $[1, 1, 2, 3, 4]$ so the answer is $3$. It can be shown that you cannot achieve a better answer.
In the second example, there are two teams with skills $[1, 2, 5]$ and $[12, 13, 15]$ so the answer is $4 + 3 = 7$.
In the third example, there are three teams with skills $[1, 2, 5]$, $[129, 185, 581, 1041]$ and $[1580, 1909, 8150]$ so the answer is $4 + 912 + 6570 = 7486$.
|
import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int, input().split()))
B = [(a, ind) for ind, a in enumerate(A)]
B.sort()
DP = [0] * (n + 1)
for i in range(3, n - 2):
DP[i] = max(DP[i - 1], DP[i - 3] + B[i][0] - B[i - 1][0])
MAX = max(DP)
x = DP.index(MAX)
REMLIST = []
while x > 0:
if DP[x] == DP[x - 1]:
x -= 1
else:
REMLIST.append(x)
x -= 3
REMLIST.sort()
REMLIST.append(1 << 60)
print(max(A) - min(A) - MAX, len(REMLIST))
ANS = [-1] * n
remind = 0
NOW = 1
for i in range(n):
if i == REMLIST[remind]:
NOW += 1
remind += 1
ANS[B[i][1]] = NOW
print(*ANS)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
There are $n$ students at your university. The programming skill of the $i$-th student is $a_i$. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has $2 \cdot 10^5$ students ready for the finals!
Each team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of $k$ students with programming skills $a[i_1], a[i_2], \dots, a[i_k]$, then the diversity of this team is $\max\limits_{j=1}^{k} a[i_j] - \min\limits_{j=1}^{k} a[i_j]$).
The total diversity is the sum of diversities of all teams formed.
Your task is to minimize the total diversity of the division of students and find the optimal way to divide the students.
-----Input-----
The first line of the input contains one integer $n$ ($3 \le n \le 2 \cdot 10^5$) β the number of students.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the programming skill of the $i$-th student.
-----Output-----
In the first line print two integers $res$ and $k$ β the minimum total diversity of the division of students and the number of teams in your division, correspondingly.
In the second line print $n$ integers $t_1, t_2, \dots, t_n$ ($1 \le t_i \le k$), where $t_i$ is the number of team to which the $i$-th student belong.
If there are multiple answers, you can print any. Note that you don't need to minimize the number of teams. Each team should consist of at least three students.
-----Examples-----
Input
5
1 1 3 4 2
Output
3 1
1 1 1 1 1
Input
6
1 5 12 13 2 15
Output
7 2
2 2 1 1 2 1
Input
10
1 2 5 129 185 581 1041 1909 1580 8150
Output
7486 3
3 3 3 2 2 2 2 1 1 1
-----Note-----
In the first example, there is only one team with skills $[1, 1, 2, 3, 4]$ so the answer is $3$. It can be shown that you cannot achieve a better answer.
In the second example, there are two teams with skills $[1, 2, 5]$ and $[12, 13, 15]$ so the answer is $4 + 3 = 7$.
In the third example, there are three teams with skills $[1, 2, 5]$, $[129, 185, 581, 1041]$ and $[1580, 1909, 8150]$ so the answer is $4 + 912 + 6570 = 7486$.
|
import sys
from sys import stdin
n = int(stdin.readline())
ao = list(map(int, stdin.readline().split()))
ai = [(ao[i], i) for i in range(n)]
ai.sort()
a = [ai[i][0] for i in range(n)]
dp = [([float("inf")] * 3) for i in range(n + 1)]
pp = [[0, 1, 2] for i in range(n + 1)]
dp[0][0] = 0
for i in range(n):
if dp[i + 1][1] > dp[i][0] - a[i]:
dp[i + 1][1] = dp[i][0] - a[i]
pp[i + 1][1] = 0
if dp[i + 1][2] > dp[i][1]:
dp[i + 1][2] = dp[i][1]
pp[i + 1][2] = 1
if dp[i + 1][2] > dp[i][2]:
dp[i + 1][2] = dp[i][2]
pp[i + 1][2] = 2
if dp[i + 1][0] > dp[i][2] + a[i]:
dp[i + 1][0] = dp[i][2] + a[i]
pp[i + 1][0] = 2
ansmax = dp[n][0]
ans = [None] * n
now = 1
nv = pp[n][0]
for i in range(n - 1, -1, -1):
ans[ai[i][1]] = now
if nv == 0:
now += 1
nv = pp[i][nv]
print(ansmax, max(ans))
print(*ans)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
There are $n$ students at your university. The programming skill of the $i$-th student is $a_i$. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has $2 \cdot 10^5$ students ready for the finals!
Each team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of $k$ students with programming skills $a[i_1], a[i_2], \dots, a[i_k]$, then the diversity of this team is $\max\limits_{j=1}^{k} a[i_j] - \min\limits_{j=1}^{k} a[i_j]$).
The total diversity is the sum of diversities of all teams formed.
Your task is to minimize the total diversity of the division of students and find the optimal way to divide the students.
-----Input-----
The first line of the input contains one integer $n$ ($3 \le n \le 2 \cdot 10^5$) β the number of students.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the programming skill of the $i$-th student.
-----Output-----
In the first line print two integers $res$ and $k$ β the minimum total diversity of the division of students and the number of teams in your division, correspondingly.
In the second line print $n$ integers $t_1, t_2, \dots, t_n$ ($1 \le t_i \le k$), where $t_i$ is the number of team to which the $i$-th student belong.
If there are multiple answers, you can print any. Note that you don't need to minimize the number of teams. Each team should consist of at least three students.
-----Examples-----
Input
5
1 1 3 4 2
Output
3 1
1 1 1 1 1
Input
6
1 5 12 13 2 15
Output
7 2
2 2 1 1 2 1
Input
10
1 2 5 129 185 581 1041 1909 1580 8150
Output
7486 3
3 3 3 2 2 2 2 1 1 1
-----Note-----
In the first example, there is only one team with skills $[1, 1, 2, 3, 4]$ so the answer is $3$. It can be shown that you cannot achieve a better answer.
In the second example, there are two teams with skills $[1, 2, 5]$ and $[12, 13, 15]$ so the answer is $4 + 3 = 7$.
In the third example, there are three teams with skills $[1, 2, 5]$, $[129, 185, 581, 1041]$ and $[1580, 1909, 8150]$ so the answer is $4 + 912 + 6570 = 7486$.
|
n = int(input())
a = sorted((int(v), i) for i, v in enumerate(input().split()))
INF = 10**18
dp = [-INF, -INF, 0]
for i in range(n - 1):
dp.append(max(dp[-1], dp[i] + a[i + 1][0] - a[i][0]))
cur, t = n - 1, 1
o = [0] * n
while cur >= 0:
if dp[cur] == dp[cur - 1]:
o[a[cur][1]] = t
cur -= 1
else:
o[a[cur][1]] = o[a[cur - 1][1]] = o[a[cur - 2][1]] = t
cur -= 3
t += 1
print(a[-1][0] - a[0][0] - dp[n - 1], t - 1)
print(*o)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
There are $n$ students at your university. The programming skill of the $i$-th student is $a_i$. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has $2 \cdot 10^5$ students ready for the finals!
Each team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of $k$ students with programming skills $a[i_1], a[i_2], \dots, a[i_k]$, then the diversity of this team is $\max\limits_{j=1}^{k} a[i_j] - \min\limits_{j=1}^{k} a[i_j]$).
The total diversity is the sum of diversities of all teams formed.
Your task is to minimize the total diversity of the division of students and find the optimal way to divide the students.
-----Input-----
The first line of the input contains one integer $n$ ($3 \le n \le 2 \cdot 10^5$) β the number of students.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the programming skill of the $i$-th student.
-----Output-----
In the first line print two integers $res$ and $k$ β the minimum total diversity of the division of students and the number of teams in your division, correspondingly.
In the second line print $n$ integers $t_1, t_2, \dots, t_n$ ($1 \le t_i \le k$), where $t_i$ is the number of team to which the $i$-th student belong.
If there are multiple answers, you can print any. Note that you don't need to minimize the number of teams. Each team should consist of at least three students.
-----Examples-----
Input
5
1 1 3 4 2
Output
3 1
1 1 1 1 1
Input
6
1 5 12 13 2 15
Output
7 2
2 2 1 1 2 1
Input
10
1 2 5 129 185 581 1041 1909 1580 8150
Output
7486 3
3 3 3 2 2 2 2 1 1 1
-----Note-----
In the first example, there is only one team with skills $[1, 1, 2, 3, 4]$ so the answer is $3$. It can be shown that you cannot achieve a better answer.
In the second example, there are two teams with skills $[1, 2, 5]$ and $[12, 13, 15]$ so the answer is $4 + 3 = 7$.
In the third example, there are three teams with skills $[1, 2, 5]$, $[129, 185, 581, 1041]$ and $[1580, 1909, 8150]$ so the answer is $4 + 912 + 6570 = 7486$.
|
def solve_ee(ls):
dp = [float("inf")] * (len(ls) + 1)
dp[0] = 0
p = [0] * (len(ls) + 1)
for i in range(len(ls)):
for j in range(3, len(ls)):
if j <= 5 and i + j <= len(ls):
diff = ls[i + j - 1][0] - ls[i][0]
if dp[i + j] > dp[i] + diff:
p[i + j] = i
dp[i + j] = dp[i] + diff
else:
break
return dp, p
n = int(input())
arr = list(map(int, input().split()))
ls = [(v, i) for i, v in enumerate(arr)]
ls.sort()
t, p = solve_ee(ls)
cur = len(ls)
cnt = 0
team = [0] * 2 * len(ls)
while cur != 0:
for i in range(cur - 1, p[cur] - 1, -1):
team[ls[i][1]] = cnt
cnt += 1
cur = p[cur]
if len(ls) <= 5:
print(max(arr) - min(arr), 1)
else:
print(t[len(ls)], cnt)
for i in range(len(ls)):
print(team[i] + 1, end=" ")
|
FUNC_DEF ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER NUMBER FUNC_CALL VAR VAR WHILE VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING
|
There are $n$ students at your university. The programming skill of the $i$-th student is $a_i$. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has $2 \cdot 10^5$ students ready for the finals!
Each team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of $k$ students with programming skills $a[i_1], a[i_2], \dots, a[i_k]$, then the diversity of this team is $\max\limits_{j=1}^{k} a[i_j] - \min\limits_{j=1}^{k} a[i_j]$).
The total diversity is the sum of diversities of all teams formed.
Your task is to minimize the total diversity of the division of students and find the optimal way to divide the students.
-----Input-----
The first line of the input contains one integer $n$ ($3 \le n \le 2 \cdot 10^5$) β the number of students.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the programming skill of the $i$-th student.
-----Output-----
In the first line print two integers $res$ and $k$ β the minimum total diversity of the division of students and the number of teams in your division, correspondingly.
In the second line print $n$ integers $t_1, t_2, \dots, t_n$ ($1 \le t_i \le k$), where $t_i$ is the number of team to which the $i$-th student belong.
If there are multiple answers, you can print any. Note that you don't need to minimize the number of teams. Each team should consist of at least three students.
-----Examples-----
Input
5
1 1 3 4 2
Output
3 1
1 1 1 1 1
Input
6
1 5 12 13 2 15
Output
7 2
2 2 1 1 2 1
Input
10
1 2 5 129 185 581 1041 1909 1580 8150
Output
7486 3
3 3 3 2 2 2 2 1 1 1
-----Note-----
In the first example, there is only one team with skills $[1, 1, 2, 3, 4]$ so the answer is $3$. It can be shown that you cannot achieve a better answer.
In the second example, there are two teams with skills $[1, 2, 5]$ and $[12, 13, 15]$ so the answer is $4 + 3 = 7$.
In the third example, there are three teams with skills $[1, 2, 5]$, $[129, 185, 581, 1041]$ and $[1580, 1909, 8150]$ so the answer is $4 + 912 + 6570 = 7486$.
|
n = int(input())
pre = [0] * (n + 1)
dp = [10**9] * (n + 1)
oa = [0] + list(map(int, input().split()))
a = sorted(oa)
ai = {}
for i in range(1, n + 1):
ai.setdefault(oa[i], []).append(i)
mapping = {}
for i in range(1, n + 1):
mapping[i] = ai[a[i]].pop()
dp[0] = 0
for i in range(3, n + 1):
for j in range(3, min(6, i + 1)):
newval = a[i] - a[i - j + 1] + dp[i - j]
if newval < dp[i]:
dp[i] = newval
pre[i] = j
ans = [0] * (n + 1)
cur = n
teami = 1
while cur:
for i in range(pre[cur]):
ans[mapping[cur - i]] = teami
teami += 1
cur -= pre[cur]
print(dp[n], teami - 1)
print(*ans[1:])
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL FUNC_CALL VAR VAR VAR LIST VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
|
There are $n$ students at your university. The programming skill of the $i$-th student is $a_i$. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has $2 \cdot 10^5$ students ready for the finals!
Each team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of $k$ students with programming skills $a[i_1], a[i_2], \dots, a[i_k]$, then the diversity of this team is $\max\limits_{j=1}^{k} a[i_j] - \min\limits_{j=1}^{k} a[i_j]$).
The total diversity is the sum of diversities of all teams formed.
Your task is to minimize the total diversity of the division of students and find the optimal way to divide the students.
-----Input-----
The first line of the input contains one integer $n$ ($3 \le n \le 2 \cdot 10^5$) β the number of students.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the programming skill of the $i$-th student.
-----Output-----
In the first line print two integers $res$ and $k$ β the minimum total diversity of the division of students and the number of teams in your division, correspondingly.
In the second line print $n$ integers $t_1, t_2, \dots, t_n$ ($1 \le t_i \le k$), where $t_i$ is the number of team to which the $i$-th student belong.
If there are multiple answers, you can print any. Note that you don't need to minimize the number of teams. Each team should consist of at least three students.
-----Examples-----
Input
5
1 1 3 4 2
Output
3 1
1 1 1 1 1
Input
6
1 5 12 13 2 15
Output
7 2
2 2 1 1 2 1
Input
10
1 2 5 129 185 581 1041 1909 1580 8150
Output
7486 3
3 3 3 2 2 2 2 1 1 1
-----Note-----
In the first example, there is only one team with skills $[1, 1, 2, 3, 4]$ so the answer is $3$. It can be shown that you cannot achieve a better answer.
In the second example, there are two teams with skills $[1, 2, 5]$ and $[12, 13, 15]$ so the answer is $4 + 3 = 7$.
In the third example, there are three teams with skills $[1, 2, 5]$, $[129, 185, 581, 1041]$ and $[1580, 1909, 8150]$ so the answer is $4 + 912 + 6570 = 7486$.
|
import sys
reader = (line.rstrip() for line in sys.stdin)
input = reader.__next__
class SegmTree:
def __init__(self, size):
N = 1
while N < size:
N <<= 1
self.N = N
self.tree = [(float("inf"), -1)] * (2 * self.N)
def build(self):
for i in range(self.N - 1, 0, -1):
self.tree[i] = min(self.tree[i << 1], self.tree[i << 1 | 1])
def modify(self, i, value):
i += self.N
self.tree[i] = value, i - self.N
while i > 1:
self.tree[i >> 1] = min(self.tree[i], self.tree[i ^ 1])
i >>= 1
def query_range(self, l, r):
l += self.N
r += self.N
result = float("inf"), -1
while l < r:
if l & 1:
result = min(result, self.tree[l])
l += 1
if r & 1:
r -= 1
result = min(result, self.tree[r])
l >>= 1
r >>= 1
return result
n = int(input())
a = list(map(int, input().split()))
if n < 6:
print(max(a) - min(a), 1)
ans = [1] * n
print(*ans)
sys.exit()
pos = {}
for i, val in enumerate(a):
if val not in pos:
pos[val] = []
pos[val].append(i)
a.sort()
jump = []
for i in range(n - 1):
jump.append(a[i + 1] - a[i])
totalSum = a[-1] - a[0]
st = SegmTree(n - 1)
for i in range(2, min(5, n - 1 - 2)):
st.modify(i, totalSum - jump[i])
prevSep = [-1] * (n - 1)
for i in range(5, n - 1 - 2):
leftMin, sep = st.query_range(2, i - 2)
newVal = leftMin - jump[i]
prevSep[i] = sep
st.modify(i, newVal)
res, sep = st.tree[1]
seps = []
while sep > -1:
seps.append(sep)
sep = prevSep[sep]
team = 1
ans = [None] * n
for i, val in enumerate(a):
p = pos[val].pop()
ans[p] = team
if seps and i == seps[-1]:
team += 1
seps.pop()
print(res, team)
print(*ans)
|
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING NUMBER BIN_OP NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
There are $n$ students at your university. The programming skill of the $i$-th student is $a_i$. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has $2 \cdot 10^5$ students ready for the finals!
Each team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of $k$ students with programming skills $a[i_1], a[i_2], \dots, a[i_k]$, then the diversity of this team is $\max\limits_{j=1}^{k} a[i_j] - \min\limits_{j=1}^{k} a[i_j]$).
The total diversity is the sum of diversities of all teams formed.
Your task is to minimize the total diversity of the division of students and find the optimal way to divide the students.
-----Input-----
The first line of the input contains one integer $n$ ($3 \le n \le 2 \cdot 10^5$) β the number of students.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the programming skill of the $i$-th student.
-----Output-----
In the first line print two integers $res$ and $k$ β the minimum total diversity of the division of students and the number of teams in your division, correspondingly.
In the second line print $n$ integers $t_1, t_2, \dots, t_n$ ($1 \le t_i \le k$), where $t_i$ is the number of team to which the $i$-th student belong.
If there are multiple answers, you can print any. Note that you don't need to minimize the number of teams. Each team should consist of at least three students.
-----Examples-----
Input
5
1 1 3 4 2
Output
3 1
1 1 1 1 1
Input
6
1 5 12 13 2 15
Output
7 2
2 2 1 1 2 1
Input
10
1 2 5 129 185 581 1041 1909 1580 8150
Output
7486 3
3 3 3 2 2 2 2 1 1 1
-----Note-----
In the first example, there is only one team with skills $[1, 1, 2, 3, 4]$ so the answer is $3$. It can be shown that you cannot achieve a better answer.
In the second example, there are two teams with skills $[1, 2, 5]$ and $[12, 13, 15]$ so the answer is $4 + 3 = 7$.
In the third example, there are three teams with skills $[1, 2, 5]$, $[129, 185, 581, 1041]$ and $[1580, 1909, 8150]$ so the answer is $4 + 912 + 6570 = 7486$.
|
n = int(input())
aa = list(map(int, input().split()))
a = []
for i in range(n):
a.append([aa[i], i])
a.sort()
d = []
for i in range(n - 1):
d.append(a[i + 1][0] - a[i][0])
c = [(0) for i in range(n)]
i = 5
while i < n:
c[i] = max(c[i - 1], c[i - 3] + d[i - 3])
i += 1
i = n - 1
final = [(0) for k in range(n)]
j = 1
while i > 0:
if c[i] == c[i - 1]:
i -= 1
else:
i -= 3
final[i] = j
j += 1
check = [(0) for i in range(n)]
count = 1
for i in range(n):
check[a[i][1]] = count
if final[i] != 0:
count += 1
print(a[-1][0] - a[0][0] - c[-1], count)
for i in range(n):
print(check[i], end=" ")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
|
There are $n$ students at your university. The programming skill of the $i$-th student is $a_i$. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has $2 \cdot 10^5$ students ready for the finals!
Each team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of $k$ students with programming skills $a[i_1], a[i_2], \dots, a[i_k]$, then the diversity of this team is $\max\limits_{j=1}^{k} a[i_j] - \min\limits_{j=1}^{k} a[i_j]$).
The total diversity is the sum of diversities of all teams formed.
Your task is to minimize the total diversity of the division of students and find the optimal way to divide the students.
-----Input-----
The first line of the input contains one integer $n$ ($3 \le n \le 2 \cdot 10^5$) β the number of students.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the programming skill of the $i$-th student.
-----Output-----
In the first line print two integers $res$ and $k$ β the minimum total diversity of the division of students and the number of teams in your division, correspondingly.
In the second line print $n$ integers $t_1, t_2, \dots, t_n$ ($1 \le t_i \le k$), where $t_i$ is the number of team to which the $i$-th student belong.
If there are multiple answers, you can print any. Note that you don't need to minimize the number of teams. Each team should consist of at least three students.
-----Examples-----
Input
5
1 1 3 4 2
Output
3 1
1 1 1 1 1
Input
6
1 5 12 13 2 15
Output
7 2
2 2 1 1 2 1
Input
10
1 2 5 129 185 581 1041 1909 1580 8150
Output
7486 3
3 3 3 2 2 2 2 1 1 1
-----Note-----
In the first example, there is only one team with skills $[1, 1, 2, 3, 4]$ so the answer is $3$. It can be shown that you cannot achieve a better answer.
In the second example, there are two teams with skills $[1, 2, 5]$ and $[12, 13, 15]$ so the answer is $4 + 3 = 7$.
In the third example, there are three teams with skills $[1, 2, 5]$, $[129, 185, 581, 1041]$ and $[1580, 1909, 8150]$ so the answer is $4 + 912 + 6570 = 7486$.
|
n = int(input())
inf = 10**18
a = sorted((v, i) for i, v in enumerate(map(int, input().split()))) + [(inf**2, -1)] * 5
dp = [0] + [inf] * (n + 10)
dpi = [-1] * (n + 10)
for i in range(n):
for j in range(i + 2, i + 5):
cost = a[j][0] - a[i][0]
if dp[j + 1] > dp[i] + cost:
dp[j + 1] = dp[i] + cost
dpi[j + 1] = i
dest = dpi[n]
g = 1
groups = [0] * n
for i in range(n - 1, -1, -1):
groups[a[i][1]] = g
if i == dest:
dest = dpi[i]
g += 1
print(dp[n], g - 1)
print(*groups)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR BIN_OP LIST BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
There are $n$ students at your university. The programming skill of the $i$-th student is $a_i$. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has $2 \cdot 10^5$ students ready for the finals!
Each team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of $k$ students with programming skills $a[i_1], a[i_2], \dots, a[i_k]$, then the diversity of this team is $\max\limits_{j=1}^{k} a[i_j] - \min\limits_{j=1}^{k} a[i_j]$).
The total diversity is the sum of diversities of all teams formed.
Your task is to minimize the total diversity of the division of students and find the optimal way to divide the students.
-----Input-----
The first line of the input contains one integer $n$ ($3 \le n \le 2 \cdot 10^5$) β the number of students.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the programming skill of the $i$-th student.
-----Output-----
In the first line print two integers $res$ and $k$ β the minimum total diversity of the division of students and the number of teams in your division, correspondingly.
In the second line print $n$ integers $t_1, t_2, \dots, t_n$ ($1 \le t_i \le k$), where $t_i$ is the number of team to which the $i$-th student belong.
If there are multiple answers, you can print any. Note that you don't need to minimize the number of teams. Each team should consist of at least three students.
-----Examples-----
Input
5
1 1 3 4 2
Output
3 1
1 1 1 1 1
Input
6
1 5 12 13 2 15
Output
7 2
2 2 1 1 2 1
Input
10
1 2 5 129 185 581 1041 1909 1580 8150
Output
7486 3
3 3 3 2 2 2 2 1 1 1
-----Note-----
In the first example, there is only one team with skills $[1, 1, 2, 3, 4]$ so the answer is $3$. It can be shown that you cannot achieve a better answer.
In the second example, there are two teams with skills $[1, 2, 5]$ and $[12, 13, 15]$ so the answer is $4 + 3 = 7$.
In the third example, there are three teams with skills $[1, 2, 5]$, $[129, 185, 581, 1041]$ and $[1580, 1909, 8150]$ so the answer is $4 + 912 + 6570 = 7486$.
|
import sys
def main():
n = int(input())
a = readIntArr()
a2 = [[x, i + 1] for i, x in enumerate(a)]
a2.sort(key=lambda x: x[0])
dp = [inf for _ in range(n)]
dp2 = [inf for _ in range(n)]
dp2i = [(-1) for _ in range(n)]
for i in range(2, min(n, 5)):
dp[i] = a2[i][0] - a2[0][0]
dp2[i] = dp[i - 1] - a2[i][0]
dp2i[i] = i
if dp2[i] >= dp2[i - 1]:
dp2i[i] = dp2i[i - 1]
dp2[i] = dp2[i - 1]
for i in range(5, n):
dp2[i] = dp[i - 1] - a2[i][0]
dp2i[i] = i
if dp2[i] >= dp2[i - 1]:
dp2i[i] = dp2i[i - 1]
dp2[i] = dp2[i - 1]
dp[i] = a2[i][0] + dp2[i - 2]
minDiversity = dp[n - 1]
groups = []
right = n - 1
while True:
left = dp2i[right - 2]
if left == -1:
left = 0
temp = []
for i in range(left, right + 1):
temp.append(a2[i][1])
groups.append(temp)
if left == 0:
break
right = left - 1
nGroups = len(groups)
grouping = [(-1) for _ in range(n)]
for i in range(nGroups):
for student in groups[i]:
grouping[student - 1] = i + 1
print("{} {}".format(minDiversity, nGroups))
oneLineArrayPrint(grouping)
return
input = lambda: sys.stdin.readline().rstrip("\r\n")
def oneLineArrayPrint(arr):
print(" ".join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print("\n".join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print("\n".join([" ".join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
def makeArr(defaultVal, dimensionArr):
dv = defaultVal
da = dimensionArr
if len(da) == 1:
return [dv for _ in range(da[0])]
else:
return [makeArr(dv, da[1:]) for _ in range(da[0])]
def queryInteractive(x, y):
print("? {} {}".format(x, y))
sys.stdout.flush()
return int(input())
def answerInteractive(ans):
print("! {}".format(ans))
sys.stdout.flush()
inf = float("inf")
MOD = 10**9 + 7
for _abc in range(1):
main()
|
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
|
There are $n$ students at your university. The programming skill of the $i$-th student is $a_i$. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has $2 \cdot 10^5$ students ready for the finals!
Each team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of $k$ students with programming skills $a[i_1], a[i_2], \dots, a[i_k]$, then the diversity of this team is $\max\limits_{j=1}^{k} a[i_j] - \min\limits_{j=1}^{k} a[i_j]$).
The total diversity is the sum of diversities of all teams formed.
Your task is to minimize the total diversity of the division of students and find the optimal way to divide the students.
-----Input-----
The first line of the input contains one integer $n$ ($3 \le n \le 2 \cdot 10^5$) β the number of students.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the programming skill of the $i$-th student.
-----Output-----
In the first line print two integers $res$ and $k$ β the minimum total diversity of the division of students and the number of teams in your division, correspondingly.
In the second line print $n$ integers $t_1, t_2, \dots, t_n$ ($1 \le t_i \le k$), where $t_i$ is the number of team to which the $i$-th student belong.
If there are multiple answers, you can print any. Note that you don't need to minimize the number of teams. Each team should consist of at least three students.
-----Examples-----
Input
5
1 1 3 4 2
Output
3 1
1 1 1 1 1
Input
6
1 5 12 13 2 15
Output
7 2
2 2 1 1 2 1
Input
10
1 2 5 129 185 581 1041 1909 1580 8150
Output
7486 3
3 3 3 2 2 2 2 1 1 1
-----Note-----
In the first example, there is only one team with skills $[1, 1, 2, 3, 4]$ so the answer is $3$. It can be shown that you cannot achieve a better answer.
In the second example, there are two teams with skills $[1, 2, 5]$ and $[12, 13, 15]$ so the answer is $4 + 3 = 7$.
In the third example, there are three teams with skills $[1, 2, 5]$, $[129, 185, 581, 1041]$ and $[1580, 1909, 8150]$ so the answer is $4 + 912 + 6570 = 7486$.
|
n = int(input())
arr = list(map(int, input().split()))
for i in range(n):
arr[i] = arr[i], i
arr.sort()
dp = [999999999999] * (n + 1)
dp[0] = 0
came_from = [-1] * (n + 1)
for i in range(n):
if i + 3 <= n:
if dp[i] + arr[i + 2][0] - arr[i][0] < dp[i + 3]:
came_from[i + 3] = i
dp[i + 3] = min(dp[i + 3], dp[i] + arr[i + 2][0] - arr[i][0])
if i + 4 <= n:
if dp[i] + arr[i + 3][0] - arr[i][0] < dp[i + 4]:
came_from[i + 4] = i
dp[i + 4] = min(dp[i + 4], dp[i] + arr[i + 3][0] - arr[i][0])
if i + 5 <= n:
if dp[i] + arr[i + 4][0] - arr[i][0] < dp[i + 5]:
came_from[i + 5] = i
dp[i + 5] = min(dp[i + 5], dp[i] + arr[i + 4][0] - arr[i][0])
result = [0] * n
i = n
team = 1
while i > 0:
target = came_from[i]
while i > target:
result[arr[i - 1][1]] = team
i -= 1
team += 1
print(dp[n], team - 1)
print(*result, sep=" ")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
|
There are $n$ students at your university. The programming skill of the $i$-th student is $a_i$. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has $2 \cdot 10^5$ students ready for the finals!
Each team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of $k$ students with programming skills $a[i_1], a[i_2], \dots, a[i_k]$, then the diversity of this team is $\max\limits_{j=1}^{k} a[i_j] - \min\limits_{j=1}^{k} a[i_j]$).
The total diversity is the sum of diversities of all teams formed.
Your task is to minimize the total diversity of the division of students and find the optimal way to divide the students.
-----Input-----
The first line of the input contains one integer $n$ ($3 \le n \le 2 \cdot 10^5$) β the number of students.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the programming skill of the $i$-th student.
-----Output-----
In the first line print two integers $res$ and $k$ β the minimum total diversity of the division of students and the number of teams in your division, correspondingly.
In the second line print $n$ integers $t_1, t_2, \dots, t_n$ ($1 \le t_i \le k$), where $t_i$ is the number of team to which the $i$-th student belong.
If there are multiple answers, you can print any. Note that you don't need to minimize the number of teams. Each team should consist of at least three students.
-----Examples-----
Input
5
1 1 3 4 2
Output
3 1
1 1 1 1 1
Input
6
1 5 12 13 2 15
Output
7 2
2 2 1 1 2 1
Input
10
1 2 5 129 185 581 1041 1909 1580 8150
Output
7486 3
3 3 3 2 2 2 2 1 1 1
-----Note-----
In the first example, there is only one team with skills $[1, 1, 2, 3, 4]$ so the answer is $3$. It can be shown that you cannot achieve a better answer.
In the second example, there are two teams with skills $[1, 2, 5]$ and $[12, 13, 15]$ so the answer is $4 + 3 = 7$.
In the third example, there are three teams with skills $[1, 2, 5]$, $[129, 185, 581, 1041]$ and $[1580, 1909, 8150]$ so the answer is $4 + 912 + 6570 = 7486$.
|
inf = float("inf")
def f(A, T):
dp = [0] * len(A)
for i in range(len(A) - 1, -1, -1):
if len(A) - i < 3:
dp[i] = inf
elif len(A) - i == 3:
dp[i] = A[i + 2][0] - A[i][0]
T[i] = len(A)
else:
extend = dp[i + 1] + (A[i + 1][0] - A[i][0])
start = A[i + 2][0] - A[i][0] + dp[i + 3]
if extend < start:
dp[i] = extend
T[i] = T[i + 1]
else:
dp[i] = start
T[i] = i + 3
return dp[0]
n = int(input())
rest = map(int, input().split())
A = []
for i, a in enumerate(rest):
A.append((a, i))
A.sort(key=lambda x: x[0])
T = [0] * len(A)
ans = f(A, T)
X = [0] * len(A)
team = 0
index = 0
while index != len(A):
nxt = T[index]
for k in range(index, nxt):
_, idx = A[k]
X[idx] = team + 1
team += 1
index = nxt
print(ans, team)
print(" ".join(map(str, X)))
|
ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
|
There are $n$ students at your university. The programming skill of the $i$-th student is $a_i$. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has $2 \cdot 10^5$ students ready for the finals!
Each team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of $k$ students with programming skills $a[i_1], a[i_2], \dots, a[i_k]$, then the diversity of this team is $\max\limits_{j=1}^{k} a[i_j] - \min\limits_{j=1}^{k} a[i_j]$).
The total diversity is the sum of diversities of all teams formed.
Your task is to minimize the total diversity of the division of students and find the optimal way to divide the students.
-----Input-----
The first line of the input contains one integer $n$ ($3 \le n \le 2 \cdot 10^5$) β the number of students.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the programming skill of the $i$-th student.
-----Output-----
In the first line print two integers $res$ and $k$ β the minimum total diversity of the division of students and the number of teams in your division, correspondingly.
In the second line print $n$ integers $t_1, t_2, \dots, t_n$ ($1 \le t_i \le k$), where $t_i$ is the number of team to which the $i$-th student belong.
If there are multiple answers, you can print any. Note that you don't need to minimize the number of teams. Each team should consist of at least three students.
-----Examples-----
Input
5
1 1 3 4 2
Output
3 1
1 1 1 1 1
Input
6
1 5 12 13 2 15
Output
7 2
2 2 1 1 2 1
Input
10
1 2 5 129 185 581 1041 1909 1580 8150
Output
7486 3
3 3 3 2 2 2 2 1 1 1
-----Note-----
In the first example, there is only one team with skills $[1, 1, 2, 3, 4]$ so the answer is $3$. It can be shown that you cannot achieve a better answer.
In the second example, there are two teams with skills $[1, 2, 5]$ and $[12, 13, 15]$ so the answer is $4 + 3 = 7$.
In the third example, there are three teams with skills $[1, 2, 5]$, $[129, 185, 581, 1041]$ and $[1580, 1909, 8150]$ so the answer is $4 + 912 + 6570 = 7486$.
|
n = int(input())
a = list(map(int, input().split()))
idx = sorted(range(n), key=lambda i: a[i])
a = sorted(a)
diff = [(x - y) for x, y in zip(a[1:], a[:-1])]
dp = [0] * (n - 1)
mask = [0] * (n - 1)
for i in range(n - 1):
if i < 2:
continue
if i <= n - 4:
add = 0 if i == 2 else dp[i - 3]
if diff[i] + add > dp[i - 1]:
dp[i] = diff[i] + add
mask[i] = 1
else:
dp[i] = dp[i - 1]
else:
dp[i] = dp[i - 1]
i = n - 2
while i >= 0:
if mask[i] == 1:
for _ in range(2):
i -= 1
if i >= 0:
mask[i] = 0
i -= 1
comp = [None] * n
cur = 1
comp[0] = cur
for i, x in enumerate(mask):
if x == 1:
cur += 1
comp[i + 1] = cur
else:
comp[i + 1] = comp[i]
ans = [None] * n
for i, x in enumerate(idx):
ans[x] = str(comp[i])
print(str(sum(diff) - dp[-1]) + " " + str(cur))
print(" ".join(ans))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
There are $n$ students at your university. The programming skill of the $i$-th student is $a_i$. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has $2 \cdot 10^5$ students ready for the finals!
Each team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of $k$ students with programming skills $a[i_1], a[i_2], \dots, a[i_k]$, then the diversity of this team is $\max\limits_{j=1}^{k} a[i_j] - \min\limits_{j=1}^{k} a[i_j]$).
The total diversity is the sum of diversities of all teams formed.
Your task is to minimize the total diversity of the division of students and find the optimal way to divide the students.
-----Input-----
The first line of the input contains one integer $n$ ($3 \le n \le 2 \cdot 10^5$) β the number of students.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the programming skill of the $i$-th student.
-----Output-----
In the first line print two integers $res$ and $k$ β the minimum total diversity of the division of students and the number of teams in your division, correspondingly.
In the second line print $n$ integers $t_1, t_2, \dots, t_n$ ($1 \le t_i \le k$), where $t_i$ is the number of team to which the $i$-th student belong.
If there are multiple answers, you can print any. Note that you don't need to minimize the number of teams. Each team should consist of at least three students.
-----Examples-----
Input
5
1 1 3 4 2
Output
3 1
1 1 1 1 1
Input
6
1 5 12 13 2 15
Output
7 2
2 2 1 1 2 1
Input
10
1 2 5 129 185 581 1041 1909 1580 8150
Output
7486 3
3 3 3 2 2 2 2 1 1 1
-----Note-----
In the first example, there is only one team with skills $[1, 1, 2, 3, 4]$ so the answer is $3$. It can be shown that you cannot achieve a better answer.
In the second example, there are two teams with skills $[1, 2, 5]$ and $[12, 13, 15]$ so the answer is $4 + 3 = 7$.
In the third example, there are three teams with skills $[1, 2, 5]$, $[129, 185, 581, 1041]$ and $[1580, 1909, 8150]$ so the answer is $4 + 912 + 6570 = 7486$.
|
n = int(input())
arr = sorted((v, i) for i, v in enumerate(map(int, input().split(" "))))
ans = [0] * n
if n == 3:
print(arr[2][0] - arr[0][0], 1)
for i in n:
ans[i] = i
elif n == 4:
print(arr[3][0] - arr[0][0], 1)
for i in n:
ans[i] = i
else:
INF = 1000000000000000000
dp = [0, INF, INF, arr[2][0] - arr[0][0], arr[3][0] - arr[0][0]]
for i in range(4, n):
dp.append(
min(
dp[i - 2] + arr[i][0] - arr[i - 2][0],
dp[i - 3] + arr[i][0] - arr[i - 3][0],
dp[i - 4] + arr[i][0] - arr[i - 4][0],
)
)
cnt, ls = 1, n
for i in range(n, 0, -1):
ans[arr[i - 1][1]] = cnt
if dp[i - 1] + arr[ls - 1][0] - arr[i - 1][0] == dp[ls] and ls - i > 1:
ls = i - 1
cnt += 1
print(dp[n], cnt - 1)
print(*ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
There are $n$ students at your university. The programming skill of the $i$-th student is $a_i$. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has $2 \cdot 10^5$ students ready for the finals!
Each team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of $k$ students with programming skills $a[i_1], a[i_2], \dots, a[i_k]$, then the diversity of this team is $\max\limits_{j=1}^{k} a[i_j] - \min\limits_{j=1}^{k} a[i_j]$).
The total diversity is the sum of diversities of all teams formed.
Your task is to minimize the total diversity of the division of students and find the optimal way to divide the students.
-----Input-----
The first line of the input contains one integer $n$ ($3 \le n \le 2 \cdot 10^5$) β the number of students.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the programming skill of the $i$-th student.
-----Output-----
In the first line print two integers $res$ and $k$ β the minimum total diversity of the division of students and the number of teams in your division, correspondingly.
In the second line print $n$ integers $t_1, t_2, \dots, t_n$ ($1 \le t_i \le k$), where $t_i$ is the number of team to which the $i$-th student belong.
If there are multiple answers, you can print any. Note that you don't need to minimize the number of teams. Each team should consist of at least three students.
-----Examples-----
Input
5
1 1 3 4 2
Output
3 1
1 1 1 1 1
Input
6
1 5 12 13 2 15
Output
7 2
2 2 1 1 2 1
Input
10
1 2 5 129 185 581 1041 1909 1580 8150
Output
7486 3
3 3 3 2 2 2 2 1 1 1
-----Note-----
In the first example, there is only one team with skills $[1, 1, 2, 3, 4]$ so the answer is $3$. It can be shown that you cannot achieve a better answer.
In the second example, there are two teams with skills $[1, 2, 5]$ and $[12, 13, 15]$ so the answer is $4 + 3 = 7$.
In the third example, there are three teams with skills $[1, 2, 5]$, $[129, 185, 581, 1041]$ and $[1580, 1909, 8150]$ so the answer is $4 + 912 + 6570 = 7486$.
|
n = int(input())
x = list(map(int, input().split()))
y = []
for i in range(n):
y.append(x[i])
m = {}
inf = 885627645626577863562
for i in range(n):
m[x[i]] = i
x.sort()
dp, cnt, ans = [0] * n, [0] * n, [[] for i in range(n)]
dp[0] = dp[1] = inf
for i in range(2, n):
dp[i] = inf
for i2 in range(3, 6):
if i - i2 == -1:
if dp[i] > x[i] - x[i - i2 + 1]:
dp[i] = x[i] - x[i - i2 + 1]
cnt[i] = i2
elif i - i2 >= 0:
if dp[i] > dp[i - i2] + x[i] - x[i - i2 + 1]:
dp[i] = dp[i - i2] + x[i] - x[i - i2 + 1]
cnt[i] = i2
skip, temp = inf, 0
for i in range(n - 1, -1, -1):
if i <= skip:
skip = i - cnt[i]
temp += 1
if i > skip:
ans[m[x[i]]].append(temp)
print(dp[n - 1], temp)
for i in range(n):
p = m[y[i]]
print(ans[p][len(ans[p]) - 1], end=" ")
ans[p].pop()
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR
|
There are $n$ students at your university. The programming skill of the $i$-th student is $a_i$. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has $2 \cdot 10^5$ students ready for the finals!
Each team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of $k$ students with programming skills $a[i_1], a[i_2], \dots, a[i_k]$, then the diversity of this team is $\max\limits_{j=1}^{k} a[i_j] - \min\limits_{j=1}^{k} a[i_j]$).
The total diversity is the sum of diversities of all teams formed.
Your task is to minimize the total diversity of the division of students and find the optimal way to divide the students.
-----Input-----
The first line of the input contains one integer $n$ ($3 \le n \le 2 \cdot 10^5$) β the number of students.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the programming skill of the $i$-th student.
-----Output-----
In the first line print two integers $res$ and $k$ β the minimum total diversity of the division of students and the number of teams in your division, correspondingly.
In the second line print $n$ integers $t_1, t_2, \dots, t_n$ ($1 \le t_i \le k$), where $t_i$ is the number of team to which the $i$-th student belong.
If there are multiple answers, you can print any. Note that you don't need to minimize the number of teams. Each team should consist of at least three students.
-----Examples-----
Input
5
1 1 3 4 2
Output
3 1
1 1 1 1 1
Input
6
1 5 12 13 2 15
Output
7 2
2 2 1 1 2 1
Input
10
1 2 5 129 185 581 1041 1909 1580 8150
Output
7486 3
3 3 3 2 2 2 2 1 1 1
-----Note-----
In the first example, there is only one team with skills $[1, 1, 2, 3, 4]$ so the answer is $3$. It can be shown that you cannot achieve a better answer.
In the second example, there are two teams with skills $[1, 2, 5]$ and $[12, 13, 15]$ so the answer is $4 + 3 = 7$.
In the third example, there are three teams with skills $[1, 2, 5]$, $[129, 185, 581, 1041]$ and $[1580, 1909, 8150]$ so the answer is $4 + 912 + 6570 = 7486$.
|
def sort(n, a, b):
for i in range(n):
for j in range(i + 1, n):
if a[i] > a[j]:
t = a[i]
a[i] = a[j]
a[j] = t
t = pos[i]
pos[i] = pos[j]
pos[j] = t
n = int(input())
a = [0] + list(map(int, input().split()))
if n > 1000:
x = zip(a, list(range(0, n + 1)))
xs = sorted(x, key=lambda tup: tup[0])
a = [x[0] for x in xs]
pos = [x[1] for x in xs]
else:
pos = list(range(0, n + 1))
sort(n + 1, a, pos)
w = list(range(0, n + 1))
dp = list(range(0, n + 1))
dp[0] = 0
dp[1] = 10000000000
dp[2] = 10000000000
w[0], w[1], w[2] = 0, 0, 0
for i in range(3, n + 1):
dp[i] = -1
w3 = 10000000000
w4 = 10000000000
w5 = 10000000000
if i > 2:
w3 = dp[i - 3] + a[i] - a[i - 2]
if i > 3:
w4 = dp[i - 4] + a[i] - a[i - 3]
if i > 4:
w5 = dp[i - 5] + a[i] - a[i - 4]
dp[i] = min(w3, w4, w5)
if w3 == dp[i]:
w[i] = 3
elif w4 == dp[i]:
w[i] = 4
else:
w[i] = 5
count = 0
i = n
res = list(range(0, n + 1))
while i:
count += 1
for j in range(i - w[i] + 1, i + 1):
res[pos[j]] = count
i -= w[i]
res.pop(0)
print(dp[n], count)
print(*res, sep=" ")
|
FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
|
There are $n$ students at your university. The programming skill of the $i$-th student is $a_i$. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has $2 \cdot 10^5$ students ready for the finals!
Each team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of $k$ students with programming skills $a[i_1], a[i_2], \dots, a[i_k]$, then the diversity of this team is $\max\limits_{j=1}^{k} a[i_j] - \min\limits_{j=1}^{k} a[i_j]$).
The total diversity is the sum of diversities of all teams formed.
Your task is to minimize the total diversity of the division of students and find the optimal way to divide the students.
-----Input-----
The first line of the input contains one integer $n$ ($3 \le n \le 2 \cdot 10^5$) β the number of students.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the programming skill of the $i$-th student.
-----Output-----
In the first line print two integers $res$ and $k$ β the minimum total diversity of the division of students and the number of teams in your division, correspondingly.
In the second line print $n$ integers $t_1, t_2, \dots, t_n$ ($1 \le t_i \le k$), where $t_i$ is the number of team to which the $i$-th student belong.
If there are multiple answers, you can print any. Note that you don't need to minimize the number of teams. Each team should consist of at least three students.
-----Examples-----
Input
5
1 1 3 4 2
Output
3 1
1 1 1 1 1
Input
6
1 5 12 13 2 15
Output
7 2
2 2 1 1 2 1
Input
10
1 2 5 129 185 581 1041 1909 1580 8150
Output
7486 3
3 3 3 2 2 2 2 1 1 1
-----Note-----
In the first example, there is only one team with skills $[1, 1, 2, 3, 4]$ so the answer is $3$. It can be shown that you cannot achieve a better answer.
In the second example, there are two teams with skills $[1, 2, 5]$ and $[12, 13, 15]$ so the answer is $4 + 3 = 7$.
In the third example, there are three teams with skills $[1, 2, 5]$, $[129, 185, 581, 1041]$ and $[1580, 1909, 8150]$ so the answer is $4 + 912 + 6570 = 7486$.
|
n = int(input().strip())
order = dict()
nums = [(int(i), index) for index, i in enumerate(input().strip().split())]
nums.sort()
order = [i[1] for i in nums]
nums = [i[0] for i in nums]
dp = [(100000010000) for i in range(n + 1)]
dp[0] = 0
p = [(-1) for i in range(n + 1)]
for i in range(n + 1):
for j in range(3, 6):
if i + j <= n:
diff = nums[i + j - 1] - nums[i]
if dp[i + j] > dp[i] + diff:
p[i + j] = i
dp[i + j] = dp[i] + diff
cur = n
cnt = 0
ans = dict()
while cur != 0:
for i in range(cur - 1, p[cur] - 1, -1):
ans[order[i]] = cnt
cnt += 1
cur = p[cur]
print(dp[n], ans[order[0]] + 1)
for i in range(n):
print(ans[i] + 1, end=" ")
print()
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR
|
There are $n$ students at your university. The programming skill of the $i$-th student is $a_i$. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has $2 \cdot 10^5$ students ready for the finals!
Each team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of $k$ students with programming skills $a[i_1], a[i_2], \dots, a[i_k]$, then the diversity of this team is $\max\limits_{j=1}^{k} a[i_j] - \min\limits_{j=1}^{k} a[i_j]$).
The total diversity is the sum of diversities of all teams formed.
Your task is to minimize the total diversity of the division of students and find the optimal way to divide the students.
-----Input-----
The first line of the input contains one integer $n$ ($3 \le n \le 2 \cdot 10^5$) β the number of students.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the programming skill of the $i$-th student.
-----Output-----
In the first line print two integers $res$ and $k$ β the minimum total diversity of the division of students and the number of teams in your division, correspondingly.
In the second line print $n$ integers $t_1, t_2, \dots, t_n$ ($1 \le t_i \le k$), where $t_i$ is the number of team to which the $i$-th student belong.
If there are multiple answers, you can print any. Note that you don't need to minimize the number of teams. Each team should consist of at least three students.
-----Examples-----
Input
5
1 1 3 4 2
Output
3 1
1 1 1 1 1
Input
6
1 5 12 13 2 15
Output
7 2
2 2 1 1 2 1
Input
10
1 2 5 129 185 581 1041 1909 1580 8150
Output
7486 3
3 3 3 2 2 2 2 1 1 1
-----Note-----
In the first example, there is only one team with skills $[1, 1, 2, 3, 4]$ so the answer is $3$. It can be shown that you cannot achieve a better answer.
In the second example, there are two teams with skills $[1, 2, 5]$ and $[12, 13, 15]$ so the answer is $4 + 3 = 7$.
In the third example, there are three teams with skills $[1, 2, 5]$, $[129, 185, 581, 1041]$ and $[1580, 1909, 8150]$ so the answer is $4 + 912 + 6570 = 7486$.
|
n = int(input())
l = list(map(int, input().split()))
a = [[l[i], i] for i in range(n)]
a.sort()
ll = l.copy()
ll.sort()
if n < 6:
print(a[-1][0] - a[0][0], 1)
print(*([1] * n))
else:
b = [(ll[i] - ll[i - 1]) for i in range(3, n - 2)]
dp = [[0, 0] for i in range(len(b) + 5)]
i = len(b) - 1
dp[i] = [0, b[i]]
while i >= 0:
dp[i][0] = max(dp[i + 1])
dp[i][1] = max(dp[i + 3]) + b[i]
i -= 1
odp = []
i = 0
while True:
if dp[i] == [0, 0]:
break
if dp[i][1] > dp[i][0]:
odp.append(i)
i += 3
else:
i += 1
odp = [(i + 3) for i in odp]
grupy = [0] * n
grupa = 1
wsk = 0
for i in range(n):
if wsk <= len(odp) - 1:
if i == odp[wsk]:
wsk += 1
grupa += 1
grupy[a[i][1]] = grupa
print(ll[-1] - ll[0] - max(dp[0]), grupa)
print(*grupy)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR LIST NUMBER VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR LIST NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
There are $n$ students at your university. The programming skill of the $i$-th student is $a_i$. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has $2 \cdot 10^5$ students ready for the finals!
Each team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of $k$ students with programming skills $a[i_1], a[i_2], \dots, a[i_k]$, then the diversity of this team is $\max\limits_{j=1}^{k} a[i_j] - \min\limits_{j=1}^{k} a[i_j]$).
The total diversity is the sum of diversities of all teams formed.
Your task is to minimize the total diversity of the division of students and find the optimal way to divide the students.
-----Input-----
The first line of the input contains one integer $n$ ($3 \le n \le 2 \cdot 10^5$) β the number of students.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the programming skill of the $i$-th student.
-----Output-----
In the first line print two integers $res$ and $k$ β the minimum total diversity of the division of students and the number of teams in your division, correspondingly.
In the second line print $n$ integers $t_1, t_2, \dots, t_n$ ($1 \le t_i \le k$), where $t_i$ is the number of team to which the $i$-th student belong.
If there are multiple answers, you can print any. Note that you don't need to minimize the number of teams. Each team should consist of at least three students.
-----Examples-----
Input
5
1 1 3 4 2
Output
3 1
1 1 1 1 1
Input
6
1 5 12 13 2 15
Output
7 2
2 2 1 1 2 1
Input
10
1 2 5 129 185 581 1041 1909 1580 8150
Output
7486 3
3 3 3 2 2 2 2 1 1 1
-----Note-----
In the first example, there is only one team with skills $[1, 1, 2, 3, 4]$ so the answer is $3$. It can be shown that you cannot achieve a better answer.
In the second example, there are two teams with skills $[1, 2, 5]$ and $[12, 13, 15]$ so the answer is $4 + 3 = 7$.
In the third example, there are three teams with skills $[1, 2, 5]$, $[129, 185, 581, 1041]$ and $[1580, 1909, 8150]$ so the answer is $4 + 912 + 6570 = 7486$.
|
n = int(input())
a = list(map(int, input().split()))
a = sorted([(num, i) for i, num in enumerate(a)])
INF = 10**18
diff = a[-1][0] - a[0][0]
dp = [0] * (n + 1)
max_1 = 0
max_2 = 0
ind = []
for i in range(3, n - 2):
max_1 = max(max_1, dp[i - 2])
max_2 = max(max_2, dp[i])
tmp1 = a[i][0] - a[i - 1][0] + max_1
tmp2 = max_2
if tmp1 <= tmp2:
dp[i + 1] = tmp2
else:
dp[i + 1] = tmp1
ind.append(i)
ans_ind = []
ans = diff - dp[n - 2]
for i in ind[::-1]:
if not ans_ind:
ans_ind.append(i)
elif ans_ind[-1] >= i + 3:
ans_ind.append(i)
print(ans, len(ans_ind) + 1)
pos = 0
group = 1
group_li = [0] * n
for num in ans_ind[::-1]:
while pos < num:
group_li[a[pos][1]] = group
pos += 1
group += 1
while pos < n:
group_li[a[pos][1]] = group
pos += 1
print(*group_li)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
There are $n$ students at your university. The programming skill of the $i$-th student is $a_i$. As a coach, you want to divide them into teams to prepare them for the upcoming ICPC finals. Just imagine how good this university is if it has $2 \cdot 10^5$ students ready for the finals!
Each team should consist of at least three students. Each student should belong to exactly one team. The diversity of a team is the difference between the maximum programming skill of some student that belongs to this team and the minimum programming skill of some student that belongs to this team (in other words, if the team consists of $k$ students with programming skills $a[i_1], a[i_2], \dots, a[i_k]$, then the diversity of this team is $\max\limits_{j=1}^{k} a[i_j] - \min\limits_{j=1}^{k} a[i_j]$).
The total diversity is the sum of diversities of all teams formed.
Your task is to minimize the total diversity of the division of students and find the optimal way to divide the students.
-----Input-----
The first line of the input contains one integer $n$ ($3 \le n \le 2 \cdot 10^5$) β the number of students.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the programming skill of the $i$-th student.
-----Output-----
In the first line print two integers $res$ and $k$ β the minimum total diversity of the division of students and the number of teams in your division, correspondingly.
In the second line print $n$ integers $t_1, t_2, \dots, t_n$ ($1 \le t_i \le k$), where $t_i$ is the number of team to which the $i$-th student belong.
If there are multiple answers, you can print any. Note that you don't need to minimize the number of teams. Each team should consist of at least three students.
-----Examples-----
Input
5
1 1 3 4 2
Output
3 1
1 1 1 1 1
Input
6
1 5 12 13 2 15
Output
7 2
2 2 1 1 2 1
Input
10
1 2 5 129 185 581 1041 1909 1580 8150
Output
7486 3
3 3 3 2 2 2 2 1 1 1
-----Note-----
In the first example, there is only one team with skills $[1, 1, 2, 3, 4]$ so the answer is $3$. It can be shown that you cannot achieve a better answer.
In the second example, there are two teams with skills $[1, 2, 5]$ and $[12, 13, 15]$ so the answer is $4 + 3 = 7$.
In the third example, there are three teams with skills $[1, 2, 5]$, $[129, 185, 581, 1041]$ and $[1580, 1909, 8150]$ so the answer is $4 + 912 + 6570 = 7486$.
|
n = int(input())
inp = input().split()
d = [(int(inp[i]), i) for i in range(n)]
d.sort()
mmm = d[-1][0] - d[0][0]
dif = [(d[i + 1][0] - d[i][0]) for i in range(n - 1)]
if n < 6:
print(sum(dif), 1)
print("1 " * n)
exit()
dp = [-1] * (n - 1)
dpp = [-1] * (n - 1)
ninf = -10000000000
dp[0] = ninf
dp[1] = ninf
dp[2] = dif[2]
dp[3] = dif[3]
dp[4] = dif[4]
dpp[0] = -1
dpp[1] = -1
dpp[2] = -1
dpp[3] = -1
dpp[4] = -1
for i in range(5, n - 1):
ans = -1
src = -1
for j in range(3, 6):
if i - j >= 0 and dp[i - j] + dif[i] > ans:
ans = dp[i - j] + dif[i]
src = i - j
dp[i] = ans
dpp[i] = src
ans = -1
src = -1
for j in range(n - 4, n - 7, -1):
if dp[j] > ans:
ans = dp[j]
src = j
l = []
while src != -1:
l.append(src)
src = dpp[src]
l.reverse()
ll = [1]
lind = 0
ind = 0
team = 1
kk = len(l)
for ind in range(1, n):
if lind < kk and l[lind] == ind - 1:
team += 1
lind += 1
ll.append(team)
aaa = [0] * n
for i in range(n):
dd = ll[i]
aaa[d[i][1]] = dd
print(mmm - ans, team)
print(*aaa)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
You've got array A, consisting of n integers and a positive integer k. Array A is indexed by integers from 1 to n.
You need to permute the array elements so that value $\sum_{i = 1}^{n - k}|A [ i ] - A [ i + k ]|$ became minimal possible. In particular, it is allowed not to change order of elements at all.
-----Input-----
The first line contains two integers n, k (2 β€ n β€ 3Β·10^5, 1 β€ k β€ min(5000, n - 1)).
The second line contains n integers A[1], A[2], ..., A[n] ( - 10^9 β€ A[i] β€ 10^9), separate by spaces β elements of the array A.
-----Output-----
Print the minimum possible value of the sum described in the statement.
-----Examples-----
Input
3 2
1 2 4
Output
1
Input
5 2
3 -5 3 -5 3
Output
0
Input
6 3
4 3 4 3 2 5
Output
3
-----Note-----
In the first test one of the optimal permutations is 1Β 4Β 2.
In the second test the initial order is optimal.
In the third test one of the optimal permutations is 2Β 3Β 4Β 4Β 3Β 5.
|
INF = 10**18 + 179
[n, k], a = [list(map(int, input().split())) for x in range(2)]
a.sort()
dp, l = [([0] * (k - n % k + 1)) for x in range(n % k + 1)], n // k
for i in range(n % k + 1):
for j in range(k - n % k + 1):
pos = i * (l + 1) + j * l
dp[i][j] = (
min(
dp[i - 1][j] + a[pos - 1] - a[pos - l - 1] if i else INF,
dp[i][j - 1] + a[pos - 1] - a[pos - l] if j else INF,
)
if i or j
else 0
)
print(dp[n % k][k - n % k])
|
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN LIST VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR
|
You've got array A, consisting of n integers and a positive integer k. Array A is indexed by integers from 1 to n.
You need to permute the array elements so that value $\sum_{i = 1}^{n - k}|A [ i ] - A [ i + k ]|$ became minimal possible. In particular, it is allowed not to change order of elements at all.
-----Input-----
The first line contains two integers n, k (2 β€ n β€ 3Β·10^5, 1 β€ k β€ min(5000, n - 1)).
The second line contains n integers A[1], A[2], ..., A[n] ( - 10^9 β€ A[i] β€ 10^9), separate by spaces β elements of the array A.
-----Output-----
Print the minimum possible value of the sum described in the statement.
-----Examples-----
Input
3 2
1 2 4
Output
1
Input
5 2
3 -5 3 -5 3
Output
0
Input
6 3
4 3 4 3 2 5
Output
3
-----Note-----
In the first test one of the optimal permutations is 1Β 4Β 2.
In the second test the initial order is optimal.
In the third test one of the optimal permutations is 2Β 3Β 4Β 4Β 3Β 5.
|
f = lambda: list(map(int, input().split()))
n, k = f()
p = sorted(f())
m, d = n // k, n % k
u, v = d + 1, k - d + 1
g = [0] * u * v
i = 0
for a in range(u):
j = a * m + a - 1
for b in range(v):
x = g[i - 1] + p[j] - p[j - m + 1] if b else 9000000000.0
y = g[i - v] + p[j] - p[j - m] if a else 9000000000.0
if i:
g[i] = min(x, y)
i += 1
j += m
print(g[-1])
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
|
You've got array A, consisting of n integers and a positive integer k. Array A is indexed by integers from 1 to n.
You need to permute the array elements so that value $\sum_{i = 1}^{n - k}|A [ i ] - A [ i + k ]|$ became minimal possible. In particular, it is allowed not to change order of elements at all.
-----Input-----
The first line contains two integers n, k (2 β€ n β€ 3Β·10^5, 1 β€ k β€ min(5000, n - 1)).
The second line contains n integers A[1], A[2], ..., A[n] ( - 10^9 β€ A[i] β€ 10^9), separate by spaces β elements of the array A.
-----Output-----
Print the minimum possible value of the sum described in the statement.
-----Examples-----
Input
3 2
1 2 4
Output
1
Input
5 2
3 -5 3 -5 3
Output
0
Input
6 3
4 3 4 3 2 5
Output
3
-----Note-----
In the first test one of the optimal permutations is 1Β 4Β 2.
In the second test the initial order is optimal.
In the third test one of the optimal permutations is 2Β 3Β 4Β 4Β 3Β 5.
|
inf = 10**10
n, k = map(int, input().split())
a = sorted(map(int, input().split()))
L, M = n // k, n % k
dp = [([0] * (k - M + 1)) for i in range(M + 1)]
for i in range(M + 1):
for j in range(k - M + 1):
pos = i * (L + 1) + j * L
dp[i][j] = (
min(
dp[i - 1][j] + a[pos - 1] - a[pos - L - 1] if i else inf,
dp[i][j - 1] + a[pos - 1] - a[pos - L] if j else inf,
)
if i or j
else 0
)
print(dp[M][k - M])
|
ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR
|
You've got array A, consisting of n integers and a positive integer k. Array A is indexed by integers from 1 to n.
You need to permute the array elements so that value $\sum_{i = 1}^{n - k}|A [ i ] - A [ i + k ]|$ became minimal possible. In particular, it is allowed not to change order of elements at all.
-----Input-----
The first line contains two integers n, k (2 β€ n β€ 3Β·10^5, 1 β€ k β€ min(5000, n - 1)).
The second line contains n integers A[1], A[2], ..., A[n] ( - 10^9 β€ A[i] β€ 10^9), separate by spaces β elements of the array A.
-----Output-----
Print the minimum possible value of the sum described in the statement.
-----Examples-----
Input
3 2
1 2 4
Output
1
Input
5 2
3 -5 3 -5 3
Output
0
Input
6 3
4 3 4 3 2 5
Output
3
-----Note-----
In the first test one of the optimal permutations is 1Β 4Β 2.
In the second test the initial order is optimal.
In the third test one of the optimal permutations is 2Β 3Β 4Β 4Β 3Β 5.
|
def solve(n, k, As):
As.sort()
m, r = divmod(n, k)
dp = [0] * (r + 1)
for i in range(1, k):
im = i * m
new_dp = [0] * (r + 1)
new_dp[0] = dp[0] + As[im] - As[im - 1]
for h in range(1, min(i, r) + 1):
new_dp[h] = max(dp[h], dp[h - 1]) + As[im + h] - As[im + h - 1]
dp = new_dp
return As[-1] - As[0] - max(dp[r], dp[r - 1])
n, k = map(int, input().split())
As = list(map(int, input().split()))
print(solve(n, k, As))
|
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$.
The level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located).
The level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers.
You have $t$ seconds to complete the level β that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions:
if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time).
Note that after each action both your coordinate and the coordinate of your squad should be integers.
You have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds.
-----Input-----
The first line contains four integers $m$, $n$, $k$ and $t$ ($1 \le m, n, k, t \le 2 \cdot 10^5$, $n < t$) β the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively.
The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier.
Then $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \le l_i \le r_i \le n$, $1 \le d_i \le 2 \cdot 10^5$) β the location of the trap, the location where the trap can be disarmed, and its danger level, respectively.
-----Output-----
Print one integer β the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds.
-----Example-----
Input
5 6 4 14
1 2 3 4 5
1 5 2
1 2 5
2 3 5
3 5 3
Output
3
-----Note-----
In the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows:
go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad.
The whole plan can be executed in $13$ seconds.
|
class Traps:
def __init__(self, l, r, d):
self.before_l = l - 1
self.r = r
self.d = d
def qujianbingji(x, y):
if y[0] < x[0]:
if y[1] < x[0]:
return [y, x]
elif y[1] <= x[1]:
z = [y[0], x[1]]
return [z]
else:
return [y]
elif y[0] < x[1]:
if y[1] <= x[1]:
return [x]
else:
z = [x[0], y[1]]
return [z]
else:
return [x, y]
def main():
m, n, k, t = map(int, input().split())
lista = list(map(int, input().split()))
lista.sort(reverse=True)
list_traps = []
for i in range(k):
l, r, d = map(int, input().split())
list_traps.append(Traps(l, r, d))
list_traps.sort(key=lambda x: x.before_l)
T = 0
left, right = -1, m
while right - left > 1:
dangerous_traps = []
mid = (left + right) // 2
for trap in list_traps:
if trap.d > lista[mid]:
dangerous_traps.append(trap)
if dangerous_traps == []:
T = n + 1
else:
space = 0
finalbing = [[dangerous_traps[0].before_l, dangerous_traps[0].r]]
for dangerous_trap in dangerous_traps:
c = qujianbingji(
finalbing[-1], [dangerous_trap.before_l, dangerous_trap.r]
)
del finalbing[-1]
if len(c) == 2:
finalbing.append(c[0])
finalbing.append(c[1])
else:
finalbing.append(c[0])
lenfb = len(finalbing)
if lenfb == 1:
T = n + 1 + (finalbing[0][1] - finalbing[0][0]) * 2
else:
for x in range(lenfb - 1):
space += finalbing[x + 1][0] - finalbing[x][1]
maxlen = finalbing[-1][1] - finalbing[0][0]
T = n + 1 + 2 * (maxlen - space)
if T > t:
left, right = left, mid
count = mid
else:
left, right = mid, right
count = mid + 1
print(count)
main()
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN LIST VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER RETURN LIST VAR RETURN LIST VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN LIST VAR ASSIGN VAR LIST VAR NUMBER VAR NUMBER RETURN LIST VAR RETURN LIST VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR LIST ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST LIST VAR NUMBER VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER LIST VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$.
The level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located).
The level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers.
You have $t$ seconds to complete the level β that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions:
if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time).
Note that after each action both your coordinate and the coordinate of your squad should be integers.
You have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds.
-----Input-----
The first line contains four integers $m$, $n$, $k$ and $t$ ($1 \le m, n, k, t \le 2 \cdot 10^5$, $n < t$) β the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively.
The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier.
Then $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \le l_i \le r_i \le n$, $1 \le d_i \le 2 \cdot 10^5$) β the location of the trap, the location where the trap can be disarmed, and its danger level, respectively.
-----Output-----
Print one integer β the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds.
-----Example-----
Input
5 6 4 14
1 2 3 4 5
1 5 2
1 2 5
2 3 5
3 5 3
Output
3
-----Note-----
In the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows:
go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad.
The whole plan can be executed in $13$ seconds.
|
from sys import stdin
input = stdin.readline
m, n, k, t = map(int, input().split())
a = list(map(int, input().split()))
trap = []
def check(x):
avg = a[-x]
cost = n + 1
nr = 0
for i in range(k):
xx, y, d = trap[i]
if d <= avg:
continue
if y > nr:
cost += (y - max(xx - 1, nr)) * 2
nr = y
return cost <= t
for i in range(k):
trap.append(list(map(int, input().split())))
trap.sort()
a.sort()
l = 0
r = m + 1
while r - l > 1:
mid = (l + r) // 2
if check(mid):
l = mid
else:
r = mid
print(l)
|
ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$.
The level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located).
The level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers.
You have $t$ seconds to complete the level β that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions:
if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time).
Note that after each action both your coordinate and the coordinate of your squad should be integers.
You have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds.
-----Input-----
The first line contains four integers $m$, $n$, $k$ and $t$ ($1 \le m, n, k, t \le 2 \cdot 10^5$, $n < t$) β the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively.
The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier.
Then $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \le l_i \le r_i \le n$, $1 \le d_i \le 2 \cdot 10^5$) β the location of the trap, the location where the trap can be disarmed, and its danger level, respectively.
-----Output-----
Print one integer β the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds.
-----Example-----
Input
5 6 4 14
1 2 3 4 5
1 5 2
1 2 5
2 3 5
3 5 3
Output
3
-----Note-----
In the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows:
go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad.
The whole plan can be executed in $13$ seconds.
|
import sys
input = sys.stdin.readline
m, n, k, t = list(map(int, input().split()))
A = list(map(int, input().split()))
T = [tuple(map(int, input().split())) for i in range(k)]
A.sort(reverse=True)
SLIST = [0] * (2 * 10**5 + 2)
ind = 0
for i in range(A[0], -1, -1):
SLIST[i] = SLIST[i + 1]
while ind < m and A[ind] == i:
SLIST[i] += 1
ind += 1
SLIST = [SLIST[0]] + SLIST
NG = 0
OK = 2 * 10**5 + 1
while OK - NG > 1:
mid = (OK + NG) // 2
USE = [0] * (2 * 10**5 + 3)
for l, r, d in T:
if d >= mid:
USE[l - 1] += 1
USE[r] -= 1
for i in range(1, 2 * 10**5 + 3):
USE[i] += USE[i - 1]
if (2 * 10**5 + 3 - USE.count(0)) * 2 + n + 1 <= t:
OK = mid
else:
NG = mid
print(SLIST[OK])
|
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$.
The level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located).
The level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers.
You have $t$ seconds to complete the level β that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions:
if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time).
Note that after each action both your coordinate and the coordinate of your squad should be integers.
You have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds.
-----Input-----
The first line contains four integers $m$, $n$, $k$ and $t$ ($1 \le m, n, k, t \le 2 \cdot 10^5$, $n < t$) β the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively.
The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier.
Then $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \le l_i \le r_i \le n$, $1 \le d_i \le 2 \cdot 10^5$) β the location of the trap, the location where the trap can be disarmed, and its danger level, respectively.
-----Output-----
Print one integer β the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds.
-----Example-----
Input
5 6 4 14
1 2 3 4 5
1 5 2
1 2 5
2 3 5
3 5 3
Output
3
-----Note-----
In the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows:
go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad.
The whole plan can be executed in $13$ seconds.
|
import sys
input = sys.stdin.readline
def checker(no):
pivot = agility[-no]
time = 0
squad = 0
captain = 0
for i in range(len(specs)):
if specs[i][2] <= pivot:
continue
elif captain < specs[i][0]:
time += specs[i][0] - 1 - squad + (captain - squad) * 2
captain, squad = specs[i][1], specs[i][0] - 1
else:
captain = max(captain, specs[i][1])
if time > t:
return False
else:
return True
m, n, k, t = map(int, input().split())
agility = [int(i) for i in input().split()]
specs = []
for i in range(k):
l, r, d = map(int, input().split())
specs.append([l, r, d])
specs.sort()
agility.sort()
specs.append([n + 2, n + 7, 10**6])
beg = 0
last = m + 1
while last - beg > 1:
mid = (beg + last) // 2
count = checker(mid)
if count:
beg = mid
else:
last = mid
print(beg)
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$.
The level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located).
The level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers.
You have $t$ seconds to complete the level β that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions:
if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time).
Note that after each action both your coordinate and the coordinate of your squad should be integers.
You have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds.
-----Input-----
The first line contains four integers $m$, $n$, $k$ and $t$ ($1 \le m, n, k, t \le 2 \cdot 10^5$, $n < t$) β the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively.
The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier.
Then $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \le l_i \le r_i \le n$, $1 \le d_i \le 2 \cdot 10^5$) β the location of the trap, the location where the trap can be disarmed, and its danger level, respectively.
-----Output-----
Print one integer β the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds.
-----Example-----
Input
5 6 4 14
1 2 3 4 5
1 5 2
1 2 5
2 3 5
3 5 3
Output
3
-----Note-----
In the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows:
go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad.
The whole plan can be executed in $13$ seconds.
|
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
M, N, K, T = map(int, input().split())
SOL = list(map(int, input().split()))
SOL = sorted(SOL, reverse=True)
traps = [list(map(lambda x: int(x), input().split())) for _ in range(K)]
traps = sorted(traps, key=lambda x: (x[0], x[1]))
lower = 0
high = M
while lower <= high:
mid = (lower + high) // 2
lower_sol = SOL[mid - 1] if mid - 1 >= 0 else float("inf")
add = 0
right_most = 0
for l, r, d in traps:
if d <= lower_sol:
continue
elif l > right_most:
add += r - l + 1
right_most = r
else:
add += max(r - right_most, 0)
right_most = max(r, right_most)
cost = N + 1 + add * 2
if cost <= T:
lower = mid + 1
else:
high = mid - 1
print(lower - 1)
|
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
You are playing a computer game, where you lead a party of $m$ soldiers. Each soldier is characterised by his agility $a_i$.
The level you are trying to get through can be represented as a straight line segment from point $0$ (where you and your squad is initially located) to point $n + 1$ (where the boss is located).
The level is filled with $k$ traps. Each trap is represented by three numbers $l_i$, $r_i$ and $d_i$. $l_i$ is the location of the trap, and $d_i$ is the danger level of the trap: whenever a soldier with agility lower than $d_i$ steps on a trap (that is, moves to the point $l_i$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $r_i$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers.
You have $t$ seconds to complete the level β that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions:
if your location is $x$, you may move to $x + 1$ or $x - 1$. This action consumes one second; if your location is $x$ and the location of your squad is $x$, you may move to $x + 1$ or to $x - 1$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $d_i$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $x$ and there is a trap $i$ with $r_i = x$, you may disarm this trap. This action is done instantly (it consumes no time).
Note that after each action both your coordinate and the coordinate of your squad should be integers.
You have to choose the maximum number of soldiers such that they all can be brought from the point $0$ to the point $n + 1$ (where the boss waits) in no more than $t$ seconds.
-----Input-----
The first line contains four integers $m$, $n$, $k$ and $t$ ($1 \le m, n, k, t \le 2 \cdot 10^5$, $n < t$) β the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively.
The second line contains $m$ integers $a_1$, $a_2$, ..., $a_m$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the agility of the $i$-th soldier.
Then $k$ lines follow, containing the descriptions of traps. Each line contains three numbers $l_i$, $r_i$ and $d_i$ ($1 \le l_i \le r_i \le n$, $1 \le d_i \le 2 \cdot 10^5$) β the location of the trap, the location where the trap can be disarmed, and its danger level, respectively.
-----Output-----
Print one integer β the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $t$ seconds.
-----Example-----
Input
5 6 4 14
1 2 3 4 5
1 5 2
1 2 5
2 3 5
3 5 3
Output
3
-----Note-----
In the first example you may take soldiers with agility $3$, $4$ and $5$ with you. The course of action is as follows:
go to $2$ without your squad; disarm the trap $2$; go to $3$ without your squad; disartm the trap $3$; go to $0$ without your squad; go to $7$ with your squad.
The whole plan can be executed in $13$ seconds.
|
import sys
input = sys.stdin.readline
M, N, K, T = map(int, input().split())
A = list(map(int, input().split()))
LRD = [list(map(int, input().split())) for _ in range(K)]
A.append(10**15)
A.sort(reverse=True)
l = 0
r = M + 1
while r - l > 1:
m = (r + l) // 2
D_limit = A[m]
P = [0] * (N + 2)
for L, R, D in LRD:
if D_limit < D:
P[L] += 1
P[R + 1] -= 1
c = 0
for i in range(N + 1):
P[i + 1] += P[i]
if P[i + 1] > 0:
c += 1
time = N + 1 + 2 * c
if time <= T:
l = m
else:
r = m
print(l)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.