problem_id
stringlengths
3
7
contestId
stringclasses
660 values
problem_index
stringclasses
27 values
programmingLanguage
stringclasses
3 values
testset
stringclasses
5 values
incorrect_passedTestCount
float64
0
146
incorrect_timeConsumedMillis
float64
15
4.26k
incorrect_memoryConsumedBytes
float64
0
271M
incorrect_submission_id
stringlengths
7
9
incorrect_source
stringlengths
10
27.7k
correct_passedTestCount
float64
2
360
correct_timeConsumedMillis
int64
30
8.06k
correct_memoryConsumedBytes
int64
0
475M
correct_submission_id
stringlengths
7
9
correct_source
stringlengths
28
21.2k
contest_name
stringclasses
664 values
contest_type
stringclasses
3 values
contest_start_year
int64
2.01k
2.02k
time_limit
float64
0.5
15
memory_limit
float64
64
1.02k
title
stringlengths
2
54
description
stringlengths
35
3.16k
input_format
stringlengths
67
1.76k
output_format
stringlengths
18
1.06k
interaction_format
null
note
stringclasses
840 values
examples
stringlengths
34
1.16k
rating
int64
800
3.4k
tags
stringclasses
533 values
testset_size
int64
2
360
official_tests
stringlengths
44
19.7M
official_tests_complete
bool
1 class
input_mode
stringclasses
1 value
generated_checker
stringclasses
231 values
executable
bool
1 class
31/D
31
D
Python 3
TESTS
1
92
0
190465947
w,h,n=map(int,input().split()) N=101 x=[[0]*N for _ in range(N)] y=[[0]*N for _ in range(N)] x[0][w],y[h][0],p=2,2,[] for a,b,c,d in [[*map(int,input().split())] for i in range(n)]: m,n=sorted([(a,b),(c,d)]) x[m[0]][m[1]]=1 y[m[0]][m[1]]=1 if m[0]==n[0]: x[n[0]][n[1]]=2 else: y[n[0]][n[1]]=2 def v(a,b): s,t=1,1 while x[a][b+s]==0: s+=1 if x[a][b+s]==1: v(a,b+s) while y[a+t][b]==0: t+=1 if y[a+t][b]==1: v(a+t,b) p.append(s*t) v(0,0) print(*sorted(p))
33
218
307,200
85902665
WHn = [[0,0]] # WHn[0].extend(list(map(int, input().split(' ')))) WHn[0].extend([int(x) for x in input().split(' ')]) n = range(WHn[0][-1]) lines = [] lines_aux = [] for i in n: lines.append( [int(x) for x in input().split(' ')] ) lines_aux.append(True) while any(lines_aux): for i in n: if lines_aux[i]: for unidad in WHn: rangex = range(unidad[0], unidad[2]+1) rangey = range(unidad[1], unidad[3]+1) if (lines[i][0] in rangex) and (lines[i][3] in rangey) and (lines[i][2] in rangex) and (lines[i][1] in rangey) and\ ( (lines[i][0:3:2] == unidad[0:3:2]) or (lines[i][1:4:2] == unidad[1:4:2]) ): WHn.append([unidad[0],unidad[1],lines[i][2],lines[i][3]]) WHn.append([lines[i][0],lines[i][1],unidad[2],unidad[3]]) WHn.remove(unidad) lines_aux[i] = False break for i,unidad in enumerate(WHn): WHn[i] = (unidad[2] - unidad[0])*(unidad[3] - unidad[1]) WHn.sort() result = '' for i in WHn: result += '{} ' print(result[:-1].format(*WHn))
Codeforces Beta Round 31 (Div. 2, Codeforces format)
CF
2,010
2
256
Chocolate
Bob has a rectangular chocolate bar of the size W × H. He introduced a cartesian coordinate system so that the point (0, 0) corresponds to the lower-left corner of the bar, and the point (W, H) corresponds to the upper-right corner. Bob decided to split the bar into pieces by breaking it. Each break is a segment parallel to one of the coordinate axes, which connects the edges of the bar. More formally, each break goes along the line x = xc or y = yc, where xc and yc are integers. It should divide one part of the bar into two non-empty parts. After Bob breaks some part into two parts, he breaks the resulting parts separately and independently from each other. Also he doesn't move the parts of the bar. Bob made n breaks and wrote them down in his notebook in arbitrary order. At the end he got n + 1 parts. Now he wants to calculate their areas. Bob is lazy, so he asks you to do this task.
The first line contains 3 integers W, H and n (1 ≤ W, H, n ≤ 100) — width of the bar, height of the bar and amount of breaks. Each of the following n lines contains four integers xi, 1, yi, 1, xi, 2, yi, 2 — coordinates of the endpoints of the i-th break (0 ≤ xi, 1 ≤ xi, 2 ≤ W, 0 ≤ yi, 1 ≤ yi, 2 ≤ H, or xi, 1 = xi, 2, or yi, 1 = yi, 2). Breaks are given in arbitrary order. It is guaranteed that the set of breaks is correct, i.e. there is some order of the given breaks that each next break divides exactly one part of the bar into two non-empty parts.
Output n + 1 numbers — areas of the resulting parts in the increasing order.
null
null
[{"input": "2 2 2\n1 0 1 2\n0 1 1 1", "output": "1 1 2"}, {"input": "2 2 3\n1 0 1 2\n0 1 1 1\n1 1 2 1", "output": "1 1 1 1"}, {"input": "2 4 2\n0 1 2 1\n0 3 2 3", "output": "2 2 4"}]
2,000
["dfs and similar", "implementation"]
33
[{"input": "2 2 2\r\n1 0 1 2\r\n0 1 1 1\r\n", "output": "1 1 2 "}, {"input": "2 2 3\r\n1 0 1 2\r\n0 1 1 1\r\n1 1 2 1\r\n", "output": "1 1 1 1 "}, {"input": "2 4 2\r\n0 1 2 1\r\n0 3 2 3\r\n", "output": "2 2 4 "}, {"input": "5 5 3\r\n2 1 2 5\r\n0 1 5 1\r\n4 0 4 1\r\n", "output": "1 4 8 12 "}, {"input": "10 10 4\r\n9 0 9 10\r\n4 0 4 10\r\n1 0 1 10\r\n1 4 4 4\r\n", "output": "10 10 12 18 50 "}, {"input": "9 8 5\r\n4 3 4 4\r\n0 4 9 4\r\n5 4 5 8\r\n0 3 9 3\r\n1 4 1 8\r\n", "output": "4 4 5 16 16 27 "}, {"input": "100 100 1\r\n0 14 100 14\r\n", "output": "1400 8600 "}]
false
stdio
null
true
19/A
19
A
Python 3
TESTS
3
124
5,632,000
35554519
teams,n = [], int(input()) for notused in range(n): teams.append([input(), 0, 0, 0]) for notused in range(n*(n-1)//2): players, score = input().split(" ") players,score = players.split("-"), list(map(int, score.split(":"))) for team in teams: if team[0] == players[0]: if score[0] > score[1]: team[1] += 3 if score[0] == score[1]: team[1] += 1 team[2] += score[0] - score[1] team[3] += score[0] if team[0] == players[1]: if score[0] < score[1]: team[1] += 3 if score[0] == score[1]: team[1] += 1 team[2] += score[1] - score[0] team[3] += score[1] teams.sort(key=lambda x: x[3]) teams.sort(key=lambda x: x[2]) teams.sort(key=lambda x: x[1]) winner = teams[n//2:n] for play in winner: print (play[0])
29
92
102,400
205467181
class Info: def __init__(self, newTeamName, newPoints, newGoalDiff, newScoredGoals): self.teamName = newTeamName self.points = newPoints self.goalDiff = newGoalDiff self.scoredGoals = newScoredGoals def __str__(self): return f'teamName: \'{self.teamName}\', points: {self.points}, goalDiff: {self.goalDiff}, scoredGoals: {self.scoredGoals}' def out(cont): for item in cont: print(item) def findIndexByName(cont, searchName): for i in range(len(cont)): if cont[i].teamName == searchName: return i return -1 n, cont = int(input()), [] for i in range(n): cont.append(Info(input(), 0, 0, 0)) ''' 01234567890123456789 line = 'Barcelona-Real 15:10' ''' for i in range(n * (n - 1) // 2): line = input() dashInd = line.index('-') spaceInd = line.index(' ') colonInd = line.index(':') team1Name = line[:dashInd] team2Name = line[dashInd + 1:spaceInd] goal1 = int(line[spaceInd + 1: colonInd]) goal2 = int(line[colonInd + 1:]) team1Index = findIndexByName(cont, team1Name) team2Index = findIndexByName(cont, team2Name) # update points if goal1 > goal2: cont[team1Index].points += 3 elif goal1 < goal2: cont[team2Index].points += 3 else: cont[team1Index].points += 1 cont[team2Index].points += 1 # update goal diff cont[team1Index].goalDiff += goal1 - goal2 cont[team2Index].goalDiff += goal2 - goal1 # update scored goals cont[team1Index].scoredGoals += goal1 cont[team2Index].scoredGoals += goal2 cont.sort(key=lambda obj: (obj.points, obj.goalDiff, obj.scoredGoals), reverse=True) del cont[n // 2:] cont.sort(key=lambda obj: obj.teamName) # out(cont) for obj in cont: print(obj.teamName)
Codeforces Beta Round 19
ICPC
2,010
2
64
World Football Cup
Everyone knows that 2010 FIFA World Cup is being held in South Africa now. By the decision of BFA (Berland's Football Association) next World Cup will be held in Berland. BFA took the decision to change some World Cup regulations: - the final tournament features n teams (n is always even) - the first n / 2 teams (according to the standings) come through to the knockout stage - the standings are made on the following principle: for a victory a team gets 3 points, for a draw — 1 point, for a defeat — 0 points. In the first place, teams are ordered in the standings in decreasing order of their points; in the second place — in decreasing order of the difference between scored and missed goals; in the third place — in the decreasing order of scored goals - it's written in Berland's Constitution that the previous regulation helps to order the teams without ambiguity. You are asked to write a program that, by the given list of the competing teams and the results of all the matches, will find the list of teams that managed to get through to the knockout stage.
The first input line contains the only integer n (1 ≤ n ≤ 50) — amount of the teams, taking part in the final tournament of World Cup. The following n lines contain the names of these teams, a name is a string of lower-case and upper-case Latin letters, its length doesn't exceed 30 characters. The following n·(n - 1) / 2 lines describe the held matches in the format name1-name2 num1:num2, where name1, name2 — names of the teams; num1, num2 (0 ≤ num1, num2 ≤ 100) — amount of the goals, scored by the corresponding teams. Accuracy of the descriptions is guaranteed: there are no two team names coinciding accurate to the letters' case; there is no match, where a team plays with itself; each match is met in the descriptions only once.
Output n / 2 lines — names of the teams, which managed to get through to the knockout stage in lexicographical order. Output each name in a separate line. No odd characters (including spaces) are allowed. It's guaranteed that the described regulations help to order the teams without ambiguity.
null
null
[{"input": "4\nA\nB\nC\nD\nA-B 1:1\nA-C 2:2\nA-D 1:0\nB-C 1:0\nB-D 0:3\nC-D 0:3", "output": "A\nD"}, {"input": "2\na\nA\na-A 2:1", "output": "a"}]
1,400
["implementation"]
29
[{"input": "4\r\nA\r\nB\r\nC\r\nD\r\nA-B 1:1\r\nA-C 2:2\r\nA-D 1:0\r\nB-C 1:0\r\nB-D 0:3\r\nC-D 0:3\r\n", "output": "A\r\nD\r\n"}, {"input": "2\r\na\r\nA\r\na-A 2:1\r\n", "output": "a\r\n"}, {"input": "2\r\nEULEUbCmfrmqxtzvg\r\nuHGRmKUhDcxcfqyruwzen\r\nuHGRmKUhDcxcfqyruwzen-EULEUbCmfrmqxtzvg 13:92\r\n", "output": "EULEUbCmfrmqxtzvg\r\n"}, {"input": "6\r\nA\r\nB\r\nC\r\nD\r\nE\r\nF\r\nA-B 1:0\r\nA-C 0:0\r\nA-D 1:0\r\nA-E 5:5\r\nA-F 0:1\r\nB-C 1:0\r\nB-D 1:0\r\nB-E 1:0\r\nB-F 0:2\r\nC-D 2:2\r\nC-E 1:0\r\nC-F 1:0\r\nD-E 1:0\r\nD-F 1:0\r\nE-F 0:1\r\n", "output": "A\r\nB\r\nF\r\n"}, {"input": "6\r\nA\r\nB\r\nC\r\nD\r\nE\r\nF\r\nA-B 1:0\r\nA-C 0:0\r\nA-D 1:0\r\nA-E 5:5\r\nA-F 0:1\r\nB-C 1:0\r\nB-D 1:0\r\nB-E 1:0\r\nB-F 0:2\r\nC-D 7:7\r\nC-E 1:0\r\nC-F 1:0\r\nD-E 1:0\r\nD-F 1:0\r\nE-F 0:1\r\n", "output": "B\r\nC\r\nF\r\n"}]
false
stdio
null
true
19/A
19
A
Python 3
TESTS
3
124
204,800
112877762
import sys from collections import defaultdict POINTS_WIN = 3 POINTS_DRAW = 1 POINTS_LOSS = 0 def sort_order(item: dict) -> tuple: return -item["points"], -item["score_diff"], -item["score"] def ranking(teams: list, games: list) -> list: teams_data = {t: defaultdict(int, {"name": t}) for t in teams} for lt, rt, ls, rs in games: teams_data[lt]["score"] = ls teams_data[rt]["score"] = rs if ls == rs: teams_data[lt]["points"] += POINTS_DRAW teams_data[rt]["points"] += POINTS_DRAW elif ls < rs: teams_data[lt]["points"] += POINTS_LOSS teams_data[lt]["score_diff"] -= rs - ls teams_data[rt]["points"] += POINTS_WIN teams_data[rt]["score_diff"] += rs - ls else: teams_data[lt]["points"] += POINTS_WIN teams_data[lt]["score_diff"] -= ls - rs teams_data[rt]["points"] += POINTS_LOSS teams_data[rt]["score_diff"] += ls - rs return list(sorted(teams_data.values(), key=sort_order))[: int(len(teams) / 2)] def parse_teams(n: int) -> list: teams = [] for i in range(n): teams.append(input()) return teams def parse_games(n: int) -> list: games = [] for _ in range(int(n * (n - 1) / 2)): line = sys.stdin.readline().strip() teams, scores = line.split(" ") left_team, right_team = teams.split("-") left_score, right_score = map(int, scores.split(":")) games.append((left_team, right_team, left_score, right_score)) return games if __name__ == "__main__": n = int(input()) teams = parse_teams(n) games = parse_games(n) # print("teams", teams) # print("games", games) qualified = ranking(teams, games) for team in sorted([t["name"] for t in qualified]): print(team)
29
92
102,400
217802461
class heSo: def __init__(self) -> None: self.scoredGoals = 0; self.missedGoals = 0; self.points = 0; class solve: def __init__(self) -> None: self.n = 0; self.team = dict(); def vao(self) -> None: self.n = int(input()); for _ in range(self.n): x = input(); self.team[x] = heSo(); def tinhHeSo(self, team1, team2, tiSo) -> None: diem1, diem2 = map(int,tiSo.split(":")); self.team[team1].scoredGoals += diem1; self.team[team1].missedGoals += diem2; self.team[team2].scoredGoals += diem2; self.team[team2].missedGoals += diem1; if(diem1 > diem2): self.team[team1].points += 3; elif(diem1 < diem2): self.team[team2].points += 3; else: self.team[team1].points += 1; self.team[team2].points += 1; def comp(self, x): return x[1].points, x[1].scoredGoals - x[1].missedGoals, x[1].scoredGoals; def lam(self) -> None: for _ in range(self.n * (self.n - 1) // 2): s1, s2 = input().split(" "); team1, team2 = s1.split("-"); self.tinhHeSo(team1, team2,s2); """ for i in self.team.items(): print(i[0],": ", i[1].scoredGoals, i[1].missedGoals, i[1].points, sep= " "); """ k = sorted(self.team.items(), key= self.comp, reverse= True); ketQua = [k[i][0] for i in range(self.n // 2)]; ketQua.sort(); for i in ketQua: print(i); def ra(self) -> None: pass; def main(): p = solve(); p.vao(); p.lam(); p.ra(); main();
Codeforces Beta Round 19
ICPC
2,010
2
64
World Football Cup
Everyone knows that 2010 FIFA World Cup is being held in South Africa now. By the decision of BFA (Berland's Football Association) next World Cup will be held in Berland. BFA took the decision to change some World Cup regulations: - the final tournament features n teams (n is always even) - the first n / 2 teams (according to the standings) come through to the knockout stage - the standings are made on the following principle: for a victory a team gets 3 points, for a draw — 1 point, for a defeat — 0 points. In the first place, teams are ordered in the standings in decreasing order of their points; in the second place — in decreasing order of the difference between scored and missed goals; in the third place — in the decreasing order of scored goals - it's written in Berland's Constitution that the previous regulation helps to order the teams without ambiguity. You are asked to write a program that, by the given list of the competing teams and the results of all the matches, will find the list of teams that managed to get through to the knockout stage.
The first input line contains the only integer n (1 ≤ n ≤ 50) — amount of the teams, taking part in the final tournament of World Cup. The following n lines contain the names of these teams, a name is a string of lower-case and upper-case Latin letters, its length doesn't exceed 30 characters. The following n·(n - 1) / 2 lines describe the held matches in the format name1-name2 num1:num2, where name1, name2 — names of the teams; num1, num2 (0 ≤ num1, num2 ≤ 100) — amount of the goals, scored by the corresponding teams. Accuracy of the descriptions is guaranteed: there are no two team names coinciding accurate to the letters' case; there is no match, where a team plays with itself; each match is met in the descriptions only once.
Output n / 2 lines — names of the teams, which managed to get through to the knockout stage in lexicographical order. Output each name in a separate line. No odd characters (including spaces) are allowed. It's guaranteed that the described regulations help to order the teams without ambiguity.
null
null
[{"input": "4\nA\nB\nC\nD\nA-B 1:1\nA-C 2:2\nA-D 1:0\nB-C 1:0\nB-D 0:3\nC-D 0:3", "output": "A\nD"}, {"input": "2\na\nA\na-A 2:1", "output": "a"}]
1,400
["implementation"]
29
[{"input": "4\r\nA\r\nB\r\nC\r\nD\r\nA-B 1:1\r\nA-C 2:2\r\nA-D 1:0\r\nB-C 1:0\r\nB-D 0:3\r\nC-D 0:3\r\n", "output": "A\r\nD\r\n"}, {"input": "2\r\na\r\nA\r\na-A 2:1\r\n", "output": "a\r\n"}, {"input": "2\r\nEULEUbCmfrmqxtzvg\r\nuHGRmKUhDcxcfqyruwzen\r\nuHGRmKUhDcxcfqyruwzen-EULEUbCmfrmqxtzvg 13:92\r\n", "output": "EULEUbCmfrmqxtzvg\r\n"}, {"input": "6\r\nA\r\nB\r\nC\r\nD\r\nE\r\nF\r\nA-B 1:0\r\nA-C 0:0\r\nA-D 1:0\r\nA-E 5:5\r\nA-F 0:1\r\nB-C 1:0\r\nB-D 1:0\r\nB-E 1:0\r\nB-F 0:2\r\nC-D 2:2\r\nC-E 1:0\r\nC-F 1:0\r\nD-E 1:0\r\nD-F 1:0\r\nE-F 0:1\r\n", "output": "A\r\nB\r\nF\r\n"}, {"input": "6\r\nA\r\nB\r\nC\r\nD\r\nE\r\nF\r\nA-B 1:0\r\nA-C 0:0\r\nA-D 1:0\r\nA-E 5:5\r\nA-F 0:1\r\nB-C 1:0\r\nB-D 1:0\r\nB-E 1:0\r\nB-F 0:2\r\nC-D 7:7\r\nC-E 1:0\r\nC-F 1:0\r\nD-E 1:0\r\nD-F 1:0\r\nE-F 0:1\r\n", "output": "B\r\nC\r\nF\r\n"}]
false
stdio
null
true
19/A
19
A
Python 3
TESTS
3
124
0
141663393
n = int(input()) d ={} for i in range(n): n1 = input() d[n1] = 0 for i in range((n*(n-1))//2): s1,s2 = input().split() a,b = s1.split('-') score_a,score_b = s2.split(':') score_a = int(score_a) score_b = int(score_b) if(score_a>score_b): d[a]+=3 elif(score_a == score_b): d[a] += 1 d[b] += 1 else: d[b] += 3 d1 = sorted(d.items(),key=lambda kv:(kv[1],kv[0])) ans = [] for i in range(n-1,n//2-1,-1): ans.append(d1[i][0]) for i in sorted(ans): print(i)
29
122
102,400
166676630
from functools import cmp_to_key class Team: def __init__(self, newTeamName, newPoints, newGoalDiff, newScoredGoals): self.teamName = newTeamName self.points = newPoints self.goalDiff = newGoalDiff self.scoredGoals = newScoredGoals def __str__(self): return f"TeamName: '{self.teamName}', points: {self.points}, " + \ f"goalDifference: {self.goalDiff}, scoredGoals: {self.scoredGoals}" def output(cont): for obj in cont: print(obj) def indexOf(cont, searchName): for i in range(len(cont)): if cont[i].teamName == searchName: return i return -1 # def compareByPointsThenByGoalDiffThenByScoredGoals(obj1, obj2): # point1 = obj1.Point # point2 = obj2.Point # goalDiff1 = obj1.GoalDiff # goalDiff2 = obj2.GoalDiff # scoredGoals1 = obj1.ScoredGoals # scoredGoals2 = obj2.ScoredGoals # # if point1 != point2: # return point1 - point2 # if goalDiff1 != goalDiff2: # return goalDiff1 - goalDiff2 # # return scoredGoals1 - scoredGoals2 # # # def compareByName(obj1, obj2): # name1 = obj1.teamName # name2 = obj2.teamName # len1 = len(name1) # len2 = len(name2) # minimum_length = 0 # # if len1 < len2: # minimum_length = len1 # else: # minimum_length = len2 # # for i in range(): # if name1[i] != name2[i]: # return name2[i] - name1[i] # # return len2 - len1 n, cont = int(input()), [] for i in range(n): newTeamName = input() obj = Team(newTeamName, 0, 0, 0) cont.append(obj) # output(cont) for i in range(n * (n - 1) // 2): line = input() dashInd = line.find('-') spaceInd = line.find(' ') colonInd = line.find(':') team1Name = line[0: dashInd] team2Name = line[dashInd + 1: spaceInd] goal1 = int(line[spaceInd + 1: colonInd]) goal2 = int(line[colonInd + 1:]) team1Index = indexOf(cont, team1Name) team2Index = indexOf(cont, team2Name) # Update Points if goal1 > goal2: cont[team1Index].points += 3 elif goal1 < goal2: cont[team2Index].points += 3 else: cont[team1Index].points += 1 cont[team2Index].points += 1 # Update Goal Difference cont[team1Index].goalDiff += goal1 - goal2 cont[team2Index].goalDiff += goal2 - goal1 # Update Scored Goals cont[team1Index].scoredGoals += goal1 cont[team2Index].scoredGoals += goal2 # cont.sort(key=cmp_to_key(compareByPointsThenByGoalDiffThenByScoredGoals)) cont.sort(key=lambda obj: (obj.points, obj.goalDiff, obj.scoredGoals), reverse=True) del cont[n // 2:] cont.sort(key=lambda obj: obj.teamName) for item in cont: print(item.teamName)
Codeforces Beta Round 19
ICPC
2,010
2
64
World Football Cup
Everyone knows that 2010 FIFA World Cup is being held in South Africa now. By the decision of BFA (Berland's Football Association) next World Cup will be held in Berland. BFA took the decision to change some World Cup regulations: - the final tournament features n teams (n is always even) - the first n / 2 teams (according to the standings) come through to the knockout stage - the standings are made on the following principle: for a victory a team gets 3 points, for a draw — 1 point, for a defeat — 0 points. In the first place, teams are ordered in the standings in decreasing order of their points; in the second place — in decreasing order of the difference between scored and missed goals; in the third place — in the decreasing order of scored goals - it's written in Berland's Constitution that the previous regulation helps to order the teams without ambiguity. You are asked to write a program that, by the given list of the competing teams and the results of all the matches, will find the list of teams that managed to get through to the knockout stage.
The first input line contains the only integer n (1 ≤ n ≤ 50) — amount of the teams, taking part in the final tournament of World Cup. The following n lines contain the names of these teams, a name is a string of lower-case and upper-case Latin letters, its length doesn't exceed 30 characters. The following n·(n - 1) / 2 lines describe the held matches in the format name1-name2 num1:num2, where name1, name2 — names of the teams; num1, num2 (0 ≤ num1, num2 ≤ 100) — amount of the goals, scored by the corresponding teams. Accuracy of the descriptions is guaranteed: there are no two team names coinciding accurate to the letters' case; there is no match, where a team plays with itself; each match is met in the descriptions only once.
Output n / 2 lines — names of the teams, which managed to get through to the knockout stage in lexicographical order. Output each name in a separate line. No odd characters (including spaces) are allowed. It's guaranteed that the described regulations help to order the teams without ambiguity.
null
null
[{"input": "4\nA\nB\nC\nD\nA-B 1:1\nA-C 2:2\nA-D 1:0\nB-C 1:0\nB-D 0:3\nC-D 0:3", "output": "A\nD"}, {"input": "2\na\nA\na-A 2:1", "output": "a"}]
1,400
["implementation"]
29
[{"input": "4\r\nA\r\nB\r\nC\r\nD\r\nA-B 1:1\r\nA-C 2:2\r\nA-D 1:0\r\nB-C 1:0\r\nB-D 0:3\r\nC-D 0:3\r\n", "output": "A\r\nD\r\n"}, {"input": "2\r\na\r\nA\r\na-A 2:1\r\n", "output": "a\r\n"}, {"input": "2\r\nEULEUbCmfrmqxtzvg\r\nuHGRmKUhDcxcfqyruwzen\r\nuHGRmKUhDcxcfqyruwzen-EULEUbCmfrmqxtzvg 13:92\r\n", "output": "EULEUbCmfrmqxtzvg\r\n"}, {"input": "6\r\nA\r\nB\r\nC\r\nD\r\nE\r\nF\r\nA-B 1:0\r\nA-C 0:0\r\nA-D 1:0\r\nA-E 5:5\r\nA-F 0:1\r\nB-C 1:0\r\nB-D 1:0\r\nB-E 1:0\r\nB-F 0:2\r\nC-D 2:2\r\nC-E 1:0\r\nC-F 1:0\r\nD-E 1:0\r\nD-F 1:0\r\nE-F 0:1\r\n", "output": "A\r\nB\r\nF\r\n"}, {"input": "6\r\nA\r\nB\r\nC\r\nD\r\nE\r\nF\r\nA-B 1:0\r\nA-C 0:0\r\nA-D 1:0\r\nA-E 5:5\r\nA-F 0:1\r\nB-C 1:0\r\nB-D 1:0\r\nB-E 1:0\r\nB-F 0:2\r\nC-D 7:7\r\nC-E 1:0\r\nC-F 1:0\r\nD-E 1:0\r\nD-F 1:0\r\nE-F 0:1\r\n", "output": "B\r\nC\r\nF\r\n"}]
false
stdio
null
true
713/C
713
C
Python 3
TESTS
1
77
6,963,200
126556899
def test(a): result=all(a[i]<=a[i+1] for i in range(len(a)-1)) return result n=int(input()) a=list(map(int, input().split())) d=0 while test(a)==False: while a[0]>a[1]: a[0]-=1 d+=1 for i in range(1,n-1): while a[i]>a[i+1]: a[i+1]+=1 d+=1 print(d)
57
171
8,294,400
174346205
import sys import io import os # region IO BUFSIZE = 8192 class FastIO(io.IOBase): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.buffer = io.BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(io.IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) def input(): return sys.stdin.readline().rstrip('\r\n') def read_int_list(): return list(map(int, input().split())) def read_int_tuple(): return tuple(map(int, input().split())) def read_int(): return int(input()) # endregion # region local test # if 'AW' in os.environ.get('COMPUTERNAME', ''): # test_no = 1 # f = open(os.path.dirname(__file__) + f'\\in{test_no}.txt', 'r') # def input(): # return f.readline().rstrip("\r\n") # endregion n = read_int() nums = read_int_list() normal = [x - i for i, x in enumerate(nums)] xs = sorted(set(normal)) inv = {x: i for i, x in enumerate(xs)} m = len(xs) inf = 123456789012345 dp = [abs(normal[0] - x) for x in xs] for i in range(1, m): if dp[i] > dp[i - 1]: dp[i] = dp[i - 1] for i in range(1, n): x = normal[i] np = [inf] * m for j in range(m): np[j] = min(np[j - 1], dp[j] + abs(x - xs[j])) dp = np # print(dp) print(min(dp))
Codeforces Round 371 (Div. 1)
CF
2,016
5
256
Sonya and Problem Wihtout a Legend
Sonya was unable to think of a story for this problem, so here comes the formal description. You are given the array containing n positive integers. At one turn you can pick any element and increase or decrease it by 1. The goal is the make the array strictly increasing by making the minimum possible number of operations. You are allowed to change elements in any way, they can become negative or equal to 0.
The first line of the input contains a single integer n (1 ≤ n ≤ 3000) — the length of the array. Next line contains n integer ai (1 ≤ ai ≤ 109).
Print the minimum number of operation required to make the array strictly increasing.
null
In the first sample, the array is going to look as follows: 2 3 5 6 7 9 11 |2 - 2| + |1 - 3| + |5 - 5| + |11 - 6| + |5 - 7| + |9 - 9| + |11 - 11| = 9 And for the second sample: 1 2 3 4 5 |5 - 1| + |4 - 2| + |3 - 3| + |2 - 4| + |1 - 5| = 12
[{"input": "7\n2 1 5 11 5 9 11", "output": "9"}, {"input": "5\n5 4 3 2 1", "output": "12"}]
2,300
["dp", "sortings"]
57
[{"input": "7\r\n2 1 5 11 5 9 11\r\n", "output": "9\r\n"}, {"input": "5\r\n5 4 3 2 1\r\n", "output": "12\r\n"}, {"input": "2\r\n1 1000\r\n", "output": "0\r\n"}, {"input": "2\r\n1000 1\r\n", "output": "1000\r\n"}, {"input": "5\r\n100 80 60 70 90\r\n", "output": "54\r\n"}, {"input": "10\r\n10 16 17 11 1213 1216 1216 1209 3061 3062\r\n", "output": "16\r\n"}, {"input": "20\r\n103 103 110 105 107 119 113 121 116 132 128 124 128 125 138 137 140 136 154 158\r\n", "output": "43\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}, {"input": "5\r\n1 1 1 2 3\r\n", "output": "3\r\n"}, {"input": "1\r\n1000\r\n", "output": "0\r\n"}, {"input": "50\r\n499 780 837 984 481 526 944 482 862 136 265 605 5 631 974 967 574 293 969 467 573 845 102 224 17 873 648 120 694 996 244 313 404 129 899 583 541 314 525 496 443 857 297 78 575 2 430 137 387 319\r\n", "output": "12423\r\n"}, {"input": "75\r\n392 593 98 533 515 448 220 310 386 79 539 294 208 828 75 534 875 493 94 205 656 105 546 493 60 188 222 108 788 504 809 621 934 455 307 212 630 298 938 62 850 421 839 134 950 256 934 817 209 559 866 67 990 835 534 672 468 768 757 516 959 893 275 315 692 927 321 554 801 805 885 12 67 245 495\r\n", "output": "17691\r\n"}, {"input": "10\r\n26 723 970 13 422 968 875 329 234 983\r\n", "output": "2546\r\n"}, {"input": "20\r\n245 891 363 6 193 704 420 447 237 947 664 894 512 194 513 616 671 623 686 378\r\n", "output": "3208\r\n"}, {"input": "5\r\n850 840 521 42 169\r\n", "output": "1485\r\n"}]
false
stdio
null
true
16/E
16
E
Python 3
TESTS
1
186
6,656,000
91466375
n = int(input()) for i in range(n): mul = 1 arr = list(map(float,input().split())) for j in range(n): if j!=i: mul = mul * arr[j] print(mul,end=" ")
20
934
4,300,800
181369804
import sys, os, io input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline n = int(input()) a = [list(map(float, input().split())) for _ in range(n)] pow2 = [1] for _ in range(n): pow2.append(2 * pow2[-1]) m = pow2[n] dp = [0] * m dp[-1] = 1 for i in range(m - 1, 0, -1): x = [] for j in range(n): if i & pow2[j]: x.append(j) u = len(x) * (len(x) - 1) // 2 if not u or not dp[i]: continue dpi = dp[i] for j in x: aj = a[j] for k in x: if j == k: continue l = i ^ pow2[k] dp[l] += dpi * aj[k] / u ans = [dp[i] for i in pow2[:-1]] sys.stdout.write(" ".join(map(str, ans)))
Codeforces Beta Round 16 (Div. 2 Only)
ICPC
2,010
3
128
Fish
n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability aij, and the second will eat up the first with the probability aji = 1 - aij. The described process goes on until there are at least two fish in the lake. For each fish find out the probability that it will survive to be the last in the lake.
The first line contains integer n (1 ≤ n ≤ 18) — the amount of fish in the lake. Then there follow n lines with n real numbers each — matrix a. aij (0 ≤ aij ≤ 1) — the probability that fish with index i eats up fish with index j. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: aij = 1 - aji. All real numbers are given with not more than 6 characters after the decimal point.
Output n space-separated real numbers accurate to not less than 6 decimal places. Number with index i should be equal to the probability that fish with index i will survive to be the last in the lake.
null
null
[{"input": "2\n0 0.5\n0.5 0", "output": "0.500000 0.500000"}, {"input": "5\n0 1 1 1 1\n0 0 0.5 0.5 0.5\n0 0.5 0 0.5 0.5\n0 0.5 0.5 0 0.5\n0 0.5 0.5 0.5 0", "output": "1.000000 0.000000 0.000000 0.000000 0.000000"}]
1,900
["bitmasks", "dp", "probabilities"]
20
[{"input": "2\r\n0 0.5\r\n0.5 0\r\n", "output": "0.500000 0.500000 "}, {"input": "4\r\n0 0.5 0.5 0.5\r\n0.5 0 0.5 0.5\r\n0.5 0.5 0 0.5\r\n0.5 0.5 0.5 0\r\n", "output": "0.250000 0.250000 0.250000 0.250000 "}, {"input": "5\r\n0 1 1 1 1\r\n0 0 0.5 0.5 0.5\r\n0 0.5 0 0.5 0.5\r\n0 0.5 0.5 0 0.5\r\n0 0.5 0.5 0.5 0\r\n", "output": "1.000000 0.000000 0.000000 0.000000 0.000000 "}, {"input": "1\r\n0.000\r\n", "output": "1.000000 "}, {"input": "2\r\n0.000 0.551\r\n0.449 0.000\r\n", "output": "0.551000 0.449000 "}, {"input": "3\r\n0.000 0.817 0.584\r\n0.183 0.000 0.665\r\n0.416 0.335 0.000\r\n", "output": "0.564400 0.208967 0.226632 "}, {"input": "4\r\n0.000 0.083 0.548 0.503\r\n0.917 0.000 0.395 0.144\r\n0.452 0.605 0.000 0.991\r\n0.497 0.856 0.009 0.000\r\n", "output": "0.163512 0.222554 0.463543 0.150390 "}, {"input": "5\r\n0.000 0.349 0.202 0.088 0.431\r\n0.651 0.000 0.435 0.627 0.564\r\n0.798 0.565 0.000 0.725 0.949\r\n0.912 0.373 0.275 0.000 0.027\r\n0.569 0.436 0.051 0.973 0.000\r\n", "output": "0.059303 0.233839 0.494324 0.093917 0.118617 "}, {"input": "8\r\n0.000 0.147 0.783 0.224 0.220 0.651 0.453 0.209\r\n0.853 0.000 0.246 0.076 0.018 0.349 0.896 0.315\r\n0.217 0.754 0.000 0.307 0.968 0.400 0.531 0.086\r\n0.776 0.924 0.693 0.000 0.707 0.842 0.116 0.949\r\n0.780 0.982 0.032 0.293 0.000 0.908 0.307 0.266\r\n0.349 0.651 0.600 0.158 0.092 0.000 0.066 0.909\r\n0.547 0.104 0.469 0.884 0.693 0.934 0.000 0.251\r\n0.791 0.685 0.914 0.051 0.734 0.091 0.749 0.000\r\n", "output": "0.056312 0.054963 0.091124 0.315966 0.093803 0.056812 0.187952 0.143068 "}]
false
stdio
import sys def read_floats_from_file(path): with open(path, 'r') as f: line = f.readline().strip() return list(map(float, line.split())) def main(): input_path, output_path, submission_path = sys.argv[1:4] expected = read_floats_from_file(output_path) submission = read_floats_from_file(submission_path) if len(expected) != len(submission): print(0) return eps = 1e-6 for e, s in zip(expected, submission): if abs(e - s) > eps + 1e-12: print(0) return print(1) if __name__ == "__main__": main()
true
16/E
16
E
Python 3
TESTS
1
60
0
182130459
n = int(input()) a = [] for i in range(n): a.append(list(map(float, input().split()))) output = [0] * n for i in range(n): p = 1 for j in range(n): if i != j: p *= a[i][j] output[i] = format(p, '.6f') for x in range(n): if x == n - 1: print(output[x]) else: print(output[x], end=" ")
20
1,090
7,372,800
180427251
def Is_bit_on(mask, i): return ((mask >> i) & 1) == 1 def turn_bit_off(mask, i): return ~(1 << i) & mask def turn_bit_on(mask, i): return (1 << i) | mask def count_on_bit(mask, n): count = 0 for i in range(n): if Is_bit_on(mask, i): count += 1 return count n = int(input()) probs = [0] * n for i in range(n): probs[i] = [0] * n for i in range(n): p = [float(a) for a in input().split()] for j in range(n): probs[i][j] = p[j] dp_size = turn_bit_on(0, n) dp = [0] * dp_size dp[dp_size - 1] = 1 for mask in range((1 << n) - 1, 0, -1): cnt = count_on_bit(mask, n) stats = (cnt * (cnt - 1)) // 2 for i in range(n): if not Is_bit_on(mask, i): continue for j in range(n): if (i == j) or not Is_bit_on(mask, j): continue dp[int(turn_bit_off(int(mask), j))] += dp[int(mask)] * probs[i][j] / stats results = [] for i in range(n): print(format(dp[turn_bit_on(0, i)], '.6f'), '', end='')
Codeforces Beta Round 16 (Div. 2 Only)
ICPC
2,010
3
128
Fish
n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability aij, and the second will eat up the first with the probability aji = 1 - aij. The described process goes on until there are at least two fish in the lake. For each fish find out the probability that it will survive to be the last in the lake.
The first line contains integer n (1 ≤ n ≤ 18) — the amount of fish in the lake. Then there follow n lines with n real numbers each — matrix a. aij (0 ≤ aij ≤ 1) — the probability that fish with index i eats up fish with index j. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: aij = 1 - aji. All real numbers are given with not more than 6 characters after the decimal point.
Output n space-separated real numbers accurate to not less than 6 decimal places. Number with index i should be equal to the probability that fish with index i will survive to be the last in the lake.
null
null
[{"input": "2\n0 0.5\n0.5 0", "output": "0.500000 0.500000"}, {"input": "5\n0 1 1 1 1\n0 0 0.5 0.5 0.5\n0 0.5 0 0.5 0.5\n0 0.5 0.5 0 0.5\n0 0.5 0.5 0.5 0", "output": "1.000000 0.000000 0.000000 0.000000 0.000000"}]
1,900
["bitmasks", "dp", "probabilities"]
20
[{"input": "2\r\n0 0.5\r\n0.5 0\r\n", "output": "0.500000 0.500000 "}, {"input": "4\r\n0 0.5 0.5 0.5\r\n0.5 0 0.5 0.5\r\n0.5 0.5 0 0.5\r\n0.5 0.5 0.5 0\r\n", "output": "0.250000 0.250000 0.250000 0.250000 "}, {"input": "5\r\n0 1 1 1 1\r\n0 0 0.5 0.5 0.5\r\n0 0.5 0 0.5 0.5\r\n0 0.5 0.5 0 0.5\r\n0 0.5 0.5 0.5 0\r\n", "output": "1.000000 0.000000 0.000000 0.000000 0.000000 "}, {"input": "1\r\n0.000\r\n", "output": "1.000000 "}, {"input": "2\r\n0.000 0.551\r\n0.449 0.000\r\n", "output": "0.551000 0.449000 "}, {"input": "3\r\n0.000 0.817 0.584\r\n0.183 0.000 0.665\r\n0.416 0.335 0.000\r\n", "output": "0.564400 0.208967 0.226632 "}, {"input": "4\r\n0.000 0.083 0.548 0.503\r\n0.917 0.000 0.395 0.144\r\n0.452 0.605 0.000 0.991\r\n0.497 0.856 0.009 0.000\r\n", "output": "0.163512 0.222554 0.463543 0.150390 "}, {"input": "5\r\n0.000 0.349 0.202 0.088 0.431\r\n0.651 0.000 0.435 0.627 0.564\r\n0.798 0.565 0.000 0.725 0.949\r\n0.912 0.373 0.275 0.000 0.027\r\n0.569 0.436 0.051 0.973 0.000\r\n", "output": "0.059303 0.233839 0.494324 0.093917 0.118617 "}, {"input": "8\r\n0.000 0.147 0.783 0.224 0.220 0.651 0.453 0.209\r\n0.853 0.000 0.246 0.076 0.018 0.349 0.896 0.315\r\n0.217 0.754 0.000 0.307 0.968 0.400 0.531 0.086\r\n0.776 0.924 0.693 0.000 0.707 0.842 0.116 0.949\r\n0.780 0.982 0.032 0.293 0.000 0.908 0.307 0.266\r\n0.349 0.651 0.600 0.158 0.092 0.000 0.066 0.909\r\n0.547 0.104 0.469 0.884 0.693 0.934 0.000 0.251\r\n0.791 0.685 0.914 0.051 0.734 0.091 0.749 0.000\r\n", "output": "0.056312 0.054963 0.091124 0.315966 0.093803 0.056812 0.187952 0.143068 "}]
false
stdio
import sys def read_floats_from_file(path): with open(path, 'r') as f: line = f.readline().strip() return list(map(float, line.split())) def main(): input_path, output_path, submission_path = sys.argv[1:4] expected = read_floats_from_file(output_path) submission = read_floats_from_file(submission_path) if len(expected) != len(submission): print(0) return eps = 1e-6 for e, s in zip(expected, submission): if abs(e - s) > eps + 1e-12: print(0) return print(1) if __name__ == "__main__": main()
true
167/A
167
A
PyPy 3
TESTS
1
140
1,638,400
117057941
import collections import functools import math import sys def In(): return map(int, sys.stdin.readline().split()) input = sys.stdin.readline import functools import math import sys def In(): return map(int, sys.stdin.readline().split()) input = sys.stdin.readline def wiztroll(): def calcnewd(old, plus): return (old * 2 + plus) / 2 def calctime(d, old, new): return d * 2 / (old + new) n, maxac, dest = In() blockingtime = 0 for _ in range(n): start, pera = In() preacd = 0 nowspeeed = 0 time = start + 1 while True: if preacd + calcnewd(nowspeeed, min(maxac, pera - nowspeeed)) > dest: left = dest - preacd ans = time + calctime(left, nowspeeed, min(maxac, pera - nowspeeed)) blockingtime = max(ans, blockingtime) print(ans) break if nowspeeed + maxac > pera: preacd += calcnewd(nowspeeed, pera - nowspeeed) left = dest - preacd ans = max(left / pera + time, blockingtime) blockingtime = max(ans, blockingtime) print(ans) break preacd += calcnewd(nowspeeed, maxac) nowspeeed += maxac time += 1 wiztroll()
57
218
12,697,600
183322705
import sys input = sys.stdin.readline n, a, d = map(int, input().split()) p = [0]*n for i in range(n): t, v = map(int,input().split()) x = v/a y = (2*d/a) ** 0.5 p[i] = t+y if y < x else t + d/v + x/2 p[i] = max(p[i-1],p[i]) print('\n'.join(map(str, p)))
Codeforces Round 114 (Div. 1)
CF
2,012
1
256
Wizards and Trolleybuses
In some country live wizards. They love to ride trolleybuses. A city in this country has a trolleybus depot with n trolleybuses. Every day the trolleybuses leave the depot, one by one and go to the final station. The final station is at a distance of d meters from the depot. We know for the i-th trolleybus that it leaves at the moment of time ti seconds, can go at a speed of no greater than vi meters per second, and accelerate with an acceleration no greater than a meters per second squared. A trolleybus can decelerate as quickly as you want (magic!). It can change its acceleration as fast as you want, as well. Note that the maximum acceleration is the same for all trolleys. Despite the magic the trolleys are still powered by an electric circuit and cannot overtake each other (the wires are to blame, of course). If a trolleybus catches up with another one, they go together one right after the other until they arrive at the final station. Also, the drivers are driving so as to arrive at the final station as quickly as possible. You, as head of the trolleybuses' fans' club, are to determine for each trolley the minimum time by which it can reach the final station. At the time of arrival at the destination station the trolleybus does not necessarily have zero speed. When a trolley is leaving the depot, its speed is considered equal to zero. From the point of view of physics, the trolleybuses can be considered as material points, and also we should ignore the impact on the speed of a trolley bus by everything, except for the acceleration and deceleration provided by the engine.
The first input line contains three space-separated integers n, a, d (1 ≤ n ≤ 105, 1 ≤ a, d ≤ 106) — the number of trolleybuses, their maximum acceleration and the distance from the depot to the final station, correspondingly. Next n lines contain pairs of integers ti vi (0 ≤ t1 < t2... < tn - 1 < tn ≤ 106, 1 ≤ vi ≤ 106) — the time when the i-th trolleybus leaves the depot and its maximum speed, correspondingly. The numbers in the lines are separated by spaces.
For each trolleybus print a single line the time it arrives to the final station. Print the times for the trolleybuses in the order in which the trolleybuses are given in the input. The answer will be accepted if the absolute or relative error doesn't exceed 10 - 4.
null
In the first sample the second trolleybus will catch up with the first one, that will happen at distance 510.5 meters from the depot. The trolleybuses will go the remaining 9489.5 meters together at speed 10 meters per second. As a result, both trolleybuses will arrive to the final station by the moment of time 1000.5 seconds. The third trolleybus will not catch up with them. It will arrive to the final station by the moment of time 11000.05 seconds.
[{"input": "3 10 10000\n0 10\n5 11\n1000 1", "output": "1000.5000000000\n1000.5000000000\n11000.0500000000"}, {"input": "1 2 26\n28 29", "output": "33.0990195136"}]
1,600
["implementation", "math"]
57
[{"input": "3 10 10000\r\n0 10\r\n5 11\r\n1000 1\r\n", "output": "1000.5000000000\r\n1000.5000000000\r\n11000.0500000000\r\n"}, {"input": "1 2 26\r\n28 29\r\n", "output": "33.0990195136\r\n"}, {"input": "7 8 3\r\n1 3\r\n5 26\r\n7 3\r\n10 15\r\n18 7\r\n21 17\r\n23 21\r\n", "output": "2.1875000000\r\n5.8660254038\r\n8.1875000000\r\n10.8660254038\r\n18.8660254038\r\n21.8660254038\r\n23.8660254038\r\n"}, {"input": "3 6 6\r\n2 10\r\n14 19\r\n18 14\r\n", "output": "3.4142135624\r\n15.4142135624\r\n19.4142135624\r\n"}, {"input": "10 7 8\r\n2 4\r\n3 13\r\n4 7\r\n5 1\r\n9 16\r\n10 9\r\n12 18\r\n16 4\r\n17 16\r\n20 6\r\n", "output": "4.2857142857\r\n4.5118578920\r\n5.6428571429\r\n13.0714285714\r\n13.0714285714\r\n13.0714285714\r\n13.5118578920\r\n18.2857142857\r\n18.5118578920\r\n21.7619047619\r\n"}, {"input": "8 4 13\r\n0 18\r\n6 24\r\n10 25\r\n11 5\r\n12 18\r\n20 22\r\n21 8\r\n22 12\r\n", "output": "2.5495097568\r\n8.5495097568\r\n12.5495097568\r\n14.2250000000\r\n14.5495097568\r\n22.5495097568\r\n23.6250000000\r\n24.5495097568\r\n"}, {"input": "1 2 7\r\n20 13\r\n", "output": "22.6457513111\r\n"}, {"input": "3 3 3\r\n13 1\r\n18 12\r\n19 2\r\n", "output": "16.1666666667\r\n19.4142135624\r\n20.8333333333\r\n"}, {"input": "8 7 21\r\n2 11\r\n3 4\r\n4 3\r\n9 23\r\n15 9\r\n16 5\r\n22 17\r\n24 10\r\n", "output": "4.6948051948\r\n8.5357142857\r\n11.2142857143\r\n11.4494897428\r\n17.9761904762\r\n20.5571428571\r\n24.4495798319\r\n26.8142857143\r\n"}, {"input": "3 6 19\r\n12 3\r\n20 24\r\n30 2\r\n", "output": "18.5833333333\r\n22.5166114784\r\n39.6666666667\r\n"}, {"input": "4 5 14\r\n11 1\r\n16 20\r\n17 15\r\n21 7\r\n", "output": "25.1000000000\r\n25.1000000000\r\n25.1000000000\r\n25.1000000000\r\n"}, {"input": "1 1 722397\r\n556297 454495\r\n", "output": "557498.9958402590\r\n"}, {"input": "1 100000 363166\r\n560443 753304\r\n", "output": "560445.6950547304\r\n"}, {"input": "1 124232 477338\r\n899117 898233\r\n", "output": "899119.7721151346\r\n"}, {"input": "1 1000000 1000000\r\n0 1000000\r\n", "output": "1.5000000000\r\n"}, {"input": "1 1 1\r\n0 1000000\r\n", "output": "1.4142135624\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path) as f: n, a, d = map(int, f.readline().split()) # Read correct output correct = [] with open(output_path) as f: for line in f: line = line.strip() if line: correct.append(float(line)) if len(correct) != n: print(0) return # Read submission output submission = [] try: with open(submission_path) as f: submission_lines = f.readlines() except: print(0) return if len(submission_lines) != n: print(0) return for line in submission_lines: stripped = line.strip() if not stripped: print(0) return try: t = float(stripped) except ValueError: print(0) return submission.append(t) # Check each value eps = 1e-4 for c, s in zip(correct, submission): abs_err = abs(c - s) if abs_err <= eps: continue rel_err = abs_err / abs(c) if rel_err <= eps: continue print(0) return print(1) if __name__ == "__main__": main()
true
355/B
355
B
Python 3
TESTS
0
30
0
232588311
def min_burles_sum(c1, c2, c3, c4, n, m, bus_rides, trolley_rides): bus_cost = min(c1 * ride for ride in bus_rides) trolley_cost = min(c1 * ride for ride in trolley_rides) unlimited_both_cost = min(c4, min(c2, c3) * (min(bus_rides) + min(trolley_rides))) return min(bus_cost + trolley_cost, unlimited_both_cost) c1, c2, c3, c4 = map(int, input().split()) n, m = map(int, input().split()) bus_rides = list(map(int, input().split())) trolley_rides = list(map(int, input().split())) result = min_burles_sum(c1, c2, c3, c4, n, m, bus_rides, trolley_rides) print(result)
27
46
307,200
4779983
c1,c2,c3,c4=map(int, input().split()) n,m=map(int, input().split()) a=list(map(int, input().split())) b=list(map(int, input().split())) mn1,mn2=0,0 for i in range(n): if(a[i]<c2//c1+1): mn1+=c1*a[i] else: mn1+=c2 for i in range(m): if(b[i]<c2//c1+1): mn2+=c1*b[i] else: mn2+=c2 print(min(c4,2*c3,mn1+c3,mn2+c3,mn1+mn2))
Codeforces Round 206 (Div. 2)
CF
2,013
1
256
Vasya and Public Transport
Vasya often uses public transport. The transport in the city is of two types: trolleys and buses. The city has n buses and m trolleys, the buses are numbered by integers from 1 to n, the trolleys are numbered by integers from 1 to m. Public transport is not free. There are 4 types of tickets: 1. A ticket for one ride on some bus or trolley. It costs c1 burles; 2. A ticket for an unlimited number of rides on some bus or on some trolley. It costs c2 burles; 3. A ticket for an unlimited number of rides on all buses or all trolleys. It costs c3 burles; 4. A ticket for an unlimited number of rides on all buses and trolleys. It costs c4 burles. Vasya knows for sure the number of rides he is going to make and the transport he is going to use. He asked you for help to find the minimum sum of burles he will have to spend on the tickets.
The first line contains four integers c1, c2, c3, c4 (1 ≤ c1, c2, c3, c4 ≤ 1000) — the costs of the tickets. The second line contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of buses and trolleys Vasya is going to use. The third line contains n integers ai (0 ≤ ai ≤ 1000) — the number of times Vasya is going to use the bus number i. The fourth line contains m integers bi (0 ≤ bi ≤ 1000) — the number of times Vasya is going to use the trolley number i.
Print a single number — the minimum sum of burles Vasya will have to spend on the tickets.
null
In the first sample the profitable strategy is to buy two tickets of the first type (for the first bus), one ticket of the second type (for the second bus) and one ticket of the third type (for all trolleys). It totals to (2·1) + 3 + 7 = 12 burles. In the second sample the profitable strategy is to buy one ticket of the fourth type. In the third sample the profitable strategy is to buy two tickets of the third type: for all buses and for all trolleys.
[{"input": "1 3 7 19\n2 3\n2 5\n4 4 4", "output": "12"}, {"input": "4 3 2 1\n1 3\n798\n1 2 3", "output": "1"}, {"input": "100 100 8 100\n3 5\n7 94 12\n100 1 47 0 42", "output": "16"}]
1,100
["greedy", "implementation"]
27
[{"input": "1 3 7 19\r\n2 3\r\n2 5\r\n4 4 4\r\n", "output": "12\r\n"}, {"input": "4 3 2 1\r\n1 3\r\n798\r\n1 2 3\r\n", "output": "1\r\n"}, {"input": "100 100 8 100\r\n3 5\r\n7 94 12\r\n100 1 47 0 42\r\n", "output": "16\r\n"}, {"input": "3 103 945 1000\r\n7 9\r\n34 35 34 35 34 35 34\r\n0 0 0 0 0 0 0 0 0\r\n", "output": "717\r\n"}, {"input": "7 11 597 948\r\n4 1\r\n5 1 0 11\r\n7\r\n", "output": "40\r\n"}, {"input": "7 32 109 645\r\n1 3\r\n0\r\n0 0 0\r\n", "output": "0\r\n"}, {"input": "680 871 347 800\r\n10 100\r\n872 156 571 136 703 201 832 213 15 333\r\n465 435 870 95 660 237 694 594 423 405 27 866 325 490 255 989 128 345 278 125 708 210 771 848 961 448 871 190 745 343 532 174 103 999 874 221 252 500 886 129 185 208 137 425 800 34 696 39 198 981 91 50 545 885 194 583 475 415 162 712 116 911 313 488 646 189 429 756 728 30 985 114 823 111 106 447 296 430 307 388 345 458 84 156 169 859 274 934 634 62 12 839 323 831 24 907 703 754 251 938\r\n", "output": "694\r\n"}, {"input": "671 644 748 783\r\n100 10\r\n520 363 816 957 635 753 314 210 763 819 27 970 520 164 195 230 708 587 568 707 343 30 217 227 755 277 773 497 900 589 826 666 115 784 494 467 217 892 658 388 764 812 248 447 876 581 94 915 675 967 508 754 768 79 261 934 603 712 20 199 997 501 465 91 897 257 820 645 217 105 564 8 668 171 168 18 565 840 418 42 808 918 409 617 132 268 13 161 194 628 213 199 545 448 113 410 794 261 211 539\r\n147 3 178 680 701 193 697 666 846 389\r\n", "output": "783\r\n"}, {"input": "2 7 291 972\r\n63 92\r\n7 0 0 6 0 13 0 20 2 8 0 17 7 0 0 0 0 2 2 0 0 8 20 0 0 0 3 0 0 0 4 20 0 0 0 12 0 8 17 9 0 0 0 0 4 0 0 0 17 11 3 0 2 15 0 18 11 19 14 0 0 20 13\r\n0 0 0 3 7 0 0 0 0 8 13 6 15 0 7 0 0 20 0 0 12 0 12 0 15 0 0 1 11 14 0 11 12 0 0 0 0 0 16 16 0 17 20 0 11 0 0 20 14 0 16 0 3 6 12 0 0 0 0 0 15 3 0 9 17 12 20 17 0 0 0 0 15 9 0 14 10 10 1 20 16 17 20 6 6 0 0 16 4 6 0 7\r\n", "output": "494\r\n"}, {"input": "4 43 490 945\r\n63 92\r\n0 0 0 0 0 0 6 5 18 0 6 4 0 17 0 19 0 19 7 16 0 0 0 9 10 13 7 0 10 16 0 0 0 0 0 14 0 14 9 15 0 0 2 0 0 0 0 5 0 0 0 11 11 0 0 0 0 0 10 12 3 0 0\r\n0 12 0 18 7 7 0 0 9 0 0 13 17 0 18 12 4 0 0 14 18 20 0 0 12 9 17 1 19 0 11 0 5 0 0 14 0 0 16 0 19 15 9 14 7 10 0 19 19 0 0 1 0 0 0 6 0 0 0 6 0 20 1 9 0 0 10 17 5 2 5 4 16 6 0 11 0 8 13 4 0 2 0 0 13 10 0 13 0 0 8 4\r\n", "output": "945\r\n"}, {"input": "2 50 258 922\r\n42 17\r\n0 2 0 1 0 1 0 11 18 9 0 0 0 0 10 15 17 4 20 0 5 0 0 13 13 0 0 2 0 7 0 20 4 0 19 3 7 0 0 0 0 0\r\n8 4 19 0 0 19 14 17 6 0 18 0 0 0 0 9 0\r\n", "output": "486\r\n"}, {"input": "1 1 3 4\r\n2 3\r\n1 1\r\n1 1 1\r\n", "output": "4\r\n"}, {"input": "4 4 4 1\r\n1 1\r\n0\r\n0\r\n", "output": "0\r\n"}, {"input": "100 100 1 100\r\n10 10\r\n100 100 100 100 100 100 100 100 100 100\r\n100 100 100 100 100 100 100 100 100 100\r\n", "output": "2\r\n"}]
false
stdio
null
true
16/E
16
E
PyPy 3-64
TESTS
1
92
0
226065864
n = int(input()) a = [[float(x) for x in input().split()] for _ in range(n)] products = [1] * n for row in a: for c, p in enumerate(row): products[c] *= 1-p for p in products: print(f'{p:f}', end=' ')
20
1,526
10,444,800
176502052
def get_bit_count(value): n = 0 while value: n += 1 value &= value-1 return n def pMove(ss, s, p, fish, n): num = 0 x = get_bit_count(ss) den = (x * (x - 1)) / 2 for i in range(n): if s & (1 << i) != 0: num += p[i][fish] return num / den def dp(n, p, s, memo): if s == 2 ** n - 1: return 1.0 if memo[s] > -0.9: return memo[s] ans = 0 for i in range(n): if s & (1 << i) == 0: ss = s ^ (1 << i) ans += pMove(ss, s, p, i, n) * dp(n, p, ss, memo) memo[s] = ans return ans def main(): n = int(input()) memo = [-1] * (2 ** n) p = [[0] * n for _ in range(n)] for i in range(n): p[i] = [float(x) for x in input().split()] for i in range(n): print(round(dp(n, p, 1 << i, memo), 6), end=" ") main()
Codeforces Beta Round 16 (Div. 2 Only)
ICPC
2,010
3
128
Fish
n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability aij, and the second will eat up the first with the probability aji = 1 - aij. The described process goes on until there are at least two fish in the lake. For each fish find out the probability that it will survive to be the last in the lake.
The first line contains integer n (1 ≤ n ≤ 18) — the amount of fish in the lake. Then there follow n lines with n real numbers each — matrix a. aij (0 ≤ aij ≤ 1) — the probability that fish with index i eats up fish with index j. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: aij = 1 - aji. All real numbers are given with not more than 6 characters after the decimal point.
Output n space-separated real numbers accurate to not less than 6 decimal places. Number with index i should be equal to the probability that fish with index i will survive to be the last in the lake.
null
null
[{"input": "2\n0 0.5\n0.5 0", "output": "0.500000 0.500000"}, {"input": "5\n0 1 1 1 1\n0 0 0.5 0.5 0.5\n0 0.5 0 0.5 0.5\n0 0.5 0.5 0 0.5\n0 0.5 0.5 0.5 0", "output": "1.000000 0.000000 0.000000 0.000000 0.000000"}]
1,900
["bitmasks", "dp", "probabilities"]
20
[{"input": "2\r\n0 0.5\r\n0.5 0\r\n", "output": "0.500000 0.500000 "}, {"input": "4\r\n0 0.5 0.5 0.5\r\n0.5 0 0.5 0.5\r\n0.5 0.5 0 0.5\r\n0.5 0.5 0.5 0\r\n", "output": "0.250000 0.250000 0.250000 0.250000 "}, {"input": "5\r\n0 1 1 1 1\r\n0 0 0.5 0.5 0.5\r\n0 0.5 0 0.5 0.5\r\n0 0.5 0.5 0 0.5\r\n0 0.5 0.5 0.5 0\r\n", "output": "1.000000 0.000000 0.000000 0.000000 0.000000 "}, {"input": "1\r\n0.000\r\n", "output": "1.000000 "}, {"input": "2\r\n0.000 0.551\r\n0.449 0.000\r\n", "output": "0.551000 0.449000 "}, {"input": "3\r\n0.000 0.817 0.584\r\n0.183 0.000 0.665\r\n0.416 0.335 0.000\r\n", "output": "0.564400 0.208967 0.226632 "}, {"input": "4\r\n0.000 0.083 0.548 0.503\r\n0.917 0.000 0.395 0.144\r\n0.452 0.605 0.000 0.991\r\n0.497 0.856 0.009 0.000\r\n", "output": "0.163512 0.222554 0.463543 0.150390 "}, {"input": "5\r\n0.000 0.349 0.202 0.088 0.431\r\n0.651 0.000 0.435 0.627 0.564\r\n0.798 0.565 0.000 0.725 0.949\r\n0.912 0.373 0.275 0.000 0.027\r\n0.569 0.436 0.051 0.973 0.000\r\n", "output": "0.059303 0.233839 0.494324 0.093917 0.118617 "}, {"input": "8\r\n0.000 0.147 0.783 0.224 0.220 0.651 0.453 0.209\r\n0.853 0.000 0.246 0.076 0.018 0.349 0.896 0.315\r\n0.217 0.754 0.000 0.307 0.968 0.400 0.531 0.086\r\n0.776 0.924 0.693 0.000 0.707 0.842 0.116 0.949\r\n0.780 0.982 0.032 0.293 0.000 0.908 0.307 0.266\r\n0.349 0.651 0.600 0.158 0.092 0.000 0.066 0.909\r\n0.547 0.104 0.469 0.884 0.693 0.934 0.000 0.251\r\n0.791 0.685 0.914 0.051 0.734 0.091 0.749 0.000\r\n", "output": "0.056312 0.054963 0.091124 0.315966 0.093803 0.056812 0.187952 0.143068 "}]
false
stdio
import sys def read_floats_from_file(path): with open(path, 'r') as f: line = f.readline().strip() return list(map(float, line.split())) def main(): input_path, output_path, submission_path = sys.argv[1:4] expected = read_floats_from_file(output_path) submission = read_floats_from_file(submission_path) if len(expected) != len(submission): print(0) return eps = 1e-6 for e, s in zip(expected, submission): if abs(e - s) > eps + 1e-12: print(0) return print(1) if __name__ == "__main__": main()
true
961/E
961
E
PyPy 3
TESTS
3
218
0
58887987
from sys import stdin from sys import setrecursionlimit as SRL; SRL(10**7) rd = stdin.readline rrd = lambda: map(int, rd().strip().split()) n = int(rd()) a = list(rrd()) bit = [0] * (n+10) def get(x): tot = 0 while x: tot += bit[x] x -= x&(-x) return tot def ins(x): while x<=(n+1): bit[x] += 1 x += x&(-x) b = [] preans = 0 for i,v in enumerate(a): if v > i: preans += 1 b.append([i+1,v]) b.sort(key=lambda x:x[1]) ans = 0 i = 0 for x in b: while i<n and i < x[0]: ins(a[i]) i += 1 ans += i - get(x[0] - 1) print((ans-preans)//2)
30
498
44,236,800
128480912
from collections import defaultdict import sys input = sys.stdin.readline def make_tree(n): tree = [0] * (n + 1) return tree def get_sum(i): s = 0 while i > 0: s += tree[i] i -= i & -i return s def get_sum_segment(s, t): if s > t: return 0 ans = get_sum(t) - get_sum(s - 1) return ans def add(i, x): while i <= n: tree[i] += x i += i & -i n = int(input()) a = list(map(int, input().split())) d = defaultdict(lambda : []) tree = make_tree(n) for i in range(n): a[i] = min(a[i], n) d[a[i]].append(i) add(i + 1, 1) ans = 0 for i in range(n): ans += get_sum_segment(i + 2, a[i]) for j in d[i + 1]: add(j + 1, -1) print(ans)
Educational Codeforces Round 41 (Rated for Div. 2)
ICPC
2,018
2
256
Tufurama
One day Polycarp decided to rewatch his absolute favourite episode of well-known TV series "Tufurama". He was pretty surprised when he got results only for season 7 episode 3 with his search query of "Watch Tufurama season 3 episode 7 online full hd free". This got Polycarp confused — what if he decides to rewatch the entire series someday and won't be able to find the right episodes to watch? Polycarp now wants to count the number of times he will be forced to search for an episode using some different method. TV series have n seasons (numbered 1 through n), the i-th season has ai episodes (numbered 1 through ai). Polycarp thinks that if for some pair of integers x and y (x < y) exist both season x episode y and season y episode x then one of these search queries will include the wrong results. Help Polycarp to calculate the number of such pairs!
The first line contains one integer n (1 ≤ n ≤ 2·105) — the number of seasons. The second line contains n integers separated by space a1, a2, ..., an (1 ≤ ai ≤ 109) — number of episodes in each season.
Print one integer — the number of pairs x and y (x < y) such that there exist both season x episode y and season y episode x.
null
Possible pairs in the second example: 1. x = 1, y = 2 (season 1 episode 2 $$\text{The formula used to produce the text is not provided in the image.}$$ season 2 episode 1); 2. x = 2, y = 3 (season 2 episode 3 $$\text{The formula used to produce the text is not provided in the image.}$$ season 3 episode 2); 3. x = 1, y = 3 (season 1 episode 3 $$\text{The formula used to produce the text is not provided in the image.}$$ season 3 episode 1). In the third example: 1. x = 1, y = 2 (season 1 episode 2 $$\text{The formula used to produce the text is not provided in the image.}$$ season 2 episode 1); 2. x = 1, y = 3 (season 1 episode 3 $$\text{The formula used to produce the text is not provided in the image.}$$ season 3 episode 1).
[{"input": "5\n1 2 3 4 5", "output": "0"}, {"input": "3\n8 12 7", "output": "3"}, {"input": "3\n3 2 1", "output": "2"}]
1,900
["data structures"]
30
[{"input": "5\r\n1 2 3 4 5\r\n", "output": "0\r\n"}, {"input": "3\r\n8 12 7\r\n", "output": "3\r\n"}, {"input": "3\r\n3 2 1\r\n", "output": "2\r\n"}, {"input": "5\r\n2 3 4 5 6\r\n", "output": "4\r\n"}, {"input": "8\r\n7 2 6 6 5 1 4 9\r\n", "output": "9\r\n"}, {"input": "10\r\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\r\n", "output": "45\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}]
false
stdio
null
true
962/E
962
E
PyPy 3
TESTS
0
61
20,172,800
129567902
class SegTree: def __init__(self, init_val, ide_ele, segfunc): self.n = len(init_val) self.num = 2**(self.n-1).bit_length() self.ide_ele = ide_ele self.segfunc = segfunc self.seg = [ide_ele]*2*self.num # set_val for i in range(self.n): self.seg[i+self.num] = init_val[i] # built for i in range(self.num-1, 0, -1): self.seg[i] = self.segfunc(self.seg[2*i], self.seg[2*i+1]) def update(self, k, x): k += self.num self.seg[k] = x while k: k = k >> 1 self.seg[k] = self.segfunc(self.seg[2*k], self.seg[2*k+1]) def query(self, l, r): if r <= l: return self.ide_ele l += self.num r += self.num lres = self.ide_ele rres = self.ide_ele while l < r: if r & 1: r -= 1 rres = self.segfunc(self.seg[r], rres) if l & 1: lres = self.segfunc(lres, self.seg[l]) l += 1 l = l >> 1 r = r >> 1 res = self.segfunc(lres, rres) return res def __str__(self): # for debug arr = [self.query(i,i+1) for i in range(self.n)] return str(arr) INF = 10**18 import sys import io, os input = sys.stdin.readline n = int(input()) B = [] R = [] P = [] for i in range(n): x, c = map(str, input().split()) x = int(x) if c == 'B': B.append(x) elif c == 'R': R.append(x) else: P.append(x) if len(P) <= 1: B += P R += P B.sort() R.sort() ans = B[-1]-B[0]+R[-1]-R[0] print(ans) exit() B.sort() R.sort() P.sort() BD = [B[i+1]-B[i] for i in range(len(B)-1)] RD = [R[i+1]-R[i] for i in range(len(R)-1)] BM = SegTree(BD, -INF, max) RM = SegTree(RD, -INF, max) ans = 0 import bisect for i, p in enumerate(P): bl = bisect.bisect_right(B, p) br = bisect.bisect_left(B, p) rl = bisect.bisect_right(R, p) rr = bisect.bisect_left(R, p) if i == 0: if bl != 0: ans += p-B[0] if rl != 0: ans += p-R[0] elif i == len(P)-1: if br != len(B): ans += B[-1]-p if rr != len(R): ans += R[-1]-p if i != len(P)-1: if bl != br and rl != rr: d1 = (P[i+1]-P[i])*2 d2 = (P[i+1]-P[i])*3-BM.query(bl, br)-RM.query(rl, rr) ans += min(d1, d2) elif bl == br and rl == rr: ans += P[i+1]-P[i] else: ans += (P[i+1]-P[i])*2 print(i, ans) print(ans)
45
358
11,776,000
129791972
import sys input = sys.stdin.readline def solve(): n = int(input()) p = None B = [] R = [] r = 0 for i in range(n): x, c = input().split() x = int(x) if c == 'B': B.append(x) elif c == 'R': R.append(x) else: if p is None: if len(B) != 0: r += x - B[0] if len(R) != 0: r += x - R[0] else: if len(B) != 0: g = -1 for i in range(1,len(B)): g = max(g, B[i] - B[i-1]) g = max(g, x - B[-1], B[0] - p) else: g = x - p if len(R) != 0: h = -1 for i in range(1,len(R)): h = max(h, R[i] - R[i-1]) h = max(h, x - R[-1], R[0] - p) else: h = x - p r += min(2 * (x-p), 3 * (x - p) - g - h) p = x B = [] R = [] if p is None: if len(B) > 0: r += B[-1] - B[0] if len(R) > 0: r += R[-1] - R[0] else: if len(B) != 0: r += B[-1] - p if len(R) != 0: r += R[-1] - p print(r) solve()
Educational Codeforces Round 42 (Rated for Div. 2)
ICPC
2,018
2
256
Byteland, Berland and Disputed Cities
The cities of Byteland and Berland are located on the axis $$$Ox$$$. In addition, on this axis there are also disputed cities, which belong to each of the countries in their opinion. Thus, on the line $$$Ox$$$ there are three types of cities: - the cities of Byteland, - the cities of Berland, - disputed cities. Recently, the project BNET has been launched — a computer network of a new generation. Now the task of the both countries is to connect the cities so that the network of this country is connected. The countries agreed to connect the pairs of cities with BNET cables in such a way that: - If you look at the only cities of Byteland and the disputed cities, then in the resulting set of cities, any city should be reachable from any other one by one or more cables, - If you look at the only cities of Berland and the disputed cities, then in the resulting set of cities, any city should be reachable from any other one by one or more cables. Thus, it is necessary to choose a set of pairs of cities to connect by cables in such a way that both conditions are satisfied simultaneously. Cables allow bi-directional data transfer. Each cable connects exactly two distinct cities. The cost of laying a cable from one city to another is equal to the distance between them. Find the minimum total cost of laying a set of cables so that two subsets of cities (Byteland and disputed cities, Berland and disputed cities) are connected. Each city is a point on the line $$$Ox$$$. It is technically possible to connect the cities $$$a$$$ and $$$b$$$ with a cable so that the city $$$c$$$ ($$$a < c < b$$$) is not connected to this cable, where $$$a$$$, $$$b$$$ and $$$c$$$ are simultaneously coordinates of the cities $$$a$$$, $$$b$$$ and $$$c$$$.
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^{5}$$$) — the number of cities. The following $$$n$$$ lines contains an integer $$$x_i$$$ and the letter $$$c_i$$$ ($$$-10^{9} \le x_i \le 10^{9}$$$) — the coordinate of the city and its type. If the city belongs to Byteland, $$$c_i$$$ equals to 'B'. If the city belongs to Berland, $$$c_i$$$ equals to «R». If the city is disputed, $$$c_i$$$ equals to 'P'. All cities have distinct coordinates. Guaranteed, that the cities are given in the increasing order of their coordinates.
Print the minimal total length of such set of cables, that if we delete all Berland cities ($$$c_i$$$='R'), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables. Similarly, if we delete all Byteland cities ($$$c_i$$$='B'), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables.
null
In the first example, you should connect the first city with the second, the second with the third, and the third with the fourth. The total length of the cables will be $$$5 + 3 + 4 = 12$$$. In the second example there are no disputed cities, so you need to connect all the neighboring cities of Byteland and all the neighboring cities of Berland. The cities of Berland have coordinates $$$10, 21, 32$$$, so to connect them you need two cables of length $$$11$$$ and $$$11$$$. The cities of Byteland have coordinates $$$14$$$ and $$$16$$$, so to connect them you need one cable of length $$$2$$$. Thus, the total length of all cables is $$$11 + 11 + 2 = 24$$$.
[{"input": "4\n-5 R\n0 P\n3 P\n7 B", "output": "12"}, {"input": "5\n10 R\n14 B\n16 B\n21 R\n32 R", "output": "24"}]
2,200
["constructive algorithms", "greedy"]
45
[{"input": "4\r\n-5 R\r\n0 P\r\n3 P\r\n7 B\r\n", "output": "12\r\n"}, {"input": "5\r\n10 R\r\n14 B\r\n16 B\r\n21 R\r\n32 R\r\n", "output": "24\r\n"}, {"input": "10\r\n66 R\r\n67 R\r\n72 R\r\n73 R\r\n76 R\r\n78 B\r\n79 B\r\n83 B\r\n84 B\r\n85 P\r\n", "output": "26\r\n"}, {"input": "10\r\n61 R\r\n64 R\r\n68 R\r\n71 R\r\n72 R\r\n73 R\r\n74 P\r\n86 P\r\n87 B\r\n90 B\r\n", "output": "29\r\n"}, {"input": "15\r\n-9518 R\r\n-6858 P\r\n-6726 B\r\n-6486 R\r\n-4496 P\r\n-4191 P\r\n-772 B\r\n-258 R\r\n-194 P\r\n1035 R\r\n2297 P\r\n4816 B\r\n5779 R\r\n9342 B\r\n9713 B\r\n", "output": "25088\r\n"}, {"input": "6\r\n-8401 R\r\n-5558 P\r\n-3457 P\r\n-2361 R\r\n6966 P\r\n8140 B\r\n", "output": "17637\r\n"}, {"input": "2\r\n1 R\r\n2 R\r\n", "output": "1\r\n"}, {"input": "2\r\n-1000000000 B\r\n1000000000 R\r\n", "output": "0\r\n"}, {"input": "2\r\n-1000000000 P\r\n1000000000 P\r\n", "output": "2000000000\r\n"}, {"input": "2\r\n-1000000000 B\r\n1000000000 P\r\n", "output": "2000000000\r\n"}, {"input": "9\r\n-105 R\r\n-81 B\r\n-47 P\r\n-25 R\r\n-23 B\r\n55 P\r\n57 R\r\n67 B\r\n76 P\r\n", "output": "272\r\n"}, {"input": "6\r\n-13 R\r\n-10 P\r\n-6 R\r\n-1 P\r\n4 R\r\n10 P\r\n", "output": "32\r\n"}, {"input": "8\r\n-839 P\r\n-820 P\r\n-488 P\r\n-334 R\r\n-83 B\r\n187 R\r\n380 B\r\n804 P\r\n", "output": "2935\r\n"}, {"input": "8\r\n-12 P\r\n-9 B\r\n-2 R\r\n-1 R\r\n2 B\r\n8 B\r\n9 R\r\n15 P\r\n", "output": "54\r\n"}, {"input": "6\r\n0 B\r\n3 P\r\n7 B\r\n9 B\r\n11 P\r\n13 B\r\n", "output": "17\r\n"}]
false
stdio
null
true
665/B
665
B
PyPy 3-64
TESTS
3
46
0
147572563
n,m,k=map(int,input().split()) arr=list(map(int,input().split())) quer=[] for i in range(m): quer.append(list(map(int,input().split()))) ans=0 for i in range(m): a=quer[i] for j in a: d=arr.index(j) ans+=d+1 for ind in range(d-1,-1,-1): arr[ind+1]=arr[ind] arr[0]=j print(ans)
10
61
5,120,000
18482341
ln=input().split(" ") n=int(ln[0]) m=int(ln[1]) k=int(ln[2]) x=0 pila=input().split(" ") for i in range(n): cus=input().split(" ") for j in range(m): ind=pila.index(cus[j]) x=x+ind+1 pila.insert(0,pila.pop(ind)) print(x)
Educational Codeforces Round 12
ICPC
2,016
1
256
Shopping
Ayush is a cashier at the shopping center. Recently his department has started a ''click and collect" service which allows users to shop online. The store contains k items. n customers have already used the above service. Each user paid for m items. Let aij denote the j-th item in the i-th person's order. Due to the space limitations all the items are arranged in one single row. When Ayush receives the i-th order he will find one by one all the items aij (1 ≤ j ≤ m) in the row. Let pos(x) denote the position of the item x in the row at the moment of its collection. Then Ayush takes time equal to pos(ai1) + pos(ai2) + ... + pos(aim) for the i-th customer. When Ayush accesses the x-th element he keeps a new stock in the front of the row and takes away the x-th element. Thus the values are updating. Your task is to calculate the total time it takes for Ayush to process all the orders. You can assume that the market has endless stock.
The first line contains three integers n, m and k (1 ≤ n, k ≤ 100, 1 ≤ m ≤ k) — the number of users, the number of items each user wants to buy and the total number of items at the market. The next line contains k distinct integers pl (1 ≤ pl ≤ k) denoting the initial positions of the items in the store. The items are numbered with integers from 1 to k. Each of the next n lines contains m distinct integers aij (1 ≤ aij ≤ k) — the order of the i-th person.
Print the only integer t — the total time needed for Ayush to process all the orders.
null
Customer 1 wants the items 1 and 5. pos(1) = 3, so the new positions are: [1, 3, 4, 2, 5]. pos(5) = 5, so the new positions are: [5, 1, 3, 4, 2]. Time taken for the first customer is 3 + 5 = 8. Customer 2 wants the items 3 and 1. pos(3) = 3, so the new positions are: [3, 5, 1, 4, 2]. pos(1) = 3, so the new positions are: [1, 3, 5, 4, 2]. Time taken for the second customer is 3 + 3 = 6. Total time is 8 + 6 = 14. Formally pos(x) is the index of x in the current row.
[{"input": "2 2 5\n3 4 1 2 5\n1 5\n3 1", "output": "14"}]
1,400
["brute force"]
10
[{"input": "2 2 5\r\n3 4 1 2 5\r\n1 5\r\n3 1\r\n", "output": "14\r\n"}, {"input": "4 4 4\r\n1 2 3 4\r\n3 4 2 1\r\n4 3 2 1\r\n4 1 2 3\r\n4 1 2 3\r\n", "output": "59\r\n"}, {"input": "1 1 1\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "10 1 100\r\n1 55 67 75 40 86 24 84 82 26 81 23 70 79 51 54 21 78 31 98 68 93 66 88 99 65 20 52 35 85 16 12 94 100 59 56 18 33 47 46 71 8 38 57 2 92 3 95 6 4 87 22 48 80 15 29 11 45 72 76 44 60 91 90 39 74 41 36 13 27 53 83 32 5 30 63 89 64 49 17 9 97 69 14 50 77 37 96 10 42 28 34 61 19 73 7 62 43 58 25\r\n33\r\n69\r\n51\r\n7\r\n68\r\n70\r\n1\r\n35\r\n24\r\n7\r\n", "output": "335\r\n"}, {"input": "100 1 1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n", "output": "100\r\n"}, {"input": "3 2 3\r\n3 1 2\r\n1 2\r\n2 1\r\n2 3\r\n", "output": "13\r\n"}, {"input": "10 10 10\r\n3 4 1 2 8 9 5 10 6 7\r\n9 10 7 8 6 1 2 3 4 5\r\n2 5 3 6 1 4 9 7 8 10\r\n2 9 1 8 4 7 5 10 6 3\r\n10 9 7 1 3 6 2 8 5 4\r\n2 5 1 3 7 10 4 9 8 6\r\n6 1 8 7 9 2 3 5 4 10\r\n1 3 2 8 6 9 4 10 5 7\r\n5 2 4 8 6 1 10 9 3 7\r\n5 1 7 10 4 6 2 8 9 3\r\n2 1 3 9 7 10 6 4 8 5\r\n", "output": "771\r\n"}]
false
stdio
null
true
241/A
241
A
PyPy 3
TESTS
5
248
102,400
62920711
# CF 241/A 1300 # S = supply in city i # D = distance from c[i] to c[i + 1] # k = refresh interval # min time to arrive at C[n] where n = m + 1 def f(k, D, S): n = len(D) lastpositive = n fuel = 0 for i in range(n - 1, -1, -1): fuel += (S[i] - D[i]) if fuel >= 0: lastpositive = i fuel = 0 time = sum(D) if lastpositive is not None: maxprev = [] for i in range(lastpositive): if len(maxprev) > 0: idx = maxprev[-1] if S[i] > S[idx]: maxprev.append(i) else: maxprev.append(idx) else: maxprev.append(i) fuel = 0 for i in range(lastpositive - 1, -1, -1): if fuel > 0: fuel = 0 fuel += (S[i] - D[i]) if i == maxprev[i]: # best station for i..lastpos while fuel < 0: time += k fuel += S[i] return time assert f(10, [5], [5]) == 5 assert f(10, [6], [5]) == 16 assert f(6, [1, 2, 5, 2], [2, 3, 3, 4]) == 10 assert f(5, [10, 10, 10], [5, 10, 1]) == 40 assert f(3, [5, 6], [5, 5]) == 14 assert f(3, [11, 8, 8, 12, 17, 4, 4, 25, 39, 37, 31, 32, 38, 34, 29, 29, 34, 39, 39, 39, 17, 9, 24, 6], [3, 5, 4, 3, 3, 3, 4, 3, 4, 3, 3, 3, 3, 4, 3, 3, 4, 3, 4, 3, 3, 3, 3, 3]) == 862 m, k = map(int, input().split()) D = list(map(int, input().split())) S = list(map(int, input().split())) ans = f(k, D, S) print(ans)
52
62
307,200
4927898
n, k = map(int, input().split()) l, a = list(map(int, input().split())), list(map(int, input().split())) v, t, s = 0, 0, sum(l) for i in range(n): l[i] -= a[i] L, A = [l[0]], [a[0]] for i in range(1, n): if a[i] <= A[-1]: L[-1] += l[i] else: A.append(a[i]) L.append(l[i]) for i in range(len(A)): d = L[i] - v if d > 0: u = (d - 1) // A[i] + 1 v += u * A[i] t += u * k v -= L[i] print(t + s)
Bayan 2012-2013 Elimination Round (ACM ICPC Rules, English statements)
ICPC
2,012
2
256
Old Peykan
There are n cities in the country where the Old Peykan lives. These cities are located on a straight line, we'll denote them from left to right as c1, c2, ..., cn. The Old Peykan wants to travel from city c1 to cn using roads. There are (n - 1) one way roads, the i-th road goes from city ci to city ci + 1 and is di kilometers long. The Old Peykan travels 1 kilometer in 1 hour and consumes 1 liter of fuel during this time. Each city ci (except for the last city cn) has a supply of si liters of fuel which immediately transfers to the Old Peykan if it passes the city or stays in it. This supply refreshes instantly k hours after it transfers. The Old Peykan can stay in a city for a while and fill its fuel tank many times. Initially (at time zero) the Old Peykan is at city c1 and s1 liters of fuel is transferred to it's empty tank from c1's supply. The Old Peykan's fuel tank capacity is unlimited. Old Peykan can not continue its travel if its tank is emptied strictly between two cities. Find the minimum time the Old Peykan needs to reach city cn.
The first line of the input contains two space-separated integers m and k (1 ≤ m, k ≤ 1000). The value m specifies the number of roads between cities which is equal to n - 1. The next line contains m space-separated integers d1, d2, ..., dm (1 ≤ di ≤ 1000) and the following line contains m space-separated integers s1, s2, ..., sm (1 ≤ si ≤ 1000).
In the only line of the output print a single integer — the minimum time required for The Old Peykan to reach city cn from city c1.
null
In the second sample above, the Old Peykan stays in c1 for 3 hours.
[{"input": "4 6\n1 2 5 2\n2 3 3 4", "output": "10"}, {"input": "2 3\n5 6\n5 5", "output": "14"}]
1,300
["greedy"]
52
[{"input": "4 6\r\n1 2 5 2\r\n2 3 3 4\r\n", "output": "10\r\n"}, {"input": "2 3\r\n5 6\r\n5 5\r\n", "output": "14\r\n"}, {"input": "24 3\r\n11 8 8 12 17 4 4 25 39 37 31 32 38 34 29 29 34 39 39 39 17 9 24 6\r\n3 5 4 3 3 3 4 3 4 3 3 3 3 4 3 3 4 3 4 3 3 3 3 3\r\n", "output": "862\r\n"}, {"input": "43 5\r\n6 7 15 12 15 7 22 33 38 15 7 23 31 21 26 41 25 14 26 33 5 28 22 6 35 17 19 32 41 27 20 25 5 32 37 19 40 9 25 22 10 24 9\r\n3 5 3 6 5 4 5 3 3 3 3 6 6 3 3 3 3 3 3 3 3 6 3 3 4 3 4 3 6 4 3 6 3 4 6 3 4 5 4 4 3 3 5\r\n", "output": "1566\r\n"}]
false
stdio
null
true
1298/E
978
F
PyPy 3-64
TESTS
4
62
0
230899558
class UFDS: def __init__(self, n): self.parents = list(range(n)) self.ranks = [0] * n self.sizes = [1] * n self.numdisjoint = n def find(self, x): xp = x children = [] while xp != self.parents[xp]: children.append(xp) xp = self.parents[xp] for c in children: self.parents[c] = xp return xp def union(self, a, b): ap = self.find(a) bp = self.find(b) if ap == bp: return if self.ranks[ap] < self.ranks[bp]: self.parents[ap] = bp self.sizes[bp] += self.sizes[ap] elif self.ranks[bp] < self.ranks[ap]: self.parents[bp] = ap self.sizes[ap] += self.sizes[bp] else: self.parents[bp] = ap self.ranks[ap] += 1 self.sizes[ap] += self.sizes[bp] self.numdisjoint -= 1 def size(self, x): return self.sizes[self.find(x)] if __name__ == "__main__": n, k = map(int, input().split()) programers = list(map(int, input().split())) #programers.sort() UnionFind = UFDS(n) for _ in range(k): a, b = map(int, input().split()) UnionFind.union(a - 1, b - 1) ans = [0] * n for i in range(n): for j in range(0, n): #print(i, j, UnionFind.find(i), UnionFind.find(j)) if UnionFind.find(i) != UnionFind.find(j) and programers[i] > programers[j]: ans[i] += 1 print(*ans)
41
561
33,177,600
226929994
# https://codeforces.com/contest/978 import sys input = lambda: sys.stdin.readline().rstrip() # faster! n, k = map(int, input().split()) r = list(map(int, input().split())) ans = [0] * n idx = sorted(range(n), key=lambda i: r[i]) duplicate = 0 for i in range(1, n): if r[idx[i]] == r[idx[i - 1]]: ans[idx[i]] = ans[idx[i - 1]] duplicate += 1 else: ans[idx[i]] = ans[idx[i - 1]] + 1 + duplicate duplicate = 0 for _ in range(k): x, y = map(lambda x: int(x) - 1, input().split()) if r[x] < r[y]: ans[y] -= 1 elif r[x] > r[y]: ans[x] -= 1 print(*ans)
Kotlin Heroes: Practice 3
ICPC
2,020
3
256
Mentors
In BerSoft $$$n$$$ programmers work, the programmer $$$i$$$ is characterized by a skill $$$r_i$$$. A programmer $$$a$$$ can be a mentor of a programmer $$$b$$$ if and only if the skill of the programmer $$$a$$$ is strictly greater than the skill of the programmer $$$b$$$ $$$(r_a > r_b)$$$ and programmers $$$a$$$ and $$$b$$$ are not in a quarrel. You are given the skills of each programmers and a list of $$$k$$$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $$$i$$$, find the number of programmers, for which the programmer $$$i$$$ can be a mentor.
The first line contains two integers $$$n$$$ and $$$k$$$ $$$(2 \le n \le 2 \cdot 10^5$$$, $$$0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$$$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $$$r_1, r_2, \dots, r_n$$$ $$$(1 \le r_i \le 10^{9})$$$, where $$$r_i$$$ equals to the skill of the $$$i$$$-th programmer. Each of the following $$$k$$$ lines contains two distinct integers $$$x$$$, $$$y$$$ $$$(1 \le x, y \le n$$$, $$$x \ne y)$$$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $$$x$$$ is in a quarrel with $$$y$$$ then $$$y$$$ is in a quarrel with $$$x$$$. Guaranteed, that for each pair $$$(x, y)$$$ there are no other pairs $$$(x, y)$$$ and $$$(y, x)$$$ in the input.
Print $$$n$$$ integers, the $$$i$$$-th number should be equal to the number of programmers, for which the $$$i$$$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input.
null
In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
[{"input": "4 2\n10 4 10 15\n1 2\n4 3", "output": "0 0 1 2"}, {"input": "10 4\n5 4 1 5 4 3 7 1 2 5\n4 6\n2 1\n10 8\n3 5", "output": "5 4 0 5 3 3 9 0 2 5"}]
null
["*special", "data structures", "implementation"]
41
[{"input": "4 2\r\n10 4 10 15\r\n1 2\r\n4 3\r\n", "output": "0 0 1 2 \r\n"}, {"input": "10 4\r\n5 4 1 5 4 3 7 1 2 5\r\n4 6\r\n2 1\r\n10 8\r\n3 5\r\n", "output": "5 4 0 5 3 3 9 0 2 5 \r\n"}, {"input": "2 0\r\n3 1\r\n", "output": "1 0 \r\n"}, {"input": "2 0\r\n1 1\r\n", "output": "0 0 \r\n"}, {"input": "10 35\r\n322022227 751269818 629795150 369443545 344607287 250044294 476897672 184054549 986884572 917181121\r\n6 3\r\n7 3\r\n1 9\r\n7 9\r\n10 7\r\n3 4\r\n8 6\r\n7 4\r\n6 10\r\n7 2\r\n3 5\r\n6 9\r\n3 10\r\n8 7\r\n6 5\r\n8 1\r\n8 5\r\n1 7\r\n8 10\r\n8 2\r\n1 5\r\n10 4\r\n6 7\r\n4 6\r\n2 6\r\n5 4\r\n9 10\r\n9 2\r\n4 8\r\n5 9\r\n4 1\r\n3 2\r\n2 1\r\n4 2\r\n9 8\r\n", "output": "1 1 2 0 0 0 1 0 2 3 \r\n"}]
false
stdio
null
true
1298/E
978
F
PyPy 3-64
TESTS
4
62
0
213140394
from collections import * import sys input = sys.stdin.readline MOD = 10**9 + 7 def read_array(factory): return [factory(num) for num in input().strip().split()] def print_array(arr): print(" ".join(map(str, arr))) # Left insertion point since strictly greater for mentor def bin_search(arr, target): lo, hi = 0, len(arr) while lo < hi: mid = (lo + hi) // 2 if arr[mid] < target: lo = mid + 1 else: hi = mid return lo def solve(r): # All programmers not in the same connected component with skill # strictly greater # O(nlogn) r_sorted = sorted(r) mentors = {} for i in range(len(r)): mentors[i] = bin_search(r_sorted, r[i]) # Ratings for each component components = defaultdict(list) for i in range(len(r)): components[find(i)].append(r[i]) for c in components: components[c].sort() for i in range(len(r)): mentors[i] -= bin_search(components[find(i)], r[i]) return [mentors[i] for i in range(len(r))] #################### # Read in input: n, k = read_array(int) r = read_array(int) roots = [i for i in range(n)] ranks = [1 for _ in range(n)] def find(i): if roots[i] == i: return i roots[i] = find(roots[i]) return roots[i] def union(i, j): root_i = find(i) root_j = find(j) if root_i != root_j: if ranks[root_i] < ranks[root_j]: roots[root_i] = roots[root_j] elif ranks[root_i] == ranks[root_j]: roots[root_i] = roots[root_j] ranks[root_j] += 1 else: roots[root_j] = roots[root_i] for _ in range(k): x, y = read_array(int) x, y = x-1, y-1 union(x, y) ans = solve(r) print_array(ans)
41
576
52,736,000
172445737
import sys input = sys.stdin.readline import bisect n, k = map(int, input().split()) w = list(map(int, input().split())) q = sorted(w) d = [[] for i in range(n)] for i in range(k): a, b = map(lambda x:int(x)-1, input().split()) d[a].append(b) d[b].append(a) for i in range(n): x = bisect.bisect_left(q, w[i]) for j in d[i]: if w[j] < w[i]: x -= 1 print(max(x, 0), end=' ')
Kotlin Heroes: Practice 3
ICPC
2,020
3
256
Mentors
In BerSoft $$$n$$$ programmers work, the programmer $$$i$$$ is characterized by a skill $$$r_i$$$. A programmer $$$a$$$ can be a mentor of a programmer $$$b$$$ if and only if the skill of the programmer $$$a$$$ is strictly greater than the skill of the programmer $$$b$$$ $$$(r_a > r_b)$$$ and programmers $$$a$$$ and $$$b$$$ are not in a quarrel. You are given the skills of each programmers and a list of $$$k$$$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $$$i$$$, find the number of programmers, for which the programmer $$$i$$$ can be a mentor.
The first line contains two integers $$$n$$$ and $$$k$$$ $$$(2 \le n \le 2 \cdot 10^5$$$, $$$0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$$$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $$$r_1, r_2, \dots, r_n$$$ $$$(1 \le r_i \le 10^{9})$$$, where $$$r_i$$$ equals to the skill of the $$$i$$$-th programmer. Each of the following $$$k$$$ lines contains two distinct integers $$$x$$$, $$$y$$$ $$$(1 \le x, y \le n$$$, $$$x \ne y)$$$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $$$x$$$ is in a quarrel with $$$y$$$ then $$$y$$$ is in a quarrel with $$$x$$$. Guaranteed, that for each pair $$$(x, y)$$$ there are no other pairs $$$(x, y)$$$ and $$$(y, x)$$$ in the input.
Print $$$n$$$ integers, the $$$i$$$-th number should be equal to the number of programmers, for which the $$$i$$$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input.
null
In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
[{"input": "4 2\n10 4 10 15\n1 2\n4 3", "output": "0 0 1 2"}, {"input": "10 4\n5 4 1 5 4 3 7 1 2 5\n4 6\n2 1\n10 8\n3 5", "output": "5 4 0 5 3 3 9 0 2 5"}]
null
["*special", "data structures", "implementation"]
41
[{"input": "4 2\r\n10 4 10 15\r\n1 2\r\n4 3\r\n", "output": "0 0 1 2 \r\n"}, {"input": "10 4\r\n5 4 1 5 4 3 7 1 2 5\r\n4 6\r\n2 1\r\n10 8\r\n3 5\r\n", "output": "5 4 0 5 3 3 9 0 2 5 \r\n"}, {"input": "2 0\r\n3 1\r\n", "output": "1 0 \r\n"}, {"input": "2 0\r\n1 1\r\n", "output": "0 0 \r\n"}, {"input": "10 35\r\n322022227 751269818 629795150 369443545 344607287 250044294 476897672 184054549 986884572 917181121\r\n6 3\r\n7 3\r\n1 9\r\n7 9\r\n10 7\r\n3 4\r\n8 6\r\n7 4\r\n6 10\r\n7 2\r\n3 5\r\n6 9\r\n3 10\r\n8 7\r\n6 5\r\n8 1\r\n8 5\r\n1 7\r\n8 10\r\n8 2\r\n1 5\r\n10 4\r\n6 7\r\n4 6\r\n2 6\r\n5 4\r\n9 10\r\n9 2\r\n4 8\r\n5 9\r\n4 1\r\n3 2\r\n2 1\r\n4 2\r\n9 8\r\n", "output": "1 1 2 0 0 0 1 0 2 3 \r\n"}]
false
stdio
null
true
1298/E
978
F
Python 3
TESTS
3
31
307,200
208836481
import copy n,k=map(int,input().split()) skills=list(map(int,input().split())) mot3arkin=[[]for i in range(n+1)] for i in range(k): x,y=map(int,input().split()) mot3arkin[x].append(y) mot3arkin[y].append(x) skills1=copy.deepcopy(skills) skills1.sort(reverse=True) for i in range(1,n+1): adad=0 may9arrihech=0 for j in mot3arkin[i]: if skills[j-1]<skills[i-1]: may9arrihech+=1 f=0 while skills1[skills1.index(skills[i-1])]==skills1[skills1.index(skills[i-1])+f]: adad+=1 f+=1 if skills1.index(skills[i-1])+f==n: break adad-=1 if (n-1-skills1.index(skills[i-1])-may9arrihech-adad>0): print (n-1-skills1.index(skills[i-1])-may9arrihech-adad,end=' ') else: print(0,end=' ')
41
623
51,097,600
226929592
# https://codeforces.com/contest/978 import sys input = lambda: sys.stdin.readline().rstrip() # faster! n, k = map(int, input().split()) r = list(map(int, input().split())) xy = [tuple(map(lambda x: int(x) - 1, input().split())) for _ in range(k)] idx = sorted(range(n), key=lambda i: r[i]) ans = [0] * n duplicate = 0 for i in range(1, n): if r[idx[i]] == r[idx[i - 1]]: ans[idx[i]] = ans[idx[i - 1]] duplicate += 1 else: ans[idx[i]] = ans[idx[i - 1]] + 1 + duplicate duplicate = 0 for x, y in xy: if r[x] < r[y]: ans[y] -= 1 elif r[x] > r[y]: ans[x] -= 1 print(*ans)
Kotlin Heroes: Practice 3
ICPC
2,020
3
256
Mentors
In BerSoft $$$n$$$ programmers work, the programmer $$$i$$$ is characterized by a skill $$$r_i$$$. A programmer $$$a$$$ can be a mentor of a programmer $$$b$$$ if and only if the skill of the programmer $$$a$$$ is strictly greater than the skill of the programmer $$$b$$$ $$$(r_a > r_b)$$$ and programmers $$$a$$$ and $$$b$$$ are not in a quarrel. You are given the skills of each programmers and a list of $$$k$$$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $$$i$$$, find the number of programmers, for which the programmer $$$i$$$ can be a mentor.
The first line contains two integers $$$n$$$ and $$$k$$$ $$$(2 \le n \le 2 \cdot 10^5$$$, $$$0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$$$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $$$r_1, r_2, \dots, r_n$$$ $$$(1 \le r_i \le 10^{9})$$$, where $$$r_i$$$ equals to the skill of the $$$i$$$-th programmer. Each of the following $$$k$$$ lines contains two distinct integers $$$x$$$, $$$y$$$ $$$(1 \le x, y \le n$$$, $$$x \ne y)$$$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $$$x$$$ is in a quarrel with $$$y$$$ then $$$y$$$ is in a quarrel with $$$x$$$. Guaranteed, that for each pair $$$(x, y)$$$ there are no other pairs $$$(x, y)$$$ and $$$(y, x)$$$ in the input.
Print $$$n$$$ integers, the $$$i$$$-th number should be equal to the number of programmers, for which the $$$i$$$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input.
null
In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
[{"input": "4 2\n10 4 10 15\n1 2\n4 3", "output": "0 0 1 2"}, {"input": "10 4\n5 4 1 5 4 3 7 1 2 5\n4 6\n2 1\n10 8\n3 5", "output": "5 4 0 5 3 3 9 0 2 5"}]
null
["*special", "data structures", "implementation"]
41
[{"input": "4 2\r\n10 4 10 15\r\n1 2\r\n4 3\r\n", "output": "0 0 1 2 \r\n"}, {"input": "10 4\r\n5 4 1 5 4 3 7 1 2 5\r\n4 6\r\n2 1\r\n10 8\r\n3 5\r\n", "output": "5 4 0 5 3 3 9 0 2 5 \r\n"}, {"input": "2 0\r\n3 1\r\n", "output": "1 0 \r\n"}, {"input": "2 0\r\n1 1\r\n", "output": "0 0 \r\n"}, {"input": "10 35\r\n322022227 751269818 629795150 369443545 344607287 250044294 476897672 184054549 986884572 917181121\r\n6 3\r\n7 3\r\n1 9\r\n7 9\r\n10 7\r\n3 4\r\n8 6\r\n7 4\r\n6 10\r\n7 2\r\n3 5\r\n6 9\r\n3 10\r\n8 7\r\n6 5\r\n8 1\r\n8 5\r\n1 7\r\n8 10\r\n8 2\r\n1 5\r\n10 4\r\n6 7\r\n4 6\r\n2 6\r\n5 4\r\n9 10\r\n9 2\r\n4 8\r\n5 9\r\n4 1\r\n3 2\r\n2 1\r\n4 2\r\n9 8\r\n", "output": "1 1 2 0 0 0 1 0 2 3 \r\n"}]
false
stdio
null
true
1298/E
978
F
PyPy 3-64
TESTS
4
46
0
179489888
n, k = map(int, input().split()) t = list(map(int, input().split())) t = list(enumerate(t)) r = sorted(t, key=lambda x: x[1]) #print(r) kol = [0] * n ans = [0] * n cnt = 0 for i in range(n - 1): if r[i][1] < r[i + 1][1]: kol[i + 1] = kol[i] + 1 + cnt cnt = 0 else: kol[i + 1] = kol[i] cnt += 1 #print(kol) for i in range(len(kol)): ans[r[i][0]] = kol[i] #print(ans) for i in range(k): x, y = map(int, input().split()) if ans[x - 1] > ans[y - 1]: ans[x - 1] -= 1 elif ans[x - 1] < ans[y - 1]: ans[y - 1] -= 1 print(*ans)
41
639
57,856,000
211072288
import sys input = lambda: sys.stdin.readline().rstrip() from collections import deque,defaultdict,Counter from itertools import permutations,combinations from bisect import * from heapq import * from math import ceil,gcd,lcm,floor,comb N,M = map(int,input().split()) A = list(map(int,input().split())) P = [[] for _ in range(N)] for _ in range(M): a,b = map(int,input().split()) P[a-1].append(A[b-1]) P[b-1].append(A[a-1]) for i in range(N):P[i].sort() A1 = sorted(A) ans = [] for i in range(N): a = bisect_left(A1,A[i]) b = bisect_left(P[i],A[i]) ans.append(a-b) print(*ans)
Kotlin Heroes: Practice 3
ICPC
2,020
3
256
Mentors
In BerSoft $$$n$$$ programmers work, the programmer $$$i$$$ is characterized by a skill $$$r_i$$$. A programmer $$$a$$$ can be a mentor of a programmer $$$b$$$ if and only if the skill of the programmer $$$a$$$ is strictly greater than the skill of the programmer $$$b$$$ $$$(r_a > r_b)$$$ and programmers $$$a$$$ and $$$b$$$ are not in a quarrel. You are given the skills of each programmers and a list of $$$k$$$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $$$i$$$, find the number of programmers, for which the programmer $$$i$$$ can be a mentor.
The first line contains two integers $$$n$$$ and $$$k$$$ $$$(2 \le n \le 2 \cdot 10^5$$$, $$$0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$$$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $$$r_1, r_2, \dots, r_n$$$ $$$(1 \le r_i \le 10^{9})$$$, where $$$r_i$$$ equals to the skill of the $$$i$$$-th programmer. Each of the following $$$k$$$ lines contains two distinct integers $$$x$$$, $$$y$$$ $$$(1 \le x, y \le n$$$, $$$x \ne y)$$$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $$$x$$$ is in a quarrel with $$$y$$$ then $$$y$$$ is in a quarrel with $$$x$$$. Guaranteed, that for each pair $$$(x, y)$$$ there are no other pairs $$$(x, y)$$$ and $$$(y, x)$$$ in the input.
Print $$$n$$$ integers, the $$$i$$$-th number should be equal to the number of programmers, for which the $$$i$$$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input.
null
In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
[{"input": "4 2\n10 4 10 15\n1 2\n4 3", "output": "0 0 1 2"}, {"input": "10 4\n5 4 1 5 4 3 7 1 2 5\n4 6\n2 1\n10 8\n3 5", "output": "5 4 0 5 3 3 9 0 2 5"}]
null
["*special", "data structures", "implementation"]
41
[{"input": "4 2\r\n10 4 10 15\r\n1 2\r\n4 3\r\n", "output": "0 0 1 2 \r\n"}, {"input": "10 4\r\n5 4 1 5 4 3 7 1 2 5\r\n4 6\r\n2 1\r\n10 8\r\n3 5\r\n", "output": "5 4 0 5 3 3 9 0 2 5 \r\n"}, {"input": "2 0\r\n3 1\r\n", "output": "1 0 \r\n"}, {"input": "2 0\r\n1 1\r\n", "output": "0 0 \r\n"}, {"input": "10 35\r\n322022227 751269818 629795150 369443545 344607287 250044294 476897672 184054549 986884572 917181121\r\n6 3\r\n7 3\r\n1 9\r\n7 9\r\n10 7\r\n3 4\r\n8 6\r\n7 4\r\n6 10\r\n7 2\r\n3 5\r\n6 9\r\n3 10\r\n8 7\r\n6 5\r\n8 1\r\n8 5\r\n1 7\r\n8 10\r\n8 2\r\n1 5\r\n10 4\r\n6 7\r\n4 6\r\n2 6\r\n5 4\r\n9 10\r\n9 2\r\n4 8\r\n5 9\r\n4 1\r\n3 2\r\n2 1\r\n4 2\r\n9 8\r\n", "output": "1 1 2 0 0 0 1 0 2 3 \r\n"}]
false
stdio
null
true
1298/E
978
F
PyPy 3-64
TESTS
4
61
0
230899970
input = __import__('sys').stdin.readline if __name__ == "__main__": n, k = map(int, input().split()) programers = list(map(int, input().split())) pairs = {} for _ in range(k): a, b = map(int, input().split()) pairs[a - 1] = b-1 pairs[b - 1] = a-1 ans = [0] * n for i in range(n): for j in range(0, n): #print(i, j, UnionFind.find(i), UnionFind.find(j)) if (pairs.get(i) != j or pairs.get(j) != i) and programers[i] > programers[j]: ans[i] += 1 print(*ans)
41
670
40,448,000
38179172
import sys,bisect read=lambda:map(int,sys.stdin.buffer.readline().split()) n,k=read() r=list(read()) v=r.copy() v.sort() t=[bisect.bisect_left(v,x) for x in r] for _ in range(k): a,b=read();a-=1;b-=1 if r[a]>r[b]: t[a]-=1 if r[b]>r[a]: t[b]-=1 print(*t)
Kotlin Heroes: Practice 3
ICPC
2,020
3
256
Mentors
In BerSoft $$$n$$$ programmers work, the programmer $$$i$$$ is characterized by a skill $$$r_i$$$. A programmer $$$a$$$ can be a mentor of a programmer $$$b$$$ if and only if the skill of the programmer $$$a$$$ is strictly greater than the skill of the programmer $$$b$$$ $$$(r_a > r_b)$$$ and programmers $$$a$$$ and $$$b$$$ are not in a quarrel. You are given the skills of each programmers and a list of $$$k$$$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $$$i$$$, find the number of programmers, for which the programmer $$$i$$$ can be a mentor.
The first line contains two integers $$$n$$$ and $$$k$$$ $$$(2 \le n \le 2 \cdot 10^5$$$, $$$0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$$$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $$$r_1, r_2, \dots, r_n$$$ $$$(1 \le r_i \le 10^{9})$$$, where $$$r_i$$$ equals to the skill of the $$$i$$$-th programmer. Each of the following $$$k$$$ lines contains two distinct integers $$$x$$$, $$$y$$$ $$$(1 \le x, y \le n$$$, $$$x \ne y)$$$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $$$x$$$ is in a quarrel with $$$y$$$ then $$$y$$$ is in a quarrel with $$$x$$$. Guaranteed, that for each pair $$$(x, y)$$$ there are no other pairs $$$(x, y)$$$ and $$$(y, x)$$$ in the input.
Print $$$n$$$ integers, the $$$i$$$-th number should be equal to the number of programmers, for which the $$$i$$$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input.
null
In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
[{"input": "4 2\n10 4 10 15\n1 2\n4 3", "output": "0 0 1 2"}, {"input": "10 4\n5 4 1 5 4 3 7 1 2 5\n4 6\n2 1\n10 8\n3 5", "output": "5 4 0 5 3 3 9 0 2 5"}]
null
["*special", "data structures", "implementation"]
41
[{"input": "4 2\r\n10 4 10 15\r\n1 2\r\n4 3\r\n", "output": "0 0 1 2 \r\n"}, {"input": "10 4\r\n5 4 1 5 4 3 7 1 2 5\r\n4 6\r\n2 1\r\n10 8\r\n3 5\r\n", "output": "5 4 0 5 3 3 9 0 2 5 \r\n"}, {"input": "2 0\r\n3 1\r\n", "output": "1 0 \r\n"}, {"input": "2 0\r\n1 1\r\n", "output": "0 0 \r\n"}, {"input": "10 35\r\n322022227 751269818 629795150 369443545 344607287 250044294 476897672 184054549 986884572 917181121\r\n6 3\r\n7 3\r\n1 9\r\n7 9\r\n10 7\r\n3 4\r\n8 6\r\n7 4\r\n6 10\r\n7 2\r\n3 5\r\n6 9\r\n3 10\r\n8 7\r\n6 5\r\n8 1\r\n8 5\r\n1 7\r\n8 10\r\n8 2\r\n1 5\r\n10 4\r\n6 7\r\n4 6\r\n2 6\r\n5 4\r\n9 10\r\n9 2\r\n4 8\r\n5 9\r\n4 1\r\n3 2\r\n2 1\r\n4 2\r\n9 8\r\n", "output": "1 1 2 0 0 0 1 0 2 3 \r\n"}]
false
stdio
null
true
469/B
469
B
Python 3
TESTS
1
30
0
225550094
def function(little_x): for i in range(len(little_x)): little_x[i][0] = little_x[i][0] + 1 little_x[i][1] = little_x[i][1] + 1 return little_x result = 0 p, q, l, r = map(int, input().split()) z = [] x = [] for i in range(p): a, b = map(int, input().split()) z.append([a, b]) for i in range(q): c, d = map(int, input().split()) x.append([c, d]) # print(z, x) for i in range(r - l): # print(x) for j in range(len(x)): flag = 0 for k in range(len(z)): for k2 in range(x[j][0], x[j][1] + 1): # print(k2, z[k][0], z[k][1]) # print(k2 in range(z[k][0], z[k][1] + 1)) if k2 in range(z[k][0], z[k][1] + 1): result += 1 flag = 1 break if flag == 1: flag = 0 break x = function(x) print(result)
31
93
2,048,000
224035520
p, q, l, r = map(int, input().split()) intervals = [False] * 1001 for k in range(p): i, j = map(int, input().split()) for index in range(i, j+1): intervals[index] = True arr = [] for k in range(q): i, j = map(int, input().split()) arr.append((i, j)) res = 0 for i in range(l, r+1): for x, y in arr: x1 = x + i y1 = y + i find = False for index in range(x1, y1+1): if index > 1000: break if intervals[index] == True: res += 1 find = True break if find: break print(res)
Codeforces Round 268 (Div. 2)
CF
2,014
1
256
Chat Online
Little X and Little Z are good friends. They always chat online. But both of them have schedules. Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i). If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that?
The first line contains four space-separated integers p, q, l, r (1 ≤ p, q ≤ 50; 0 ≤ l ≤ r ≤ 1000). Each of the next p lines contains two space-separated integers ai, bi (0 ≤ ai < bi ≤ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 ≤ cj < dj ≤ 1000). It's guaranteed that bi < ai + 1 and dj < cj + 1 for all valid i and j.
Output a single integer — the number of moments of time from the segment [l, r] which suit for online conversation.
null
null
[{"input": "1 1 0 4\n2 3\n0 1", "output": "3"}, {"input": "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17", "output": "20"}]
1,300
["implementation"]
31
[{"input": "1 1 0 4\r\n2 3\r\n0 1\r\n", "output": "3\r\n"}, {"input": "2 3 0 20\r\n15 17\r\n23 26\r\n1 4\r\n7 11\r\n15 17\r\n", "output": "20\r\n"}, {"input": "5 2 27 452\r\n148 154\r\n421 427\r\n462 470\r\n777 786\r\n969 978\r\n245 247\r\n313 322\r\n", "output": "54\r\n"}, {"input": "3 6 25 785\r\n273 275\r\n391 397\r\n775 783\r\n84 89\r\n348 354\r\n480 483\r\n552 556\r\n711 716\r\n838 844\r\n", "output": "99\r\n"}, {"input": "1 6 364 421\r\n649 688\r\n31 39\r\n438 441\r\n516 524\r\n609 615\r\n708 710\r\n850 851\r\n", "output": "0\r\n"}, {"input": "7 10 87 239\r\n8 23\r\n325 374\r\n516 540\r\n633 658\r\n728 742\r\n833 839\r\n960 995\r\n99 114\r\n213 232\r\n264 269\r\n321 327\r\n492 540\r\n551 559\r\n587 590\r\n625 637\r\n704 723\r\n750 764\r\n", "output": "151\r\n"}, {"input": "33 17 295 791\r\n41 49\r\n66 73\r\n95 102\r\n118 126\r\n157 158\r\n189 198\r\n228 237\r\n247 251\r\n301 307\r\n318 326\r\n328 333\r\n356 363\r\n373 381\r\n454 460\r\n463 466\r\n471 477\r\n500 501\r\n505 510\r\n559 566\r\n585 588\r\n597 604\r\n675 684\r\n688 695\r\n699 705\r\n749 755\r\n780 788\r\n869 873\r\n879 888\r\n890 892\r\n909 918\r\n953 954\r\n973 978\r\n993 996\r\n53 60\r\n68 74\r\n102 105\r\n149 153\r\n191 206\r\n230 242\r\n249 252\r\n320 325\r\n483 488\r\n509 513\r\n523 524\r\n544 560\r\n651 655\r\n670 672\r\n691 708\r\n742 748\r\n920 939\r\n", "output": "497\r\n"}, {"input": "5 4 520 527\r\n257 263\r\n309 315\r\n434 439\r\n540 541\r\n759 763\r\n168 176\r\n313 316\r\n546 548\r\n880 881\r\n", "output": "0\r\n"}, {"input": "11 40 58 976\r\n14 22\r\n85 92\r\n110 112\r\n173 181\r\n188 190\r\n212 218\r\n494 498\r\n530 531\r\n647 656\r\n943 948\r\n960 968\r\n2 16\r\n20 31\r\n40 86\r\n90 91\r\n93 96\r\n103 118\r\n128 155\r\n170 173\r\n176 192\r\n205 207\r\n210 217\r\n237 245\r\n250 280\r\n302 304\r\n311 328\r\n332 376\r\n387 419\r\n422 442\r\n449 454\r\n462 491\r\n496 498\r\n501 516\r\n525 552\r\n556 565\r\n567 573\r\n577 578\r\n586 635\r\n651 677\r\n688 699\r\n704 714\r\n718 719\r\n733 766\r\n768 769\r\n773 811\r\n822 826\r\n830 870\r\n885 917\r\n937 961\r\n971 981\r\n991 1000\r\n", "output": "909\r\n"}, {"input": "10 28 435 847\r\n48 57\r\n86 95\r\n186 188\r\n297 304\r\n500 506\r\n594 600\r\n602 609\r\n802 811\r\n906 911\r\n916 921\r\n20 22\r\n115 118\r\n139 141\r\n170 171\r\n184 188\r\n193 197\r\n227 230\r\n232 235\r\n250 252\r\n256 260\r\n271 275\r\n322 324\r\n326 328\r\n379 381\r\n433 434\r\n528 529\r\n563 565\r\n583 584\r\n614 615\r\n617 619\r\n631 632\r\n726 729\r\n750 754\r\n814 817\r\n853 855\r\n881 882\r\n946 947\r\n981 984\r\n", "output": "284\r\n"}, {"input": "13 42 292 733\r\n304 308\r\n362 365\r\n396 397\r\n686 688\r\n719 722\r\n815 816\r\n901 902\r\n904 905\r\n921 924\r\n929 933\r\n953 954\r\n977 980\r\n982 985\r\n28 30\r\n36 37\r\n108 112\r\n123 127\r\n134 135\r\n151 153\r\n160 162\r\n203 205\r\n208 212\r\n240 243\r\n261 264\r\n271 274\r\n294 297\r\n356 358\r\n383 385\r\n408 412\r\n415 418\r\n435 439\r\n444 447\r\n500 502\r\n507 511\r\n515 519\r\n525 529\r\n541 543\r\n566 569\r\n577 579\r\n606 609\r\n618 622\r\n655 656\r\n715 718\r\n744 745\r\n767 771\r\n798 802\r\n828 830\r\n839 840\r\n842 845\r\n893 894\r\n897 898\r\n902 906\r\n958 960\r\n981 983\r\n988 992\r\n", "output": "412\r\n"}, {"input": "5 46 211 943\r\n367 371\r\n405 408\r\n639 642\r\n821 825\r\n974 978\r\n13 14\r\n31 34\r\n36 39\r\n48 51\r\n82 86\r\n173 174\r\n193 196\r\n227 230\r\n249 250\r\n259 261\r\n292 294\r\n325 327\r\n329 330\r\n352 353\r\n380 383\r\n390 391\r\n398 399\r\n411 414\r\n418 420\r\n424 427\r\n478 479\r\n488 489\r\n500 502\r\n511 514\r\n534 535\r\n548 552\r\n581 584\r\n596 600\r\n609 611\r\n618 622\r\n641 642\r\n656 657\r\n674 678\r\n707 709\r\n718 720\r\n746 748\r\n752 755\r\n771 773\r\n775 777\r\n865 869\r\n892 896\r\n910 911\r\n946 948\r\n963 964\r\n977 978\r\n986 990\r\n", "output": "428\r\n"}, {"input": "1 8 169 472\r\n63 553\r\n32 65\r\n75 204\r\n263 277\r\n289 326\r\n342 441\r\n452 911\r\n945 956\r\n971 986\r\n", "output": "304\r\n"}, {"input": "2 1 17 179\r\n159 202\r\n579 602\r\n115 126\r\n", "output": "55\r\n"}, {"input": "1 1 0 1000\r\n0 1000\r\n0 1000\r\n", "output": "1001\r\n"}, {"input": "10 11 201 515\r\n2 3\r\n102 103\r\n202 203\r\n302 304\r\n401 402\r\n502 503\r\n600 603\r\n701 702\r\n800 803\r\n900 902\r\n1 2\r\n103 104\r\n201 202\r\n302 304\r\n400 404\r\n501 504\r\n600 601\r\n603 604\r\n701 704\r\n800 801\r\n900 902\r\n", "output": "26\r\n"}, {"input": "1 7 140 478\r\n400 404\r\n3 4\r\n100 105\r\n301 304\r\n403 405\r\n504 506\r\n600 605\r\n904 906\r\n", "output": "16\r\n"}, {"input": "9 8 453 552\r\n5 18\r\n23 28\r\n125 132\r\n208 209\r\n215 230\r\n516 527\r\n808 819\r\n906 913\r\n926 929\r\n4 18\r\n221 229\r\n316 330\r\n403 424\r\n506 516\r\n805 828\r\n904 914\r\n916 917\r\n", "output": "49\r\n"}, {"input": "9 1 24 52\r\n63 94\r\n150 164\r\n244 275\r\n379 385\r\n413 420\r\n441 490\r\n506 545\r\n614 661\r\n752 776\r\n531 534\r\n", "output": "0\r\n"}, {"input": "1 1 0 0\r\n1 3\r\n2 5\r\n", "output": "1\r\n"}]
false
stdio
null
true
520/C
520
C
PyPy 3-64
TESTS
0
30
0
224964163
import sys input = sys.stdin.readline from collections import Counter n = int(input()) chars = input() cnt = Counter(chars) value_cnts = Counter(cnt.values()) ans = 1 mod = 10 ** 9 + 7 for idx in range(n): ans *= value_cnts[cnt[chars[idx]]] ans %= mod print(ans)
25
62
204,800
10113424
n = int(input()) s = input() num = s.count("A") num1 = s.count("C") num2 = s.count("T") num3 = s.count("G") num4 = max(num,num1,num2,num3) num5 = 0 if num == num4: num5 += 1 if num1 == num4: num5 += 1 if num2 == num4: num5 += 1 if num3 == num4: num5 += 1 if num5 == 1 or n == 1: print(1) else: print((num5**n)%(10**9+7))
Codeforces Round 295 (Div. 2)
CF
2,015
2
256
DNA Alignment
Vasya became interested in bioinformatics. He's going to write an article about similar cyclic DNA sequences, so he invented a new method for determining the similarity of cyclic sequences. Let's assume that strings s and t have the same length n, then the function h(s, t) is defined as the number of positions in which the respective symbols of s and t are the same. Function h(s, t) can be used to define the function of Vasya distance ρ(s, t): $$\rho(s,t) = \sum_{i=0}^{n-1} \sum_{j=0}^{n-1} h(\text{shift}(s,i), \text{shift}(t,j)).$$ ρ("AGC", "CGT") = h("AGC", "CGT") + h("AGC", "GTC") + h("AGC", "TCG") + h("GCA", "CGT") + h("GCA", "GTC") + h("GCA", "TCG") + h("CAG", "CGT") + h("CAG", "GTC") + h("CAG", "TCG") = 1 + 1 + 0 + 0 + 1 + 1 + 1 + 0 + 1 = 6 Vasya found a string s of length n on the Internet. Now he wants to count how many strings t there are such that the Vasya distance from the string s attains maximum possible value. Formally speaking, t must satisfy the equation: $$\rho(s,t) = \max_{u:|u|=|s|} \rho(s,u)$$. Vasya could not try all possible strings to find an answer, so he needs your help. As the answer may be very large, count the number of such strings modulo 109 + 7.
The first line of the input contains a single integer n (1 ≤ n ≤ 105). The second line of the input contains a single string of length n, consisting of characters "ACGT".
Print a single number — the answer modulo 109 + 7.
null
Please note that if for two distinct strings t1 and t2 values ρ(s, t1) и ρ(s, t2) are maximum among all possible t, then both strings must be taken into account in the answer even if one of them can be obtained by a circular shift of another one. In the first sample, there is ρ("C", "C") = 1, for the remaining strings t of length 1 the value of ρ(s, t) is 0. In the second sample, ρ("AG", "AG") = ρ("AG", "GA") = ρ("AG", "AA") = ρ("AG", "GG") = 4. In the third sample, ρ("TTT", "TTT") = 27
[{"input": "1\nC", "output": "1"}, {"input": "2\nAG", "output": "4"}, {"input": "3\nTTT", "output": "1"}]
1,500
["math", "strings"]
25
[{"input": "1\r\nC\r\n", "output": "1\r\n"}, {"input": "2\r\nAG\r\n", "output": "4\r\n"}, {"input": "3\r\nTTT\r\n", "output": "1\r\n"}, {"input": "4\r\nGACT\r\n", "output": "256\r\n"}, {"input": "1\r\nT\r\n", "output": "1\r\n"}, {"input": "2\r\nAG\r\n", "output": "4\r\n"}, {"input": "3\r\nGCA\r\n", "output": "27\r\n"}, {"input": "5\r\nACGTC\r\n", "output": "1\r\n"}, {"input": "15\r\nAGCGAATCCCATTGT\r\n", "output": "14348907\r\n"}, {"input": "20\r\nTAAGCGACCAGGTGCTTTAC\r\n", "output": "511620083\r\n"}, {"input": "30\r\nCCTTTCGGGGCGCGTTGGCCTTTGTCCTGC\r\n", "output": "130653412\r\n"}, {"input": "318\r\nTATCAATCGGTACGTGCGCATCATTGTCAATCGGGCTTCATGGCTTGCGGGCGCTACCCGAGGGGAAGCTGCGGACAGGTAGGTAAGATACACACGAACCAAACGGAGTTATGTTGGATAAATTGGCTGGAAGGGCGTAGGTATATCGAGTCGCGGACCTGGCATAGACTATCAGGGGCAGCGGTACAAGGCAACCGTGAGCGGGGTCTGCCCACCATTAGACCGATGCGCCGGCTCGTATATGTGATTCTGGTGAAAAGTATCATGCCGGGACGCGTAATGACCCGGCTGGCTAATCCACCGTGGCAGCAAAATAAC\r\n", "output": "1\r\n"}]
false
stdio
null
true
802/M
802
M
Python 3
TESTS
1
30
4,198,400
135260545
a,b=input().split();print(sum(list(map(int,input().split()))[:int(b)]))
14
46
0
192618520
# LUOGU_RID: 101671699 n, k, *a = map(int, open(0).read().split()) print(sum(sorted(a)[:k]))
Helvetic Coding Contest 2017 online mirror (teams allowed, unrated)
ICPC
2,017
2
256
April Fools' Problem (easy)
The marmots have prepared a very easy problem for this year's HC2 – this one. It involves numbers n, k and a sequence of n positive integers a1, a2, ..., an. They also came up with a beautiful and riveting story for the problem statement. It explains what the input means, what the program should output, and it also reads like a good criminal. However I, Heidi, will have none of that. As my joke for today, I am removing the story from the statement and replacing it with these two unhelpful paragraphs. Now solve the problem, fools!
The first line of the input contains two space-separated integers n and k (1 ≤ k ≤ n ≤ 2200). The second line contains n space-separated integers a1, ..., an (1 ≤ ai ≤ 104).
Output one number.
null
null
[{"input": "8 5\n1 1 1 1 1 1 1 1", "output": "5"}, {"input": "10 3\n16 8 2 4 512 256 32 128 64 1", "output": "7"}, {"input": "5 1\n20 10 50 30 46", "output": "10"}, {"input": "6 6\n6 6 6 6 6 6", "output": "36"}, {"input": "1 1\n100", "output": "100"}]
1,200
["greedy", "sortings"]
14
[{"input": "8 5\r\n1 1 1 1 1 1 1 1\r\n", "output": "5"}, {"input": "10 3\r\n16 8 2 4 512 256 32 128 64 1\r\n", "output": "7"}, {"input": "5 1\r\n20 10 50 30 46\r\n", "output": "10"}, {"input": "6 6\r\n6 6 6 6 6 6\r\n", "output": "36"}, {"input": "1 1\r\n100\r\n", "output": "100"}, {"input": "1 1\r\n1\r\n", "output": "1"}, {"input": "10 5\r\n147 1917 5539 7159 5763 416 711 1412 6733 4402\r\n", "output": "4603"}, {"input": "100 60\r\n1443 3849 6174 8249 696 8715 3461 9159 4468 2496 3044 2301 2437 7559 7235 7956 8959 2036 4399 9595 8664 9743 7688 3730 3705 1203 9332 7088 8563 3823 2794 8014 6951 1160 8616 970 9885 2421 6510 4885 5246 6146 8849 5141 8602 9486 7257 3300 8323 4797 4082 7135 80 9622 4543 6567 2747 5013 4626 9091 9028 9851 1654 7021 6843 3209 5350 3809 4697 4617 4450 81 5208 1877 2897 6115 3191 2878 9258 2849 8103 6678 8714 8024 80 9894 321 8074 6797 457 1348 8652 811 7215 4381 5000 7406 7899 9974 844\r\n", "output": "206735"}]
false
stdio
null
true
69/B
69
B
PyPy 3-64
TESTS
2
92
0
180501312
n, m = map(int, input().split()) people = [] races = 0 for _ in range(m): a = list(map(int, input().split())) races = max(races, a[1]) people.append(a) ans = 0 for section in range(1, races + 1): curr = 0 for person in range(m): if people[person][0] <= section <= people[person][1] and people[person][2] <= people[curr][2] and people[person][3] > people[curr][3]: curr = person ans += people[curr][3] print(ans)
67
154
0
108454863
import math #t=int(input()) #lst = list(map(int, input().strip().split(' '))) n,m = map(int, input().strip().split(' ')) lst=[0]*n time=[10000]*n for i in range(m): l,r,t,c = map(int, input().strip().split(' ')) for j in range(l,r+1): if t<time[j-1]: time[j-1]=t lst[j-1]=c print(sum(lst))
Codeforces Beta Round 63 (Div. 2)
CF
2,011
2
256
Bets
In Chelyabinsk lives a much respected businessman Nikita with a strange nickname "Boss". Once Nikita decided to go with his friend Alex to the Summer Biathlon World Cup. Nikita, as a very important person, received a token which allows to place bets on each section no more than on one competitor. To begin with friends learned the rules: in the race there are n sections of equal length and m participants. The participants numbered from 1 to m. About each participant the following is known: - li — the number of the starting section, - ri — the number of the finishing section (li ≤ ri), - ti — the time a biathlete needs to complete an section of the path, - ci — the profit in roubles. If the i-th sportsman wins on one of the sections, the profit will be given to the man who had placed a bet on that sportsman. The i-th biathlete passes the sections from li to ri inclusive. The competitor runs the whole way in (ri - li + 1)·ti time units. It takes him exactly ti time units to pass each section. In case of the athlete's victory on k sections the man who has betted on him receives k·ci roubles. In each section the winner is determined independently as follows: if there is at least one biathlete running this in this section, then among all of them the winner is the one who has ran this section in minimum time (spent minimum time passing this section). In case of equality of times the athlete with the smaller index number wins. If there are no participants in this section, then the winner in this section in not determined. We have to say that in the summer biathlon all the participants are moving at a constant speed. We should also add that Nikita can bet on each section and on any contestant running in this section. Help the friends find the maximum possible profit.
The first line contains two integers n and m (1 ≤ n, m ≤ 100). Then follow m lines, each containing 4 integers li, ri, ti, ci (1 ≤ li ≤ ri ≤ n, 1 ≤ ti, ci ≤ 1000).
Print a single integer, the maximal profit in roubles that the friends can get. In each of n sections it is not allowed to place bets on more than one sportsman.
null
In the first test the optimal bet is: in the 1-2 sections on biathlete 1, in section 3 on biathlete 3, in section 4 on biathlete 4. Total: profit of 5 rubles for 1 section, the profit of 5 rubles for 2 section, profit of 30 rubles for a 3 section, profit of 20 rubles for 4 section. Total profit 60 rubles. In the second test the optimal bet is: on 1 and 5 sections on biathlete 1, in the 2-4 sections on biathlete 2, in the 6-7 sections on athlete 4. There is no winner in the 8 section. Total: profit of 10 rubles for 1 section, the profit of 15 rubles for 2,3,4 section, profit of 10 rubles for a 5 section, profit of 20 rubles for 6, 7 section. Total profit 105 rubles.
[{"input": "4 4\n1 4 20 5\n1 3 21 10\n3 3 4 30\n3 4 4 20", "output": "60"}, {"input": "8 4\n1 5 24 10\n2 4 6 15\n4 6 30 50\n6 7 4 20", "output": "105"}]
1,200
["greedy", "implementation"]
67
[{"input": "4 4\r\n1 4 20 5\r\n1 3 21 10\r\n3 3 4 30\r\n3 4 4 20\r\n", "output": "60"}, {"input": "8 4\r\n1 5 24 10\r\n2 4 6 15\r\n4 6 30 50\r\n6 7 4 20\r\n", "output": "105"}, {"input": "2 2\r\n1 2 3 1\r\n2 2 3 10\r\n", "output": "2"}, {"input": "20 30\r\n15 17 54 46\r\n4 18 26 18\r\n18 20 49 94\r\n12 12 83 12\r\n11 13 88 47\r\n8 8 8 12\r\n18 18 94 2\r\n14 17 88 96\r\n19 19 62 97\r\n1 12 81 67\r\n10 12 78 26\r\n19 20 63 93\r\n9 20 38 32\r\n7 9 90 17\r\n9 10 19 60\r\n16 16 47 29\r\n1 6 62 29\r\n12 18 74 89\r\n5 5 97 92\r\n5 17 16 25\r\n11 19 2 76\r\n3 15 61 29\r\n5 7 73 54\r\n19 20 91 91\r\n4 17 28 61\r\n9 13 56 81\r\n10 11 82 80\r\n10 11 82 70\r\n5 10 66 38\r\n10 19 22 61\r\n", "output": "958"}, {"input": "20 30\r\n4 13 78 11\r\n13 19 56 41\r\n15 15 46 83\r\n4 9 74 72\r\n17 20 97 7\r\n15 20 29 48\r\n8 17 44 85\r\n4 18 26 46\r\n16 17 9 90\r\n16 16 39 89\r\n13 14 46 63\r\n14 18 67 18\r\n12 20 84 48\r\n10 20 49 32\r\n10 14 14 11\r\n6 18 80 84\r\n3 20 13 97\r\n12 20 62 42\r\n12 14 64 71\r\n5 19 38 17\r\n17 18 99 18\r\n11 15 83 22\r\n4 11 65 99\r\n8 16 89 45\r\n11 20 15 39\r\n8 13 85 26\r\n5 19 84 3\r\n10 16 26 45\r\n13 16 81 37\r\n3 5 100 42\r\n", "output": "1732"}, {"input": "20 30\r\n4 12 30 83\r\n3 3 91 46\r\n5 11 82 84\r\n20 20 29 36\r\n3 13 89 29\r\n11 14 40 80\r\n9 20 90 21\r\n14 19 23 74\r\n8 9 13 88\r\n12 18 13 95\r\n13 18 48 29\r\n8 17 13 15\r\n7 15 18 51\r\n9 20 87 51\r\n12 20 40 32\r\n4 11 34 11\r\n3 19 22 20\r\n19 19 53 5\r\n16 18 52 30\r\n5 19 52 71\r\n19 19 99 95\r\n14 18 15 28\r\n20 20 91 64\r\n15 16 55 47\r\n1 9 40 40\r\n9 17 93 82\r\n7 16 10 75\r\n1 15 100 24\r\n10 10 35 84\r\n1 2 4 7\r\n", "output": "1090"}, {"input": "20 30\r\n20 20 43 41\r\n5 13 99 35\r\n9 15 79 12\r\n4 20 75 16\r\n20 20 4 94\r\n14 14 1 1\r\n5 5 4 92\r\n14 19 52 30\r\n19 20 61 14\r\n10 12 34 89\r\n11 15 27 12\r\n14 18 64 25\r\n11 14 37 14\r\n19 19 56 20\r\n19 20 61 11\r\n13 16 48 36\r\n14 16 82 73\r\n16 17 82 26\r\n1 5 55 91\r\n10 13 24 33\r\n3 19 91 70\r\n10 15 87 53\r\n3 5 92 80\r\n10 10 13 24\r\n9 9 38 20\r\n13 20 80 38\r\n5 10 71 23\r\n6 19 43 90\r\n13 20 10 55\r\n11 14 29 62\r\n", "output": "1261"}, {"input": "20 30\r\n15 15 14 51\r\n17 20 3 20\r\n14 16 59 66\r\n14 15 48 22\r\n18 19 72 26\r\n13 14 60 72\r\n8 13 69 57\r\n4 12 3 82\r\n1 8 80 37\r\n18 19 40 33\r\n9 9 32 55\r\n13 15 67 5\r\n10 13 37 1\r\n19 19 39 11\r\n17 19 28 88\r\n8 19 88 87\r\n16 20 26 2\r\n18 18 11 46\r\n14 14 14 20\r\n15 15 78 100\r\n18 19 53 32\r\n12 13 59 66\r\n11 18 38 36\r\n5 8 14 97\r\n8 18 80 97\r\n6 19 81 17\r\n13 19 65 93\r\n8 10 77 3\r\n20 20 70 60\r\n17 17 28 35\r\n", "output": "1003"}, {"input": "20 30\r\n5 10 38 50\r\n17 18 86 42\r\n4 13 91 90\r\n20 20 45 31\r\n3 3 16 11\r\n16 16 80 66\r\n19 19 96 26\r\n15 20 7 84\r\n9 18 45 36\r\n5 19 89 6\r\n9 9 4 58\r\n9 14 97 31\r\n6 12 74 90\r\n4 5 84 2\r\n12 19 92 48\r\n16 16 92 55\r\n9 15 88 38\r\n6 14 8 66\r\n14 17 71 91\r\n20 20 58 20\r\n8 18 5 47\r\n7 19 67 43\r\n19 19 88 80\r\n9 12 35 86\r\n4 4 82 22\r\n7 8 72 82\r\n8 10 61 92\r\n20 20 77 93\r\n15 19 66 20\r\n20 20 8 10\r\n", "output": "911"}, {"input": "20 30\r\n1 20 49 91\r\n15 15 60 37\r\n14 14 3 79\r\n11 12 81 66\r\n8 12 71 31\r\n3 13 8 14\r\n2 10 11 35\r\n19 20 40 28\r\n12 14 6 75\r\n16 18 100 100\r\n20 20 89 74\r\n16 16 27 98\r\n18 18 21 24\r\n1 18 69 98\r\n7 13 70 57\r\n9 20 41 79\r\n17 17 75 75\r\n11 16 19 14\r\n1 15 62 59\r\n12 15 33 91\r\n3 17 10 79\r\n15 16 100 80\r\n10 16 5 5\r\n9 19 53 100\r\n9 18 65 42\r\n5 13 34 13\r\n6 13 61 47\r\n17 19 68 25\r\n5 5 42 59\r\n6 8 48 92\r\n", "output": "492"}, {"input": "100 1\r\n22 59 287 173\r\n", "output": "6574"}, {"input": "100 1\r\n8 31 93 267\r\n", "output": "6408"}, {"input": "100 1\r\n72 82 727 390\r\n", "output": "4290"}, {"input": "100 1\r\n14 25 343 50\r\n", "output": "600"}, {"input": "100 1\r\n73 75 59 176\r\n", "output": "528"}, {"input": "21 2\r\n19 20 253 233\r\n3 17 23 150\r\n", "output": "2716"}, {"input": "47 18\r\n14 38 376 96\r\n21 34 749 32\r\n7 20 409 146\r\n41 41 740 9\r\n14 16 526 128\r\n38 47 518 147\r\n3 26 7 16\r\n25 31 155 75\r\n32 36 164 12\r\n5 33 436 150\r\n22 41 477 52\r\n4 13 166 6\r\n38 44 664 133\r\n2 33 452 16\r\n30 40 623 48\r\n37 37 250 122\r\n25 34 506 109\r\n36 38 716 78\r\n", "output": "2091"}, {"input": "1 1\r\n1 1 1 1\r\n", "output": "1"}, {"input": "1 1\r\n1 1 1000 1000\r\n", "output": "1000"}]
false
stdio
null
true
617/C
617
C
PyPy 3
TESTS
6
155
3,686,400
228382968
import math import sys #input = sys.stdin.readline USE_FILE = False def calc_distance(x1, y1, x2, y2): return (x1 - x2) ** 2 + (y1 - y2) ** 2 def main(): n, x1, y1, x2, y2 = [int(i) for i in input().split()] points = [] d = [] for i in range(n): x, y = tuple(map(int, input().split())) d.append((calc_distance(x1, y1, x, y), calc_distance(x2, y2, x, y))) max_r1 = 0 max_r2 = 0 n = len(d) res = 999999999999999999999999999999999999999999999 for i in range(n): max_r1 = d[i][0] max_r2 = 0 for j in range(n): if max_r1 < d[j][0]: max_r2 = max(max_r2, d[j][1]) res = min(max_r1 + max_r2, res) return res if __name__=="__main__": if USE_FILE: import sys sys.stdin = open('/home/stefan/input.txt', 'r') print(main())
31
78
307,200
15529005
#!/usr/bin/env python3 from itertools import islice import math def squared_distance(p1, p2): return (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2 def main(): n, x1, y1, x2, y2 = list(map(int, input().split())) f1, f2 = (x1, y1), (x2, y2) flowers = sorted([(int(x), int(y)) for x, y in (input().split() for _ in range(n))], key=lambda x: squared_distance(x, f1), reverse=True) ans = squared_distance(f1, flowers[0]) max_distance_2 = 0 for i in range(1, n): d1 = squared_distance(f1, flowers[i]) max_distance_2 = max(max_distance_2, squared_distance(f2, flowers[i - 1])) ans = min(ans, d1 + max_distance_2) max_distance_2 = max(max_distance_2, squared_distance(f2, flowers[-1])) ans = min(ans, max_distance_2) print(int(ans)) if __name__ == '__main__': main()
Codeforces Round 340 (Div. 2)
CF
2,016
2
256
Watering Flowers
A flowerbed has many flowers and two fountains. You can adjust the water pressure and set any values r1(r1 ≥ 0) and r2(r2 ≥ 0), giving the distances at which the water is spread from the first and second fountain respectively. You have to set such r1 and r2 that all the flowers are watered, that is, for each flower, the distance between the flower and the first fountain doesn't exceed r1, or the distance to the second fountain doesn't exceed r2. It's OK if some flowers are watered by both fountains. You need to decrease the amount of water you need, that is set such r1 and r2 that all the flowers are watered and the r12 + r22 is minimum possible. Find this minimum value.
The first line of the input contains integers n, x1, y1, x2, y2 (1 ≤ n ≤ 2000, - 107 ≤ x1, y1, x2, y2 ≤ 107) — the number of flowers, the coordinates of the first and the second fountain. Next follow n lines. The i-th of these lines contains integers xi and yi ( - 107 ≤ xi, yi ≤ 107) — the coordinates of the i-th flower. It is guaranteed that all n + 2 points in the input are distinct.
Print the minimum possible value r12 + r22. Note, that in this problem optimal answer is always integer.
null
The first sample is (r12 = 5, r22 = 1): The second sample is (r12 = 1, r22 = 32):
[{"input": "2 -1 0 5 3\n0 2\n5 2", "output": "6"}, {"input": "4 0 0 5 0\n9 4\n8 3\n-1 0\n1 4", "output": "33"}]
1,600
["implementation"]
31
[{"input": "2 -1 0 5 3\r\n0 2\r\n5 2\r\n", "output": "6\r\n"}, {"input": "4 0 0 5 0\r\n9 4\r\n8 3\r\n-1 0\r\n1 4\r\n", "output": "33\r\n"}, {"input": "5 -6 -4 0 10\r\n-7 6\r\n-9 7\r\n-5 -1\r\n-2 1\r\n-8 10\r\n", "output": "100\r\n"}, {"input": "10 -68 10 87 22\r\n30 89\r\n82 -97\r\n-52 25\r\n76 -22\r\n-20 95\r\n21 25\r\n2 -3\r\n45 -7\r\n-98 -56\r\n-15 16\r\n", "output": "22034\r\n"}, {"input": "1 -10000000 -10000000 -10000000 -9999999\r\n10000000 10000000\r\n", "output": "799999960000001\r\n"}]
false
stdio
null
true
665/B
665
B
PyPy 3-64
TESTS
3
46
0
147573049
n,m,k=map(int,input().split()) arr=list(map(int,input().split())) quer=[] for i in range(m): quer.append(list(map(int,input().split()))) ans=0 for i in range(m): a=quer[i] for j in a: d=arr.index(j) ans+=d+1 arr.pop(d) arr=[j]+arr print(ans)
10
62
0
212894629
import sys readline=sys.stdin.readline N,M,K=map(int,readline().split()) P=list(map(int,readline().split())) ans=0 for n in range(N): A=list(map(int,readline().split())) for a in A: i=P.index(a) ans+=i+1 P=[a]+P[:i]+P[i+1:] print(ans)
Educational Codeforces Round 12
ICPC
2,016
1
256
Shopping
Ayush is a cashier at the shopping center. Recently his department has started a ''click and collect" service which allows users to shop online. The store contains k items. n customers have already used the above service. Each user paid for m items. Let aij denote the j-th item in the i-th person's order. Due to the space limitations all the items are arranged in one single row. When Ayush receives the i-th order he will find one by one all the items aij (1 ≤ j ≤ m) in the row. Let pos(x) denote the position of the item x in the row at the moment of its collection. Then Ayush takes time equal to pos(ai1) + pos(ai2) + ... + pos(aim) for the i-th customer. When Ayush accesses the x-th element he keeps a new stock in the front of the row and takes away the x-th element. Thus the values are updating. Your task is to calculate the total time it takes for Ayush to process all the orders. You can assume that the market has endless stock.
The first line contains three integers n, m and k (1 ≤ n, k ≤ 100, 1 ≤ m ≤ k) — the number of users, the number of items each user wants to buy and the total number of items at the market. The next line contains k distinct integers pl (1 ≤ pl ≤ k) denoting the initial positions of the items in the store. The items are numbered with integers from 1 to k. Each of the next n lines contains m distinct integers aij (1 ≤ aij ≤ k) — the order of the i-th person.
Print the only integer t — the total time needed for Ayush to process all the orders.
null
Customer 1 wants the items 1 and 5. pos(1) = 3, so the new positions are: [1, 3, 4, 2, 5]. pos(5) = 5, so the new positions are: [5, 1, 3, 4, 2]. Time taken for the first customer is 3 + 5 = 8. Customer 2 wants the items 3 and 1. pos(3) = 3, so the new positions are: [3, 5, 1, 4, 2]. pos(1) = 3, so the new positions are: [1, 3, 5, 4, 2]. Time taken for the second customer is 3 + 3 = 6. Total time is 8 + 6 = 14. Formally pos(x) is the index of x in the current row.
[{"input": "2 2 5\n3 4 1 2 5\n1 5\n3 1", "output": "14"}]
1,400
["brute force"]
10
[{"input": "2 2 5\r\n3 4 1 2 5\r\n1 5\r\n3 1\r\n", "output": "14\r\n"}, {"input": "4 4 4\r\n1 2 3 4\r\n3 4 2 1\r\n4 3 2 1\r\n4 1 2 3\r\n4 1 2 3\r\n", "output": "59\r\n"}, {"input": "1 1 1\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "10 1 100\r\n1 55 67 75 40 86 24 84 82 26 81 23 70 79 51 54 21 78 31 98 68 93 66 88 99 65 20 52 35 85 16 12 94 100 59 56 18 33 47 46 71 8 38 57 2 92 3 95 6 4 87 22 48 80 15 29 11 45 72 76 44 60 91 90 39 74 41 36 13 27 53 83 32 5 30 63 89 64 49 17 9 97 69 14 50 77 37 96 10 42 28 34 61 19 73 7 62 43 58 25\r\n33\r\n69\r\n51\r\n7\r\n68\r\n70\r\n1\r\n35\r\n24\r\n7\r\n", "output": "335\r\n"}, {"input": "100 1 1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n", "output": "100\r\n"}, {"input": "3 2 3\r\n3 1 2\r\n1 2\r\n2 1\r\n2 3\r\n", "output": "13\r\n"}, {"input": "10 10 10\r\n3 4 1 2 8 9 5 10 6 7\r\n9 10 7 8 6 1 2 3 4 5\r\n2 5 3 6 1 4 9 7 8 10\r\n2 9 1 8 4 7 5 10 6 3\r\n10 9 7 1 3 6 2 8 5 4\r\n2 5 1 3 7 10 4 9 8 6\r\n6 1 8 7 9 2 3 5 4 10\r\n1 3 2 8 6 9 4 10 5 7\r\n5 2 4 8 6 1 10 9 3 7\r\n5 1 7 10 4 6 2 8 9 3\r\n2 1 3 9 7 10 6 4 8 5\r\n", "output": "771\r\n"}]
false
stdio
null
true
178/A1
178
A1
Python 3
TESTS1
4
186
307,200
95153124
import math n = int(input()) seq = list(map(lambda x: int(x), input().split())) last = seq[n - 1] def roundPO2(v): v = v - 1; v = v | v >> 1; v = v | v >> 2; v = v | v >> 4; v = v | v >> 8; v = v | v >> 16; v = v + 1; return v for k in range(1, n): move = 0 stacking = dict() for j in range(1, k + 1): po2 = roundPO2(k + 1 - j) #print('po2 of {0} - {1} is {2}'.format(k, j, po2)) #print('{0} + {1} > {2}'.format(po2, j, n)) while po2 + j > n: po2 = (math.log(po2, 2) - 1) ** 2 movePos = po2 + j #print('movePos: ' + str(movePos)) w = seq[j-1] + (stacking[j] if j in stacking else 0) #print('seq[j-1]' + str(seq[j-1])) #print('weight: ' + str(w)) if movePos <= k: stacking[movePos] = w + (stacking[movePos] if movePos in stacking else 0) move = move + w print(move)
12
62
0
190196910
import math n=int(input()) a=[int(x) for x in input().split()] steps=0 for i in range(1, n): t=(int)(math.log2(n-i)) steps+=a[i-1] a[i+(2**t)-1]+=a[i-1] a[i-1]=0 print(steps)
ABBYY Cup 2.0 - Hard
ICPC
2,012
2
256
Educational Game
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below. The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some prefix of the sequence) equal to zero for some fixed k (k < n), and this should be done in the smallest possible number of moves. One move is choosing an integer i (1 ≤ i ≤ n) such that ai > 0 and an integer t (t ≥ 0) such that i + 2t ≤ n. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of ai + 2t is increased by 1. For example, let n = 4 and a = (1, 0, 1, 2), then it is possible to make move i = 3, t = 0 and get a = (1, 0, 0, 3) or to make move i = 1, t = 1 and get a = (0, 0, 2, 2) (the only possible other move is i = 1, t = 0). You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1 ≤ k < n).
The first input line contains a single integer n. The second line contains n integers ai (0 ≤ ai ≤ 104), separated by single spaces. The input limitations for getting 20 points are: - 1 ≤ n ≤ 300 The input limitations for getting 50 points are: - 1 ≤ n ≤ 2000 The input limitations for getting 100 points are: - 1 ≤ n ≤ 105
Print exactly n - 1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.
null
null
[{"input": "4\n1 0 1 2", "output": "1\n1\n3"}, {"input": "8\n1 2 3 4 5 6 7 8", "output": "1\n3\n6\n10\n16\n24\n40"}]
1,000
[]
12
[{"input": "4\r\n1 0 1 2\r\n", "output": "1\r\n1\r\n3\r\n"}, {"input": "8\r\n1 2 3 4 5 6 7 8\r\n", "output": "1\r\n3\r\n6\r\n10\r\n16\r\n24\r\n40\r\n"}, {"input": "5\r\n4 1 4 7 6\r\n", "output": "4\r\n5\r\n9\r\n17\r\n"}, {"input": "9\r\n13 13 7 11 3 9 3 5 5\r\n", "output": "13\r\n26\r\n33\r\n44\r\n47\r\n69\r\n79\r\n117\r\n"}, {"input": "30\r\n8 17 20 15 18 15 20 10 5 13 5 4 15 9 11 14 18 15 7 16 18 9 17 7 10 9 5 13 17 16\r\n", "output": "8\r\n25\r\n45\r\n60\r\n78\r\n93\r\n113\r\n123\r\n128\r\n141\r\n146\r\n150\r\n165\r\n174\r\n185\r\n199\r\n225\r\n257\r\n284\r\n315\r\n351\r\n375\r\n423\r\n454\r\n495\r\n549\r\n634\r\n713\r\n907\r\n"}, {"input": "80\r\n72 66 82 46 44 22 63 92 71 65 5 30 45 84 29 73 9 90 25 19 26 15 12 29 33 19 85 92 91 66 83 39 100 53 20 99 11 81 26 41 36 51 21 72 28 100 34 3 24 58 11 85 73 18 4 45 90 99 42 85 26 71 58 49 76 32 88 13 40 98 57 95 20 36 70 66 75 12 54 96\r\n", "output": "72\n138\n220\n266\n310\n332\n395\n487\n558\n623\n628\n658\n703\n787\n816\n889\n898\n988\n1013\n1032\n1058\n1073\n1085\n1114\n1147\n1166\n1251\n1343\n1434\n1500\n1583\n1622\n1722\n1775\n1795\n1894\n1905\n1986\n2012\n2053\n2089\n2140\n2161\n2233\n2261\n2361\n2395\n2398\n2431\n2579\n2615\n2719\n2818\n2851\n2867\n2941\n3064\n3182\n3309\n3486\n3603\n3740\n3881\n3969\n4250\n4549\n4775\n5037\n5231\n5465\n5627\n5929\n6460\n7029\n7478\n8085\n9075\n10211\n12070\n"}]
false
stdio
null
true
175/B
175
B
Python 3
TESTS
3
92
0
27107788
n = int(input()) player_points = {} for _ in range(n): name, points = input().split() points = int(points) if name in player_points: if player_points[name] < points: player_points[name] = points else: player_points[name] = points points_list = sorted(player_points.values(), reverse=True) m = len(player_points) print(m) for player, points in player_points.items(): if points_list[m // 2] > points: print(player, "noob") elif points_list[((m + 4) // 5) - 1] > points and \ points_list[((m + 1) // 2) - 1] <= points: print(player, "random") elif points_list[((m + 9) // 10) - 1] > points and \ points_list[((m + 4) // 5) - 1] <= points: print(player, "average") elif points_list[((m + 99) // 100) - 1] > points and \ points_list[((m + 9) // 10) - 1] <= points: print(player, "hardcore") else: print(player, "pro")
46
342
24,064,000
131139063
from collections import defaultdict, OrderedDict n = int(input()) u = defaultdict(int) for _ in range(n) : name, score = input().split() u[name] = max(u[name], int(score)) scoreToPlayers = defaultdict(list) for pl in u : scoreToPlayers[u[pl]].append(pl) v = OrderedDict(sorted(scoreToPlayers.items())) res = [] before = 0 for (i, s) in enumerate(v): perc = (len(v[s]) + before) / len(u) r = "noob" if 0.5 <= perc < 0.8 : r = "random" elif 0.8 <= perc < 0.9 : r = "average" elif 0.9 <= perc < 0.99 : r = "hardcore" elif 0.99 <= perc : r = "pro" for name in v[s] : res.append((name, r)) before += len(v[s]) print(len(res)) for (name, category) in res : print(name, category)
Codeforces Round 115
CF
2,012
2
256
Plane of Tanks: Pro
Vasya has been playing Plane of Tanks with his friends the whole year. Now it is time to divide the participants into several categories depending on their results. A player is given a non-negative integer number of points in each round of the Plane of Tanks. Vasya wrote results for each round of the last year. He has n records in total. In order to determine a player's category consider the best result obtained by the player and the best results of other players. The player belongs to category: - "noob" — if more than 50% of players have better results; - "random" — if his result is not worse than the result that 50% of players have, but more than 20% of players have better results; - "average" — if his result is not worse than the result that 80% of players have, but more than 10% of players have better results; - "hardcore" — if his result is not worse than the result that 90% of players have, but more than 1% of players have better results; - "pro" — if his result is not worse than the result that 99% of players have. When the percentage is calculated the player himself is taken into account. That means that if two players played the game and the first one gained 100 points and the second one 1000 points, then the first player's result is not worse than the result that 50% of players have, and the second one is not worse than the result that 100% of players have. Vasya gave you the last year Plane of Tanks results. Help Vasya determine each player's category.
The first line contains the only integer number n (1 ≤ n ≤ 1000) — a number of records with the players' results. Each of the next n lines contains a player's name and the amount of points, obtained by the player for the round, separated with a space. The name contains not less than 1 and no more than 10 characters. The name consists of lowercase Latin letters only. It is guaranteed that any two different players have different names. The amount of points, obtained by the player for the round, is a non-negative integer number and does not exceed 1000.
Print on the first line the number m — the number of players, who participated in one round at least. Each one of the next m lines should contain a player name and a category he belongs to, separated with space. Category can be one of the following: "noob", "random", "average", "hardcore" or "pro" (without quotes). The name of each player should be printed only once. Player names with respective categories can be printed in an arbitrary order.
null
In the first example the best result, obtained by artem is not worse than the result that 25% of players have (his own result), so he belongs to category "noob". vasya and kolya have best results not worse than the results that 75% players have (both of them and artem), so they belong to category "random". igor has best result not worse than the result that 100% of players have (all other players and himself), so he belongs to category "pro". In the second example both players have the same amount of points, so they have results not worse than 100% players have, so they belong to category "pro".
[{"input": "5\nvasya 100\nvasya 200\nartem 100\nkolya 200\nigor 250", "output": "4\nartem noob\nigor pro\nkolya random\nvasya random"}, {"input": "3\nvasya 200\nkolya 1000\nvasya 1000", "output": "2\nkolya pro\nvasya pro"}]
1,400
["implementation"]
78
[{"input": "5\r\nvasya 100\r\nvasya 200\r\nartem 100\r\nkolya 200\r\nigor 250\r\n", "output": "4\r\nartem noob\r\nigor pro\r\nkolya random\r\nvasya random\r\n"}, {"input": "3\r\nvasya 200\r\nkolya 1000\r\nvasya 1000\r\n", "output": "2\r\nkolya pro\r\nvasya pro\r\n"}, {"input": "1\r\nvasya 1000\r\n", "output": "1\r\nvasya pro\r\n"}, {"input": "5\r\nvasya 1000\r\nvasya 100\r\nkolya 200\r\npetya 300\r\noleg 400\r\n", "output": "4\r\nkolya noob\r\noleg random\r\npetya random\r\nvasya pro\r\n"}, {"input": "10\r\na 1\r\nb 2\r\nc 3\r\nd 4\r\ne 5\r\nf 6\r\ng 7\r\nh 8\r\ni 9\r\nj 10\r\n", "output": "10\r\na noob\r\nb noob\r\nc noob\r\nd noob\r\ne random\r\nf random\r\ng random\r\nh average\r\ni hardcore\r\nj pro\r\n"}, {"input": "10\r\nj 10\r\ni 9\r\nh 8\r\ng 7\r\nf 6\r\ne 5\r\nd 4\r\nc 3\r\nb 2\r\na 1\r\n", "output": "10\r\na noob\r\nb noob\r\nc noob\r\nd noob\r\ne random\r\nf random\r\ng random\r\nh average\r\ni hardcore\r\nj pro\r\n"}, {"input": "1\r\ntest 0\r\n", "output": "1\r\ntest pro\r\n"}]
false
stdio
import sys def main(input_path, output_path, submission_path): # Read input data with open(input_path) as f: n = int(f.readline()) players = {} for _ in range(n): name, points = f.readline().strip().split() points = int(points) if name not in players or points > players[name]: players[name] = points m = len(players) best_scores = list(players.values()) # Compute correct categories correct = {} for name in players: score = players[name] cnt = sum(1 for s in best_scores if s > score) percentage = (cnt * 100.0) / m if percentage > 50: cat = 'noob' elif percentage > 20: cat = 'random' elif percentage > 10: cat = 'average' elif percentage > 1: cat = 'hardcore' else: cat = 'pro' correct[name] = cat # Read submission output with open(submission_path) as f: lines = f.read().splitlines() if not lines: print(0) return try: m_sub = int(lines[0]) except: print(0) return if m_sub != m: print(0) return submission = {} for line in lines[1:]: parts = line.strip().split() if len(parts) != 2: print(0) return name, cat = parts if name in submission: print(0) return submission[name] = cat # Check all players are present and correct if submission.keys() != correct.keys(): print(0) return for name, cat in submission.items(): if correct.get(name) != cat: print(0) return print(1) if __name__ == "__main__": input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] main(input_path, output_path, submission_path)
true
60/E
60
E
Python 3
TESTS
3
186
307,200
91180189
import sys I = lambda: int(input()) RL = readline = lambda: sys.stdin.readline().strip('\n') RM = readmap = lambda x = int: map(x,readline().split(' ')) #E n,x,y,p = RM() a = [*RM()] s = temps = sum(a) ends = a[0] + a[-1] for i in range(x): temps = (3*temps - ends)%p ends = a[0] + a[-2] + x*a[-1] for i in range(y): temps = (3*temps - ends)%p print(temps)
58
2,088
80,179,200
99377097
#!/usr/bin/pypy3 from sys import stdin, stdout input, print = stdin.readline, stdout.write p = 0 def readints(): return list(map(int, input().split())) def writeln(x): print(str(x) + '\n') def mod(x): return (x % p + p) % p def matmul(a, b): n = len(a) c = [[0 for x in range(n)] for y in range(n)] for i in range(n): for j in range(n): for k in range(n): c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % p return c def matpow(b, p): n = len(b) res = [[0 if x != y else 1 for x in range(n)] for y in range(n)] while p: if p & 1: res = matmul(res, b) b = matmul(b, b) p >>= 1 return res n, x, y, p = readints() a = [each % p for each in sorted(readints())] if n == 1: writeln(a[0]) exit(0) """ [a sum] * [[1 -1], = [a sum] [0 3]] [sc mv] * [[0 1], [1, 1]] = [mv mv+sc] """ b = matpow([ [1, -1], [0, 3]], x) sum0 = matmul([ [a[0] + a[-1], sum(a)], [0, 0]], b)[0][1] b = matpow([ [0, 1], [1, 1]], x) maxa = matmul( [[a[-2], a[-1]], [0, 0]], b)[0][1] b = matpow([ [1, -1], [0, 3]], y) sum1 = matmul([ [a[0] + maxa, sum0], [0, 0]], b)[0][1] writeln(mod(sum1))
Codeforces Beta Round 56
CF
2,011
3
256
Mushroom Gnomes
Once upon a time in the thicket of the mushroom forest lived mushroom gnomes. They were famous among their neighbors for their magic mushrooms. Their magic nature made it possible that between every two neighboring mushrooms every minute grew another mushroom with the weight equal to the sum of weights of two neighboring ones. The mushroom gnomes loved it when everything was in order, that's why they always planted the mushrooms in one line in the order of their weights' increasing. Well... The gnomes planted the mushrooms and went to eat. After x minutes they returned and saw that new mushrooms had grown up, so that the increasing order had been violated. The gnomes replanted all the mushrooms in the correct order, that is, they sorted the mushrooms in the order of the weights' increasing. And went to eat again (those gnomes were quite big eaters). What total weights modulo p will the mushrooms have in another y minutes?
The first line contains four integers n, x, y, p (1 ≤ n ≤ 106, 0 ≤ x, y ≤ 1018, x + y > 0, 2 ≤ p ≤ 109) which represent the number of mushrooms, the number of minutes after the first replanting, the number of minutes after the second replanting and the module. The next line contains n integers ai which represent the mushrooms' weight in the non-decreasing order (0 ≤ ai ≤ 109). Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).
The answer should contain a single number which is the total weights of the mushrooms modulo p in the end after x + y minutes.
null
null
[{"input": "2 1 0 657276545\n1 2", "output": "6"}, {"input": "2 1 1 888450282\n1 2", "output": "14"}, {"input": "4 5 0 10000\n1 2 3 4", "output": "1825"}]
2,600
["math", "matrices"]
58
[{"input": "2 1 0 657276545\r\n1 2\r\n", "output": "6\r\n"}, {"input": "2 1 1 888450282\r\n1 2\r\n", "output": "14\r\n"}, {"input": "4 5 0 10000\r\n1 2 3 4\r\n", "output": "1825\r\n"}, {"input": "4 0 8 78731972\r\n1 52 76 81\r\n", "output": "1108850\r\n"}, {"input": "4 0 8 414790855\r\n1 88 97 99\r\n", "output": "1541885\r\n"}, {"input": "11 10 6 560689961\r\n2 17 20 24 32 37 38 39 40 61 86\r\n", "output": "9840917\r\n"}, {"input": "8 4 9 371687114\r\n1 7 22 31 35 38 62 84\r\n", "output": "1827639\r\n"}, {"input": "4 8 6 398388678\r\n21 22 78 88\r\n", "output": "338926799\r\n"}, {"input": "33 93 37 411512841\r\n71 76 339 357 511 822 1564 1747 1974 2499 2763 3861 3950 4140 4306 4992 5056 5660 5694 5773 6084 6512 6742 6898 7133 8616 8772 8852 8918 9046 9572 9679 9708\r\n", "output": "158919800\r\n"}, {"input": "1 1 0 2\r\n1\r\n", "output": "1\r\n"}, {"input": "1 1 1 2\r\n1000000000\r\n", "output": "0\r\n"}, {"input": "1 1 1 2\r\n0\r\n", "output": "0\r\n"}, {"input": "1 1 1 2\r\n2\r\n", "output": "0\r\n"}, {"input": "1 0 1 1000000000\r\n0\r\n", "output": "0\r\n"}, {"input": "1 1 1 1000000000\r\n1\r\n", "output": "1\r\n"}, {"input": "1 0 1 1000000000\r\n1000000000\r\n", "output": "0\r\n"}, {"input": "2 1 1 1000\r\n0 0\r\n", "output": "0\r\n"}, {"input": "2 1000000000 1000000000000 10000\r\n1 2\r\n", "output": "3\r\n"}]
false
stdio
null
true
886/F
886
F
Python 3
PRETESTS
2
108
16,896,000
32266125
n=int(input()) a=[[0]*2000 for i in range(2000)] for i in range(n): s=input().split() k1=int(s[0])-1 k2=int(s[1])-1 a[k1][k2]=1 k=-1 for i in range(n): for j in range(i,n): if a[i][j]==a[j][i] and a[j][i]!=0: k+=2 print(k)
51
794
19,046,400
211944348
from fractions import Fraction import time class Point: def __init__(self, x, y): self.x = x self.y = y def to_tuple(self): return (self.x, self.y) def __repr__(self): return "Point({}, {})".format(self.x, self.y) def __eq__(self, other): return self.to_tuple() == other.to_tuple() def __hash__(self): return hash(self.to_tuple()) def __neg__(self): return Point(-self.x, -self.y) def __add__(self, other): return Point(self.x+other.x, self.y+other.y) def __sub__(self, other): return self+(-other) def scalar_mul(self, mu): return Point(mu*self.x, mu*self.y) def int_divide(self, den): return Point(self.x//den, self.y//den) class Line: def __init__(self, a, b, c): # ax+by+c=0 self.a = a self.b = b self.c = c def __repr__(self): return "{}*x + {}*y + {} = 0".format(self.a, self.b, self.c) @classmethod def between_two_points(cls, P, Q): return cls(P.y-Q.y, Q.x-P.x, P.x*Q.y-P.y*Q.x) def evaluate(self, P): return self.a*P.x+self.b*P.y+self.c def direction(self): if self.a == 0: return (0, 1) return (1, Fraction(self.b, self.a)) true_start = time.time() n = int(input()) points = set() center = Point(0, 0) for i in range(n): row = input().split(" ") cur = Point(int(row[0]), int(row[1])).scalar_mul(2*n) center += cur points.add(cur) center = center.int_divide(n) dcenter = center+center sym_points_set = set() for p in points: sym_points_set.add(dcenter-p) nosym = list(points - sym_points_set) if len(nosym) == 0: print(-1) exit(0) cnt = 0 p0 = nosym[0] good_lines = set() for p in nosym: m = (p+p0).int_divide(2) line = Line.between_two_points(m, center) distances = list(map(line.evaluate, nosym)) ok = True mydict = {} for dd in distances: dda = abs(dd) if dda not in mydict: mydict[dda] = 1 else: mydict[dda] += 1 for k in mydict: if mydict[k] % 2 == 1 and k != 0: ok = False break if ok: good_lines.add(line.direction()) print(len(good_lines))
Технокубок 2018 - Отборочный Раунд 3
CF
2,017
2
256
Symmetric Projections
You are given a set of n points on the plane. A line containing the origin is called good, if projection of the given set to this line forms a symmetric multiset of points. Find the total number of good lines. Multiset is a set where equal elements are allowed. Multiset is called symmetric, if there is a point P on the plane such that the multiset is centrally symmetric in respect of point P.
The first line contains a single integer n (1 ≤ n ≤ 2000) — the number of points in the set. Each of the next n lines contains two integers xi and yi ( - 106 ≤ xi, yi ≤ 106) — the coordinates of the points. It is guaranteed that no two points coincide.
If there are infinitely many good lines, print -1. Otherwise, print single integer — the number of good lines.
null
Picture to the first sample test: In the second sample, any line containing the origin is good.
[{"input": "3\n1 2\n2 1\n3 3", "output": "3"}, {"input": "2\n4 3\n1 2", "output": "-1"}]
2,900
["geometry"]
51
[{"input": "3\r\n1 2\r\n2 1\r\n3 3\r\n", "output": "3\r\n"}, {"input": "2\r\n4 3\r\n1 2\r\n", "output": "-1\r\n"}, {"input": "6\r\n0 4\r\n1 5\r\n2 1\r\n3 2\r\n4 3\r\n5 0\r\n", "output": "5\r\n"}, {"input": "1\r\n5 2\r\n", "output": "-1\r\n"}, {"input": "4\r\n2 4\r\n1 2\r\n0 0\r\n-2 -4\r\n", "output": "1\r\n"}, {"input": "10\r\n0 5\r\n1 0\r\n2 3\r\n3 2\r\n4 6\r\n5 9\r\n6 1\r\n7 8\r\n8 4\r\n9 7\r\n", "output": "5\r\n"}, {"input": "9\r\n-1000000 -500000\r\n-750000 250000\r\n-500000 1000000\r\n-250000 -250000\r\n0 -1000000\r\n250000 750000\r\n500000 0\r\n750000 -750000\r\n1000000 500000\r\n", "output": "5\r\n"}, {"input": "10\r\n-84 -60\r\n-41 -100\r\n8 -8\r\n-52 -62\r\n-61 -76\r\n-52 -52\r\n14 -11\r\n-2 -54\r\n46 8\r\n26 -17\r\n", "output": "0\r\n"}, {"input": "5\r\n-1000000 -500000\r\n-500000 0\r\n0 500000\r\n500000 1000000\r\n1000000 -1000000\r\n", "output": "3\r\n"}, {"input": "6\r\n-100000 100000\r\n-60000 -20000\r\n-20000 20000\r\n20000 60000\r\n60000 -100000\r\n100000 -60000\r\n", "output": "5\r\n"}, {"input": "8\r\n-10000 4285\r\n-7143 -10000\r\n-4286 -1429\r\n-1429 -7143\r\n1428 1428\r\n4285 9999\r\n7142 7142\r\n9999 -4286\r\n", "output": "5\r\n"}, {"input": "10\r\n-1000000 -777778\r\n-777778 555554\r\n-555556 333332\r\n-333334 111110\r\n-111112 999998\r\n111110 -333334\r\n333332 -1000000\r\n555554 -555556\r\n777776 -111112\r\n999998 777776\r\n", "output": "3\r\n"}, {"input": "7\r\n14 -3\r\n2 -13\r\n12 -1\r\n10 -7\r\n8 -11\r\n4 -9\r\n6 -5\r\n", "output": "5\r\n"}, {"input": "24\r\n-1 -7\r\n-37 -45\r\n-1 -97\r\n-37 -25\r\n9 -107\r\n-47 -85\r\n-73 -43\r\n-73 -63\r\n9 -87\r\n-63 -3\r\n-47 -35\r\n-47 -15\r\n15 -39\r\n-11 -87\r\n-63 -73\r\n-17 -65\r\n-1 -77\r\n9 -17\r\n-53 -63\r\n-1 -27\r\n-63 -53\r\n-57 -25\r\n-11 3\r\n-11 -17\r\n", "output": "2\r\n"}, {"input": "8\r\n11 -3\r\n12 -5\r\n10 -6\r\n9 -4\r\n8 -8\r\n6 -7\r\n7 -10\r\n5 -9\r\n", "output": "4\r\n"}, {"input": "32\r\n16 37\r\n-26 41\r\n5 -6\r\n12 -5\r\n17 -30\r\n-31 -14\r\n-35 4\r\n-23 -20\r\n17 -20\r\n-25 34\r\n-33 40\r\n-32 33\r\n15 24\r\n22 -25\r\n-30 -21\r\n13 -12\r\n6 -13\r\n6 37\r\n-40 -1\r\n22 25\r\n16 17\r\n-16 21\r\n11 42\r\n11 32\r\n-26 21\r\n-35 -6\r\n12 -25\r\n23 18\r\n-21 16\r\n-24 -13\r\n-21 26\r\n-30 -1\r\n", "output": "3\r\n"}, {"input": "10\r\n-7 6\r\n-16 11\r\n-9 -5\r\n3 4\r\n-8 12\r\n-17 6\r\n2 -1\r\n-5 15\r\n-7 4\r\n-6 -2\r\n", "output": "-1\r\n"}, {"input": "10\r\n-8 11\r\n1 10\r\n2 10\r\n2 11\r\n0 9\r\n3 12\r\n-7 16\r\n11 4\r\n4 8\r\n12 9\r\n", "output": "3\r\n"}, {"input": "10\r\n9 6\r\n8 1\r\n-10 13\r\n-11 8\r\n-1 6\r\n0 8\r\n-2 7\r\n-1 7\r\n1 17\r\n-3 -3\r\n", "output": "3\r\n"}, {"input": "20\r\n12 -3\r\n-18 -24\r\n-13 7\r\n17 -23\r\n15 11\r\n-17 5\r\n0 -26\r\n18 10\r\n12 -18\r\n-14 -26\r\n-20 -24\r\n16 4\r\n-19 -21\r\n-14 -11\r\n-15 -19\r\n-18 12\r\n16 10\r\n-2 12\r\n11 9\r\n13 -25\r\n", "output": "5\r\n"}, {"input": "50\r\n-38 -107\r\n-34 -75\r\n-200 -143\r\n-222 -139\r\n-34 55\r\n-102 -79\r\n48 -99\r\n2 -237\r\n-118 -167\r\n-56 -41\r\n10 17\r\n68 -89\r\n-32 41\r\n-100 -93\r\n84 -1\r\n86 -15\r\n46 -145\r\n-58 -117\r\n8 31\r\n-36 -61\r\n-12 21\r\n-116 79\r\n88 -205\r\n70 -103\r\n-78 -37\r\n106 -5\r\n-96 -201\r\n-60 -103\r\n-54 45\r\n-138 -177\r\n-178 -47\r\n-154 -5\r\n-138 83\r\n44 -131\r\n-76 -191\r\n-176 -61\r\n-14 -65\r\n-210 53\r\n-116 -181\r\n-74 -205\r\n-174 -15\r\n0 -223\r\n-136 69\r\n-198 -57\r\n-76 -51\r\n-152 -19\r\n-80 -83\r\n22 -227\r\n24 -141\r\n-220 -153\r\n", "output": "7\r\n"}, {"input": "6\r\n-10 -10\r\n-10 10\r\n10 10\r\n10 -10\r\n10 11\r\n10 -11\r\n", "output": "1\r\n"}]
false
stdio
null
true
713/C
713
C
Python 3
TESTS
0
31
0
200936261
n = int(input()) a = list(map(int, input().split())) ops = 0 prev = a[0] for i in range(1, n): if a[i] <= prev: diff = prev - a[i] + 1 a[i] += diff ops += diff prev = a[i] print(ops)
57
187
7,168,000
174348022
from heapq import heappop, heappush, heapreplace import sys import io import os # region IO BUFSIZE = 8192 class FastIO(io.IOBase): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.buffer = io.BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(io.IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) def input(): return sys.stdin.readline().rstrip('\r\n') def read_int_list(): return list(map(int, input().split())) def read_int_tuple(): return tuple(map(int, input().split())) def read_int(): return int(input()) # endregion # region local test # if 'AW' in os.environ.get('COMPUTERNAME', ''): # test_no = 1 # f = open(os.path.dirname(__file__) + f'\\in{test_no}.txt', 'r') # def input(): # return f.readline().rstrip("\r\n") # endregion n = read_int() nums = read_int_list() hp, res = [-nums[0]], 0 for i in range(1, n): x = nums[i] - i heappush(hp, -x) if -hp[0] > x: res += -hp[0] - x heappop(hp) heappush(hp, -x) print(res) # normal = [x - i for i, x in enumerate(nums)] # xs = sorted(set(normal)) # inv = {x: i for i, x in enumerate(xs)} # m = len(xs) # inf = 123456789012345 # dp = [abs(normal[0] - x) for x in xs] # for i in range(1, m): # if dp[i] > dp[i - 1]: # dp[i] = dp[i - 1] # for i in range(1, n): # x = normal[i] # np = [inf] * m # for j in range(m): # np[j] = min(np[j - 1], dp[j] + abs(x - xs[j])) # dp = np # # print(dp) # print(min(dp))
Codeforces Round 371 (Div. 1)
CF
2,016
5
256
Sonya and Problem Wihtout a Legend
Sonya was unable to think of a story for this problem, so here comes the formal description. You are given the array containing n positive integers. At one turn you can pick any element and increase or decrease it by 1. The goal is the make the array strictly increasing by making the minimum possible number of operations. You are allowed to change elements in any way, they can become negative or equal to 0.
The first line of the input contains a single integer n (1 ≤ n ≤ 3000) — the length of the array. Next line contains n integer ai (1 ≤ ai ≤ 109).
Print the minimum number of operation required to make the array strictly increasing.
null
In the first sample, the array is going to look as follows: 2 3 5 6 7 9 11 |2 - 2| + |1 - 3| + |5 - 5| + |11 - 6| + |5 - 7| + |9 - 9| + |11 - 11| = 9 And for the second sample: 1 2 3 4 5 |5 - 1| + |4 - 2| + |3 - 3| + |2 - 4| + |1 - 5| = 12
[{"input": "7\n2 1 5 11 5 9 11", "output": "9"}, {"input": "5\n5 4 3 2 1", "output": "12"}]
2,300
["dp", "sortings"]
57
[{"input": "7\r\n2 1 5 11 5 9 11\r\n", "output": "9\r\n"}, {"input": "5\r\n5 4 3 2 1\r\n", "output": "12\r\n"}, {"input": "2\r\n1 1000\r\n", "output": "0\r\n"}, {"input": "2\r\n1000 1\r\n", "output": "1000\r\n"}, {"input": "5\r\n100 80 60 70 90\r\n", "output": "54\r\n"}, {"input": "10\r\n10 16 17 11 1213 1216 1216 1209 3061 3062\r\n", "output": "16\r\n"}, {"input": "20\r\n103 103 110 105 107 119 113 121 116 132 128 124 128 125 138 137 140 136 154 158\r\n", "output": "43\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}, {"input": "5\r\n1 1 1 2 3\r\n", "output": "3\r\n"}, {"input": "1\r\n1000\r\n", "output": "0\r\n"}, {"input": "50\r\n499 780 837 984 481 526 944 482 862 136 265 605 5 631 974 967 574 293 969 467 573 845 102 224 17 873 648 120 694 996 244 313 404 129 899 583 541 314 525 496 443 857 297 78 575 2 430 137 387 319\r\n", "output": "12423\r\n"}, {"input": "75\r\n392 593 98 533 515 448 220 310 386 79 539 294 208 828 75 534 875 493 94 205 656 105 546 493 60 188 222 108 788 504 809 621 934 455 307 212 630 298 938 62 850 421 839 134 950 256 934 817 209 559 866 67 990 835 534 672 468 768 757 516 959 893 275 315 692 927 321 554 801 805 885 12 67 245 495\r\n", "output": "17691\r\n"}, {"input": "10\r\n26 723 970 13 422 968 875 329 234 983\r\n", "output": "2546\r\n"}, {"input": "20\r\n245 891 363 6 193 704 420 447 237 947 664 894 512 194 513 616 671 623 686 378\r\n", "output": "3208\r\n"}, {"input": "5\r\n850 840 521 42 169\r\n", "output": "1485\r\n"}]
false
stdio
null
true
292/A
292
A
Python 3
TESTS
1
218
0
53372785
r = lambda: int(input()) ra = lambda: [*map(int, input().split())] a = [] t, q, mq = 0, 0, 0 n = r() for i in range(n): a.append(ra()) for i in range(n): if i==0: q = a[i][1] t = a[i][0] else: q+= a[i][1]-a[i][0]+t if q<0: q = 0 if q>mq: mq = q t = a[i][0] t = a[i][0]+q print(t, mq)
38
154
0
14651197
pt, s, vs = 0, 0, 0 for i in range(int(input())): t, c = map(int, input().split()) s = max(s - (t - pt), 0) + c vs = max(vs, s) pt = t print(pt + s, vs)
Croc Champ 2013 - Round 1
CF
2,013
2
256
SMSC
Some large corporation where Polycarpus works has its own short message service center (SMSC). The center's task is to send all sorts of crucial information. Polycarpus decided to check the efficiency of the SMSC. For that, he asked to give him the statistics of the performance of the SMSC for some period of time. In the end, Polycarpus got a list of n tasks that went to the SMSC of the corporation. Each task was described by the time it was received by the SMSC and the number of text messages to send. More formally, the i-th task was described by two integers ti and ci — the receiving time (the second) and the number of the text messages, correspondingly. Polycarpus knows that the SMSC cannot send more than one text message per second. The SMSC uses a queue to organize its work. Consider a time moment x, the SMSC work at that moment as follows: 1. If at the time moment x the queue is not empty, then SMSC sends one message from the queue (SMSC gets the message from the head of the queue). Otherwise it doesn't send messages at the time moment x. 2. If at the time moment x SMSC receives a task, then it adds to the queue all the messages from this task (SMSC adds messages to the tail of the queue). Note, that the messages from the task cannot be send at time moment x. That's because the decision about sending message or not is made at point 1 before adding these messages to the queue. Given the information about all n tasks, Polycarpus wants to count two values: the time when the last text message was sent and the maximum size of the queue at some time. Help him count these two characteristics he needs to evaluate the efficiency of the SMSC.
The first line contains a single integer n (1 ≤ n ≤ 103) — the number of tasks of the SMSC. Next n lines contain the tasks' descriptions: the i-th line contains two space-separated integers ti and ci (1 ≤ ti, ci ≤ 106) — the time (the second) when the i-th task was received and the number of messages to send, correspondingly. It is guaranteed that all tasks were received at different moments of time. It is guaranteed that the tasks are sorted in the chronological order, that is, ti < ti + 1 for all integer i (1 ≤ i < n).
In a single line print two space-separated integers — the time when the last text message was sent and the maximum queue size at a certain moment of time.
null
In the first test sample: - second 1: the first message has appeared in the queue, the queue's size is 1; - second 2: the first message is sent, the second message has been received, the queue's size is 1; - second 3: the second message is sent, the queue's size is 0, Thus, the maximum size of the queue is 1, the last message was sent at the second 3.
[{"input": "2\n1 1\n2 1", "output": "3 1"}, {"input": "1\n1000000 10", "output": "1000010 10"}, {"input": "3\n3 3\n4 3\n5 3", "output": "12 7"}]
1,100
["implementation"]
38
[{"input": "2\r\n1 1\r\n2 1\r\n", "output": "3 1\r\n"}, {"input": "1\r\n1000000 10\r\n", "output": "1000010 10\r\n"}, {"input": "3\r\n3 3\r\n4 3\r\n5 3\r\n", "output": "12 7\r\n"}, {"input": "1\r\n1 1\r\n", "output": "2 1\r\n"}, {"input": "2\r\n1 11\r\n100 10\r\n", "output": "110 11\r\n"}, {"input": "4\r\n1 10\r\n2 9\r\n3 8\r\n40 3\r\n", "output": "43 25\r\n"}, {"input": "5\r\n2 1\r\n5 2\r\n6 1\r\n7 1\r\n8 1\r\n", "output": "10 2\r\n"}, {"input": "4\r\n10 1000\r\n99998 20\r\n99999 10\r\n1000000 100\r\n", "output": "1000100 1000\r\n"}, {"input": "6\r\n10 10\r\n100 500\r\n200 500\r\n500 1\r\n999995 4\r\n999996 15\r\n", "output": "1000014 900\r\n"}, {"input": "10\r\n1 5\r\n2 5\r\n3 10\r\n4 8\r\n5 5\r\n6 4\r\n7 8\r\n8 9\r\n9 2\r\n10 10\r\n", "output": "67 57\r\n"}, {"input": "10\r\n26 4\r\n85 97\r\n86 62\r\n87 74\r\n92 8\r\n93 81\r\n97 12\r\n98 25\r\n99 31\r\n100 3\r\n", "output": "478 378\r\n"}, {"input": "10\r\n964416 3980\r\n987048 334\r\n999576 6922\r\n999684 2385\r\n999896 6558\r\n999948 3515\r\n999966 1517\r\n999984 2233\r\n999988 7242\r\n999994 91\r\n", "output": "1030039 30045\r\n"}, {"input": "5\r\n987640 52\r\n994481 69\r\n995526 50\r\n996631 75\r\n999763 22\r\n", "output": "999785 75\r\n"}, {"input": "23\r\n5 1045\r\n12 703\r\n16 26\r\n23 3384\r\n28 4563\r\n30 4501\r\n34 1033\r\n35 1393\r\n36 4095\r\n37 1279\r\n38 1787\r\n39 770\r\n40 5362\r\n41 4569\r\n42 3148\r\n43 2619\r\n44 5409\r\n45 3919\r\n46 732\r\n47 1297\r\n48 4512\r\n49 3231\r\n50 5169\r\n", "output": "64551 64501\r\n"}]
false
stdio
null
true
768/E
768
E
PyPy 3-64
TESTS
0
31
0
205295822
n=input i=int a=0 for _ in range(i(n())):a^=i((8*i(n())+8)**0.5-1)//2 print(['YES','NO'][a&1])
71
1,294
0
230897029
ans=0 for _ in range(int(input())): ans^=int((8*int(input())+1)**0.5-1)//2 print(['YES', 'NO'][ans>0])
Divide by Zero 2017 and Codeforces Round 399 (Div. 1 + Div. 2, combined)
CF
2,017
3
256
Game of Stones
Sam has been teaching Jon the Game of Stones to sharpen his mind and help him devise a strategy to fight the white walkers. The rules of this game are quite simple: - The game starts with n piles of stones indexed from 1 to n. The i-th pile contains si stones. - The players make their moves alternatively. A move is considered as removal of some number of stones from a pile. Removal of 0 stones does not count as a move. - The player who is unable to make a move loses. Now Jon believes that he is ready for battle, but Sam does not think so. To prove his argument, Sam suggested that they play a modified version of the game. In this modified version, no move can be made more than once on a pile. For example, if 4 stones are removed from a pile, 4 stones cannot be removed from that pile again. Sam sets up the game and makes the first move. Jon believes that Sam is just trying to prevent him from going to battle. Jon wants to know if he can win if both play optimally.
First line consists of a single integer n (1 ≤ n ≤ 106) — the number of piles. Each of next n lines contains an integer si (1 ≤ si ≤ 60) — the number of stones in i-th pile.
Print a single line containing "YES" (without quotes) if Jon wins, otherwise print "NO" (without quotes)
null
In the first case, Sam removes all the stones and Jon loses. In second case, the following moves are possible by Sam: $$\{1,2\} \rightarrow \{0,2\}, \{1,2\} \rightarrow \{1,0\}, \{1,2\} \rightarrow \{1,1\}$$ In each of these cases, last move can be made by Jon to win the game as follows: $${ \{ 0, 2 \} } \rightarrow { \{ 0, 0 \} }, { \{ 1, 0 \} } \rightarrow { \{ 0, 0 \} }, { \{ 1, 1 \} } \rightarrow { \{ 0, 1 \} }$$
[{"input": "1\n5", "output": "NO"}, {"input": "2\n1\n2", "output": "YES"}]
2,100
["bitmasks", "dp", "games"]
71
[{"input": "1\r\n5\r\n", "output": "NO"}, {"input": "2\r\n1\r\n2\r\n", "output": "YES"}, {"input": "3\r\n34\r\n44\r\n21\r\n", "output": "NO"}, {"input": "6\r\n34\r\n44\r\n21\r\n55\r\n1\r\n36\r\n", "output": "NO"}, {"input": "14\r\n34\r\n44\r\n21\r\n55\r\n1\r\n36\r\n53\r\n31\r\n58\r\n59\r\n11\r\n40\r\n20\r\n32\r\n", "output": "NO"}, {"input": "10\r\n34\r\n44\r\n21\r\n55\r\n1\r\n36\r\n53\r\n31\r\n58\r\n59\r\n", "output": "NO"}, {"input": "12\r\n34\r\n44\r\n21\r\n55\r\n1\r\n36\r\n53\r\n31\r\n58\r\n59\r\n11\r\n40\r\n", "output": "NO"}, {"input": "118\r\n34\r\n44\r\n21\r\n55\r\n1\r\n36\r\n53\r\n31\r\n58\r\n59\r\n11\r\n40\r\n20\r\n32\r\n43\r\n48\r\n16\r\n5\r\n35\r\n20\r\n21\r\n36\r\n15\r\n2\r\n11\r\n56\r\n58\r\n2\r\n40\r\n47\r\n29\r\n21\r\n4\r\n21\r\n1\r\n25\r\n51\r\n55\r\n17\r\n40\r\n56\r\n35\r\n51\r\n1\r\n34\r\n18\r\n54\r\n44\r\n1\r\n43\r\n16\r\n28\r\n21\r\n14\r\n57\r\n53\r\n29\r\n44\r\n59\r\n54\r\n47\r\n21\r\n43\r\n41\r\n11\r\n37\r\n30\r\n4\r\n39\r\n47\r\n40\r\n50\r\n52\r\n9\r\n32\r\n1\r\n19\r\n30\r\n20\r\n6\r\n25\r\n42\r\n34\r\n38\r\n42\r\n46\r\n35\r\n28\r\n20\r\n47\r\n60\r\n46\r\n35\r\n59\r\n24\r\n11\r\n25\r\n27\r\n9\r\n51\r\n39\r\n35\r\n22\r\n24\r\n10\r\n48\r\n6\r\n30\r\n10\r\n33\r\n51\r\n45\r\n38\r\n8\r\n51\r\n8\r\n7\r\n46\r\n", "output": "NO"}, {"input": "124\r\n34\r\n44\r\n21\r\n55\r\n1\r\n36\r\n53\r\n31\r\n58\r\n59\r\n11\r\n40\r\n20\r\n32\r\n43\r\n48\r\n16\r\n5\r\n35\r\n20\r\n21\r\n36\r\n15\r\n2\r\n11\r\n56\r\n58\r\n2\r\n40\r\n47\r\n29\r\n21\r\n4\r\n21\r\n1\r\n25\r\n51\r\n55\r\n17\r\n40\r\n56\r\n35\r\n51\r\n1\r\n34\r\n18\r\n54\r\n44\r\n1\r\n43\r\n16\r\n28\r\n21\r\n14\r\n57\r\n53\r\n29\r\n44\r\n59\r\n54\r\n47\r\n21\r\n43\r\n41\r\n11\r\n37\r\n30\r\n4\r\n39\r\n47\r\n40\r\n50\r\n52\r\n9\r\n32\r\n1\r\n19\r\n30\r\n20\r\n6\r\n25\r\n42\r\n34\r\n38\r\n42\r\n46\r\n35\r\n28\r\n20\r\n47\r\n60\r\n46\r\n35\r\n59\r\n24\r\n11\r\n25\r\n27\r\n9\r\n51\r\n39\r\n35\r\n22\r\n24\r\n10\r\n48\r\n6\r\n30\r\n10\r\n33\r\n51\r\n45\r\n38\r\n8\r\n51\r\n8\r\n7\r\n46\r\n49\r\n27\r\n16\r\n13\r\n4\r\n54\r\n", "output": "NO"}, {"input": "15\r\n34\r\n44\r\n21\r\n55\r\n1\r\n36\r\n53\r\n31\r\n58\r\n59\r\n11\r\n40\r\n20\r\n32\r\n43\r\n", "output": "NO"}, {"input": "2\r\n34\r\n44\r\n", "output": "NO"}]
false
stdio
null
true
366/D
366
D
Python 3
TESTS
1
62
1,228,800
19067340
from sys import stdin, stdout, stderr, exit from math import log, factorial import re from queue import PriorityQueue import functools from time import time # inputf = open('input.txt', 'r') # outputf = open('output.txt', 'w') def readil(): return list(map(int, stdin.readline().strip().split())) class time_it(object): ''' Simple decorator, that prints func execution time to stderr ''' def __init__(self, func): self.func = func def __call__(self): st = time() self.func() stderr.write('%s: %f' % (self.func.__name__, time() - st)) n = 0 m = 0 g = list() @functools.lru_cache(1024) def getl(t: tuple): try: return max(0, t[1] - t[0] + 1) except Exception as _: print(t, file=stderr) raise Exception('wtf') @functools.lru_cache(0) def comb(t1, t2): if(t1 == (0, 0)): return t2 if(t2 == (0, 0)): return t1 try: if(max(t1[0], t2[0]) > min(t1[1], t2[1])): return (0, 0) return (max(t1[0], t2[0]), min(t1[1], t2[1])) except Exception as _: print(t1, t2, file=stderr) raise Exception('wtf') @time_it def main(): global n n, m = readil() g = [list() for i in range(n)] for _ in range(m): a, b, l, r = readil() if(a == b): continue a -= 1 b -= 1 g[a].append((b, l, r)) g[b].append((a, l, r)) q = PriorityQueue(maxsize=2 * n) d = list((0, 0) for i in range(n)) q.put_nowait((1, 0)) while not q.empty(): dist, v = q.get_nowait() if(getl(d[v]) > dist): continue # stderr.write('vertex: \t' + str(v) + '\n') for tpl in g[v]: u, l, r = tpl if(getl(comb(d[v], (l, r))) > getl(d[u])): d[u] = comb((l, r), d[v]) q.put_nowait((getl(d[u]), u)) if(getl(d[n - 1]) == 0): stdout.write('Nice work, Dima!') else: stdout.write(str(getl(d[n - 1]))) if __name__ == '__main__': main() # inputf.close() # outputf.close()
30
1,029
12,185,600
218085952
class Node: def __init__(self, a, b, l, r): self.a = a self.b = b self.l = l self.r = r def func(a, p): if p[a] == -1 or p[a] == a: return a p[a] = func(p[a], p) return p[a] n, m = map(int, input().split()) node = [] for _ in range(m): a, b, l, r = map(int, input().split()) node.append(Node(a, b, l, r)) node.sort(key=lambda a: a.r, reverse=True) res = 0 for i in range(m): p = [-1] * (n + 1) for j in range(m): if node[j].r < node[i].l: break elif node[j].l > node[i].l: continue else: p[func(node[j].a, p)] = func(node[j].b, p) if func(1, p) == func(n, p): res = max(res, node[j].r - node[i].l + 1) break if res: print(res) else: print("Nice work, Dima!")# 1691624285.9143853
Codeforces Round 214 (Div. 2)
CF
2,013
3
256
Dima and Trap Graph
Dima and Inna love spending time together. The problem is, Seryozha isn't too enthusiastic to leave his room for some reason. But Dima and Inna love each other so much that they decided to get criminal... Dima constructed a trap graph. He shouted: "Hey Seryozha, have a look at my cool graph!" to get his roommate interested and kicked him into the first node. A trap graph is an undirected graph consisting of n nodes and m edges. For edge number k, Dima denoted a range of integers from lk to rk (lk ≤ rk). In order to get out of the trap graph, Seryozha initially (before starting his movements) should pick some integer (let's call it x), then Seryozha must go some way from the starting node with number 1 to the final node with number n. At that, Seryozha can go along edge k only if lk ≤ x ≤ rk. Seryozha is a mathematician. He defined the loyalty of some path from the 1-st node to the n-th one as the number of integers x, such that if he initially chooses one of them, he passes the whole path. Help Seryozha find the path of maximum loyalty and return to his room as quickly as possible!
The first line of the input contains two integers n and m (2 ≤ n ≤ 103, 0 ≤ m ≤ 3·103). Then follow m lines describing the edges. Each line contains four integers ak, bk, lk and rk (1 ≤ ak, bk ≤ n, 1 ≤ lk ≤ rk ≤ 106). The numbers mean that in the trap graph the k-th edge connects nodes ak and bk, this edge corresponds to the range of integers from lk to rk. Note that the given graph can have loops and multiple edges.
In a single line of the output print an integer — the maximum loyalty among all paths from the first node to the n-th one. If such paths do not exist or the maximum loyalty equals 0, print in a single line "Nice work, Dima!" without the quotes.
null
Explanation of the first example. Overall, we have 2 ways to get from node 1 to node 4: first you must go along the edge 1-2 with range [1-10], then along one of the two edges 2-4. One of them contains range [3-5], that is, we can pass through with numbers 3, 4, 5. So the loyalty of such path is 3. If we go along edge 2-4 with range [2-7], then we can pass through with numbers 2, 3, 4, 5, 6, 7. The loyalty is 6. That is the answer. The edge 1-2 have no influence on the answer because its range includes both ranges of the following edges.
[{"input": "4 4\n1 2 1 10\n2 4 3 5\n1 3 1 5\n2 4 2 7", "output": "6"}, {"input": "5 6\n1 2 1 10\n2 5 11 20\n1 4 2 5\n1 3 10 11\n3 4 12 10000\n4 5 6 6", "output": "Nice work, Dima!"}]
2,000
["binary search", "data structures", "dfs and similar", "dsu", "shortest paths", "two pointers"]
30
[{"input": "4 4\r\n1 2 1 10\r\n2 4 3 5\r\n1 3 1 5\r\n2 4 2 7\r\n", "output": "6\r\n"}, {"input": "5 6\r\n1 2 1 10\r\n2 5 11 20\r\n1 4 2 5\r\n1 3 10 11\r\n3 4 12 10000\r\n4 5 6 6\r\n", "output": "Nice work, Dima!\r\n"}, {"input": "6 6\r\n1 2 1 10\r\n2 3 1 10\r\n3 6 1 1\r\n1 4 1 4\r\n4 5 1 3\r\n5 6 1 3\r\n", "output": "3\r\n"}, {"input": "2 1\r\n1 2 1 1\r\n", "output": "1\r\n"}, {"input": "10 0\r\n", "output": "Nice work, Dima!\r\n"}, {"input": "5 5\r\n1 5 9403 40347\r\n1 3 13851 29314\r\n4 5 1315 561894\r\n3 5 2748 33090\r\n5 3 10717 32306\r\n", "output": "30945\r\n"}, {"input": "1000 0\r\n", "output": "Nice work, Dima!\r\n"}]
false
stdio
null
true
618/B
618
B
Python 3
TESTS
2
61
4,608,000
24031245
n = int(input()) b = [] for i in range(n): b.append(max([int(i) for i in input().split()])) for i in range(n): if b[i] == n-1: b[i+1] = n print(b[i], end=' ')
23
46
0
15655973
n = int(input()) a = [list(map(int, input().split(' '))) for i in range(n)] ans = [0]*n for i in range(1, n+1): for j in range(n): if ans[j] == 0 and a[j].count(i) == n-i: ans[j] = str(i) break print(' '.join(ans))
Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)
CF
2,016
2
256
Guess the Permutation
Bob has a permutation of integers from 1 to n. Denote this permutation as p. The i-th element of p will be denoted as pi. For all pairs of distinct integers i, j between 1 and n, he wrote the number ai, j = min(pi, pj). He writes ai, i = 0 for all integer i from 1 to n. Bob gave you all the values of ai, j that he wrote down. Your job is to reconstruct any permutation that could have generated these values. The input will be formed so that it is guaranteed that there is at least one solution that is consistent with the information given.
The first line of the input will contain a single integer n (2 ≤ n ≤ 50). The next n lines will contain the values of ai, j. The j-th number on the i-th line will represent ai, j. The i-th number on the i-th line will be 0. It's guaranteed that ai, j = aj, i and there is at least one solution consistent with the information given.
Print n space separated integers, which represents a permutation that could have generated these values. If there are multiple possible solutions, print any of them.
null
In the first case, the answer can be {1, 2} or {2, 1}. In the second case, another possible answer is {2, 4, 5, 1, 3}.
[{"input": "2\n0 1\n1 0", "output": "2 1"}, {"input": "5\n0 2 2 1 2\n2 0 4 1 3\n2 4 0 1 3\n1 1 1 0 1\n2 3 3 1 0", "output": "2 5 4 1 3"}]
1,100
["constructive algorithms"]
23
[{"input": "2\r\n0 1\r\n1 0\r\n", "output": "2 1\r\n"}, {"input": "5\r\n0 2 2 1 2\r\n2 0 4 1 3\r\n2 4 0 1 3\r\n1 1 1 0 1\r\n2 3 3 1 0\r\n", "output": "2 5 4 1 3\r\n"}, {"input": "10\r\n0 1 5 2 5 3 4 5 5 5\r\n1 0 1 1 1 1 1 1 1 1\r\n5 1 0 2 6 3 4 6 6 6\r\n2 1 2 0 2 2 2 2 2 2\r\n5 1 6 2 0 3 4 8 8 7\r\n3 1 3 2 3 0 3 3 3 3\r\n4 1 4 2 4 3 0 4 4 4\r\n5 1 6 2 8 3 4 0 9 7\r\n5 1 6 2 8 3 4 9 0 7\r\n5 1 6 2 7 3 4 7 7 0\r\n", "output": "5 1 6 2 8 3 4 10 9 7\r\n"}, {"input": "4\r\n0 1 3 2\r\n1 0 1 1\r\n3 1 0 2\r\n2 1 2 0\r\n", "output": "4 1 3 2\r\n"}, {"input": "7\r\n0 3 2 4 1 4 4\r\n3 0 2 3 1 3 3\r\n2 2 0 2 1 2 2\r\n4 3 2 0 1 5 5\r\n1 1 1 1 0 1 1\r\n4 3 2 5 1 0 6\r\n4 3 2 5 1 6 0\r\n", "output": "4 3 2 5 1 7 6\r\n"}, {"input": "10\r\n0 4 4 1 4 4 4 2 3 4\r\n4 0 5 1 6 8 9 2 3 7\r\n4 5 0 1 5 5 5 2 3 5\r\n1 1 1 0 1 1 1 1 1 1\r\n4 6 5 1 0 6 6 2 3 6\r\n4 8 5 1 6 0 8 2 3 7\r\n4 9 5 1 6 8 0 2 3 7\r\n2 2 2 1 2 2 2 0 2 2\r\n3 3 3 1 3 3 3 2 0 3\r\n4 7 5 1 6 7 7 2 3 0\r\n", "output": "4 10 5 1 6 8 9 2 3 7\r\n"}, {"input": "13\r\n0 5 5 2 5 4 5 5 3 5 5 5 1\r\n5 0 6 2 6 4 6 6 3 6 6 6 1\r\n5 6 0 2 10 4 7 10 3 8 10 9 1\r\n2 2 2 0 2 2 2 2 2 2 2 2 1\r\n5 6 10 2 0 4 7 12 3 8 11 9 1\r\n4 4 4 2 4 0 4 4 3 4 4 4 1\r\n5 6 7 2 7 4 0 7 3 7 7 7 1\r\n5 6 10 2 12 4 7 0 3 8 11 9 1\r\n3 3 3 2 3 3 3 3 0 3 3 3 1\r\n5 6 8 2 8 4 7 8 3 0 8 8 1\r\n5 6 10 2 11 4 7 11 3 8 0 9 1\r\n5 6 9 2 9 4 7 9 3 8 9 0 1\r\n1 1 1 1 1 1 1 1 1 1 1 1 0\r\n", "output": "5 6 10 2 13 4 7 12 3 8 11 9 1\r\n"}]
false
stdio
import sys def main(input_path, output_path, sub_path): with open(input_path) as f: lines = [line.strip() for line in f.readlines()] n = int(lines[0]) a = [list(map(int, line.split())) for line in lines[1:n+1]] with open(sub_path) as f: sub_line = f.read().strip() p = list(map(int, sub_line.split())) if len(p) != n or sorted(p) != list(range(1, n+1)): print(0) return for i in range(n): for j in range(n): if i != j and min(p[i], p[j]) != a[i][j]: print(0) return print(1) if __name__ == "__main__": input_path, output_path, sub_path = sys.argv[1:4] main(input_path, output_path, sub_path)
true
318/B
318
B
PyPy 3-64
TESTS
3
122
0
220882281
s = input() n = len(s) count = 0 left = 0 right = 0 while right < n: if s[right:right+5] == "heavy": while s[left:left+5] != "heavy": left += 1 count += 1 left += 5 right += 1 print(count + 1)
30
186
9,625,600
231252091
z=r=0 for w in input().split("heavy"):r+=w.count("metal")*z;z+=1 print(r)
Codeforces Round 188 (Div. 2)
CF
2,013
2
256
Strings of Power
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string.
Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters.
Print exactly one number — the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
null
In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
[{"input": "heavymetalisheavymetal", "output": "3"}, {"input": "heavymetalismetal", "output": "2"}, {"input": "trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou", "output": "3"}]
1,300
["implementation", "strings", "two pointers"]
30
[{"input": "heavymetalisheavymetal\r\n", "output": "3"}, {"input": "heavymetalismetal\r\n", "output": "2"}, {"input": "trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou\r\n", "output": "3"}, {"input": "fpgzbvhheavymheheavyzmheavyavyebknkhheavyhsbqmmetheavyalmetalheavyyomtua\r\n", "output": "5"}, {"input": "metametaheavyetalalmetalavylkeoheavyhemetaleavycdk\r\n", "output": "3"}, {"input": "hg\r\n", "output": "0"}]
false
stdio
null
true
618/B
618
B
Python 3
TESTS
2
109
307,200
57066612
n = int(input()) l = [] for i in range(n): temp = input().split(' ') t = [] for j in range(len(temp)): t.append(int(temp[j])) l.append(t) o = [0 for i in range(n)] o[0] = l[0][1] for i in range(1, n): if l[0][i] < o[0]: o[i] = l[0][i] while 0 in o: if o.count(0) == 1: for x in range(n): if o[x] == 0: o[x] = max(o)+1 t1 = 0 t2 = 0 for i in range(1, n): for j in range(n): if l[i].count(l[i][j]) > 1: o[i] = l[i][j] break elif l[i][j] > t1: t1 = l[i][j] t2 = 1 elif l[i][j] == t1 and t1 != 0: t2 = 2 if t2 == 2 and o.count(0) == 2: for x in range(n): if o[x] == 0: o[x] = t1 break t1 = 0 t2 = 0 for x in o: print(x, end=' ')
23
46
0
15661546
n=int(input());v=[];tmp=0;t=False for i in range(n): v.append([0 for i in range(n+1)]) p=[0 for i in range(n)]; for i in range(n): inp=list(map(int,input().split(" "))) for k in inp: v[i][k]+=1 for i in range(1,n+1): for j in range(n): if ((p[j]==0) and (v[j][i]==n-i)): p[j]=i for i in p: if (t==0) and (i==n-1): print(n,end=" ") else: print(i,end=" ") if (i==n-1): t=1
Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)
CF
2,016
2
256
Guess the Permutation
Bob has a permutation of integers from 1 to n. Denote this permutation as p. The i-th element of p will be denoted as pi. For all pairs of distinct integers i, j between 1 and n, he wrote the number ai, j = min(pi, pj). He writes ai, i = 0 for all integer i from 1 to n. Bob gave you all the values of ai, j that he wrote down. Your job is to reconstruct any permutation that could have generated these values. The input will be formed so that it is guaranteed that there is at least one solution that is consistent with the information given.
The first line of the input will contain a single integer n (2 ≤ n ≤ 50). The next n lines will contain the values of ai, j. The j-th number on the i-th line will represent ai, j. The i-th number on the i-th line will be 0. It's guaranteed that ai, j = aj, i and there is at least one solution consistent with the information given.
Print n space separated integers, which represents a permutation that could have generated these values. If there are multiple possible solutions, print any of them.
null
In the first case, the answer can be {1, 2} or {2, 1}. In the second case, another possible answer is {2, 4, 5, 1, 3}.
[{"input": "2\n0 1\n1 0", "output": "2 1"}, {"input": "5\n0 2 2 1 2\n2 0 4 1 3\n2 4 0 1 3\n1 1 1 0 1\n2 3 3 1 0", "output": "2 5 4 1 3"}]
1,100
["constructive algorithms"]
23
[{"input": "2\r\n0 1\r\n1 0\r\n", "output": "2 1\r\n"}, {"input": "5\r\n0 2 2 1 2\r\n2 0 4 1 3\r\n2 4 0 1 3\r\n1 1 1 0 1\r\n2 3 3 1 0\r\n", "output": "2 5 4 1 3\r\n"}, {"input": "10\r\n0 1 5 2 5 3 4 5 5 5\r\n1 0 1 1 1 1 1 1 1 1\r\n5 1 0 2 6 3 4 6 6 6\r\n2 1 2 0 2 2 2 2 2 2\r\n5 1 6 2 0 3 4 8 8 7\r\n3 1 3 2 3 0 3 3 3 3\r\n4 1 4 2 4 3 0 4 4 4\r\n5 1 6 2 8 3 4 0 9 7\r\n5 1 6 2 8 3 4 9 0 7\r\n5 1 6 2 7 3 4 7 7 0\r\n", "output": "5 1 6 2 8 3 4 10 9 7\r\n"}, {"input": "4\r\n0 1 3 2\r\n1 0 1 1\r\n3 1 0 2\r\n2 1 2 0\r\n", "output": "4 1 3 2\r\n"}, {"input": "7\r\n0 3 2 4 1 4 4\r\n3 0 2 3 1 3 3\r\n2 2 0 2 1 2 2\r\n4 3 2 0 1 5 5\r\n1 1 1 1 0 1 1\r\n4 3 2 5 1 0 6\r\n4 3 2 5 1 6 0\r\n", "output": "4 3 2 5 1 7 6\r\n"}, {"input": "10\r\n0 4 4 1 4 4 4 2 3 4\r\n4 0 5 1 6 8 9 2 3 7\r\n4 5 0 1 5 5 5 2 3 5\r\n1 1 1 0 1 1 1 1 1 1\r\n4 6 5 1 0 6 6 2 3 6\r\n4 8 5 1 6 0 8 2 3 7\r\n4 9 5 1 6 8 0 2 3 7\r\n2 2 2 1 2 2 2 0 2 2\r\n3 3 3 1 3 3 3 2 0 3\r\n4 7 5 1 6 7 7 2 3 0\r\n", "output": "4 10 5 1 6 8 9 2 3 7\r\n"}, {"input": "13\r\n0 5 5 2 5 4 5 5 3 5 5 5 1\r\n5 0 6 2 6 4 6 6 3 6 6 6 1\r\n5 6 0 2 10 4 7 10 3 8 10 9 1\r\n2 2 2 0 2 2 2 2 2 2 2 2 1\r\n5 6 10 2 0 4 7 12 3 8 11 9 1\r\n4 4 4 2 4 0 4 4 3 4 4 4 1\r\n5 6 7 2 7 4 0 7 3 7 7 7 1\r\n5 6 10 2 12 4 7 0 3 8 11 9 1\r\n3 3 3 2 3 3 3 3 0 3 3 3 1\r\n5 6 8 2 8 4 7 8 3 0 8 8 1\r\n5 6 10 2 11 4 7 11 3 8 0 9 1\r\n5 6 9 2 9 4 7 9 3 8 9 0 1\r\n1 1 1 1 1 1 1 1 1 1 1 1 0\r\n", "output": "5 6 10 2 13 4 7 12 3 8 11 9 1\r\n"}]
false
stdio
import sys def main(input_path, output_path, sub_path): with open(input_path) as f: lines = [line.strip() for line in f.readlines()] n = int(lines[0]) a = [list(map(int, line.split())) for line in lines[1:n+1]] with open(sub_path) as f: sub_line = f.read().strip() p = list(map(int, sub_line.split())) if len(p) != n or sorted(p) != list(range(1, n+1)): print(0) return for i in range(n): for j in range(n): if i != j and min(p[i], p[j]) != a[i][j]: print(0) return print(1) if __name__ == "__main__": input_path, output_path, sub_path = sys.argv[1:4] main(input_path, output_path, sub_path)
true
618/B
618
B
Python 3
TESTS
2
108
0
52303661
n = int(input()) x = list() for i in range(n): y = input().split(' ') y = [int(k) for k in y] y = int(max(y)) x.append(y) s = set() for i in range(n): s.add(i+1) for i in range(len(x)): if x[i] in x[i+1:]: tmp = x[i+1:].index(x[i]) + i t = x[i] + 1 while t not in s: t += 1 s.remove(t) x[tmp] = t print(str(x).replace('[','').replace(']','').replace(',', ''))
23
46
0
15984403
#Problem 618B Codeforce Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) n = int(input()) a = [0] * n res = ['0'] * n for j in range(0,n): a[j] = [int(num) for num in input().split()] maxx = 0 for i in range(0,n): if a[j][i] > maxx: maxx = a[j][i] res[j] = str(maxx) for j in range(0,len(res)): if res[j] == str(n - 1): res[j] = str(n) break for j in res: print(j," ",end = '')
Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)
CF
2,016
2
256
Guess the Permutation
Bob has a permutation of integers from 1 to n. Denote this permutation as p. The i-th element of p will be denoted as pi. For all pairs of distinct integers i, j between 1 and n, he wrote the number ai, j = min(pi, pj). He writes ai, i = 0 for all integer i from 1 to n. Bob gave you all the values of ai, j that he wrote down. Your job is to reconstruct any permutation that could have generated these values. The input will be formed so that it is guaranteed that there is at least one solution that is consistent with the information given.
The first line of the input will contain a single integer n (2 ≤ n ≤ 50). The next n lines will contain the values of ai, j. The j-th number on the i-th line will represent ai, j. The i-th number on the i-th line will be 0. It's guaranteed that ai, j = aj, i and there is at least one solution consistent with the information given.
Print n space separated integers, which represents a permutation that could have generated these values. If there are multiple possible solutions, print any of them.
null
In the first case, the answer can be {1, 2} or {2, 1}. In the second case, another possible answer is {2, 4, 5, 1, 3}.
[{"input": "2\n0 1\n1 0", "output": "2 1"}, {"input": "5\n0 2 2 1 2\n2 0 4 1 3\n2 4 0 1 3\n1 1 1 0 1\n2 3 3 1 0", "output": "2 5 4 1 3"}]
1,100
["constructive algorithms"]
23
[{"input": "2\r\n0 1\r\n1 0\r\n", "output": "2 1\r\n"}, {"input": "5\r\n0 2 2 1 2\r\n2 0 4 1 3\r\n2 4 0 1 3\r\n1 1 1 0 1\r\n2 3 3 1 0\r\n", "output": "2 5 4 1 3\r\n"}, {"input": "10\r\n0 1 5 2 5 3 4 5 5 5\r\n1 0 1 1 1 1 1 1 1 1\r\n5 1 0 2 6 3 4 6 6 6\r\n2 1 2 0 2 2 2 2 2 2\r\n5 1 6 2 0 3 4 8 8 7\r\n3 1 3 2 3 0 3 3 3 3\r\n4 1 4 2 4 3 0 4 4 4\r\n5 1 6 2 8 3 4 0 9 7\r\n5 1 6 2 8 3 4 9 0 7\r\n5 1 6 2 7 3 4 7 7 0\r\n", "output": "5 1 6 2 8 3 4 10 9 7\r\n"}, {"input": "4\r\n0 1 3 2\r\n1 0 1 1\r\n3 1 0 2\r\n2 1 2 0\r\n", "output": "4 1 3 2\r\n"}, {"input": "7\r\n0 3 2 4 1 4 4\r\n3 0 2 3 1 3 3\r\n2 2 0 2 1 2 2\r\n4 3 2 0 1 5 5\r\n1 1 1 1 0 1 1\r\n4 3 2 5 1 0 6\r\n4 3 2 5 1 6 0\r\n", "output": "4 3 2 5 1 7 6\r\n"}, {"input": "10\r\n0 4 4 1 4 4 4 2 3 4\r\n4 0 5 1 6 8 9 2 3 7\r\n4 5 0 1 5 5 5 2 3 5\r\n1 1 1 0 1 1 1 1 1 1\r\n4 6 5 1 0 6 6 2 3 6\r\n4 8 5 1 6 0 8 2 3 7\r\n4 9 5 1 6 8 0 2 3 7\r\n2 2 2 1 2 2 2 0 2 2\r\n3 3 3 1 3 3 3 2 0 3\r\n4 7 5 1 6 7 7 2 3 0\r\n", "output": "4 10 5 1 6 8 9 2 3 7\r\n"}, {"input": "13\r\n0 5 5 2 5 4 5 5 3 5 5 5 1\r\n5 0 6 2 6 4 6 6 3 6 6 6 1\r\n5 6 0 2 10 4 7 10 3 8 10 9 1\r\n2 2 2 0 2 2 2 2 2 2 2 2 1\r\n5 6 10 2 0 4 7 12 3 8 11 9 1\r\n4 4 4 2 4 0 4 4 3 4 4 4 1\r\n5 6 7 2 7 4 0 7 3 7 7 7 1\r\n5 6 10 2 12 4 7 0 3 8 11 9 1\r\n3 3 3 2 3 3 3 3 0 3 3 3 1\r\n5 6 8 2 8 4 7 8 3 0 8 8 1\r\n5 6 10 2 11 4 7 11 3 8 0 9 1\r\n5 6 9 2 9 4 7 9 3 8 9 0 1\r\n1 1 1 1 1 1 1 1 1 1 1 1 0\r\n", "output": "5 6 10 2 13 4 7 12 3 8 11 9 1\r\n"}]
false
stdio
import sys def main(input_path, output_path, sub_path): with open(input_path) as f: lines = [line.strip() for line in f.readlines()] n = int(lines[0]) a = [list(map(int, line.split())) for line in lines[1:n+1]] with open(sub_path) as f: sub_line = f.read().strip() p = list(map(int, sub_line.split())) if len(p) != n or sorted(p) != list(range(1, n+1)): print(0) return for i in range(n): for j in range(n): if i != j and min(p[i], p[j]) != a[i][j]: print(0) return print(1) if __name__ == "__main__": input_path, output_path, sub_path = sys.argv[1:4] main(input_path, output_path, sub_path)
true
665/B
665
B
PyPy 3-64
TESTS
3
62
0
167288122
from sys import stdin def inta(arr): return [int(x) for x in arr] def sr(): return stdin.readline().strip() from collections import deque n, m, k = inta(sr().split()) ps = inta(sr().split()) q = deque(ps) sm = 0 for _ in range(m): w = inta(sr().split()) for ww in w: for i, v in enumerate(q): if v == ww: sm += i + 1 q.remove(ww) q.appendleft(ww) break print(sm)
10
62
1,740,800
211415219
n,m,k=map(int,input().split()) l=list(map(int,input().split())) ans=0 while n: n-=1 temp=list(map(int,input().split())) pos=-1 for i in range(m): for j in range(k): if(l[j]==temp[i]): pos=j+1 ans+=pos # print(pos) break f=l[pos-1] l.remove(l[pos-1]) l.insert(0,f) print(ans)
Educational Codeforces Round 12
ICPC
2,016
1
256
Shopping
Ayush is a cashier at the shopping center. Recently his department has started a ''click and collect" service which allows users to shop online. The store contains k items. n customers have already used the above service. Each user paid for m items. Let aij denote the j-th item in the i-th person's order. Due to the space limitations all the items are arranged in one single row. When Ayush receives the i-th order he will find one by one all the items aij (1 ≤ j ≤ m) in the row. Let pos(x) denote the position of the item x in the row at the moment of its collection. Then Ayush takes time equal to pos(ai1) + pos(ai2) + ... + pos(aim) for the i-th customer. When Ayush accesses the x-th element he keeps a new stock in the front of the row and takes away the x-th element. Thus the values are updating. Your task is to calculate the total time it takes for Ayush to process all the orders. You can assume that the market has endless stock.
The first line contains three integers n, m and k (1 ≤ n, k ≤ 100, 1 ≤ m ≤ k) — the number of users, the number of items each user wants to buy and the total number of items at the market. The next line contains k distinct integers pl (1 ≤ pl ≤ k) denoting the initial positions of the items in the store. The items are numbered with integers from 1 to k. Each of the next n lines contains m distinct integers aij (1 ≤ aij ≤ k) — the order of the i-th person.
Print the only integer t — the total time needed for Ayush to process all the orders.
null
Customer 1 wants the items 1 and 5. pos(1) = 3, so the new positions are: [1, 3, 4, 2, 5]. pos(5) = 5, so the new positions are: [5, 1, 3, 4, 2]. Time taken for the first customer is 3 + 5 = 8. Customer 2 wants the items 3 and 1. pos(3) = 3, so the new positions are: [3, 5, 1, 4, 2]. pos(1) = 3, so the new positions are: [1, 3, 5, 4, 2]. Time taken for the second customer is 3 + 3 = 6. Total time is 8 + 6 = 14. Formally pos(x) is the index of x in the current row.
[{"input": "2 2 5\n3 4 1 2 5\n1 5\n3 1", "output": "14"}]
1,400
["brute force"]
10
[{"input": "2 2 5\r\n3 4 1 2 5\r\n1 5\r\n3 1\r\n", "output": "14\r\n"}, {"input": "4 4 4\r\n1 2 3 4\r\n3 4 2 1\r\n4 3 2 1\r\n4 1 2 3\r\n4 1 2 3\r\n", "output": "59\r\n"}, {"input": "1 1 1\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "10 1 100\r\n1 55 67 75 40 86 24 84 82 26 81 23 70 79 51 54 21 78 31 98 68 93 66 88 99 65 20 52 35 85 16 12 94 100 59 56 18 33 47 46 71 8 38 57 2 92 3 95 6 4 87 22 48 80 15 29 11 45 72 76 44 60 91 90 39 74 41 36 13 27 53 83 32 5 30 63 89 64 49 17 9 97 69 14 50 77 37 96 10 42 28 34 61 19 73 7 62 43 58 25\r\n33\r\n69\r\n51\r\n7\r\n68\r\n70\r\n1\r\n35\r\n24\r\n7\r\n", "output": "335\r\n"}, {"input": "100 1 1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n", "output": "100\r\n"}, {"input": "3 2 3\r\n3 1 2\r\n1 2\r\n2 1\r\n2 3\r\n", "output": "13\r\n"}, {"input": "10 10 10\r\n3 4 1 2 8 9 5 10 6 7\r\n9 10 7 8 6 1 2 3 4 5\r\n2 5 3 6 1 4 9 7 8 10\r\n2 9 1 8 4 7 5 10 6 3\r\n10 9 7 1 3 6 2 8 5 4\r\n2 5 1 3 7 10 4 9 8 6\r\n6 1 8 7 9 2 3 5 4 10\r\n1 3 2 8 6 9 4 10 5 7\r\n5 2 4 8 6 1 10 9 3 7\r\n5 1 7 10 4 6 2 8 9 3\r\n2 1 3 9 7 10 6 4 8 5\r\n", "output": "771\r\n"}]
false
stdio
null
true
618/B
618
B
Python 3
TESTS
2
31
0
145716658
N = int(input()) ans = [0] * N for i in range(N): perm = list(map(int, input().split())) ans[i] = max(max(perm), ans[i]) last = ans[i] for j in range(i + 1, N): if (ans[i] == perm[j]): ans[j] = last + 1 last = ans[j] elif (ans[i] < perm[j]): ans[j] = ans[i] ans[i] = perm[j] else: ans[j] = perm[j] print(*ans)
23
46
0
146889653
n=int(input()) a=[] for i in range(n): a.append(list(map(int,input().split()))) k=a[0][1] s=[0]*n for i in range(n): f=0 for j in range(n): f=max(a[i][j],f) if f in s: s[i]=n else: s[i]=f s=list(map(str,s)) print(" ".join(s))
Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)
CF
2,016
2
256
Guess the Permutation
Bob has a permutation of integers from 1 to n. Denote this permutation as p. The i-th element of p will be denoted as pi. For all pairs of distinct integers i, j between 1 and n, he wrote the number ai, j = min(pi, pj). He writes ai, i = 0 for all integer i from 1 to n. Bob gave you all the values of ai, j that he wrote down. Your job is to reconstruct any permutation that could have generated these values. The input will be formed so that it is guaranteed that there is at least one solution that is consistent with the information given.
The first line of the input will contain a single integer n (2 ≤ n ≤ 50). The next n lines will contain the values of ai, j. The j-th number on the i-th line will represent ai, j. The i-th number on the i-th line will be 0. It's guaranteed that ai, j = aj, i and there is at least one solution consistent with the information given.
Print n space separated integers, which represents a permutation that could have generated these values. If there are multiple possible solutions, print any of them.
null
In the first case, the answer can be {1, 2} or {2, 1}. In the second case, another possible answer is {2, 4, 5, 1, 3}.
[{"input": "2\n0 1\n1 0", "output": "2 1"}, {"input": "5\n0 2 2 1 2\n2 0 4 1 3\n2 4 0 1 3\n1 1 1 0 1\n2 3 3 1 0", "output": "2 5 4 1 3"}]
1,100
["constructive algorithms"]
23
[{"input": "2\r\n0 1\r\n1 0\r\n", "output": "2 1\r\n"}, {"input": "5\r\n0 2 2 1 2\r\n2 0 4 1 3\r\n2 4 0 1 3\r\n1 1 1 0 1\r\n2 3 3 1 0\r\n", "output": "2 5 4 1 3\r\n"}, {"input": "10\r\n0 1 5 2 5 3 4 5 5 5\r\n1 0 1 1 1 1 1 1 1 1\r\n5 1 0 2 6 3 4 6 6 6\r\n2 1 2 0 2 2 2 2 2 2\r\n5 1 6 2 0 3 4 8 8 7\r\n3 1 3 2 3 0 3 3 3 3\r\n4 1 4 2 4 3 0 4 4 4\r\n5 1 6 2 8 3 4 0 9 7\r\n5 1 6 2 8 3 4 9 0 7\r\n5 1 6 2 7 3 4 7 7 0\r\n", "output": "5 1 6 2 8 3 4 10 9 7\r\n"}, {"input": "4\r\n0 1 3 2\r\n1 0 1 1\r\n3 1 0 2\r\n2 1 2 0\r\n", "output": "4 1 3 2\r\n"}, {"input": "7\r\n0 3 2 4 1 4 4\r\n3 0 2 3 1 3 3\r\n2 2 0 2 1 2 2\r\n4 3 2 0 1 5 5\r\n1 1 1 1 0 1 1\r\n4 3 2 5 1 0 6\r\n4 3 2 5 1 6 0\r\n", "output": "4 3 2 5 1 7 6\r\n"}, {"input": "10\r\n0 4 4 1 4 4 4 2 3 4\r\n4 0 5 1 6 8 9 2 3 7\r\n4 5 0 1 5 5 5 2 3 5\r\n1 1 1 0 1 1 1 1 1 1\r\n4 6 5 1 0 6 6 2 3 6\r\n4 8 5 1 6 0 8 2 3 7\r\n4 9 5 1 6 8 0 2 3 7\r\n2 2 2 1 2 2 2 0 2 2\r\n3 3 3 1 3 3 3 2 0 3\r\n4 7 5 1 6 7 7 2 3 0\r\n", "output": "4 10 5 1 6 8 9 2 3 7\r\n"}, {"input": "13\r\n0 5 5 2 5 4 5 5 3 5 5 5 1\r\n5 0 6 2 6 4 6 6 3 6 6 6 1\r\n5 6 0 2 10 4 7 10 3 8 10 9 1\r\n2 2 2 0 2 2 2 2 2 2 2 2 1\r\n5 6 10 2 0 4 7 12 3 8 11 9 1\r\n4 4 4 2 4 0 4 4 3 4 4 4 1\r\n5 6 7 2 7 4 0 7 3 7 7 7 1\r\n5 6 10 2 12 4 7 0 3 8 11 9 1\r\n3 3 3 2 3 3 3 3 0 3 3 3 1\r\n5 6 8 2 8 4 7 8 3 0 8 8 1\r\n5 6 10 2 11 4 7 11 3 8 0 9 1\r\n5 6 9 2 9 4 7 9 3 8 9 0 1\r\n1 1 1 1 1 1 1 1 1 1 1 1 0\r\n", "output": "5 6 10 2 13 4 7 12 3 8 11 9 1\r\n"}]
false
stdio
import sys def main(input_path, output_path, sub_path): with open(input_path) as f: lines = [line.strip() for line in f.readlines()] n = int(lines[0]) a = [list(map(int, line.split())) for line in lines[1:n+1]] with open(sub_path) as f: sub_line = f.read().strip() p = list(map(int, sub_line.split())) if len(p) != n or sorted(p) != list(range(1, n+1)): print(0) return for i in range(n): for j in range(n): if i != j and min(p[i], p[j]) != a[i][j]: print(0) return print(1) if __name__ == "__main__": input_path, output_path, sub_path = sys.argv[1:4] main(input_path, output_path, sub_path)
true
282/B
282
B
Python 3
TESTS
1
62
0
230848452
n = int(input()) eggs = [] for i in range(n): a, g = map(int, input().split()) eggs.append((g - a, i)) eggs.sort(reverse=True) a_count = n // 2 g_count = n - a_count if abs(sum([eggs[i][0] for i in range(a_count)]) - sum([eggs[i][0] for i in range(g_count)])) > 500: print("-1") else: ans = ["G"] * n for i in range(a_count): ans[eggs[i][1]] = "A" print("".join(ans))
54
1,652
10,752,000
229981075
import sys n = int(sys.stdin.readline()) d = 0 for _ in range(n): a, g = map(int, sys.stdin.readline().split()) if d + a <= 500: d += a print('A', end='') else: d -= g print('G', end='')# 1698396157.2401366
Codeforces Round 173 (Div. 2)
CF
2,013
5
256
Painting Eggs
The Bitlandians are quite weird people. They have very peculiar customs. As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work. The kids are excited because just as is customary, they're going to be paid for the job! Overall uncle J. has got n eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000. Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500. Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.
The first line contains integer n (1 ≤ n ≤ 106) — the number of eggs. Next n lines contain two integers ai and gi each (0 ≤ ai, gi ≤ 1000; ai + gi = 1000): ai is the price said by A. for the i-th egg and gi is the price said by G. for the i-th egg.
If it is impossible to assign the painting, print "-1" (without quotes). Otherwise print a string, consisting of n letters "G" and "A". The i-th letter of this string should represent the child who will get the i-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as Sa, and the money Uncle J. must pay G. for the painting as Sg, then this inequality must hold: |Sa - Sg| ≤ 500. If there are several solutions, you are allowed to print any of them.
null
null
[{"input": "2\n1 999\n999 1", "output": "AG"}, {"input": "3\n400 600\n400 600\n400 600", "output": "AGA"}]
1,500
["greedy", "math"]
54
[{"input": "2\r\n1 999\r\n999 1\r\n", "output": "AG\r\n"}, {"input": "3\r\n400 600\r\n400 600\r\n400 600\r\n", "output": "AGA\r\n"}, {"input": "2\r\n500 500\r\n500 500\r\n", "output": "AG\r\n"}, {"input": "1\r\n1 999\r\n", "output": "A\r\n"}, {"input": "10\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n1 999\r\n", "output": "AAAAAAAAAA\r\n"}, {"input": "2\r\n499 501\r\n501 499\r\n", "output": "AG\r\n"}, {"input": "3\r\n500 500\r\n1 999\r\n400 600\r\n", "output": "AGA\r\n"}, {"input": "1\r\n0 1000\r\n", "output": "A\r\n"}, {"input": "1\r\n500 500\r\n", "output": "A\r\n"}, {"input": "1\r\n1000 0\r\n", "output": "G\r\n"}]
false
stdio
import sys def main(input_path, output_path, submission_path): # Read input data with open(input_path) as f: n = int(f.readline()) eggs = [tuple(map(int, line.split())) for line in f] # Read reference and submission outputs with open(output_path) as f: ref_output = f.read().strip() with open(submission_path) as f: sub_output = f.read().strip() # Check if reference says it's impossible if ref_output == "-1": return 1 if sub_output == "-1" else 0 # Check submission is not -1 if sub_output == "-1": return 0 # Validate submission format if len(sub_output) != n or any(c not in 'AG' for c in sub_output): return 0 # Calculate Sa and Sg sa, sg = 0, 0 for i in range(n): a, g = eggs[i] if sub_output[i] == 'A': sa += a else: sg += g # Check the absolute difference condition if abs(sa - sg) > 500: return 0 return 1 if __name__ == "__main__": input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3] score = main(input_path, output_path, submission_path) print(score)
true
852/B
852
B
PyPy 3-64
TESTS
1
61
132,198,400
221227648
import sys sys.setrecursionlimit(100000) input=lambda:sys.stdin.readline().strip() write=lambda x:sys.stdout.write(str(x)+'\n') # from decimal import Decimal # from random import randint # from copy import deepcopy from collections import deque,Counter # from heapq import heapify,heappush,heappop # from bisect import bisect_left,bisect,insort from math import inf,sqrt,gcd,pow,ceil,floor,log,log2,log10,pi,sin,cos,tan,asin,acos,atan # from functools import cmp_to_key # from itertools import permutations,combinations def mul1(a,b): res=[0]*m for i in range(m): for j in range(m): res[i]=(res[i]+a[j]*b[j][i])%mod return res def mul2(a,b): res=[[0]*m for i in range(m)] for i in range(m): for j in range(m): for k in range(m): res[i][j]=(res[i][j]+a[i][k]*b[k][j])%mod return res def change(a): res=[[0]*m for i in range(m)] for i in range(m): for j in range(n): res[i][(i+a[j])%m]=1 return res def solve(n,l,m,mod): sequence=[0]*m for i in range(n): sequence[a[i]%m]=1 matrix=change(b) l-=2 while l: if l&1: sequence=mul1(sequence,matrix) matrix=mul2(matrix,matrix) l>>=1 ans=0 for i in range(n): ans=(ans+sequence[(-c[i]-b[i])%m])%mod return ans n,l,m=map(int,input().split()) mod=1000000007 a=list(map(int,input().split())) b=list(map(int,input().split())) c=list(map(int,input().split())) print(solve(n,l,m,mod))
20
779
122,265,600
157949693
import sys from array import array class Matrix: def __init__(self, r, c, mat=None, id=None): self.r, self.c = r, c if mat != None: self.mat = [[mat[i][j] for j in range(c)] for i in range(r)] else: self.mat = [[0 for i in range(c)] for j in range(r)] if id != None: for i in range(r): self.mat[i][i] = 1 def __add__(self, other): mat0 = Matrix(self.r, self.c) for i in range(self.r): for j in range(self.c): mat0[i][j] = self[i][j] + other[i][j] return mat0 def __mul__(self, other): mat0 = Matrix(self.r, other.c) for i in range(self.r): for j in range(other.c): for k in range(self.c): mat0[i][j] += self[i][k] * other[k][j] mat0[i][j] %= 10 ** 9 + 7 return mat0 def dot_mul(self, other): res = 0 for i in range(self.r): for j in range(self.c): res += self.mat[i][j] * other.mat[j][i] return res def trace(self): res = 0 for i in range(self.r): res += self.mat[i][i] return res def __pow__(self, p, modulo=None): sq, mat = Matrix(self.r, self.r, id=1), self while p: if p & 1: p -= 1 sq = sq * mat p //= 2 mat *= mat return sq def __getitem__(self, item): return self.mat[item] input = lambda: sys.stdin.buffer.readline().decode().strip() city, layer, m = map(int, input().split()) be, mid, en = [array('i', [int(x) for x in input().split()]) for _ in range(3)] mem, coff, dp_ = [0] * m, [], [[0] for _ in range(m)] for i in range(city): mem[mid[i] % m] += 1 for i in range(m): coff.append([]) for j in range(m): coff[-1].append(mem[(i - j) % m]) for i in range(city): dp_[be[i] % m][0] += 1 dp = (Matrix(m, m, coff) ** (layer - 2)) * Matrix(m, 1, dp_) res = 0 for i in range(city): res += dp[(-mid[i] - en[i]) % m][0] print(res % (10 ** 9 + 7))
Bubble Cup X - Finals [Online Mirror]
ICPC
2,017
2
256
Neural Network country
Due to the recent popularity of the Deep learning new countries are starting to look like Neural Networks. That is, the countries are being built deep with many layers, each layer possibly having many cities. They also have one entry, and one exit point. There are exactly L layers, each having N cities. Let us look at the two adjacent layers L1 and L2. Each city from the layer L1 is connected to each city from the layer L2 with the traveling cost cij for $$i,j\in\{1,2,\ldots,N\}$$, and each pair of adjacent layers has the same cost in between their cities as any other pair (they just stacked the same layers, as usual). Also, the traveling costs to each city from the layer L2 are same for all cities in the L1, that is cij is the same for $$i \in \{1, 2, \ldots, N\}$$, and fixed j. Doctor G. needs to speed up his computations for this country so he asks you to find the number of paths he can take from entry to exit point such that his traveling cost is divisible by given number M.
The first line of input contains N (1 ≤ N ≤ 106), L (2 ≤ L ≤ 105) and M (2 ≤ M ≤ 100), the number of cities in each layer, the number of layers and the number that travelling cost should be divisible by, respectively. Second, third and fourth line contain N integers each denoting costs 0 ≤ cost ≤ M from entry point to the first layer, costs between adjacent layers as described above, and costs from the last layer to the exit point.
Output a single integer, the number of paths Doctor G. can take which have total cost divisible by M, modulo 109 + 7.
null
This is a country with 3 layers, each layer having 2 cities. Paths $$6 \rightarrow 2 \rightarrow 2 \rightarrow 3$$, and $$6 \rightarrow 2 \rightarrow 1 \rightarrow 4$$ are the only paths having total cost divisible by 13. Notice that input edges for layer cities have the same cost, and that they are same for all layers.
[{"input": "2 3 13\n4 6\n2 1\n3 4", "output": "2"}]
2,000
["dp", "matrices"]
20
[{"input": "2 3 13\r\n4 6\r\n2 1\r\n3 4\r\n", "output": "2"}, {"input": "2 4 5\r\n1 1\r\n1 1\r\n1 1\r\n", "output": "16"}, {"input": "1 1234 5\r\n1\r\n1\r\n1\r\n", "output": "1"}, {"input": "3 2 2\r\n0 1 0\r\n0 0 1\r\n1 1 0\r\n", "output": "3"}, {"input": "5 4 3\r\n2 1 0 1 2\r\n0 1 2 1 0\r\n1 2 1 0 2\r\n", "output": "209"}, {"input": "4 4 4\r\n0 1 3 2\r\n1 2 0 3\r\n2 3 1 0\r\n", "output": "64"}]
false
stdio
null
true
16/E
16
E
Python 3
TESTS
1
60
0
215457537
import sys; input = sys.stdin.readline def dfs(i, p, v): if v == (1 << n) - 1: return 1 if dp[p][v] > -1: return dp[p][v] dp[p][v] = 1 for q in range(n): if not v & (1 << q): dp[p][v] *= a[i][q] * dfs(i, q, v | (1 << q)) return dp[p][v] n = int(input()) a = [list(map(float, input().split())) for _ in range(n)] for i in range(n): dp = [[-1] * (1 << n) for _ in range(n)] print(dfs(i, i, 1 << i), end = ' ')
20
1,528
28,364,800
127222158
# By the grace of Goddess Saraswati and Durga# # Author: Vaibhav Tiwari # import os, sys, math from io import BytesIO, IOBase def main(): n = int(input()) prob = [] for i in range(n): prob.append(arrin(float)) FishBitmaskIterative(n,prob) # Fast IO Region BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") arrin = lambda x: list(map(x,input().split())) # really showed me how state transition is magic. def maximumSumOneDeletionAtmost(self, nums): pos = False for i in nums: if i > 0: pos = True if not pos: return max(nums) dp = {} mx = -1000000000 dp[(0, 0)] = nums[0] dp[(0, 1)] = 0 for i in range(1, len(nums)): dp[(i, 0)] = max(nums[i], nums[i] + dp[(i - 1, 0)]) dp[(i, 1)] = max(nums[i] + dp[i - 1, 1], dp[i - 1, 0]) mx = max(mx, dp[i, 1], dp[i, 0]) return max(mx, dp[0, 0], dp[0, 1]) def onesComplement(n): # Find number of bits in # the given integer number_of_bits = (int)(math.floor(math.log(n) / math.log(2))) + 1; # XOR the given integer with poe(2, # number_of_bits-1 and print the result return ((1 << number_of_bits) - 1) ^ n; def FishBitmaskIterative(n, prob): #0 in mask means fish alive #1 in bitmask means fish dead dp = [0 for _ in range(1<<n)] dp[0] = 1 for mask in range(1<<n): alive_in_the_stage_before = n - bin(mask).count("1") + 1 choosing_two = alive_in_the_stage_before*(alive_in_the_stage_before-1)/2 for alive in range(n): if not mask & 1<<alive: for dead in range(n): if mask & 1<<dead: dp[mask] +=(dp[mask ^ 1<<dead]*prob[alive][dead]/choosing_two) ma = 1<<n ans = [] for i in range(n): ans.append(dp[ma - 1 - (1 << i)]) print(*ans) if __name__ == "__main__": main()
Codeforces Beta Round 16 (Div. 2 Only)
ICPC
2,010
3
128
Fish
n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability aij, and the second will eat up the first with the probability aji = 1 - aij. The described process goes on until there are at least two fish in the lake. For each fish find out the probability that it will survive to be the last in the lake.
The first line contains integer n (1 ≤ n ≤ 18) — the amount of fish in the lake. Then there follow n lines with n real numbers each — matrix a. aij (0 ≤ aij ≤ 1) — the probability that fish with index i eats up fish with index j. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: aij = 1 - aji. All real numbers are given with not more than 6 characters after the decimal point.
Output n space-separated real numbers accurate to not less than 6 decimal places. Number with index i should be equal to the probability that fish with index i will survive to be the last in the lake.
null
null
[{"input": "2\n0 0.5\n0.5 0", "output": "0.500000 0.500000"}, {"input": "5\n0 1 1 1 1\n0 0 0.5 0.5 0.5\n0 0.5 0 0.5 0.5\n0 0.5 0.5 0 0.5\n0 0.5 0.5 0.5 0", "output": "1.000000 0.000000 0.000000 0.000000 0.000000"}]
1,900
["bitmasks", "dp", "probabilities"]
20
[{"input": "2\r\n0 0.5\r\n0.5 0\r\n", "output": "0.500000 0.500000 "}, {"input": "4\r\n0 0.5 0.5 0.5\r\n0.5 0 0.5 0.5\r\n0.5 0.5 0 0.5\r\n0.5 0.5 0.5 0\r\n", "output": "0.250000 0.250000 0.250000 0.250000 "}, {"input": "5\r\n0 1 1 1 1\r\n0 0 0.5 0.5 0.5\r\n0 0.5 0 0.5 0.5\r\n0 0.5 0.5 0 0.5\r\n0 0.5 0.5 0.5 0\r\n", "output": "1.000000 0.000000 0.000000 0.000000 0.000000 "}, {"input": "1\r\n0.000\r\n", "output": "1.000000 "}, {"input": "2\r\n0.000 0.551\r\n0.449 0.000\r\n", "output": "0.551000 0.449000 "}, {"input": "3\r\n0.000 0.817 0.584\r\n0.183 0.000 0.665\r\n0.416 0.335 0.000\r\n", "output": "0.564400 0.208967 0.226632 "}, {"input": "4\r\n0.000 0.083 0.548 0.503\r\n0.917 0.000 0.395 0.144\r\n0.452 0.605 0.000 0.991\r\n0.497 0.856 0.009 0.000\r\n", "output": "0.163512 0.222554 0.463543 0.150390 "}, {"input": "5\r\n0.000 0.349 0.202 0.088 0.431\r\n0.651 0.000 0.435 0.627 0.564\r\n0.798 0.565 0.000 0.725 0.949\r\n0.912 0.373 0.275 0.000 0.027\r\n0.569 0.436 0.051 0.973 0.000\r\n", "output": "0.059303 0.233839 0.494324 0.093917 0.118617 "}, {"input": "8\r\n0.000 0.147 0.783 0.224 0.220 0.651 0.453 0.209\r\n0.853 0.000 0.246 0.076 0.018 0.349 0.896 0.315\r\n0.217 0.754 0.000 0.307 0.968 0.400 0.531 0.086\r\n0.776 0.924 0.693 0.000 0.707 0.842 0.116 0.949\r\n0.780 0.982 0.032 0.293 0.000 0.908 0.307 0.266\r\n0.349 0.651 0.600 0.158 0.092 0.000 0.066 0.909\r\n0.547 0.104 0.469 0.884 0.693 0.934 0.000 0.251\r\n0.791 0.685 0.914 0.051 0.734 0.091 0.749 0.000\r\n", "output": "0.056312 0.054963 0.091124 0.315966 0.093803 0.056812 0.187952 0.143068 "}]
false
stdio
import sys def read_floats_from_file(path): with open(path, 'r') as f: line = f.readline().strip() return list(map(float, line.split())) def main(): input_path, output_path, submission_path = sys.argv[1:4] expected = read_floats_from_file(output_path) submission = read_floats_from_file(submission_path) if len(expected) != len(submission): print(0) return eps = 1e-6 for e, s in zip(expected, submission): if abs(e - s) > eps + 1e-12: print(0) return print(1) if __name__ == "__main__": main()
true
665/B
665
B
PyPy 3
TESTS
3
124
0
100181319
n,m,k = map(int,input().split()) P = list(map(int,input().split())) count = 0 for i in range(m): X = list(map(int,input().split())) for j in X: t = P.index(j) count += (t+1) P = [j] + P[:t] + P[t+1:] print(count)
10
62
1,945,600
213961757
n, m, k = map(int, input().split()) p = list(map(int, input().split())) t = 0 for _ in range(n): a = list(map(int, input().split())) for i in a: pos = p.index(i) t += pos + 1 p = [i] + p[:pos] + p[pos + 1:] print(t)
Educational Codeforces Round 12
ICPC
2,016
1
256
Shopping
Ayush is a cashier at the shopping center. Recently his department has started a ''click and collect" service which allows users to shop online. The store contains k items. n customers have already used the above service. Each user paid for m items. Let aij denote the j-th item in the i-th person's order. Due to the space limitations all the items are arranged in one single row. When Ayush receives the i-th order he will find one by one all the items aij (1 ≤ j ≤ m) in the row. Let pos(x) denote the position of the item x in the row at the moment of its collection. Then Ayush takes time equal to pos(ai1) + pos(ai2) + ... + pos(aim) for the i-th customer. When Ayush accesses the x-th element he keeps a new stock in the front of the row and takes away the x-th element. Thus the values are updating. Your task is to calculate the total time it takes for Ayush to process all the orders. You can assume that the market has endless stock.
The first line contains three integers n, m and k (1 ≤ n, k ≤ 100, 1 ≤ m ≤ k) — the number of users, the number of items each user wants to buy and the total number of items at the market. The next line contains k distinct integers pl (1 ≤ pl ≤ k) denoting the initial positions of the items in the store. The items are numbered with integers from 1 to k. Each of the next n lines contains m distinct integers aij (1 ≤ aij ≤ k) — the order of the i-th person.
Print the only integer t — the total time needed for Ayush to process all the orders.
null
Customer 1 wants the items 1 and 5. pos(1) = 3, so the new positions are: [1, 3, 4, 2, 5]. pos(5) = 5, so the new positions are: [5, 1, 3, 4, 2]. Time taken for the first customer is 3 + 5 = 8. Customer 2 wants the items 3 and 1. pos(3) = 3, so the new positions are: [3, 5, 1, 4, 2]. pos(1) = 3, so the new positions are: [1, 3, 5, 4, 2]. Time taken for the second customer is 3 + 3 = 6. Total time is 8 + 6 = 14. Formally pos(x) is the index of x in the current row.
[{"input": "2 2 5\n3 4 1 2 5\n1 5\n3 1", "output": "14"}]
1,400
["brute force"]
10
[{"input": "2 2 5\r\n3 4 1 2 5\r\n1 5\r\n3 1\r\n", "output": "14\r\n"}, {"input": "4 4 4\r\n1 2 3 4\r\n3 4 2 1\r\n4 3 2 1\r\n4 1 2 3\r\n4 1 2 3\r\n", "output": "59\r\n"}, {"input": "1 1 1\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "10 1 100\r\n1 55 67 75 40 86 24 84 82 26 81 23 70 79 51 54 21 78 31 98 68 93 66 88 99 65 20 52 35 85 16 12 94 100 59 56 18 33 47 46 71 8 38 57 2 92 3 95 6 4 87 22 48 80 15 29 11 45 72 76 44 60 91 90 39 74 41 36 13 27 53 83 32 5 30 63 89 64 49 17 9 97 69 14 50 77 37 96 10 42 28 34 61 19 73 7 62 43 58 25\r\n33\r\n69\r\n51\r\n7\r\n68\r\n70\r\n1\r\n35\r\n24\r\n7\r\n", "output": "335\r\n"}, {"input": "100 1 1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n", "output": "100\r\n"}, {"input": "3 2 3\r\n3 1 2\r\n1 2\r\n2 1\r\n2 3\r\n", "output": "13\r\n"}, {"input": "10 10 10\r\n3 4 1 2 8 9 5 10 6 7\r\n9 10 7 8 6 1 2 3 4 5\r\n2 5 3 6 1 4 9 7 8 10\r\n2 9 1 8 4 7 5 10 6 3\r\n10 9 7 1 3 6 2 8 5 4\r\n2 5 1 3 7 10 4 9 8 6\r\n6 1 8 7 9 2 3 5 4 10\r\n1 3 2 8 6 9 4 10 5 7\r\n5 2 4 8 6 1 10 9 3 7\r\n5 1 7 10 4 6 2 8 9 3\r\n2 1 3 9 7 10 6 4 8 5\r\n", "output": "771\r\n"}]
false
stdio
null
true
618/B
618
B
Python 3
TESTS
2
46
0
15906070
__author__ = 'Admin' n = int(input()) m = [list(map(int, input().split())) for j in range(n)] ans = [] for i in range(n): ans.append(max(m[i])) if max(m[i]) == ans[i - 1] and len(ans) > 1: ans[i] += 1 print(*ans)
23
46
0
152729567
n=int(input()) a=[] for _ in range(n): l=list(map(int,input().strip().split())) a.append(l) ans=[-1]*n for i in range(n): for j in range(n): if a[i][j]!=0: if ans[j]==-1: if ans[i]==-1: ans[i]=a[i][j] else: ans[j]=a[i][j] else: if ans[j]<a[i][j]: ans[j]=a[i][j] b=[] for x in ans: if x<0: b.append(n) elif x not in b: b.append(x) else: b.append(n) print(*b)
Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)
CF
2,016
2
256
Guess the Permutation
Bob has a permutation of integers from 1 to n. Denote this permutation as p. The i-th element of p will be denoted as pi. For all pairs of distinct integers i, j between 1 and n, he wrote the number ai, j = min(pi, pj). He writes ai, i = 0 for all integer i from 1 to n. Bob gave you all the values of ai, j that he wrote down. Your job is to reconstruct any permutation that could have generated these values. The input will be formed so that it is guaranteed that there is at least one solution that is consistent with the information given.
The first line of the input will contain a single integer n (2 ≤ n ≤ 50). The next n lines will contain the values of ai, j. The j-th number on the i-th line will represent ai, j. The i-th number on the i-th line will be 0. It's guaranteed that ai, j = aj, i and there is at least one solution consistent with the information given.
Print n space separated integers, which represents a permutation that could have generated these values. If there are multiple possible solutions, print any of them.
null
In the first case, the answer can be {1, 2} or {2, 1}. In the second case, another possible answer is {2, 4, 5, 1, 3}.
[{"input": "2\n0 1\n1 0", "output": "2 1"}, {"input": "5\n0 2 2 1 2\n2 0 4 1 3\n2 4 0 1 3\n1 1 1 0 1\n2 3 3 1 0", "output": "2 5 4 1 3"}]
1,100
["constructive algorithms"]
23
[{"input": "2\r\n0 1\r\n1 0\r\n", "output": "2 1\r\n"}, {"input": "5\r\n0 2 2 1 2\r\n2 0 4 1 3\r\n2 4 0 1 3\r\n1 1 1 0 1\r\n2 3 3 1 0\r\n", "output": "2 5 4 1 3\r\n"}, {"input": "10\r\n0 1 5 2 5 3 4 5 5 5\r\n1 0 1 1 1 1 1 1 1 1\r\n5 1 0 2 6 3 4 6 6 6\r\n2 1 2 0 2 2 2 2 2 2\r\n5 1 6 2 0 3 4 8 8 7\r\n3 1 3 2 3 0 3 3 3 3\r\n4 1 4 2 4 3 0 4 4 4\r\n5 1 6 2 8 3 4 0 9 7\r\n5 1 6 2 8 3 4 9 0 7\r\n5 1 6 2 7 3 4 7 7 0\r\n", "output": "5 1 6 2 8 3 4 10 9 7\r\n"}, {"input": "4\r\n0 1 3 2\r\n1 0 1 1\r\n3 1 0 2\r\n2 1 2 0\r\n", "output": "4 1 3 2\r\n"}, {"input": "7\r\n0 3 2 4 1 4 4\r\n3 0 2 3 1 3 3\r\n2 2 0 2 1 2 2\r\n4 3 2 0 1 5 5\r\n1 1 1 1 0 1 1\r\n4 3 2 5 1 0 6\r\n4 3 2 5 1 6 0\r\n", "output": "4 3 2 5 1 7 6\r\n"}, {"input": "10\r\n0 4 4 1 4 4 4 2 3 4\r\n4 0 5 1 6 8 9 2 3 7\r\n4 5 0 1 5 5 5 2 3 5\r\n1 1 1 0 1 1 1 1 1 1\r\n4 6 5 1 0 6 6 2 3 6\r\n4 8 5 1 6 0 8 2 3 7\r\n4 9 5 1 6 8 0 2 3 7\r\n2 2 2 1 2 2 2 0 2 2\r\n3 3 3 1 3 3 3 2 0 3\r\n4 7 5 1 6 7 7 2 3 0\r\n", "output": "4 10 5 1 6 8 9 2 3 7\r\n"}, {"input": "13\r\n0 5 5 2 5 4 5 5 3 5 5 5 1\r\n5 0 6 2 6 4 6 6 3 6 6 6 1\r\n5 6 0 2 10 4 7 10 3 8 10 9 1\r\n2 2 2 0 2 2 2 2 2 2 2 2 1\r\n5 6 10 2 0 4 7 12 3 8 11 9 1\r\n4 4 4 2 4 0 4 4 3 4 4 4 1\r\n5 6 7 2 7 4 0 7 3 7 7 7 1\r\n5 6 10 2 12 4 7 0 3 8 11 9 1\r\n3 3 3 2 3 3 3 3 0 3 3 3 1\r\n5 6 8 2 8 4 7 8 3 0 8 8 1\r\n5 6 10 2 11 4 7 11 3 8 0 9 1\r\n5 6 9 2 9 4 7 9 3 8 9 0 1\r\n1 1 1 1 1 1 1 1 1 1 1 1 0\r\n", "output": "5 6 10 2 13 4 7 12 3 8 11 9 1\r\n"}]
false
stdio
import sys def main(input_path, output_path, sub_path): with open(input_path) as f: lines = [line.strip() for line in f.readlines()] n = int(lines[0]) a = [list(map(int, line.split())) for line in lines[1:n+1]] with open(sub_path) as f: sub_line = f.read().strip() p = list(map(int, sub_line.split())) if len(p) != n or sorted(p) != list(range(1, n+1)): print(0) return for i in range(n): for j in range(n): if i != j and min(p[i], p[j]) != a[i][j]: print(0) return print(1) if __name__ == "__main__": input_path, output_path, sub_path = sys.argv[1:4] main(input_path, output_path, sub_path)
true
387/B
387
B
Python 3
TESTS
9
93
7,372,800
37442934
n, m = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) count = 0 i = 0 if B[0] >= A[n-1]: if n > m: print(n-m) exit() else: print(0) exit() for j in range(0, m): if B[j] >= A[i]: i += 1 if i == n - 1: print(0) exit() print(n-i)
41
46
204,800
140628444
inp=list(map(int,input().split())) n=inp[0] # min num of problems m=inp[1] # num of problems he has a=list(map(int,input().split())) # problems to put b=list(map(int,input().split())) # problems he already has la=len(a) lb=len(b) pa=0 # pointer in first array pb=0 # pointer in second array c=0 while(pb<lb and pa<la): if(a[pa]<=b[pb]): c=c+1 pa=pa+1 pb=pb+1 else: pb=pb+1 if(c>la): print(0) else: print(la-c)
Codeforces Round 227 (Div. 2)
CF
2,014
1
256
George and Round
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi. To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities. George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data. However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 < a2 < ... < an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
Print a single integer — the answer to the problem.
null
In the first sample the set of the prepared problems meets the requirements for a good round. In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round. In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
[{"input": "3 5\n1 2 3\n1 2 2 3 3", "output": "0"}, {"input": "3 5\n1 2 3\n1 1 1 1 1", "output": "2"}, {"input": "3 1\n2 3 4\n1", "output": "3"}]
1,200
["brute force", "greedy", "two pointers"]
41
[{"input": "3 5\r\n1 2 3\r\n1 2 2 3 3\r\n", "output": "0\r\n"}, {"input": "3 5\r\n1 2 3\r\n1 1 1 1 1\r\n", "output": "2\r\n"}, {"input": "3 1\r\n2 3 4\r\n1\r\n", "output": "3\r\n"}, {"input": "29 100\r\n20 32 41 67 72 155 331 382 399 412 465 470 484 511 515 529 616 637 679 715 733 763 826 843 862 903 925 979 989\r\n15 15 15 17 18 19 19 20 21 21 22 24 25 26 26 27 28 31 32 32 37 38 38 39 39 40 41 42 43 43 45 45 46 47 49 49 50 50 50 51 52 53 53 55 56 57 59 59 59 60 60 62 62 63 63 64 64 64 66 67 69 69 70 70 72 72 73 74 75 76 77 78 80 80 81 81 83 83 83 84 86 86 86 86 87 88 89 91 91 91 92 93 94 94 96 97 97 97 98 98\r\n", "output": "24\r\n"}]
false
stdio
null
true
387/B
387
B
PyPy 3
TESTS
10
202
2,252,800
69478316
n,m=map(int,input().split()) a=list(map(int,input().split())) b=list(map(int,input().split())) c=0 for i in range(len(a)): for j in range(len(b)): if a[i]==b[j]: a[i]=-1 b[j]=-1 break for i in range(len(a)): for j in range(len(b)): if b[j]>a[i]: a[i]=-1 b[j]=-1 break print(abs(a.count(-1)-len(a)))
41
46
204,800
143479221
n,m=map(int,input().split()) a=list(map(int,input().split())) b=list(map(int,input().split())) a.sort() b.sort() res=0 n-=1 m-=1 while n>=0 and m>=0: if a[n]>b[m]: res+=1 else: m-=1 n-=1 res+=1+n print(res)
Codeforces Round 227 (Div. 2)
CF
2,014
1
256
George and Round
George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let's number the problems with integers 1 through m. George estimates the i-th problem's complexity by integer bi. To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities. George has a poor imagination. It's easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c ≥ d), by changing limits on the input data. However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a good round. That's why he decided to find out the minimum number of problems he needs to come up with in addition to the m he's prepared in order to make a good round. Note that George can come up with a new problem of any complexity.
The first line contains two integers n and m (1 ≤ n, m ≤ 3000) — the minimal number of problems in a good round and the number of problems George's prepared. The second line contains space-separated integers a1, a2, ..., an (1 ≤ a1 < a2 < ... < an ≤ 106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1, b2, ..., bm (1 ≤ b1 ≤ b2... ≤ bm ≤ 106) — the complexities of the problems prepared by George.
Print a single integer — the answer to the problem.
null
In the first sample the set of the prepared problems meets the requirements for a good round. In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round. In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2, 3, 4.
[{"input": "3 5\n1 2 3\n1 2 2 3 3", "output": "0"}, {"input": "3 5\n1 2 3\n1 1 1 1 1", "output": "2"}, {"input": "3 1\n2 3 4\n1", "output": "3"}]
1,200
["brute force", "greedy", "two pointers"]
41
[{"input": "3 5\r\n1 2 3\r\n1 2 2 3 3\r\n", "output": "0\r\n"}, {"input": "3 5\r\n1 2 3\r\n1 1 1 1 1\r\n", "output": "2\r\n"}, {"input": "3 1\r\n2 3 4\r\n1\r\n", "output": "3\r\n"}, {"input": "29 100\r\n20 32 41 67 72 155 331 382 399 412 465 470 484 511 515 529 616 637 679 715 733 763 826 843 862 903 925 979 989\r\n15 15 15 17 18 19 19 20 21 21 22 24 25 26 26 27 28 31 32 32 37 38 38 39 39 40 41 42 43 43 45 45 46 47 49 49 50 50 50 51 52 53 53 55 56 57 59 59 59 60 60 62 62 63 63 64 64 64 66 67 69 69 70 70 72 72 73 74 75 76 77 78 80 80 81 81 83 83 83 84 86 86 86 86 87 88 89 91 91 91 92 93 94 94 96 97 97 97 98 98\r\n", "output": "24\r\n"}]
false
stdio
null
true
803/E
803
E
Python 3
TESTS
1
46
4,915,200
31540074
def ch(s): k=0 for i in s: if i=='W': k+=1 elif i=='L': k-=1 if (k<=-d) or (k>=d): return False break return True def lastch(s): k=0 for i in s: if i=='W': k+=1 elif i=='L': k-=1 if (k<-d) or (k>d): return False break if ((k==d) or (k==-d)) and (s[-1]!='D'): return True else: return False def comp(s): x=s.find('?') global p if x==-1: if lastch(s): print(s) p=False elif ch(s[:x]) and p: comp(s[:x]+'W'+s[x+1:]) comp(s[:x] + 'L' + s[x + 1:]) comp(s[:x] + 'D' + s[x + 1:]) n,d=map(int,input().split(' ')) p=True s=input() comp(s)
28
967
14,540,800
28180685
N,k=list(map(int,input().strip().split(' '))) S=input() # num of W-num of L=j, j>k means k-j dp=[[0 for j in range(2*k+1)]for i in range(N)] #print(dp) for i in range(len(S)): if i==0: if S[0]=='W': dp[0][1]='W' elif S[0]=='L': dp[0][-1]='L' elif S[0]=='D': dp[0][0]='D' else: dp[0][1]='W' dp[0][-1]='L' dp[0][0]='D' elif i!=len(S)-1: if S[i]=='W': for j in range(0,k): if j==0: if dp[i-1][-1]!=0: dp[i][0]='W' else: if dp[i-1][j-1]!=0: dp[i][j]='W' for j in range(1,k): if j!=k-1: if dp[i-1][-j-1]!=0: dp[i][-j]='W' elif S[i]=='L': for j in range(0,k): if dp[i-1][j+1]!=0: dp[i][j]='L' for j in range(1,k): if j==1: if dp[i-1][0]!=0: dp[i][-1]='L' else: if dp[i-1][-j+1]!=0: dp[i][-j]='L' elif S[i]=='D': for j in range(0,2*k+1): if dp[i-1][j]!=0: dp[i][j]='D' else: for j in range(0,k): if j==0: if dp[i-1][-1]!=0: dp[i][j]='W' elif dp[i-1][1]!=0: dp[i][j]='L' elif dp[i-1][0]!=0: dp[i][j]='D' else: if dp[i-1][j-1]!=0: dp[i][j]='W' elif dp[i-1][j+1]!=0: dp[i][j]='L' elif dp[i-1][j]!=0: dp[i][j]='D' for j in range(1,k): if j==1: if dp[i-1][0]!=0: dp[i][-1]='L' elif dp[i-1][-1]!=0: dp[i][-1]='D' elif dp[i-1][-2]!=0: dp[i][-1]='W' else: if dp[i-1][-(j-1)]!=0: dp[i][-j]='L' elif dp[i-1][-j]!=0: dp[i][-j]='D' elif dp[i-1][-(j+1)]!=0: dp[i][-j]='W' else: if S[i]=='W': if dp[i-1][k-1]!=0: dp[i][k]='W' elif S[i]=='L': if dp[i-1][-(k-1)]!=0: dp[i][-k]='L' elif S[i]=='D': 1 else: if dp[i-1][k-1]!=0: dp[i][k]='W' elif dp[i-1][-(k-1)]!=0: dp[i][-k]='L' #print(dp) if k>1 and N>=k: if dp[len(S)-1][k]==0 and dp[len(S)-1][-k]==0: print('NO') else: if dp[len(S)-1][k]!=0: ans='' cur=k for j in range(1,len(S)+1): temp=dp[len(S)-j][cur] if temp=='W': ans+=temp cur-=1 elif temp=='D': ans+=temp elif temp=='L': ans+=temp cur+=1 elif dp[len(S)-1][-k]!=0: ans='' cur=-k for j in range(1,len(S)+1): temp=dp[len(S)-j][cur] if temp=='W': ans+=temp cur-=1 elif temp=='D': ans+=temp elif temp=='L': ans+=temp cur+=1 ans=ans[::-1] print(ans) elif N<k: print('NO') elif k==1: shit=0 for i in range(len(S)): if i<len(S)-1: if S[i]!='?': shit=1 break if shit==1: print('NO') else: temp='' for i in range(len(S)-1): temp+='D' if S[-1]=='D': print('NO') elif S[-1]=='L': temp+='L' print(temp) else: temp+='W' print(temp)
Educational Codeforces Round 20
ICPC
2,017
2
256
Roma and Poker
Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser. Last evening Roma started to play poker. He decided to spend no more than k virtual bourles — he will stop immediately if the number of his loses exceeds the number of his wins by k. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by k. Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won k bourles or he lost. The sequence written by Roma is a string s consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and ? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met: - In the end the absolute difference between the number of wins and loses is equal to k; - There is no hand such that the absolute difference before this hand was equal to k. Help Roma to restore any such sequence.
The first line contains two numbers n (the length of Roma's sequence) and k (1 ≤ n, k ≤ 1000). The second line contains the sequence s consisting of characters W, L, D and ?. There are exactly n characters in this sequence.
If there is no valid sequence that can be obtained from s by replacing all ? characters by W, L or D, print NO. Otherwise print this sequence. If there are multiple answers, print any of them.
null
null
[{"input": "3 2\nL??", "output": "LDL"}, {"input": "3 1\nW??", "output": "NO"}, {"input": "20 5\n?LLLLLWWWWW?????????", "output": "WLLLLLWWWWWWWWLWLWDW"}]
2,000
["dp", "graphs"]
28
[{"input": "3 2\r\nL??\r\n", "output": "LDL\r\n"}, {"input": "3 1\r\nW??\r\n", "output": "NO\r\n"}, {"input": "20 5\r\n?LLLLLWWWWW?????????\r\n", "output": "WLLLLLWWWWWWWWLWLWDW\r\n"}, {"input": "5 5\r\n?WDDD\r\n", "output": "NO\r\n"}, {"input": "5 3\r\n??D??\r\n", "output": "WWDDW\r\n"}, {"input": "10 1\r\nD??W?WL?DW\r\n", "output": "NO\r\n"}, {"input": "10 3\r\nDWD?DL??LL\r\n", "output": "DWDWDLLLLL\r\n"}, {"input": "10 2\r\nLWL?WWDDW?\r\n", "output": "NO\r\n"}, {"input": "1 1\r\n?\r\n", "output": "W\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path, 'r') as f: n, k = map(int, f.readline().split()) s = f.readline().strip() with open(submission_path, 'r') as f: submission = f.read().strip() if submission == "NO": possible = check_possible(n, k, s) print(1 if not possible else 0) else: valid = check_submission(n, k, s, submission) print(1 if valid else 0) def check_possible(n, k, s): dp = [set() for _ in range(n+1)] dp[0].add(0) for i in range(n): current_char = s[i] next_dp = set() for j in dp[i]: moves = [] if current_char == 'W': moves = ['W'] elif current_char == 'L': moves = ['L'] elif current_char == 'D': moves = ['D'] else: moves = ['W', 'L', 'D'] for move in moves: new_j = j if move == 'W': new_j += 1 elif move == 'L': new_j -= 1 if i == n-1: if abs(new_j) == k: next_dp.add(new_j) else: if abs(new_j) < k: next_dp.add(new_j) dp[i+1] = next_dp return k in dp[n] or -k in dp[n] def check_submission(n, k, s, submission): if len(submission) != n: return False balance = 0 for i in range(n): sub_char = submission[i] orig_char = s[i] if orig_char != '?' and sub_char != orig_char: return False if sub_char not in ['W', 'L', 'D']: return False if sub_char == 'W': balance += 1 elif sub_char == 'L': balance -= 1 if i < n-1 and abs(balance) >= k: return False return abs(balance) == k if __name__ == "__main__": main()
true
803/E
803
E
PyPy 3
TESTS
1
218
1,536,000
54652573
#Bhargey Mehta (Sophomore) #DA-IICT, Gandhinagar import sys, math, queue #sys.stdin = open("input.txt", "r") MOD = 10**9+7 sys.setrecursionlimit(1000000) def solve(n, k): if abs(k) > K: return False if dp[n][k] is not None: return dp[n][k] #print(s[n-1], n, k) if s[n-1] == 'W': dp[n][k] = solve(n-1, k-1) elif s[n-1] == 'L': dp[n][k] = solve(n-1, k+1) elif s[n-1] == 'D': dp[n][k] = solve(n-1, k) else: dp[n][k] = solve(n-1, k-1) or solve(n-1, k+1) or solve(n-1, k) return dp[n][k] def back(n, k): if n == 0: return if s[n-1] == 'W': back(n-1, k-1) print('W', end="") elif s[n-1] == 'L': back(n-1, k+1) print('L', end="") elif s[n-1] == 'D': back(n-1, k) print('D', end="") else: if solve(n-1, k-1): back(n-1, k-1) print('W', end="") elif solve(n-1, k+1): back(n-1, k+1) print('L', end="") else: back(n-1, k) print('D', end="") N, K = map(int, input().split()) s = input() dp = [[None for i in range(2*K+1)] for j in range(N+1)] for i in range(2*K+1): dp[0][i] = False dp[0][0] = True for i in range(-K, K+1): if solve(N, i): ans = "" back(N, i) print(ans) exit() print("NO")
28
62
409,600
26883241
def ma(): s=input() v=s.split(' ') n=int(v[0]) k=int(v[1]) s=input() dp=[[0,0] for _ in range(n+1)] flag=True for i in range (1,n): c=s[i-1] if c=='?': dp[i][0]=min(dp[i-1][0]+1 ,k-1) dp[i][1]=max(dp[i-1][1]-1 ,-k+1) elif c=='D': dp[i][0]=dp[i-1][0] dp[i][1]=dp[i-1][1] elif c=='W': dp[i][0]=min(dp[i-1][0]+1 ,k-1) dp[i][1]=dp[i-1][1]+1 if dp[i][1]==k: flag=False elif c=='L': dp[i][0]=dp[i-1][0]-1 dp[i][1]=max(dp[i-1][1]-1 ,-k+1) if dp[i][0]==-k: flag=False if not flag: print('NO') return i=n if s[i-1]=='?': dp[i][0]=dp[i-1][0]+1 dp[i][1]=dp[i-1][1]-1 elif s[i-1]=='D': dp[i][0]=dp[i-1][0] dp[i][1]=dp[i-1][1] elif s[i-1]=='W': dp[i][0]=dp[i-1][0]+1 dp[i][1]=dp[i-1][1]+1 elif s[i-1]=='L': dp[i][0]=dp[i-1][0]-1 dp[i][1]=dp[i-1][1]-1 res=['?']*n if dp[i][0]==k or dp[i][1]==-k: if dp[i][0]==k: cur=k else: cur=-k for i in range(n-1,-1,-1): c=s[i] if c=='?': if cur>dp[i][0]: cur=cur-1 res[i]='W' elif dp[i][1]<=cur<=dp[i][0]: cur=cur res[i]='D' elif cur<dp[i][1]: cur=cur+1 res[i]='L' elif c=='D': cur=cur res[i]=c elif c=='W': cur=cur-1 res[i]=c elif c=='L': cur=cur+1 res[i]=c for i in range(n): print(res[i],end='') else: print('NO') ma()
Educational Codeforces Round 20
ICPC
2,017
2
256
Roma and Poker
Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser. Last evening Roma started to play poker. He decided to spend no more than k virtual bourles — he will stop immediately if the number of his loses exceeds the number of his wins by k. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by k. Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won k bourles or he lost. The sequence written by Roma is a string s consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and ? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met: - In the end the absolute difference between the number of wins and loses is equal to k; - There is no hand such that the absolute difference before this hand was equal to k. Help Roma to restore any such sequence.
The first line contains two numbers n (the length of Roma's sequence) and k (1 ≤ n, k ≤ 1000). The second line contains the sequence s consisting of characters W, L, D and ?. There are exactly n characters in this sequence.
If there is no valid sequence that can be obtained from s by replacing all ? characters by W, L or D, print NO. Otherwise print this sequence. If there are multiple answers, print any of them.
null
null
[{"input": "3 2\nL??", "output": "LDL"}, {"input": "3 1\nW??", "output": "NO"}, {"input": "20 5\n?LLLLLWWWWW?????????", "output": "WLLLLLWWWWWWWWLWLWDW"}]
2,000
["dp", "graphs"]
28
[{"input": "3 2\r\nL??\r\n", "output": "LDL\r\n"}, {"input": "3 1\r\nW??\r\n", "output": "NO\r\n"}, {"input": "20 5\r\n?LLLLLWWWWW?????????\r\n", "output": "WLLLLLWWWWWWWWLWLWDW\r\n"}, {"input": "5 5\r\n?WDDD\r\n", "output": "NO\r\n"}, {"input": "5 3\r\n??D??\r\n", "output": "WWDDW\r\n"}, {"input": "10 1\r\nD??W?WL?DW\r\n", "output": "NO\r\n"}, {"input": "10 3\r\nDWD?DL??LL\r\n", "output": "DWDWDLLLLL\r\n"}, {"input": "10 2\r\nLWL?WWDDW?\r\n", "output": "NO\r\n"}, {"input": "1 1\r\n?\r\n", "output": "W\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path, 'r') as f: n, k = map(int, f.readline().split()) s = f.readline().strip() with open(submission_path, 'r') as f: submission = f.read().strip() if submission == "NO": possible = check_possible(n, k, s) print(1 if not possible else 0) else: valid = check_submission(n, k, s, submission) print(1 if valid else 0) def check_possible(n, k, s): dp = [set() for _ in range(n+1)] dp[0].add(0) for i in range(n): current_char = s[i] next_dp = set() for j in dp[i]: moves = [] if current_char == 'W': moves = ['W'] elif current_char == 'L': moves = ['L'] elif current_char == 'D': moves = ['D'] else: moves = ['W', 'L', 'D'] for move in moves: new_j = j if move == 'W': new_j += 1 elif move == 'L': new_j -= 1 if i == n-1: if abs(new_j) == k: next_dp.add(new_j) else: if abs(new_j) < k: next_dp.add(new_j) dp[i+1] = next_dp return k in dp[n] or -k in dp[n] def check_submission(n, k, s, submission): if len(submission) != n: return False balance = 0 for i in range(n): sub_char = submission[i] orig_char = s[i] if orig_char != '?' and sub_char != orig_char: return False if sub_char not in ['W', 'L', 'D']: return False if sub_char == 'W': balance += 1 elif sub_char == 'L': balance -= 1 if i < n-1 and abs(balance) >= k: return False return abs(balance) == k if __name__ == "__main__": main()
true
803/E
803
E
Python 3
TESTS
0
31
4,915,200
26731327
n, k = map(int, input().split()) s=input() won=0 lost=0 drawn=0 un=0 for i in range(n): if s[i]=='W': won+=1 elif s[i]=='L': lost+=1 elif s[i]=='D': drawn+=1 else: un+=1 if won>=lost+k: print('NO') else: if won+un>=k and (won!=k or s[-1]=='?'): s=list(s) for i in range(n-1, -1, -1): if won>lost+k: break if s[i]=='?': s[i]='W' won+=1 for i in range(n): if s[i]=='?': s[i]='D' if lost+un>=k: s=list(s) for i in range(n-1, -1, -1): if lost>k: break if s[i]=='?': s[i]='L' un-=1 lost+=1 for i in range(n): if un==0: break if s[i]=='?': s[i]='D' un-=1 print(''.join(s))
28
93
20,889,600
180996735
n,k=map(int,input().split()) s=input() m=2*k+1 dp=[[0]*m for i in range(n+1)] dp[0][k]=1 for i in range(n): if s[i]=='L' or s[i]=='?': for j in range(m-1): dp[i+1][j]|=dp[i][j+1] if s[i]=='D' or s[i]=='?': for j in range(m): dp[i+1][j]|=dp[i][j] if s[i]=='W' or s[i]=='?': for j in range(1,m): dp[i+1][j]|=dp[i][j-1] if i!=n-1: dp[i+1][0]=0 dp[i+1][2*k]=0 if dp[n][0]: tmp=0 elif dp[n][2*k]: tmp=2*k else: print('NO') exit() ans='' for i in range(n-1,-1,-1): if s[i]=='L': ans+='L' tmp+=1 elif s[i]=='D': ans+='D' elif s[i]=='W': ans+='W' tmp-=1 else: if tmp>0 and dp[i][tmp-1]: ans+='W' tmp-=1 elif dp[i][tmp]: ans+='D' elif tmp<2*k and dp[i][tmp+1]: ans+='L' tmp+=1 else: raise Exception print(ans[::-1])
Educational Codeforces Round 20
ICPC
2,017
2
256
Roma and Poker
Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser. Last evening Roma started to play poker. He decided to spend no more than k virtual bourles — he will stop immediately if the number of his loses exceeds the number of his wins by k. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by k. Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won k bourles or he lost. The sequence written by Roma is a string s consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and ? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met: - In the end the absolute difference between the number of wins and loses is equal to k; - There is no hand such that the absolute difference before this hand was equal to k. Help Roma to restore any such sequence.
The first line contains two numbers n (the length of Roma's sequence) and k (1 ≤ n, k ≤ 1000). The second line contains the sequence s consisting of characters W, L, D and ?. There are exactly n characters in this sequence.
If there is no valid sequence that can be obtained from s by replacing all ? characters by W, L or D, print NO. Otherwise print this sequence. If there are multiple answers, print any of them.
null
null
[{"input": "3 2\nL??", "output": "LDL"}, {"input": "3 1\nW??", "output": "NO"}, {"input": "20 5\n?LLLLLWWWWW?????????", "output": "WLLLLLWWWWWWWWLWLWDW"}]
2,000
["dp", "graphs"]
28
[{"input": "3 2\r\nL??\r\n", "output": "LDL\r\n"}, {"input": "3 1\r\nW??\r\n", "output": "NO\r\n"}, {"input": "20 5\r\n?LLLLLWWWWW?????????\r\n", "output": "WLLLLLWWWWWWWWLWLWDW\r\n"}, {"input": "5 5\r\n?WDDD\r\n", "output": "NO\r\n"}, {"input": "5 3\r\n??D??\r\n", "output": "WWDDW\r\n"}, {"input": "10 1\r\nD??W?WL?DW\r\n", "output": "NO\r\n"}, {"input": "10 3\r\nDWD?DL??LL\r\n", "output": "DWDWDLLLLL\r\n"}, {"input": "10 2\r\nLWL?WWDDW?\r\n", "output": "NO\r\n"}, {"input": "1 1\r\n?\r\n", "output": "W\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] with open(input_path, 'r') as f: n, k = map(int, f.readline().split()) s = f.readline().strip() with open(submission_path, 'r') as f: submission = f.read().strip() if submission == "NO": possible = check_possible(n, k, s) print(1 if not possible else 0) else: valid = check_submission(n, k, s, submission) print(1 if valid else 0) def check_possible(n, k, s): dp = [set() for _ in range(n+1)] dp[0].add(0) for i in range(n): current_char = s[i] next_dp = set() for j in dp[i]: moves = [] if current_char == 'W': moves = ['W'] elif current_char == 'L': moves = ['L'] elif current_char == 'D': moves = ['D'] else: moves = ['W', 'L', 'D'] for move in moves: new_j = j if move == 'W': new_j += 1 elif move == 'L': new_j -= 1 if i == n-1: if abs(new_j) == k: next_dp.add(new_j) else: if abs(new_j) < k: next_dp.add(new_j) dp[i+1] = next_dp return k in dp[n] or -k in dp[n] def check_submission(n, k, s, submission): if len(submission) != n: return False balance = 0 for i in range(n): sub_char = submission[i] orig_char = s[i] if orig_char != '?' and sub_char != orig_char: return False if sub_char not in ['W', 'L', 'D']: return False if sub_char == 'W': balance += 1 elif sub_char == 'L': balance -= 1 if i < n-1 and abs(balance) >= k: return False return abs(balance) == k if __name__ == "__main__": main()
true
178/A1
178
A3
Python 3
TESTS3
2
216
0
54093826
mod = 1000000007 ii = lambda : int(input()) si = lambda : input() dgl = lambda : list(map(int, input())) f = lambda : map(int, input().split()) il = lambda : list(map(int, input().split())) ls = lambda : list(input()) n=ii() l=il() i=0 while 2**i<n-1: i+=1 i-=1 sm=0 for j in range(n-1): if j+2**i>n-1: i-=1 sm+=l[j] l[j+2**i]+=l[j] print(sm)
12
62
0
216157202
from math import log2, floor n, a = int(input()), [int(i) for i in input().split()] res = [0] * (n - 1) for k in range(n - 1): if k > 0: res[k] += res[k - 1] res[k] += a[k] p = floor(log2(n - 1 - k)) a[k + 2 ** p] += a[k] print(*res, sep="\n")
ABBYY Cup 2.0 - Hard
ICPC
2,012
2
256
Educational Game
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below. The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some prefix of the sequence) equal to zero for some fixed k (k < n), and this should be done in the smallest possible number of moves. One move is choosing an integer i (1 ≤ i ≤ n) such that ai > 0 and an integer t (t ≥ 0) such that i + 2t ≤ n. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of ai + 2t is increased by 1. For example, let n = 4 and a = (1, 0, 1, 2), then it is possible to make move i = 3, t = 0 and get a = (1, 0, 0, 3) or to make move i = 1, t = 1 and get a = (0, 0, 2, 2) (the only possible other move is i = 1, t = 0). You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1 ≤ k < n).
The first input line contains a single integer n. The second line contains n integers ai (0 ≤ ai ≤ 104), separated by single spaces. The input limitations for getting 20 points are: - 1 ≤ n ≤ 300 The input limitations for getting 50 points are: - 1 ≤ n ≤ 2000 The input limitations for getting 100 points are: - 1 ≤ n ≤ 105
Print exactly n - 1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.
null
null
[{"input": "4\n1 0 1 2", "output": "1\n1\n3"}, {"input": "8\n1 2 3 4 5 6 7 8", "output": "1\n3\n6\n10\n16\n24\n40"}]
1,000
[]
12
[{"input": "4\r\n1 0 1 2\r\n", "output": "1\r\n1\r\n3\r\n"}, {"input": "8\r\n1 2 3 4 5 6 7 8\r\n", "output": "1\r\n3\r\n6\r\n10\r\n16\r\n24\r\n40\r\n"}, {"input": "5\r\n4 1 4 7 6\r\n", "output": "4\r\n5\r\n9\r\n17\r\n"}, {"input": "9\r\n13 13 7 11 3 9 3 5 5\r\n", "output": "13\r\n26\r\n33\r\n44\r\n47\r\n69\r\n79\r\n117\r\n"}, {"input": "30\r\n8 17 20 15 18 15 20 10 5 13 5 4 15 9 11 14 18 15 7 16 18 9 17 7 10 9 5 13 17 16\r\n", "output": "8\r\n25\r\n45\r\n60\r\n78\r\n93\r\n113\r\n123\r\n128\r\n141\r\n146\r\n150\r\n165\r\n174\r\n185\r\n199\r\n225\r\n257\r\n284\r\n315\r\n351\r\n375\r\n423\r\n454\r\n495\r\n549\r\n634\r\n713\r\n907\r\n"}, {"input": "80\r\n72 66 82 46 44 22 63 92 71 65 5 30 45 84 29 73 9 90 25 19 26 15 12 29 33 19 85 92 91 66 83 39 100 53 20 99 11 81 26 41 36 51 21 72 28 100 34 3 24 58 11 85 73 18 4 45 90 99 42 85 26 71 58 49 76 32 88 13 40 98 57 95 20 36 70 66 75 12 54 96\r\n", "output": "72\n138\n220\n266\n310\n332\n395\n487\n558\n623\n628\n658\n703\n787\n816\n889\n898\n988\n1013\n1032\n1058\n1073\n1085\n1114\n1147\n1166\n1251\n1343\n1434\n1500\n1583\n1622\n1722\n1775\n1795\n1894\n1905\n1986\n2012\n2053\n2089\n2140\n2161\n2233\n2261\n2361\n2395\n2398\n2431\n2579\n2615\n2719\n2818\n2851\n2867\n2941\n3064\n3182\n3309\n3486\n3603\n3740\n3881\n3969\n4250\n4549\n4775\n5037\n5231\n5465\n5627\n5929\n6460\n7029\n7478\n8085\n9075\n10211\n12070\n"}]
false
stdio
null
true
839/C
839
C
Python 3
TESTS
38
607
152,268,800
120278205
from collections import defaultdict from sys import * import threading setrecursionlimit(10**9) threading.stack_size(10**8) def solve(): def dfs(d,a,vis,node): vis[node]=True if len(d[node])==1 and node!=1: a[node]=0 return for neighbor in d[node]: if vis[neighbor]==False: dfs(d,a,vis,neighbor) a[node]+=a[neighbor] if node==1: a[node]=((a[node])/len(d[node]))+1 else: a[node] = ((a[node]) / (len(d[node])-1)) + 1 n=int(input()) d=defaultdict(list) for i in range(n-1): u,v=map(int,input().split()) d[u].append(v) d[v].append(u) a=[0]*(n+1) vis=[False]*(n+1) dfs(d,a,vis,1) print(a[1]) threading.Thread(target=solve).start()
40
265
29,286,400
227297198
from collections import deque import sys;input = sys.stdin.readline S = lambda : input().strip() L = lambda :list(map(int, input().split())) I = lambda :int(input().strip()) T = lambda :map(int, input().split()) mod = int(1e9) + 7 n = I() graph = [[] for i in range(n)] for i in range(n-1): a,b = T() graph[a-1].append(b-1) graph[b-1].append(a-1) q = deque([(0, 1.0, 0, -1)]) ans = 0 while q: node, prob, dis, parent = q.popleft() paths = len(graph[node]) - (1 if node != 0 else 0) if paths: for nbr in graph[node]: if nbr == parent: continue q.append((nbr, prob/paths, dis+1, node)) else: ans += prob*dis print(f"{ans:.6f}")
Codeforces Round 428 (Div. 2)
CF
2,017
2
256
Journey
There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads. Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities. Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link https://en.wikipedia.org/wiki/Expected_value.
The first line contains a single integer n (1 ≤ n ≤ 100000) — number of cities. Then n - 1 lines follow. The i-th line of these lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the cities connected by the i-th road. It is guaranteed that one can reach any city from any other by the roads.
Print a number — the expected length of their journey. The journey starts in the city 1. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if $$\frac{|a-b|}{\max(1,b)} \leq 10^{-6}$$.
null
In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5. In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2.
[{"input": "4\n1 2\n1 3\n2 4", "output": "1.500000000000000"}, {"input": "5\n1 2\n1 3\n3 4\n2 5", "output": "2.000000000000000"}]
1,500
["dfs and similar", "dp", "graphs", "probabilities", "trees"]
40
[{"input": "4\r\n1 2\r\n1 3\r\n2 4\r\n", "output": "1.500000000000000\r\n"}, {"input": "5\r\n1 2\r\n1 3\r\n3 4\r\n2 5\r\n", "output": "2.000000000000000\r\n"}, {"input": "70\r\n1 25\r\n57 1\r\n18 1\r\n65 1\r\n38 1\r\n1 41\r\n1 5\r\n1 69\r\n1 3\r\n31 1\r\n1 8\r\n1 9\r\n53 1\r\n70 1\r\n45 1\r\n1 24\r\n1 42\r\n1 30\r\n1 12\r\n1 37\r\n64 1\r\n1 28\r\n1 58\r\n1 22\r\n11 1\r\n1 4\r\n1 27\r\n1 16\r\n1 21\r\n54 1\r\n1 51\r\n1 43\r\n29 1\r\n56 1\r\n1 39\r\n32 1\r\n1 15\r\n1 17\r\n1 19\r\n1 40\r\n36 1\r\n48 1\r\n63 1\r\n1 7\r\n1 47\r\n1 13\r\n1 46\r\n60 1\r\n1 6\r\n23 1\r\n20 1\r\n1 52\r\n2 1\r\n26 1\r\n1 59\r\n1 66\r\n10 1\r\n1 62\r\n1 68\r\n1 55\r\n50 1\r\n33 1\r\n44 1\r\n1 34\r\n1 35\r\n1 61\r\n14 1\r\n67 1\r\n49 1\r\n", "output": "1.000000000000000\r\n"}, {"input": "10\r\n8 6\r\n9 10\r\n8 7\r\n1 4\r\n1 8\r\n9 5\r\n9 8\r\n2 5\r\n3 1\r\n", "output": "1.500000000000000\r\n"}, {"input": "1\r\n", "output": "0.000000000000000\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] correct_output_path = sys.argv[2] submission_output_path = sys.argv[3] with open(correct_output_path, 'r') as f: correct_output = f.read().strip() with open(submission_output_path, 'r') as f: submission_output = f.read().strip() try: b = float(correct_output) a = float(submission_output) except: print(0) return denominator = max(1.0, abs(b)) error = abs(a - b) if error <= 1e-6 * denominator: print(1) else: print(0) if __name__ == "__main__": main()
true
1006/A
1006
A
Python 3
TESTS
0
15
0
171178049
n = int(input()) # input the array arr = [int(x) for x in input().split()] new_array = [] for number in arr: if number % 2 == 0: new_array.append(number - 1) else: new_array.append(number + 1) [print(x, end=" ") for x in new_array]
18
46
0
121020549
n=int(input()) li=list(map(int,input().split())) for i in range(n): if li[i]%2==0: li[i]-=1 print(" ".join([str(ele) for ele in li]))
Codeforces Round 498 (Div. 3)
ICPC
2,018
1
256
Adjacent Replacements
Mishka got an integer array $$$a$$$ of length $$$n$$$ as a birthday present (what a surprise!). Mishka doesn't like this present and wants to change it somehow. He has invented an algorithm and called it "Mishka's Adjacent Replacements Algorithm". This algorithm can be represented as a sequence of steps: - Replace each occurrence of $$$1$$$ in the array $$$a$$$ with $$$2$$$; - Replace each occurrence of $$$2$$$ in the array $$$a$$$ with $$$1$$$; - Replace each occurrence of $$$3$$$ in the array $$$a$$$ with $$$4$$$; - Replace each occurrence of $$$4$$$ in the array $$$a$$$ with $$$3$$$; - Replace each occurrence of $$$5$$$ in the array $$$a$$$ with $$$6$$$; - Replace each occurrence of $$$6$$$ in the array $$$a$$$ with $$$5$$$; - $$$\dots$$$ - Replace each occurrence of $$$10^9 - 1$$$ in the array $$$a$$$ with $$$10^9$$$; - Replace each occurrence of $$$10^9$$$ in the array $$$a$$$ with $$$10^9 - 1$$$. Note that the dots in the middle of this algorithm mean that Mishka applies these replacements for each pair of adjacent integers ($$$2i - 1, 2i$$$) for each $$$i \in\{1, 2, \ldots, 5 \cdot 10^8\}$$$ as described above. For example, for the array $$$a = [1, 2, 4, 5, 10]$$$, the following sequence of arrays represents the algorithm: $$$[1, 2, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$1$$$ with $$$2$$$) $$$\rightarrow$$$ $$$[2, 2, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$2$$$ with $$$1$$$) $$$\rightarrow$$$ $$$[1, 1, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$3$$$ with $$$4$$$) $$$\rightarrow$$$ $$$[1, 1, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$4$$$ with $$$3$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$5$$$ with $$$6$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 6, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$6$$$ with $$$5$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\rightarrow$$$ $$$\dots$$$ $$$\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$10$$$ with $$$9$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 5, 9]$$$. The later steps of the algorithm do not change the array. Mishka is very lazy and he doesn't want to apply these changes by himself. But he is very interested in their result. Help him find it.
The first line of the input contains one integer number $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of elements in Mishka's birthday present (surprisingly, an array). The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the elements of the array.
Print $$$n$$$ integers — $$$b_1, b_2, \dots, b_n$$$, where $$$b_i$$$ is the final value of the $$$i$$$-th element of the array after applying "Mishka's Adjacent Replacements Algorithm" to the array $$$a$$$. Note that you cannot change the order of elements in the array.
null
The first example is described in the problem statement.
[{"input": "5\n1 2 4 5 10", "output": "1 1 3 5 9"}, {"input": "10\n10000 10 50605065 1 5 89 5 999999999 60506056 1000000000", "output": "9999 9 50605065 1 5 89 5 999999999 60506055 999999999"}]
800
["implementation"]
18
[{"input": "5\r\n1 2 4 5 10\r\n", "output": "1 1 3 5 9\r\n"}, {"input": "10\r\n10000 10 50605065 1 5 89 5 999999999 60506056 1000000000\r\n", "output": "9999 9 50605065 1 5 89 5 999999999 60506055 999999999\r\n"}, {"input": "1\r\n999999999\r\n", "output": "999999999\r\n"}, {"input": "1\r\n1000000000\r\n", "output": "999999999\r\n"}, {"input": "1\r\n210400\r\n", "output": "210399\r\n"}, {"input": "5\r\n100000000 100000000 100000000 100000000 100000000\r\n", "output": "99999999 99999999 99999999 99999999 99999999\r\n"}, {"input": "1\r\n2441139\r\n", "output": "2441139\r\n"}, {"input": "2\r\n2 2\r\n", "output": "1 1\r\n"}, {"input": "3\r\n2 2 2\r\n", "output": "1 1 1\r\n"}, {"input": "2\r\n4 4\r\n", "output": "3 3\r\n"}]
false
stdio
null
true
1009/E
1009
E
Python 3
TESTS
4
140
614,400
40387540
from sys import stdin #from math import * line = stdin.readline().rstrip().split() n = int(line[0]) numbers = list(map(int, stdin.readline().rstrip().split())) total = 0 for i in range(len(numbers)): num = pow(2, n-(i+1), 998244353)/2 num *= (n+2-(i+1)) num *= numbers[i] num %= 998244353 total += num total %= 998244353 print(int(total))
38
280
82,432,000
151372217
import sys mod = 998244353 add = lambda a, b: (a + b) % mod mult = lambda a, b: (a * b) % mod input = lambda: sys.stdin.buffer.readline().decode().strip() n, a = int(input()), [int(x) for x in input().split()] ans, lst, po = 0, 0, 1 for i in range(n - 1, -1, -1): new = add(lst, po) ans = add(ans, mult(new, a[i])) po, lst = (po * 2) % mod, add(lst, new) print(ans)
Educational Codeforces Round 47 (Rated for Div. 2)
ICPC
2,018
1.5
256
Intercity Travelling
Leha is planning his journey from Moscow to Saratov. He hates trains, so he has decided to get from one city to another by car. The path from Moscow to Saratov can be represented as a straight line (well, it's not that straight in reality, but in this problem we will consider it to be straight), and the distance between Moscow and Saratov is $$$n$$$ km. Let's say that Moscow is situated at the point with coordinate $$$0$$$ km, and Saratov — at coordinate $$$n$$$ km. Driving for a long time may be really difficult. Formally, if Leha has already covered $$$i$$$ kilometers since he stopped to have a rest, he considers the difficulty of covering $$$(i + 1)$$$-th kilometer as $$$a_{i + 1}$$$. It is guaranteed that for every $$$i \in [1, n - 1]$$$ $$$a_i \le a_{i + 1}$$$. The difficulty of the journey is denoted as the sum of difficulties of each kilometer in the journey. Fortunately, there may be some rest sites between Moscow and Saratov. Every integer point from $$$1$$$ to $$$n - 1$$$ may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty $$$a_1$$$, the kilometer after it — difficulty $$$a_2$$$, and so on. For example, if $$$n = 5$$$ and there is a rest site in coordinate $$$2$$$, the difficulty of journey will be $$$2a_1 + 2a_2 + a_3$$$: the first kilometer will have difficulty $$$a_1$$$, the second one — $$$a_2$$$, then Leha will have a rest, and the third kilometer will have difficulty $$$a_1$$$, the fourth — $$$a_2$$$, and the last one — $$$a_3$$$. Another example: if $$$n = 7$$$ and there are rest sites in coordinates $$$1$$$ and $$$5$$$, the difficulty of Leha's journey is $$$3a_1 + 2a_2 + a_3 + a_4$$$. Leha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are $$$2^{n - 1}$$$ different distributions of rest sites (two distributions are different if there exists some point $$$x$$$ such that it contains a rest site in exactly one of these distributions). Leha considers all these distributions to be equiprobable. He wants to calculate $$$p$$$ — the expected value of difficulty of his journey. Obviously, $$$p \cdot 2^{n - 1}$$$ is an integer number. You have to calculate it modulo $$$998244353$$$.
The first line contains one number $$$n$$$ ($$$1 \le n \le 10^6$$$) — the distance from Moscow to Saratov. The second line contains $$$n$$$ integer numbers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_1 \le a_2 \le \dots \le a_n \le 10^6$$$), where $$$a_i$$$ is the difficulty of $$$i$$$-th kilometer after Leha has rested.
Print one number — $$$p \cdot 2^{n - 1}$$$, taken modulo $$$998244353$$$.
null
null
[{"input": "2\n1 2", "output": "5"}, {"input": "4\n1 3 3 7", "output": "60"}]
2,000
["combinatorics", "math", "probabilities"]
38
[{"input": "2\r\n1 2\r\n", "output": "5\r\n"}, {"input": "4\r\n1 3 3 7\r\n", "output": "60\r\n"}, {"input": "100\r\n3 3 3 4 7 8 8 8 9 9 10 12 12 13 14 14 15 15 16 17 17 20 21 21 22 22 23 25 29 31 36 37 37 38 39 40 41 41 41 42 43 44 45 46 46 47 47 49 49 49 51 52 52 53 54 55 59 59 59 60 62 63 63 64 66 69 70 71 71 72 74 76 76 77 77 78 78 79 80 81 81 82 82 84 85 86 87 87 87 89 91 92 92 92 92 97 98 99 100 100\r\n", "output": "758086002\r\n"}, {"input": "1\r\n12\r\n", "output": "12\r\n"}]
false
stdio
null
true
472/D
472
D
Python 3
TESTS
1
31
0
213886947
# LUOGU_RID: 115731710 print ("YES")
47
545
57,036,800
177433089
from math import inf from collections import * import math, os, sys, heapq, bisect, random,threading from functools import lru_cache from itertools import * import sys def inp(): return sys.stdin.readline().rstrip("\r\n") def out(var): sys.stdout.write(str(var)) # for fast output, always take string def inpu(): return int(inp()) def lis(): return list(map(int, inp().split())) def stringlis(): return list(map(str, inp().split())) def sep(): return map(int, inp().split()) def strsep(): return map(str, inp().split()) def fsep(): return map(float, inp().split()) # #include <ext/pb_ds/assoc_container.hpp> # #include <ext/pb_ds/tree_policy.hpp> # using namespace __gnu_pbds; # #define ll long long # #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> # #define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> M,M1=1000000007,998244353 def find(i,par): if par[i]!=i: par[i] = find(par[i],par) return par[i] def union(i,j,parent,rank): if rank[i]>rank[j]: parent[j] = i elif rank[i]<rank[j]: parent[i]= j else: parent[j] = i rank[i] += 1 def main(): how_much_noob_I_am = 1 # how_much_noob_I_am = inpu() for _ in range(1,how_much_noob_I_am+1): n = int(input()) arr = [] for _ in range(n): arr.append(lis()) if n == 1: if arr[0][0] == 0: print("YES") else: print("NO") exit() for i in range(n): if arr[i][i] != 0: print("NO") exit() for j in range(i + 1, n): if arr[i][j] != arr[j][i] or arr[i][j] == 0: print("NO") exit() for i in range(n): r = int(i == 0) for j in range(n): if arr[i][j] < arr[i][r] and i != j: r = j for k in range(n): if abs(arr[i][k] - arr[r][k]) != arr[r][i]: print("NO") exit() print("YES") if __name__ == '__main__': # sys.setrecursio0nlimit(2*10**5+50) # threading.stack_size(10**8) # threading.Thread(target=main).start() main()
Codeforces Round 270
CF
2,014
2
256
Design Tutorial: Inverse the Problem
There is an easy way to obtain a new task from an old one called "Inverse the problem": we give an output of the original task, and ask to generate an input, such that solution to the original problem will produce the output we provided. The hard task of Topcoder Open 2014 Round 2C, InverseRMQ, is a good example. Now let's create a task this way. We will use the task: you are given a tree, please calculate the distance between any pair of its nodes. Yes, it is very easy, but the inverse version is a bit harder: you are given an n × n distance matrix. Determine if it is the distance matrix of a weighted tree (all weights must be positive integers).
The first line contains an integer n (1 ≤ n ≤ 2000) — the number of nodes in that graph. Then next n lines each contains n integers di, j (0 ≤ di, j ≤ 109) — the distance between node i and node j.
If there exists such a tree, output "YES", otherwise output "NO".
null
In the first example, the required tree exists. It has one edge between nodes 1 and 2 with weight 2, another edge between nodes 1 and 3 with weight 7. In the second example, it is impossible because d1, 1 should be 0, but it is 1. In the third example, it is impossible because d1, 2 should equal d2, 1.
[{"input": "3\n0 2 7\n2 0 9\n7 9 0", "output": "YES"}, {"input": "3\n1 2 7\n2 0 9\n7 9 0", "output": "NO"}, {"input": "3\n0 2 2\n7 0 9\n7 9 0", "output": "NO"}, {"input": "3\n0 1 1\n1 0 1\n1 1 0", "output": "NO"}, {"input": "2\n0 0\n0 0", "output": "NO"}]
1,900
["dfs and similar", "dsu", "shortest paths", "trees"]
47
[{"input": "3\r\n0 2 7\r\n2 0 9\r\n7 9 0\r\n", "output": "YES\r\n"}, {"input": "3\r\n1 2 7\r\n2 0 9\r\n7 9 0\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 2 2\r\n7 0 9\r\n7 9 0\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 1 1\r\n1 0 1\r\n1 1 0\r\n", "output": "NO\r\n"}, {"input": "2\r\n0 0\r\n0 0\r\n", "output": "NO\r\n"}, {"input": "1\r\n0\r\n", "output": "YES\r\n"}, {"input": "1\r\n1\r\n", "output": "NO\r\n"}, {"input": "2\r\n0 1000000000\r\n1000000000 0\r\n", "output": "YES\r\n"}, {"input": "5\r\n0 0 0 0 0\r\n0 0 0 0 0\r\n0 0 0 0 0\r\n0 0 0 0 0\r\n0 0 0 0 0\r\n", "output": "NO\r\n"}, {"input": "2\r\n0 1\r\n1 0\r\n", "output": "YES\r\n"}, {"input": "4\r\n0 3 7 6\r\n3 0 4 9\r\n7 4 0 2\r\n6 9 2 0\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 1 2\r\n1 0 2\r\n2 2 0\r\n", "output": "NO\r\n"}, {"input": "3\r\n0 2 7\r\n2 0 10\r\n7 10 0\r\n", "output": "NO\r\n"}]
false
stdio
null
true
62/D
62
D
PyPy 3-64
TESTS
3
92
0
223030823
import sys N = 0 old_path = [] def read_input(): global N, old_path readline = sys.stdin.readline N, M = [int(w) for w in readline().split()] old_path = [int(w) for w in readline().split()] def solve(): def dfs(node: int, fit: bool)-> bool: # print(node, fit, stk) if len(stk) == len(old_path): if fit: return False else: nonlocal result result = stk[:] return True for child in graph[node]: if in_degree[child] == 0: continue if fit and old_path[len(stk)] == child: stk.append(child) in_degree[child] -= 1 if dfs(child, fit): return True in_degree[child] += 1 stk.pop() elif fit and old_path[len(stk)] > child: continue elif in_degree[child] > 0: stk.append(child) in_degree[child] -= 1 if dfs(child, False): return True in_degree[child] += 1 stk.pop() return False result = [] stk = [1] in_degree = [0] * (N + 1) graph = [[] for _ in range(N + 1)] for i in range(len(old_path) - 1): graph[old_path[i]].append(old_path[i + 1]) graph[old_path[i + 1]].append(old_path[i]) in_degree[old_path[i + 1]] += 1 dfs(1, True) return result def write_output(result: list): if result: print(*result) else: print('No solution') read_input() write_output(solve())
30
124
819,200
187019306
n,m = map(int,input().split()) m+=1 p = list(map(lambda x:int(x)-1,input().split())) a = [0]*m q = [[False]*n for i in range(n)] d = [[] for i in range(n)] for i in range(1,m): d[p[i]].append(p[i-1]) d[p[i-1]].append(p[i]) for i in range(n): d[i].sort() s = [(p[0],True,p[0])] l = 0 while s: v,f,vv = s[-1] if f is None: s.pop() l-=1 q[vv][v]=q[v][vv]=False continue q[vv][v]=q[v][vv]=True s[-1]=(v,None,vv) a[l]=v l+=1 if l==m: if f: continue else: break for u in reversed(d[v]): if f and u<p[l]: continue if q[u][v]: continue s.append((u,f and u==p[l],v)) if s: print(' '.join(map(lambda x:str(x+1),a))) else: print("No solution")
Codeforces Beta Round 58
CF
2,011
2
256
Wormhouse
Arnie the Worm has finished eating an apple house yet again and decided to move. He made up his mind on the plan, the way the rooms are located and how they are joined by corridors. He numbered all the rooms from 1 to n. All the corridors are bidirectional. Arnie wants the new house to look just like the previous one. That is, it should have exactly n rooms and, if a corridor from room i to room j existed in the old house, it should be built in the new one. We know that during the house constructing process Arnie starts to eat an apple starting from some room and only stops when he eats his way through all the corridors and returns to the starting room. It is also known that Arnie eats without stopping. That is, until Arnie finishes constructing the house, he is busy every moment of his time gnawing a new corridor. Arnie doesn't move along the already built corridors. However, gnawing out corridors in one and the same order any time you change a house is a very difficult activity. That's why Arnie, knowing the order in which the corridors were located in the previous house, wants to gnaw corridors in another order. It is represented as a list of rooms in the order in which they should be visited. The new list should be lexicographically smallest, but it also should be strictly lexicographically greater than the previous one. Help the worm.
The first line contains two integers n and m (3 ≤ n ≤ 100, 3 ≤ m ≤ 2000). It is the number of rooms and corridors in Arnie's house correspondingly. The next line contains m + 1 positive integers that do not exceed n. They are the description of Arnie's old path represented as a list of rooms he visited during the gnawing. It is guaranteed that the last number in the list coincides with the first one. The first room described in the list is the main entrance, that's why Arnie should begin gnawing from it. You may assume that there is no room which is connected to itself and there is at most one corridor between any pair of rooms. However, it is possible to find some isolated rooms which are disconnected from others.
Print m + 1 positive integers that do not exceed n. Those numbers are the description of the new path, according to which Arnie should gnaw out his new house. If it is impossible to find new path you should print out No solution. The first number in your answer should be equal to the last one. Also it should be equal to the main entrance.
null
null
[{"input": "3 3\n1 2 3 1", "output": "1 3 2 1"}, {"input": "3 3\n1 3 2 1", "output": "No solution"}]
2,300
["dfs and similar", "graphs"]
30
[{"input": "3 3\r\n1 2 3 1\r\n", "output": "1 3 2 1 "}, {"input": "3 3\r\n1 3 2 1\r\n", "output": "No solution"}, {"input": "4 4\r\n1 2 4 3 1\r\n", "output": "1 3 4 2 1 "}, {"input": "6 7\r\n3 2 4 1 6 5 1 3\r\n", "output": "No solution"}, {"input": "8 12\r\n4 6 5 1 4 3 1 8 3 7 8 5 4\r\n", "output": "4 6 5 1 4 3 1 8 7 3 8 5 4 "}, {"input": "5 6\r\n3 4 1 2 5 1 3\r\n", "output": "3 4 1 5 2 1 3 "}, {"input": "7 9\r\n3 2 7 3 5 1 2 6 1 3\r\n", "output": "3 2 7 3 5 1 6 2 1 3 "}, {"input": "6 7\r\n1 5 6 1 4 3 2 1\r\n", "output": "1 6 5 1 2 3 4 1 "}, {"input": "4 3\r\n1 2 3 1\r\n", "output": "1 3 2 1 "}, {"input": "10 40\r\n10 3 8 4 10 2 8 1 2 6 3 5 7 6 10 8 9 7 8 5 4 9 1 3 7 2 5 10 9 2 4 3 9 6 5 1 4 6 1 7 10\r\n", "output": "10 3 8 4 10 2 8 1 2 6 3 5 7 6 10 8 9 7 8 5 4 9 1 3 7 2 5 10 9 2 4 3 9 6 5 1 6 4 1 7 10 "}]
false
stdio
null
true
618/B
618
B
Python 3
TESTS
2
46
0
15690439
n=int(input()) a=[] for i in range(n): a.append([int(i) for i in input().split(' ')]) b=[-1 for i in range(n)] for i in range(n): cnum=set([]) for j in range(n): cnum.add(a[i][j]) if len(cnum)==n: for j in range(n): if a[i][j]==0: if j==n-1: b[i]=n-1 elif j==0: b[i]=n else: if a[i][j+1]==n-1: b[i]=n elif a[i][j-1]==n-1: b[i]=n-1 break else: b[i]=len(cnum)-1 for i in range(n): print(b[i],end=' ')
23
46
0
184312367
n = int(input()) for i in range(n): a = input() a = a.split() a = [int(x) for x in a] a_s = set(a) if len(a_s) == n: b = a.copy() b[a.index(0)] = n break for i in b: print(i, end=' ')
Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)
CF
2,016
2
256
Guess the Permutation
Bob has a permutation of integers from 1 to n. Denote this permutation as p. The i-th element of p will be denoted as pi. For all pairs of distinct integers i, j between 1 and n, he wrote the number ai, j = min(pi, pj). He writes ai, i = 0 for all integer i from 1 to n. Bob gave you all the values of ai, j that he wrote down. Your job is to reconstruct any permutation that could have generated these values. The input will be formed so that it is guaranteed that there is at least one solution that is consistent with the information given.
The first line of the input will contain a single integer n (2 ≤ n ≤ 50). The next n lines will contain the values of ai, j. The j-th number on the i-th line will represent ai, j. The i-th number on the i-th line will be 0. It's guaranteed that ai, j = aj, i and there is at least one solution consistent with the information given.
Print n space separated integers, which represents a permutation that could have generated these values. If there are multiple possible solutions, print any of them.
null
In the first case, the answer can be {1, 2} or {2, 1}. In the second case, another possible answer is {2, 4, 5, 1, 3}.
[{"input": "2\n0 1\n1 0", "output": "2 1"}, {"input": "5\n0 2 2 1 2\n2 0 4 1 3\n2 4 0 1 3\n1 1 1 0 1\n2 3 3 1 0", "output": "2 5 4 1 3"}]
1,100
["constructive algorithms"]
23
[{"input": "2\r\n0 1\r\n1 0\r\n", "output": "2 1\r\n"}, {"input": "5\r\n0 2 2 1 2\r\n2 0 4 1 3\r\n2 4 0 1 3\r\n1 1 1 0 1\r\n2 3 3 1 0\r\n", "output": "2 5 4 1 3\r\n"}, {"input": "10\r\n0 1 5 2 5 3 4 5 5 5\r\n1 0 1 1 1 1 1 1 1 1\r\n5 1 0 2 6 3 4 6 6 6\r\n2 1 2 0 2 2 2 2 2 2\r\n5 1 6 2 0 3 4 8 8 7\r\n3 1 3 2 3 0 3 3 3 3\r\n4 1 4 2 4 3 0 4 4 4\r\n5 1 6 2 8 3 4 0 9 7\r\n5 1 6 2 8 3 4 9 0 7\r\n5 1 6 2 7 3 4 7 7 0\r\n", "output": "5 1 6 2 8 3 4 10 9 7\r\n"}, {"input": "4\r\n0 1 3 2\r\n1 0 1 1\r\n3 1 0 2\r\n2 1 2 0\r\n", "output": "4 1 3 2\r\n"}, {"input": "7\r\n0 3 2 4 1 4 4\r\n3 0 2 3 1 3 3\r\n2 2 0 2 1 2 2\r\n4 3 2 0 1 5 5\r\n1 1 1 1 0 1 1\r\n4 3 2 5 1 0 6\r\n4 3 2 5 1 6 0\r\n", "output": "4 3 2 5 1 7 6\r\n"}, {"input": "10\r\n0 4 4 1 4 4 4 2 3 4\r\n4 0 5 1 6 8 9 2 3 7\r\n4 5 0 1 5 5 5 2 3 5\r\n1 1 1 0 1 1 1 1 1 1\r\n4 6 5 1 0 6 6 2 3 6\r\n4 8 5 1 6 0 8 2 3 7\r\n4 9 5 1 6 8 0 2 3 7\r\n2 2 2 1 2 2 2 0 2 2\r\n3 3 3 1 3 3 3 2 0 3\r\n4 7 5 1 6 7 7 2 3 0\r\n", "output": "4 10 5 1 6 8 9 2 3 7\r\n"}, {"input": "13\r\n0 5 5 2 5 4 5 5 3 5 5 5 1\r\n5 0 6 2 6 4 6 6 3 6 6 6 1\r\n5 6 0 2 10 4 7 10 3 8 10 9 1\r\n2 2 2 0 2 2 2 2 2 2 2 2 1\r\n5 6 10 2 0 4 7 12 3 8 11 9 1\r\n4 4 4 2 4 0 4 4 3 4 4 4 1\r\n5 6 7 2 7 4 0 7 3 7 7 7 1\r\n5 6 10 2 12 4 7 0 3 8 11 9 1\r\n3 3 3 2 3 3 3 3 0 3 3 3 1\r\n5 6 8 2 8 4 7 8 3 0 8 8 1\r\n5 6 10 2 11 4 7 11 3 8 0 9 1\r\n5 6 9 2 9 4 7 9 3 8 9 0 1\r\n1 1 1 1 1 1 1 1 1 1 1 1 0\r\n", "output": "5 6 10 2 13 4 7 12 3 8 11 9 1\r\n"}]
false
stdio
import sys def main(input_path, output_path, sub_path): with open(input_path) as f: lines = [line.strip() for line in f.readlines()] n = int(lines[0]) a = [list(map(int, line.split())) for line in lines[1:n+1]] with open(sub_path) as f: sub_line = f.read().strip() p = list(map(int, sub_line.split())) if len(p) != n or sorted(p) != list(range(1, n+1)): print(0) return for i in range(n): for j in range(n): if i != j and min(p[i], p[j]) != a[i][j]: print(0) return print(1) if __name__ == "__main__": input_path, output_path, sub_path = sys.argv[1:4] main(input_path, output_path, sub_path)
true
816/B
816
B
Python 3
TESTS
13
2,074
28,364,800
39247722
from sys import stdin,stdout n,k,q=map(int,input().split()) a=[[] for i in range(200001)];b=[0]*200001 for i in range(1,n+1): c,d=(int(x) for x in stdin.readline().split()) a[c].append(d);a[d].append(c) c=0 for i in range(1,200001): r=0 for j in a[i]: if j>i: c+=1 elif i==j: c+=1;r-=1 else: r-=1 b[i]=c c+=r c=0;d=[0]*200001 for i in range(200001): if b[i]>=k: c+=1 d[i]=c for i in range(q): c,r=(int(x) for x in stdin.readline().split()) stdout.write(str(d[r]-d[c-1])+'\n')
45
296
11,059,200
167938606
def main(): n, k, q = map(int, fin().split()) p = [0]*int(2e5+2) for i in range(n): l,r = map(int, fin().split()) p[l] += 1; p[r+1] -= 1 for i in range(1, int(2e5+2)): p[i] += p[i-1] for i in range(1, int(2e5+2)): if p[i] >= k: p[i] = p[i-1]+1 else: p[i] = p[i-1] for i in range(q): a, b = map(int, fin().split()) fout(p[b]-p[a-1]) # FastIO from sys import stdin, stdout def fin(): return stdin.readline().strip("\r\n") def fout(s): return stdout.write(str(s)+"\n") if __name__ == "__main__": t = 1 or int(fin()) for i in range(t): main()
Codeforces Round 419 (Div. 2)
CF
2,017
2.5
512
Karen and Coffee
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe". She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between li and ri degrees, inclusive, to achieve the optimal taste. Karen thinks that a temperature is admissible if at least k recipes recommend it. Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range?
The first line of input contains three integers, n, k (1 ≤ k ≤ n ≤ 200000), and q (1 ≤ q ≤ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively. The next n lines describe the recipes. Specifically, the i-th line among these contains two integers li and ri (1 ≤ li ≤ ri ≤ 200000), describing that the i-th recipe suggests that the coffee be brewed between li and ri degrees, inclusive. The next q lines describe the questions. Each of these lines contains a and b, (1 ≤ a ≤ b ≤ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive.
For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive.
null
In the first test case, Karen knows 3 recipes. 1. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive. 2. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive. 3. The third one recommends brewing the coffee between 97 and 99 degrees, inclusive. A temperature is admissible if at least 2 recipes recommend it. She asks 4 questions. In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible. In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible. In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none. In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible. In the second test case, Karen knows 2 recipes. 1. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree. 2. The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees. A temperature is admissible if at least 1 recipe recommends it. In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none.
[{"input": "3 2 4\n91 94\n92 97\n97 99\n92 94\n93 97\n95 96\n90 100", "output": "3\n3\n0\n4"}, {"input": "2 1 1\n1 1\n200000 200000\n90 100", "output": "0"}]
1,400
["binary search", "data structures", "implementation"]
45
[{"input": "3 2 4\r\n91 94\r\n92 97\r\n97 99\r\n92 94\r\n93 97\r\n95 96\r\n90 100\r\n", "output": "3\r\n3\r\n0\r\n4\r\n"}, {"input": "2 1 1\r\n1 1\r\n200000 200000\r\n90 100\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n1 1\r\n1 1\r\n", "output": "1\r\n"}, {"input": "1 1 1\r\n200000 200000\r\n200000 200000\r\n", "output": "1\r\n"}]
false
stdio
null
true
618/B
618
B
Python 3
PRETESTS
2
61
0
15661337
n = int(input()) a = [list(map(int, input().split())) for _ in range(n)] res = list() perm = list() for i in range(n): res.append(len({a[i][j] for j in range(n)}) - 1) if i >= 1 and res[i] == res[i-1]: perm.append((i, i-1, res[i])) res[i] += 1 wrong = False for x in range(i+1): if min(res[i], res[x]) != a[i][x]: wrong = True break if wrong: res[i] -= 1 res[i-1] += 1 print(" ".join(map(str, res)))
23
46
102,400
145463044
from collections import Counter n = int(input()) p = [0] * n hash_map = {} found = set() for i in range(1, n + 1): hash_map[i] = n - i for i in range(n): array = list(map(int, input().split())) count = Counter(array) for k, v in count.items(): for k1, v2 in hash_map.items(): if 1 < v2 == v and v > 1: p[i] = k while 0 in p: idx = p.index(0) if n not in p: p[idx] = n else: p[idx] = n - 1 print(*p)
Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)
CF
2,016
2
256
Guess the Permutation
Bob has a permutation of integers from 1 to n. Denote this permutation as p. The i-th element of p will be denoted as pi. For all pairs of distinct integers i, j between 1 and n, he wrote the number ai, j = min(pi, pj). He writes ai, i = 0 for all integer i from 1 to n. Bob gave you all the values of ai, j that he wrote down. Your job is to reconstruct any permutation that could have generated these values. The input will be formed so that it is guaranteed that there is at least one solution that is consistent with the information given.
The first line of the input will contain a single integer n (2 ≤ n ≤ 50). The next n lines will contain the values of ai, j. The j-th number on the i-th line will represent ai, j. The i-th number on the i-th line will be 0. It's guaranteed that ai, j = aj, i and there is at least one solution consistent with the information given.
Print n space separated integers, which represents a permutation that could have generated these values. If there are multiple possible solutions, print any of them.
null
In the first case, the answer can be {1, 2} or {2, 1}. In the second case, another possible answer is {2, 4, 5, 1, 3}.
[{"input": "2\n0 1\n1 0", "output": "2 1"}, {"input": "5\n0 2 2 1 2\n2 0 4 1 3\n2 4 0 1 3\n1 1 1 0 1\n2 3 3 1 0", "output": "2 5 4 1 3"}]
1,100
["constructive algorithms"]
23
[{"input": "2\r\n0 1\r\n1 0\r\n", "output": "2 1\r\n"}, {"input": "5\r\n0 2 2 1 2\r\n2 0 4 1 3\r\n2 4 0 1 3\r\n1 1 1 0 1\r\n2 3 3 1 0\r\n", "output": "2 5 4 1 3\r\n"}, {"input": "10\r\n0 1 5 2 5 3 4 5 5 5\r\n1 0 1 1 1 1 1 1 1 1\r\n5 1 0 2 6 3 4 6 6 6\r\n2 1 2 0 2 2 2 2 2 2\r\n5 1 6 2 0 3 4 8 8 7\r\n3 1 3 2 3 0 3 3 3 3\r\n4 1 4 2 4 3 0 4 4 4\r\n5 1 6 2 8 3 4 0 9 7\r\n5 1 6 2 8 3 4 9 0 7\r\n5 1 6 2 7 3 4 7 7 0\r\n", "output": "5 1 6 2 8 3 4 10 9 7\r\n"}, {"input": "4\r\n0 1 3 2\r\n1 0 1 1\r\n3 1 0 2\r\n2 1 2 0\r\n", "output": "4 1 3 2\r\n"}, {"input": "7\r\n0 3 2 4 1 4 4\r\n3 0 2 3 1 3 3\r\n2 2 0 2 1 2 2\r\n4 3 2 0 1 5 5\r\n1 1 1 1 0 1 1\r\n4 3 2 5 1 0 6\r\n4 3 2 5 1 6 0\r\n", "output": "4 3 2 5 1 7 6\r\n"}, {"input": "10\r\n0 4 4 1 4 4 4 2 3 4\r\n4 0 5 1 6 8 9 2 3 7\r\n4 5 0 1 5 5 5 2 3 5\r\n1 1 1 0 1 1 1 1 1 1\r\n4 6 5 1 0 6 6 2 3 6\r\n4 8 5 1 6 0 8 2 3 7\r\n4 9 5 1 6 8 0 2 3 7\r\n2 2 2 1 2 2 2 0 2 2\r\n3 3 3 1 3 3 3 2 0 3\r\n4 7 5 1 6 7 7 2 3 0\r\n", "output": "4 10 5 1 6 8 9 2 3 7\r\n"}, {"input": "13\r\n0 5 5 2 5 4 5 5 3 5 5 5 1\r\n5 0 6 2 6 4 6 6 3 6 6 6 1\r\n5 6 0 2 10 4 7 10 3 8 10 9 1\r\n2 2 2 0 2 2 2 2 2 2 2 2 1\r\n5 6 10 2 0 4 7 12 3 8 11 9 1\r\n4 4 4 2 4 0 4 4 3 4 4 4 1\r\n5 6 7 2 7 4 0 7 3 7 7 7 1\r\n5 6 10 2 12 4 7 0 3 8 11 9 1\r\n3 3 3 2 3 3 3 3 0 3 3 3 1\r\n5 6 8 2 8 4 7 8 3 0 8 8 1\r\n5 6 10 2 11 4 7 11 3 8 0 9 1\r\n5 6 9 2 9 4 7 9 3 8 9 0 1\r\n1 1 1 1 1 1 1 1 1 1 1 1 0\r\n", "output": "5 6 10 2 13 4 7 12 3 8 11 9 1\r\n"}]
false
stdio
import sys def main(input_path, output_path, sub_path): with open(input_path) as f: lines = [line.strip() for line in f.readlines()] n = int(lines[0]) a = [list(map(int, line.split())) for line in lines[1:n+1]] with open(sub_path) as f: sub_line = f.read().strip() p = list(map(int, sub_line.split())) if len(p) != n or sorted(p) != list(range(1, n+1)): print(0) return for i in range(n): for j in range(n): if i != j and min(p[i], p[j]) != a[i][j]: print(0) return print(1) if __name__ == "__main__": input_path, output_path, sub_path = sys.argv[1:4] main(input_path, output_path, sub_path)
true
665/B
665
B
PyPy 3-64
TESTS
1
30
0
167703286
import sys input = sys.stdin.readline n, m, k = map(int, input().split()) w = list(map(int, input().split())) d = [0]*(k+1) for i in range(k): d[w[i]] = i+1 c = 0 for _ in range(n): q = list(map(int, input().split())) for i in q: c += d[i] for j in range(1, k): if d[j] < d[i]: d[j] += 1 d[i] = 1 print(c)
10
62
2,048,000
167704599
import sys input = sys.stdin.readline n, m, k = map(int, input().split()) d = list(map(int, input().split())) x = [0]*(k+1) for i in range(k): x[d[i]] = i+1 c = 0 for _ in range(n): q = list(map(int, input().split())) for i in q: a = x[i] - 1 c += a+1 d = [d[a]] + d[:a] + d[a+1:] for j in range(a+1): x[d[j]] = j+1 print(c)
Educational Codeforces Round 12
ICPC
2,016
1
256
Shopping
Ayush is a cashier at the shopping center. Recently his department has started a ''click and collect" service which allows users to shop online. The store contains k items. n customers have already used the above service. Each user paid for m items. Let aij denote the j-th item in the i-th person's order. Due to the space limitations all the items are arranged in one single row. When Ayush receives the i-th order he will find one by one all the items aij (1 ≤ j ≤ m) in the row. Let pos(x) denote the position of the item x in the row at the moment of its collection. Then Ayush takes time equal to pos(ai1) + pos(ai2) + ... + pos(aim) for the i-th customer. When Ayush accesses the x-th element he keeps a new stock in the front of the row and takes away the x-th element. Thus the values are updating. Your task is to calculate the total time it takes for Ayush to process all the orders. You can assume that the market has endless stock.
The first line contains three integers n, m and k (1 ≤ n, k ≤ 100, 1 ≤ m ≤ k) — the number of users, the number of items each user wants to buy and the total number of items at the market. The next line contains k distinct integers pl (1 ≤ pl ≤ k) denoting the initial positions of the items in the store. The items are numbered with integers from 1 to k. Each of the next n lines contains m distinct integers aij (1 ≤ aij ≤ k) — the order of the i-th person.
Print the only integer t — the total time needed for Ayush to process all the orders.
null
Customer 1 wants the items 1 and 5. pos(1) = 3, so the new positions are: [1, 3, 4, 2, 5]. pos(5) = 5, so the new positions are: [5, 1, 3, 4, 2]. Time taken for the first customer is 3 + 5 = 8. Customer 2 wants the items 3 and 1. pos(3) = 3, so the new positions are: [3, 5, 1, 4, 2]. pos(1) = 3, so the new positions are: [1, 3, 5, 4, 2]. Time taken for the second customer is 3 + 3 = 6. Total time is 8 + 6 = 14. Formally pos(x) is the index of x in the current row.
[{"input": "2 2 5\n3 4 1 2 5\n1 5\n3 1", "output": "14"}]
1,400
["brute force"]
10
[{"input": "2 2 5\r\n3 4 1 2 5\r\n1 5\r\n3 1\r\n", "output": "14\r\n"}, {"input": "4 4 4\r\n1 2 3 4\r\n3 4 2 1\r\n4 3 2 1\r\n4 1 2 3\r\n4 1 2 3\r\n", "output": "59\r\n"}, {"input": "1 1 1\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "10 1 100\r\n1 55 67 75 40 86 24 84 82 26 81 23 70 79 51 54 21 78 31 98 68 93 66 88 99 65 20 52 35 85 16 12 94 100 59 56 18 33 47 46 71 8 38 57 2 92 3 95 6 4 87 22 48 80 15 29 11 45 72 76 44 60 91 90 39 74 41 36 13 27 53 83 32 5 30 63 89 64 49 17 9 97 69 14 50 77 37 96 10 42 28 34 61 19 73 7 62 43 58 25\r\n33\r\n69\r\n51\r\n7\r\n68\r\n70\r\n1\r\n35\r\n24\r\n7\r\n", "output": "335\r\n"}, {"input": "100 1 1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n", "output": "100\r\n"}, {"input": "3 2 3\r\n3 1 2\r\n1 2\r\n2 1\r\n2 3\r\n", "output": "13\r\n"}, {"input": "10 10 10\r\n3 4 1 2 8 9 5 10 6 7\r\n9 10 7 8 6 1 2 3 4 5\r\n2 5 3 6 1 4 9 7 8 10\r\n2 9 1 8 4 7 5 10 6 3\r\n10 9 7 1 3 6 2 8 5 4\r\n2 5 1 3 7 10 4 9 8 6\r\n6 1 8 7 9 2 3 5 4 10\r\n1 3 2 8 6 9 4 10 5 7\r\n5 2 4 8 6 1 10 9 3 7\r\n5 1 7 10 4 6 2 8 9 3\r\n2 1 3 9 7 10 6 4 8 5\r\n", "output": "771\r\n"}]
false
stdio
null
true
500/C
500
C
PyPy 3-64
TESTS
1
61
0
155356502
n, m = map(int, input().split()) w = list(map(int, input().split())) b = list(map(int, input().split())) s, l = set(), [] for x in b: if x not in s: l.append(x) s.add(x) for j in range(1, n + 1): if j not in s: l.append(s) ans = 0 for x in b: i = l.index(x) ans += sum(l[: i]) l = [x] + l[: i] + l[i + 1:] #print(l) print(ans)
35
62
2,150,400
200815663
import sys input = lambda: sys.stdin.readline().rstrip() N,M = map(int, input().split()) W = list(map(int, input().split())) B = list(map(int, input().split())) seen = [0]*(N+1) A = [] for b in B: if seen[b]==0: A.append(b) seen[b]=1 ans = 0 for b in B: cnt = 0 for i in range(N): if A[i]==b: ans+=cnt A = [b]+A[:i]+A[i+1:] break else: cnt+=W[A[i]-1] print(ans)
Good Bye 2014
CF
2,014
2
256
New Year Book Reading
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 ≤ i ≤ n) book is wi. As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below. 1. He lifts all the books above book x. 2. He pushes book x out of the stack. 3. He puts down the lifted books without changing their order. 4. After reading book x, he puts book x on the top of the stack. He decided to read books for m days. In the j-th (1 ≤ j ≤ m) day, he will read the book that is numbered with integer bj (1 ≤ bj ≤ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times. After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?
The first line contains two space-separated integers n (2 ≤ n ≤ 500) and m (1 ≤ m ≤ 1000) — the number of books, and the number of days for which Jaehyun would read books. The second line contains n space-separated integers w1, w2, ..., wn (1 ≤ wi ≤ 100) — the weight of each book. The third line contains m space separated integers b1, b2, ..., bm (1 ≤ bj ≤ n) — the order of books that he would read. Note that he can read the same book more than once.
Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.
null
Here's a picture depicting the example. Each vertical column presents the stacked books.
[{"input": "3 5\n1 2 3\n1 3 2 3 1", "output": "12"}]
1,600
["constructive algorithms", "greedy", "implementation", "math"]
35
[{"input": "3 5\r\n1 2 3\r\n1 3 2 3 1\r\n", "output": "12\r\n"}, {"input": "3 3\r\n10 20 30\r\n1 2 3\r\n", "output": "40\r\n"}, {"input": "2 2\r\n10 12\r\n2 1\r\n", "output": "12\r\n"}, {"input": "10 10\r\n61 59 97 16 2 94 57 48 91 93\r\n2 8 6 5 3 1 3 4 9 10\r\n", "output": "2137\r\n"}, {"input": "50 50\r\n75 71 23 37 28 23 69 75 5 62 3 11 96 100 13 50 57 51 8 90 4 6 84 27 11 89 95 81 10 62 48 52 69 87 97 95 30 74 21 42 36 64 31 80 81 50 56 53 33 99\r\n26 30 5 33 35 29 6 15 36 17 32 16 14 1 29 34 22 40 12 42 38 48 39 50 13 47 18 43 10 8 49 45 11 31 21 37 46 28 20 41 2 7 9 24 27 23 3 44 15 14\r\n", "output": "63929\r\n"}, {"input": "50 60\r\n86 57 45 93 17 12 40 10 47 80 18 80 3 9 6 55 13 99 5 76 4 70 100 55 27 91 71 3 65 93 41 74 80 56 90 50 58 13 71 9 47 52 26 73 72 21 15 81 88 28\r\n40 32 5 16 49 23 3 17 14 10 1 15 1 21 28 22 13 45 12 25 44 48 46 32 36 43 11 8 49 7 7 35 10 14 39 4 42 10 30 27 1 17 31 15 8 41 44 33 25 26 19 18 29 37 50 6 36 38 47 9\r\n", "output": "62514\r\n"}, {"input": "2 1\r\n1 2\r\n1\r\n", "output": "0\r\n"}, {"input": "2 3\r\n20 30\r\n1 1 1\r\n", "output": "0\r\n"}, {"input": "2 7\r\n20 30\r\n1 1 1 2 2 2 2\r\n", "output": "20\r\n"}, {"input": "2 10\r\n39 26\r\n1 1 2 2 2 2 2 2 1 2\r\n", "output": "104\r\n"}, {"input": "5 1\r\n16 87 36 16 81\r\n3\r\n", "output": "0\r\n"}]
false
stdio
null
true
500/C
500
C
PyPy 3-64
TESTS
1
62
0
185165980
import sys input = sys.stdin.readline from collections import deque n, m = map(int, input().split()) w = list(map(int, input().split())) s = list(map(int, input().split())) d = [0]*(n+1) f = deque([]) c = 0 for i in s: if d[i] == 0: d[i] = 1 c += sum(f) f.appendleft(i) else: for j in f: if j != i: c += w[j-1] else: break f.remove(i) f.appendleft(i) print(c)
35
77
2,252,800
192615660
n,m = list(map(int,input().split())) w = list(map(int,input().split())) a = list(map(int,input().split())) li = [] vis = [False]*n for i in range(m): if vis[a[i] - 1] == False: li.append(a[i] - 1) vis[a[i] - 1] = True ans = 0 for i in range(m): weigh = 0 for j in range(len(li)): if li[j] != a[i] - 1: weigh += w[li[j]] else: ans += weigh li = [li[j]] + li[:j] + li[j+1:] break print(ans)
Good Bye 2014
CF
2,014
2
256
New Year Book Reading
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 ≤ i ≤ n) book is wi. As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below. 1. He lifts all the books above book x. 2. He pushes book x out of the stack. 3. He puts down the lifted books without changing their order. 4. After reading book x, he puts book x on the top of the stack. He decided to read books for m days. In the j-th (1 ≤ j ≤ m) day, he will read the book that is numbered with integer bj (1 ≤ bj ≤ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times. After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?
The first line contains two space-separated integers n (2 ≤ n ≤ 500) and m (1 ≤ m ≤ 1000) — the number of books, and the number of days for which Jaehyun would read books. The second line contains n space-separated integers w1, w2, ..., wn (1 ≤ wi ≤ 100) — the weight of each book. The third line contains m space separated integers b1, b2, ..., bm (1 ≤ bj ≤ n) — the order of books that he would read. Note that he can read the same book more than once.
Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.
null
Here's a picture depicting the example. Each vertical column presents the stacked books.
[{"input": "3 5\n1 2 3\n1 3 2 3 1", "output": "12"}]
1,600
["constructive algorithms", "greedy", "implementation", "math"]
35
[{"input": "3 5\r\n1 2 3\r\n1 3 2 3 1\r\n", "output": "12\r\n"}, {"input": "3 3\r\n10 20 30\r\n1 2 3\r\n", "output": "40\r\n"}, {"input": "2 2\r\n10 12\r\n2 1\r\n", "output": "12\r\n"}, {"input": "10 10\r\n61 59 97 16 2 94 57 48 91 93\r\n2 8 6 5 3 1 3 4 9 10\r\n", "output": "2137\r\n"}, {"input": "50 50\r\n75 71 23 37 28 23 69 75 5 62 3 11 96 100 13 50 57 51 8 90 4 6 84 27 11 89 95 81 10 62 48 52 69 87 97 95 30 74 21 42 36 64 31 80 81 50 56 53 33 99\r\n26 30 5 33 35 29 6 15 36 17 32 16 14 1 29 34 22 40 12 42 38 48 39 50 13 47 18 43 10 8 49 45 11 31 21 37 46 28 20 41 2 7 9 24 27 23 3 44 15 14\r\n", "output": "63929\r\n"}, {"input": "50 60\r\n86 57 45 93 17 12 40 10 47 80 18 80 3 9 6 55 13 99 5 76 4 70 100 55 27 91 71 3 65 93 41 74 80 56 90 50 58 13 71 9 47 52 26 73 72 21 15 81 88 28\r\n40 32 5 16 49 23 3 17 14 10 1 15 1 21 28 22 13 45 12 25 44 48 46 32 36 43 11 8 49 7 7 35 10 14 39 4 42 10 30 27 1 17 31 15 8 41 44 33 25 26 19 18 29 37 50 6 36 38 47 9\r\n", "output": "62514\r\n"}, {"input": "2 1\r\n1 2\r\n1\r\n", "output": "0\r\n"}, {"input": "2 3\r\n20 30\r\n1 1 1\r\n", "output": "0\r\n"}, {"input": "2 7\r\n20 30\r\n1 1 1 2 2 2 2\r\n", "output": "20\r\n"}, {"input": "2 10\r\n39 26\r\n1 1 2 2 2 2 2 2 1 2\r\n", "output": "104\r\n"}, {"input": "5 1\r\n16 87 36 16 81\r\n3\r\n", "output": "0\r\n"}]
false
stdio
null
true
504/D
504
D
PyPy 3
TESTS
0
139
2,048,000
72290029
buck = [[0, 0] for i in range(2500)] m = int(input()) for i in range(m): a = int(input()) ok = True br = 1 for j in range(2500, -1, -1): if a & (1 << j): if(buck[j][0]): a ^= buck[j][0] br ^= buck[j][1] else: ok = False buck[j][0] = a buck[j][1] = br | (1 << i) break if not ok: print("0") else: lst = [] for j in range(2501): if br & (1 << j): lst.append(j) print(len(lst), end = ' ') for j in lst: print(j, end = ' ') print('\n', end='')
52
1,107
12,492,800
153501116
m = int(input()) base = [] for i in range(m): a = int(input()) need=0 for v,bitset in base: if a^v<a: need^=bitset a^=v if a: print(0) base.append((a,need^(1<<i))) else: res = [] for d in range(i): if need&(1<<d): res.append(d) print(len(res), *res)
Codeforces Round 285 (Div. 1)
CF
2,015
2
256
Misha and XOR
After Misha's birthday he had many large numbers left, scattered across the room. Now it's time to clean up and Misha needs to put them in a basket. He ordered this task to his pet robot that agreed to complete the task at certain conditions. Before the robot puts a number x to the basket, Misha should answer the question: is it possible to choose one or multiple numbers that already are in the basket, such that their XOR sum equals x? If the answer is positive, you also need to give the indexes of these numbers. If there are multiple options of choosing numbers, you are allowed to choose any correct option. After Misha's answer the robot puts the number to the basket. Initially the basket is empty. Each integer you put in the basket takes some number. The first integer you put into the basket take number 0, the second integer takes number 1 and so on. Misha needs to clean up the place as soon as possible but unfortunately, he isn't that good at mathematics. He asks you to help him.
The first line contains number m (1 ≤ m ≤ 2000), showing how many numbers are scattered around the room. The next m lines contain the numbers in the order in which the robot puts them in the basket. Each number is a positive integer strictly less than 10600 that doesn't contain leading zeroes.
For each number either print a 0 on the corresponding line, if the number cannot be represented as a XOR sum of numbers that are in the basket, or print integer k showing how many numbers are in the representation and the indexes of these numbers. Separate the numbers by spaces. Each number can occur in the representation at most once.
null
The XOR sum of numbers is the result of bitwise sum of numbers modulo 2.
[{"input": "7\n7\n6\n5\n4\n3\n2\n1", "output": "0\n0\n0\n3 0 1 2\n2 1 2\n2 0 2\n2 0 1"}, {"input": "2\n5\n5", "output": "0\n1 0"}]
2,700
["bitmasks"]
52
[{"input": "7\r\n7\r\n6\r\n5\r\n4\r\n3\r\n2\r\n1\r\n", "output": "0\r\n0\r\n0\r\n3 0 1 2\r\n2 1 2\r\n2 0 2\r\n2 0 1\r\n"}, {"input": "2\r\n5\r\n5\r\n", "output": "0\r\n1 0\r\n"}, {"input": "10\r\n81\r\n97\r\n12\r\n2\r\n16\r\n96\r\n80\r\n99\r\n6\r\n83\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n3 0 1 5\r\n2 1 3\r\n0\r\n2 0 3\r\n"}, {"input": "10\r\n15106\r\n13599\r\n69319\r\n33224\r\n26930\r\n94490\r\n85089\r\n60931\r\n23137\r\n62868\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n"}, {"input": "10\r\n5059464500\r\n8210395556\r\n3004213265\r\n248593357\r\n5644084048\r\n9359824793\r\n8120649160\r\n4288978422\r\n183848555\r\n8135845959\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n"}, {"input": "10\r\n4\r\n12\r\n28\r\n29\r\n31\r\n31\r\n31\r\n31\r\n31\r\n31\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n1 4\r\n1 4\r\n1 4\r\n1 4\r\n1 4\r\n"}, {"input": "10\r\n16\r\n24\r\n28\r\n30\r\n31\r\n31\r\n31\r\n31\r\n31\r\n31\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n1 4\r\n1 4\r\n1 4\r\n1 4\r\n1 4\r\n"}, {"input": "10\r\n16\r\n8\r\n4\r\n2\r\n1\r\n31\r\n31\r\n31\r\n31\r\n31\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n5 0 1 2 3 4\r\n5 0 1 2 3 4\r\n5 0 1 2 3 4\r\n5 0 1 2 3 4\r\n5 0 1 2 3 4\r\n"}, {"input": "10\r\n1\r\n2\r\n4\r\n8\r\n16\r\n31\r\n31\r\n31\r\n31\r\n31\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n5 0 1 2 3 4\r\n5 0 1 2 3 4\r\n5 0 1 2 3 4\r\n5 0 1 2 3 4\r\n5 0 1 2 3 4\r\n"}]
false
stdio
null
true
1009/G
1009
G
Python 3
TESTS
1
93
0
43482751
a = input() n = int(input()) m = len(a) se={} for z in range(n): p,q = input().split(" ") p = int(p)-1 s = [] for i in q: if i in a: s.append(i) s.sort() se[p]=s[0] stri="" if len(se)<=0: print("Impossible") exit() for i in se.keys(): j = a.find(se[i]) a = a[:j]+a[j+1:] a = sorted(a) j = 0 for i in range(m): if i in se.keys(): stri = stri + se[i] else: stri = stri + a[j] j = j+1 print(stri)
57
233
10,137,600
167672037
import sys, os, io input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline s = list(input().rstrip()) pow2 = [1] for _ in range(6): pow2.append(2 * pow2[-1]) p = pow2[6] cnt = [0] * p for i in s: cnt[pow2[i - 97]] += 1 sp = set(pow2) u = [[pow2[i]] for i in range(6)] for i in range(1, p): if i in sp: continue for j in range(6): pj = pow2[j] if i & pj: cnt[i] += cnt[pj] u[j].append(i) n = len(s) y = [p - 1] * n m = int(input()) for _ in range(m): pos, s = input().rstrip().split() x = 0 for i in s: x ^= pow2[i - 97] y[int(pos) - 1] = x c = [0] * p for i in y: c[i] += 1 v = [[i] for i in range(p)] for i in range(p - 1, -1, -1): ci = c[i] for j in range(i + 1, p): if i & j == i: c[j] += ci v[i].append(j) ans = [] for i in y: for j in v[i]: c[j] -= 1 ok = 0 for j in range(6): pj = pow2[j] if not i & pj or not cnt[pj]: continue f = 1 for k in range(1, p): if c[k] > cnt[k] - min(1, k & pj): f = 0 break if f: ok = 1 ans.append(chr(97 + j)) for k in u[j]: cnt[k] -= 1 break if not ok: ans = ["Impossible"] break sys.stdout.write("".join(ans))
Educational Codeforces Round 47 (Rated for Div. 2)
ICPC
2,018
2
256
Allowed Letters
Polycarp has just launched his new startup idea. The niche is pretty free and the key vector of development sounds really promising, so he easily found himself some investors ready to sponsor the company. However, he is yet to name the startup! Actually, Polycarp has already came up with the name but some improvement to it will never hurt. So now he wants to swap letters at some positions in it to obtain the better name. It isn't necessary for letters to be adjacent. In addition, each of the investors has chosen some index in the name and selected a set of letters that can go there. Indices chosen by different investors are pairwise distinct. If some indices aren't chosen by any investor then any letter can go there. Finally, Polycarp is sure that the smallest lexicographically name is the best. (Like why do you think Google decided to become Alphabet?) More formally, you are given a string consisting of lowercase Latin letters from "a" to "f". You can swap letters at any positions arbitrary number of times (zero swaps is also possible). What is the smallest lexicographically name you can obtain such that the letter at every position is among the allowed letters? If Polycarp can't produce any valid name then print "Impossible".
The first line is the string $$$s$$$ ($$$1 \le |s| \le 10^5$$$) — the name Polycarp has came up with. The string consists only of lowercase Latin letters from "a" to "f". The second line contains a single integer $$$m$$$ ($$$0 \le m \le |s|$$$) — the number of investors. The $$$i$$$-th of the next $$$m$$$ lines contain an integer number $$$pos_i$$$ and a non-empty string of allowed characters for $$$pos_i$$$ ($$$1 \le pos_i \le |s|$$$). Each string contains pairwise distinct letters from "a" to "f". $$$pos_1, pos_2, \dots, pos_m$$$ are pairwise distinct. If any position of the string doesn't appear in the investors demands then any letter can go in this position.
If Polycarp can't produce any valid name then print "Impossible". Otherwise print the smallest lexicographically name Polycarp can obtain by swapping letters in string $$$s$$$ such that the letter at every position is among the allowed ones.
null
null
[{"input": "bedefead\n5\n2 e\n1 dc\n5 b\n7 ef\n6 ef", "output": "deadbeef"}, {"input": "abacaba\n0", "output": "aaaabbc"}, {"input": "fc\n2\n1 cfab\n2 f", "output": "cf"}]
2,400
["bitmasks", "flows", "graph matchings", "graphs", "greedy"]
57
[{"input": "bedefead\r\n5\r\n2 e\r\n1 dc\r\n5 b\r\n7 ef\r\n6 ef\r\n", "output": "deadbeef\r\n"}, {"input": "abacaba\r\n0\r\n", "output": "aaaabbc\r\n"}, {"input": "fc\r\n2\r\n1 cfab\r\n2 f\r\n", "output": "cf\r\n"}, {"input": "bbcbbc\r\n6\r\n1 c\r\n2 c\r\n3 b\r\n4 ab\r\n5 ab\r\n6 ab\r\n", "output": "ccbbbb\r\n"}, {"input": "abcdefffffffffffffff\r\n5\r\n20 abcde\r\n19 abcde\r\n18 abcde\r\n17 abcde\r\n16 abcde\r\n", "output": "fffffffffffffffabcde\r\n"}, {"input": "abcdefffffffffffffff\r\n20\r\n1 acf\r\n2 cdef\r\n3 ef\r\n4 def\r\n5 adef\r\n6 acdef\r\n7 bdef\r\n8 abdf\r\n9 bcdf\r\n10 abf\r\n11 abf\r\n12 bcdf\r\n13 df\r\n14 df\r\n15 abcdf\r\n16 abcde\r\n17 abcde\r\n18 abcde\r\n19 abcde\r\n20 abcde\r\n", "output": "fffffffffffffffabcde\r\n"}, {"input": "aaeff\r\n5\r\n2 afbdce\r\n5 c\r\n1 dbc\r\n4 afcbde\r\n3 ef\r\n", "output": "Impossible\r\n"}, {"input": "cdff\r\n1\r\n2 ae\r\n", "output": "Impossible\r\n"}, {"input": "dfb\r\n2\r\n1 c\r\n3 cae\r\n", "output": "Impossible\r\n"}, {"input": "cefe\r\n2\r\n4 ca\r\n1 da\r\n", "output": "Impossible\r\n"}, {"input": "cdccc\r\n5\r\n2 fae\r\n1 dabc\r\n4 dcfabe\r\n3 abc\r\n5 bdcafe\r\n", "output": "Impossible\r\n"}, {"input": "bdc\r\n3\r\n1 f\r\n3 fdacb\r\n2 eb\r\n", "output": "Impossible\r\n"}, {"input": "effa\r\n3\r\n3 ca\r\n2 bd\r\n4 abfdce\r\n", "output": "Impossible\r\n"}, {"input": "bfd\r\n2\r\n2 aecf\r\n3 dfb\r\n", "output": "bfd\r\n"}, {"input": "bfb\r\n3\r\n1 f\r\n3 acdef\r\n2 cdefab\r\n", "output": "Impossible\r\n"}, {"input": "fce\r\n3\r\n3 abdecf\r\n1 efdcba\r\n2 ac\r\n", "output": "ecf\r\n"}, {"input": "ded\r\n1\r\n2 aedc\r\n", "output": "dde\r\n"}, {"input": "a\r\n1\r\n1 b\r\n", "output": "Impossible\r\n"}]
false
stdio
null
true
430/A
430
A
Python 3
PRETESTS
2
61
0
6593116
n, m = map(int, input().split()) points = input().split() segments = [input().split() for _ in range(m)] print('1 0 ' * (n // 2) + ('1' if n % 2 else ''))
12
61
0
10409824
R = lambda: list(map(int, input().split())) n, m = R() a = R() exist = [False for i in range(200)] for i in range(m): l, r = R() for j in a: if l <= j <= r: exist[j] = True b = a[:] b.sort() ans = [0 for i in range(200)] flag = 0 for i in b: if exist[i]: ans[i]=flag flag ^= 1 for i in a: print(ans[i], end=' ')
Codeforces Round 245 (Div. 2)
CF
2,014
1
256
Points and Segments (easy)
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following. Iahub wants to draw n distinct points and m segments on the OX axis. He can draw each point with either red or blue. The drawing is good if and only if the following requirement is met: for each segment [li, ri] consider all the red points belong to it (ri points), and all the blue points belong to it (bi points); each segment i should satisfy the inequality |ri - bi| ≤ 1. Iahub thinks that point x belongs to segment [l, r], if inequality l ≤ x ≤ r holds. Iahub gives to you all coordinates of points and segments. Please, help him to find any good drawing.
The first line of input contains two integers: n (1 ≤ n ≤ 100) and m (1 ≤ m ≤ 100). The next line contains n space-separated integers x1, x2, ..., xn (0 ≤ xi ≤ 100) — the coordinates of the points. The following m lines contain the descriptions of the m segments. Each line contains two integers li and ri (0 ≤ li ≤ ri ≤ 100) — the borders of the i-th segment. It's guaranteed that all the points are distinct.
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers, each integer must be 0 or 1. The i-th number denotes the color of the i-th point (0 is red, and 1 is blue). If there are multiple good drawings you can output any of them.
null
null
[{"input": "3 3\n3 7 14\n1 5\n6 10\n11 15", "output": "0 0 0"}, {"input": "3 4\n1 2 3\n1 2\n2 3\n5 6\n2 2", "output": "1 0 1"}]
1,600
["constructive algorithms", "sortings"]
12
[{"input": "3 3\r\n3 7 14\r\n1 5\r\n6 10\r\n11 15\r\n", "output": "0 0 0"}, {"input": "3 4\r\n1 2 3\r\n1 2\r\n2 3\r\n5 6\r\n2 2\r\n", "output": "1 0 1 "}, {"input": "10 10\r\n3 4 2 6 1 9 0 5 8 7\r\n5 7\r\n2 6\r\n0 1\r\n5 6\r\n3 4\r\n2 5\r\n2 10\r\n4 6\r\n3 6\r\n3 7\r\n", "output": "0 1 1 1 0 0 1 0 1 0 "}, {"input": "3 3\r\n50 51 52\r\n1 5\r\n6 10\r\n11 15\r\n", "output": "1 0 1 "}, {"input": "3 1\r\n1 2 3\r\n2 3\r\n", "output": "1 0 1 "}]
false
stdio
import sys def read_ints(file): return list(map(int, file.read().split())) def main(input_path, output_path, submission_path): with open(input_path) as f: n, m = map(int, f.readline().split()) points = list(map(int, f.readline().split())) segments = [tuple(map(int, f.readline().split())) for _ in range(m)] with open(output_path) as f: ref_output = f.read().strip().split() with open(submission_path) as f: sub_output = f.read().strip().split() # Check if submission is -1 if len(sub_output) == 1 and sub_output[0] == '-1': if len(ref_output) == 1 and ref_output[0] == '-1': print(100) return else: print(0) return # Check if reference is -1 (submission isn't) if len(ref_output) == 1 and ref_output[0] == '-1': print(0) return # Check submission has n elements if len(sub_output) != n: print(0) return # Check each element is 0 or 1 try: colors = list(map(int, sub_output)) except: print(0) return for c in colors: if c not in (0, 1): print(0) return # Create point to color mapping color_map = {p: c for p, c in zip(points, colors)} # Check each segment for l, r in segments: red = 0 blue = 0 for p in points: if l <= p <= r: if color_map[p] == 0: red += 1 else: blue += 1 if abs(red - blue) > 1: print(0) return # All checks passed print(100) if __name__ == '__main__': input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] main(input_path, output_path, submission_path)
true
430/A
430
A
Python 3
PRETESTS
2
61
0
6593524
n, m = map(int, input().split()) points = input().split() segments = [input().split() for _ in range(m)] ans = list() for i in range(n): ans.append('1' if i % 2 else '0') print(' '.join(ans))
12
61
0
15914387
input() P=list(map(int,input().split())) for a,b in sorted([[P.index(p),i%2]for i,p in enumerate(sorted(P))]):print(b)
Codeforces Round 245 (Div. 2)
CF
2,014
1
256
Points and Segments (easy)
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following. Iahub wants to draw n distinct points and m segments on the OX axis. He can draw each point with either red or blue. The drawing is good if and only if the following requirement is met: for each segment [li, ri] consider all the red points belong to it (ri points), and all the blue points belong to it (bi points); each segment i should satisfy the inequality |ri - bi| ≤ 1. Iahub thinks that point x belongs to segment [l, r], if inequality l ≤ x ≤ r holds. Iahub gives to you all coordinates of points and segments. Please, help him to find any good drawing.
The first line of input contains two integers: n (1 ≤ n ≤ 100) and m (1 ≤ m ≤ 100). The next line contains n space-separated integers x1, x2, ..., xn (0 ≤ xi ≤ 100) — the coordinates of the points. The following m lines contain the descriptions of the m segments. Each line contains two integers li and ri (0 ≤ li ≤ ri ≤ 100) — the borders of the i-th segment. It's guaranteed that all the points are distinct.
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers, each integer must be 0 or 1. The i-th number denotes the color of the i-th point (0 is red, and 1 is blue). If there are multiple good drawings you can output any of them.
null
null
[{"input": "3 3\n3 7 14\n1 5\n6 10\n11 15", "output": "0 0 0"}, {"input": "3 4\n1 2 3\n1 2\n2 3\n5 6\n2 2", "output": "1 0 1"}]
1,600
["constructive algorithms", "sortings"]
12
[{"input": "3 3\r\n3 7 14\r\n1 5\r\n6 10\r\n11 15\r\n", "output": "0 0 0"}, {"input": "3 4\r\n1 2 3\r\n1 2\r\n2 3\r\n5 6\r\n2 2\r\n", "output": "1 0 1 "}, {"input": "10 10\r\n3 4 2 6 1 9 0 5 8 7\r\n5 7\r\n2 6\r\n0 1\r\n5 6\r\n3 4\r\n2 5\r\n2 10\r\n4 6\r\n3 6\r\n3 7\r\n", "output": "0 1 1 1 0 0 1 0 1 0 "}, {"input": "3 3\r\n50 51 52\r\n1 5\r\n6 10\r\n11 15\r\n", "output": "1 0 1 "}, {"input": "3 1\r\n1 2 3\r\n2 3\r\n", "output": "1 0 1 "}]
false
stdio
import sys def read_ints(file): return list(map(int, file.read().split())) def main(input_path, output_path, submission_path): with open(input_path) as f: n, m = map(int, f.readline().split()) points = list(map(int, f.readline().split())) segments = [tuple(map(int, f.readline().split())) for _ in range(m)] with open(output_path) as f: ref_output = f.read().strip().split() with open(submission_path) as f: sub_output = f.read().strip().split() # Check if submission is -1 if len(sub_output) == 1 and sub_output[0] == '-1': if len(ref_output) == 1 and ref_output[0] == '-1': print(100) return else: print(0) return # Check if reference is -1 (submission isn't) if len(ref_output) == 1 and ref_output[0] == '-1': print(0) return # Check submission has n elements if len(sub_output) != n: print(0) return # Check each element is 0 or 1 try: colors = list(map(int, sub_output)) except: print(0) return for c in colors: if c not in (0, 1): print(0) return # Create point to color mapping color_map = {p: c for p, c in zip(points, colors)} # Check each segment for l, r in segments: red = 0 blue = 0 for p in points: if l <= p <= r: if color_map[p] == 0: red += 1 else: blue += 1 if abs(red - blue) > 1: print(0) return # All checks passed print(100) if __name__ == '__main__': input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] main(input_path, output_path, submission_path)
true
430/A
430
A
Python 3
TESTS
2
46
4,608,000
25555550
n, m = map(int, input().split()) c = list(map(int, input().split())) d = [] for i in range(m): #d.append(list(map(int, input().spilt()))) a = input() output = [] for i in range(n): if i % 2 == 0: output.append("0") else: output.append("1") print(" ".join(output))
12
61
0
149664726
def main(): a = input() b = [int(i) for i in input().split()] n = len(b) c = [(i, index) for index, i in enumerate(b)] c.sort() res = [0]*n flag = 0 for i, j in c: res[j] = flag flag ^= 1 print(" ".join(str(i) for i in res)) main()
Codeforces Round 245 (Div. 2)
CF
2,014
1
256
Points and Segments (easy)
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following. Iahub wants to draw n distinct points and m segments on the OX axis. He can draw each point with either red or blue. The drawing is good if and only if the following requirement is met: for each segment [li, ri] consider all the red points belong to it (ri points), and all the blue points belong to it (bi points); each segment i should satisfy the inequality |ri - bi| ≤ 1. Iahub thinks that point x belongs to segment [l, r], if inequality l ≤ x ≤ r holds. Iahub gives to you all coordinates of points and segments. Please, help him to find any good drawing.
The first line of input contains two integers: n (1 ≤ n ≤ 100) and m (1 ≤ m ≤ 100). The next line contains n space-separated integers x1, x2, ..., xn (0 ≤ xi ≤ 100) — the coordinates of the points. The following m lines contain the descriptions of the m segments. Each line contains two integers li and ri (0 ≤ li ≤ ri ≤ 100) — the borders of the i-th segment. It's guaranteed that all the points are distinct.
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers, each integer must be 0 or 1. The i-th number denotes the color of the i-th point (0 is red, and 1 is blue). If there are multiple good drawings you can output any of them.
null
null
[{"input": "3 3\n3 7 14\n1 5\n6 10\n11 15", "output": "0 0 0"}, {"input": "3 4\n1 2 3\n1 2\n2 3\n5 6\n2 2", "output": "1 0 1"}]
1,600
["constructive algorithms", "sortings"]
12
[{"input": "3 3\r\n3 7 14\r\n1 5\r\n6 10\r\n11 15\r\n", "output": "0 0 0"}, {"input": "3 4\r\n1 2 3\r\n1 2\r\n2 3\r\n5 6\r\n2 2\r\n", "output": "1 0 1 "}, {"input": "10 10\r\n3 4 2 6 1 9 0 5 8 7\r\n5 7\r\n2 6\r\n0 1\r\n5 6\r\n3 4\r\n2 5\r\n2 10\r\n4 6\r\n3 6\r\n3 7\r\n", "output": "0 1 1 1 0 0 1 0 1 0 "}, {"input": "3 3\r\n50 51 52\r\n1 5\r\n6 10\r\n11 15\r\n", "output": "1 0 1 "}, {"input": "3 1\r\n1 2 3\r\n2 3\r\n", "output": "1 0 1 "}]
false
stdio
import sys def read_ints(file): return list(map(int, file.read().split())) def main(input_path, output_path, submission_path): with open(input_path) as f: n, m = map(int, f.readline().split()) points = list(map(int, f.readline().split())) segments = [tuple(map(int, f.readline().split())) for _ in range(m)] with open(output_path) as f: ref_output = f.read().strip().split() with open(submission_path) as f: sub_output = f.read().strip().split() # Check if submission is -1 if len(sub_output) == 1 and sub_output[0] == '-1': if len(ref_output) == 1 and ref_output[0] == '-1': print(100) return else: print(0) return # Check if reference is -1 (submission isn't) if len(ref_output) == 1 and ref_output[0] == '-1': print(0) return # Check submission has n elements if len(sub_output) != n: print(0) return # Check each element is 0 or 1 try: colors = list(map(int, sub_output)) except: print(0) return for c in colors: if c not in (0, 1): print(0) return # Create point to color mapping color_map = {p: c for p, c in zip(points, colors)} # Check each segment for l, r in segments: red = 0 blue = 0 for p in points: if l <= p <= r: if color_map[p] == 0: red += 1 else: blue += 1 if abs(red - blue) > 1: print(0) return # All checks passed print(100) if __name__ == '__main__': input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] main(input_path, output_path, submission_path)
true
430/A
430
A
PyPy 3-64
TESTS
2
61
0
201609464
import sys input = sys.stdin.readline n, m = map(int, input().split()) w = [str(j) for i, j in sorted([(j[0], i%2) for i, j in enumerate([(j, i) for i, j in enumerate(map(int, input().split()))])])] for i in range(m): a, b = map(int, input().split()) print(' '.join(w))
12
61
0
200627128
N,M = map(int, input().split()) A = list(map(int, input().split())) B = [] for _ in range(M): a,b = map(int, input().split()) B.append((a,b)) B = [] for i,a in enumerate(A): B.append((a,i)) B.sort() ans = [0]*N for i,(a,j) in enumerate(B): ans[j] = i%2 print(*ans)
Codeforces Round 245 (Div. 2)
CF
2,014
1
256
Points and Segments (easy)
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following. Iahub wants to draw n distinct points and m segments on the OX axis. He can draw each point with either red or blue. The drawing is good if and only if the following requirement is met: for each segment [li, ri] consider all the red points belong to it (ri points), and all the blue points belong to it (bi points); each segment i should satisfy the inequality |ri - bi| ≤ 1. Iahub thinks that point x belongs to segment [l, r], if inequality l ≤ x ≤ r holds. Iahub gives to you all coordinates of points and segments. Please, help him to find any good drawing.
The first line of input contains two integers: n (1 ≤ n ≤ 100) and m (1 ≤ m ≤ 100). The next line contains n space-separated integers x1, x2, ..., xn (0 ≤ xi ≤ 100) — the coordinates of the points. The following m lines contain the descriptions of the m segments. Each line contains two integers li and ri (0 ≤ li ≤ ri ≤ 100) — the borders of the i-th segment. It's guaranteed that all the points are distinct.
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers, each integer must be 0 or 1. The i-th number denotes the color of the i-th point (0 is red, and 1 is blue). If there are multiple good drawings you can output any of them.
null
null
[{"input": "3 3\n3 7 14\n1 5\n6 10\n11 15", "output": "0 0 0"}, {"input": "3 4\n1 2 3\n1 2\n2 3\n5 6\n2 2", "output": "1 0 1"}]
1,600
["constructive algorithms", "sortings"]
12
[{"input": "3 3\r\n3 7 14\r\n1 5\r\n6 10\r\n11 15\r\n", "output": "0 0 0"}, {"input": "3 4\r\n1 2 3\r\n1 2\r\n2 3\r\n5 6\r\n2 2\r\n", "output": "1 0 1 "}, {"input": "10 10\r\n3 4 2 6 1 9 0 5 8 7\r\n5 7\r\n2 6\r\n0 1\r\n5 6\r\n3 4\r\n2 5\r\n2 10\r\n4 6\r\n3 6\r\n3 7\r\n", "output": "0 1 1 1 0 0 1 0 1 0 "}, {"input": "3 3\r\n50 51 52\r\n1 5\r\n6 10\r\n11 15\r\n", "output": "1 0 1 "}, {"input": "3 1\r\n1 2 3\r\n2 3\r\n", "output": "1 0 1 "}]
false
stdio
import sys def read_ints(file): return list(map(int, file.read().split())) def main(input_path, output_path, submission_path): with open(input_path) as f: n, m = map(int, f.readline().split()) points = list(map(int, f.readline().split())) segments = [tuple(map(int, f.readline().split())) for _ in range(m)] with open(output_path) as f: ref_output = f.read().strip().split() with open(submission_path) as f: sub_output = f.read().strip().split() # Check if submission is -1 if len(sub_output) == 1 and sub_output[0] == '-1': if len(ref_output) == 1 and ref_output[0] == '-1': print(100) return else: print(0) return # Check if reference is -1 (submission isn't) if len(ref_output) == 1 and ref_output[0] == '-1': print(0) return # Check submission has n elements if len(sub_output) != n: print(0) return # Check each element is 0 or 1 try: colors = list(map(int, sub_output)) except: print(0) return for c in colors: if c not in (0, 1): print(0) return # Create point to color mapping color_map = {p: c for p, c in zip(points, colors)} # Check each segment for l, r in segments: red = 0 blue = 0 for p in points: if l <= p <= r: if color_map[p] == 0: red += 1 else: blue += 1 if abs(red - blue) > 1: print(0) return # All checks passed print(100) if __name__ == '__main__': input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] main(input_path, output_path, submission_path)
true
430/A
430
A
Python 3
TESTS
2
46
4,608,000
23984177
n, m = map(int, input().split()) points = list(map(int, input().split())) for i in range(m): l, r = map(int, input().split()) print(' '.join([str(i%2) for i in range(n)]))
12
62
0
6595409
n, m = map(int, input().split()) points = list(map(int, input().split())) segments = [input().split() for _ in range(m)] sorted_points = sorted(points) ans = [-1] * n for i in range(n): c = '0' if i % 2 else '1' ans[points.index(sorted_points[i])] = c print(' '.join(ans))
Codeforces Round 245 (Div. 2)
CF
2,014
1
256
Points and Segments (easy)
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following. Iahub wants to draw n distinct points and m segments on the OX axis. He can draw each point with either red or blue. The drawing is good if and only if the following requirement is met: for each segment [li, ri] consider all the red points belong to it (ri points), and all the blue points belong to it (bi points); each segment i should satisfy the inequality |ri - bi| ≤ 1. Iahub thinks that point x belongs to segment [l, r], if inequality l ≤ x ≤ r holds. Iahub gives to you all coordinates of points and segments. Please, help him to find any good drawing.
The first line of input contains two integers: n (1 ≤ n ≤ 100) and m (1 ≤ m ≤ 100). The next line contains n space-separated integers x1, x2, ..., xn (0 ≤ xi ≤ 100) — the coordinates of the points. The following m lines contain the descriptions of the m segments. Each line contains two integers li and ri (0 ≤ li ≤ ri ≤ 100) — the borders of the i-th segment. It's guaranteed that all the points are distinct.
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers, each integer must be 0 or 1. The i-th number denotes the color of the i-th point (0 is red, and 1 is blue). If there are multiple good drawings you can output any of them.
null
null
[{"input": "3 3\n3 7 14\n1 5\n6 10\n11 15", "output": "0 0 0"}, {"input": "3 4\n1 2 3\n1 2\n2 3\n5 6\n2 2", "output": "1 0 1"}]
1,600
["constructive algorithms", "sortings"]
12
[{"input": "3 3\r\n3 7 14\r\n1 5\r\n6 10\r\n11 15\r\n", "output": "0 0 0"}, {"input": "3 4\r\n1 2 3\r\n1 2\r\n2 3\r\n5 6\r\n2 2\r\n", "output": "1 0 1 "}, {"input": "10 10\r\n3 4 2 6 1 9 0 5 8 7\r\n5 7\r\n2 6\r\n0 1\r\n5 6\r\n3 4\r\n2 5\r\n2 10\r\n4 6\r\n3 6\r\n3 7\r\n", "output": "0 1 1 1 0 0 1 0 1 0 "}, {"input": "3 3\r\n50 51 52\r\n1 5\r\n6 10\r\n11 15\r\n", "output": "1 0 1 "}, {"input": "3 1\r\n1 2 3\r\n2 3\r\n", "output": "1 0 1 "}]
false
stdio
import sys def read_ints(file): return list(map(int, file.read().split())) def main(input_path, output_path, submission_path): with open(input_path) as f: n, m = map(int, f.readline().split()) points = list(map(int, f.readline().split())) segments = [tuple(map(int, f.readline().split())) for _ in range(m)] with open(output_path) as f: ref_output = f.read().strip().split() with open(submission_path) as f: sub_output = f.read().strip().split() # Check if submission is -1 if len(sub_output) == 1 and sub_output[0] == '-1': if len(ref_output) == 1 and ref_output[0] == '-1': print(100) return else: print(0) return # Check if reference is -1 (submission isn't) if len(ref_output) == 1 and ref_output[0] == '-1': print(0) return # Check submission has n elements if len(sub_output) != n: print(0) return # Check each element is 0 or 1 try: colors = list(map(int, sub_output)) except: print(0) return for c in colors: if c not in (0, 1): print(0) return # Create point to color mapping color_map = {p: c for p, c in zip(points, colors)} # Check each segment for l, r in segments: red = 0 blue = 0 for p in points: if l <= p <= r: if color_map[p] == 0: red += 1 else: blue += 1 if abs(red - blue) > 1: print(0) return # All checks passed print(100) if __name__ == '__main__': input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] main(input_path, output_path, submission_path)
true
430/A
430
A
Python 3
TESTS
2
46
4,608,000
23985512
n, m = map(int, input().split()) map(int, input().split()) for i in range(m): l, r = map(int, input().split()) print(' '.join([str(i%2) for i in range(1, n+1)]))
12
62
0
6599924
n,m=list(map(int,input().split())) points = [(v,i) for i,v in enumerate(map(int,input().split()))] for _ in range(m): input() blue=True ans=[0]*n points = sorted(points) for p in points: v,i = p ans[i] = int(blue) blue=not blue for v in ans: print(v,end=' ') print()
Codeforces Round 245 (Div. 2)
CF
2,014
1
256
Points and Segments (easy)
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following. Iahub wants to draw n distinct points and m segments on the OX axis. He can draw each point with either red or blue. The drawing is good if and only if the following requirement is met: for each segment [li, ri] consider all the red points belong to it (ri points), and all the blue points belong to it (bi points); each segment i should satisfy the inequality |ri - bi| ≤ 1. Iahub thinks that point x belongs to segment [l, r], if inequality l ≤ x ≤ r holds. Iahub gives to you all coordinates of points and segments. Please, help him to find any good drawing.
The first line of input contains two integers: n (1 ≤ n ≤ 100) and m (1 ≤ m ≤ 100). The next line contains n space-separated integers x1, x2, ..., xn (0 ≤ xi ≤ 100) — the coordinates of the points. The following m lines contain the descriptions of the m segments. Each line contains two integers li and ri (0 ≤ li ≤ ri ≤ 100) — the borders of the i-th segment. It's guaranteed that all the points are distinct.
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers, each integer must be 0 or 1. The i-th number denotes the color of the i-th point (0 is red, and 1 is blue). If there are multiple good drawings you can output any of them.
null
null
[{"input": "3 3\n3 7 14\n1 5\n6 10\n11 15", "output": "0 0 0"}, {"input": "3 4\n1 2 3\n1 2\n2 3\n5 6\n2 2", "output": "1 0 1"}]
1,600
["constructive algorithms", "sortings"]
12
[{"input": "3 3\r\n3 7 14\r\n1 5\r\n6 10\r\n11 15\r\n", "output": "0 0 0"}, {"input": "3 4\r\n1 2 3\r\n1 2\r\n2 3\r\n5 6\r\n2 2\r\n", "output": "1 0 1 "}, {"input": "10 10\r\n3 4 2 6 1 9 0 5 8 7\r\n5 7\r\n2 6\r\n0 1\r\n5 6\r\n3 4\r\n2 5\r\n2 10\r\n4 6\r\n3 6\r\n3 7\r\n", "output": "0 1 1 1 0 0 1 0 1 0 "}, {"input": "3 3\r\n50 51 52\r\n1 5\r\n6 10\r\n11 15\r\n", "output": "1 0 1 "}, {"input": "3 1\r\n1 2 3\r\n2 3\r\n", "output": "1 0 1 "}]
false
stdio
import sys def read_ints(file): return list(map(int, file.read().split())) def main(input_path, output_path, submission_path): with open(input_path) as f: n, m = map(int, f.readline().split()) points = list(map(int, f.readline().split())) segments = [tuple(map(int, f.readline().split())) for _ in range(m)] with open(output_path) as f: ref_output = f.read().strip().split() with open(submission_path) as f: sub_output = f.read().strip().split() # Check if submission is -1 if len(sub_output) == 1 and sub_output[0] == '-1': if len(ref_output) == 1 and ref_output[0] == '-1': print(100) return else: print(0) return # Check if reference is -1 (submission isn't) if len(ref_output) == 1 and ref_output[0] == '-1': print(0) return # Check submission has n elements if len(sub_output) != n: print(0) return # Check each element is 0 or 1 try: colors = list(map(int, sub_output)) except: print(0) return for c in colors: if c not in (0, 1): print(0) return # Create point to color mapping color_map = {p: c for p, c in zip(points, colors)} # Check each segment for l, r in segments: red = 0 blue = 0 for p in points: if l <= p <= r: if color_map[p] == 0: red += 1 else: blue += 1 if abs(red - blue) > 1: print(0) return # All checks passed print(100) if __name__ == '__main__': input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] main(input_path, output_path, submission_path)
true
430/A
430
A
Python 3
PRETESTS
2
46
0
6590143
#Points and Segments 2 a,b=input().split() points=int(a) segments=int(b) #Next line input, n point locations. coordinates=[] coordinates=input().split() coordinates=[int(a) for a in coordinates] coordinates=sorted(coordinates) #Next m lines, get start and end of segments in a list all_segments=[] for segment in range(1, segments+1): a,b=input().split() all_segments=sorted(all_segments) #I assume if there are even points, alternate. Alternate odd too. length=len(coordinates) if length%2==0: print("1 0 "*(length//2)) else: print("1 0 "*((length-1)//2)+"1")
12
62
0
6600263
input() P=list(map(int,input().split())) l=sorted([[P.index(p),i%2] for i,p in enumerate(sorted(P))]) for a,b in l:print(b)
Codeforces Round 245 (Div. 2)
CF
2,014
1
256
Points and Segments (easy)
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following. Iahub wants to draw n distinct points and m segments on the OX axis. He can draw each point with either red or blue. The drawing is good if and only if the following requirement is met: for each segment [li, ri] consider all the red points belong to it (ri points), and all the blue points belong to it (bi points); each segment i should satisfy the inequality |ri - bi| ≤ 1. Iahub thinks that point x belongs to segment [l, r], if inequality l ≤ x ≤ r holds. Iahub gives to you all coordinates of points and segments. Please, help him to find any good drawing.
The first line of input contains two integers: n (1 ≤ n ≤ 100) and m (1 ≤ m ≤ 100). The next line contains n space-separated integers x1, x2, ..., xn (0 ≤ xi ≤ 100) — the coordinates of the points. The following m lines contain the descriptions of the m segments. Each line contains two integers li and ri (0 ≤ li ≤ ri ≤ 100) — the borders of the i-th segment. It's guaranteed that all the points are distinct.
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers, each integer must be 0 or 1. The i-th number denotes the color of the i-th point (0 is red, and 1 is blue). If there are multiple good drawings you can output any of them.
null
null
[{"input": "3 3\n3 7 14\n1 5\n6 10\n11 15", "output": "0 0 0"}, {"input": "3 4\n1 2 3\n1 2\n2 3\n5 6\n2 2", "output": "1 0 1"}]
1,600
["constructive algorithms", "sortings"]
12
[{"input": "3 3\r\n3 7 14\r\n1 5\r\n6 10\r\n11 15\r\n", "output": "0 0 0"}, {"input": "3 4\r\n1 2 3\r\n1 2\r\n2 3\r\n5 6\r\n2 2\r\n", "output": "1 0 1 "}, {"input": "10 10\r\n3 4 2 6 1 9 0 5 8 7\r\n5 7\r\n2 6\r\n0 1\r\n5 6\r\n3 4\r\n2 5\r\n2 10\r\n4 6\r\n3 6\r\n3 7\r\n", "output": "0 1 1 1 0 0 1 0 1 0 "}, {"input": "3 3\r\n50 51 52\r\n1 5\r\n6 10\r\n11 15\r\n", "output": "1 0 1 "}, {"input": "3 1\r\n1 2 3\r\n2 3\r\n", "output": "1 0 1 "}]
false
stdio
import sys def read_ints(file): return list(map(int, file.read().split())) def main(input_path, output_path, submission_path): with open(input_path) as f: n, m = map(int, f.readline().split()) points = list(map(int, f.readline().split())) segments = [tuple(map(int, f.readline().split())) for _ in range(m)] with open(output_path) as f: ref_output = f.read().strip().split() with open(submission_path) as f: sub_output = f.read().strip().split() # Check if submission is -1 if len(sub_output) == 1 and sub_output[0] == '-1': if len(ref_output) == 1 and ref_output[0] == '-1': print(100) return else: print(0) return # Check if reference is -1 (submission isn't) if len(ref_output) == 1 and ref_output[0] == '-1': print(0) return # Check submission has n elements if len(sub_output) != n: print(0) return # Check each element is 0 or 1 try: colors = list(map(int, sub_output)) except: print(0) return for c in colors: if c not in (0, 1): print(0) return # Create point to color mapping color_map = {p: c for p, c in zip(points, colors)} # Check each segment for l, r in segments: red = 0 blue = 0 for p in points: if l <= p <= r: if color_map[p] == 0: red += 1 else: blue += 1 if abs(red - blue) > 1: print(0) return # All checks passed print(100) if __name__ == '__main__': input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] main(input_path, output_path, submission_path)
true
430/A
430
A
Python 3
TESTS
2
46
0
6619187
n, m = map(int, input().split()) print('0 1 ' * (n // 2) + '0' * (n & 1))
12
62
0
6602943
import sys read = lambda t=int:list(map(t,sys.stdin.readline().split())) _, M = read() xs = read() for _ in range(M): _ = read() xs = sorted((x,i) for i,x in enumerate(xs)) xs = sorted((i,j) for j,(x,i) in enumerate(xs)) print(" ".join(str(j%2) for _,j in xs))
Codeforces Round 245 (Div. 2)
CF
2,014
1
256
Points and Segments (easy)
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following. Iahub wants to draw n distinct points and m segments on the OX axis. He can draw each point with either red or blue. The drawing is good if and only if the following requirement is met: for each segment [li, ri] consider all the red points belong to it (ri points), and all the blue points belong to it (bi points); each segment i should satisfy the inequality |ri - bi| ≤ 1. Iahub thinks that point x belongs to segment [l, r], if inequality l ≤ x ≤ r holds. Iahub gives to you all coordinates of points and segments. Please, help him to find any good drawing.
The first line of input contains two integers: n (1 ≤ n ≤ 100) and m (1 ≤ m ≤ 100). The next line contains n space-separated integers x1, x2, ..., xn (0 ≤ xi ≤ 100) — the coordinates of the points. The following m lines contain the descriptions of the m segments. Each line contains two integers li and ri (0 ≤ li ≤ ri ≤ 100) — the borders of the i-th segment. It's guaranteed that all the points are distinct.
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers, each integer must be 0 or 1. The i-th number denotes the color of the i-th point (0 is red, and 1 is blue). If there are multiple good drawings you can output any of them.
null
null
[{"input": "3 3\n3 7 14\n1 5\n6 10\n11 15", "output": "0 0 0"}, {"input": "3 4\n1 2 3\n1 2\n2 3\n5 6\n2 2", "output": "1 0 1"}]
1,600
["constructive algorithms", "sortings"]
12
[{"input": "3 3\r\n3 7 14\r\n1 5\r\n6 10\r\n11 15\r\n", "output": "0 0 0"}, {"input": "3 4\r\n1 2 3\r\n1 2\r\n2 3\r\n5 6\r\n2 2\r\n", "output": "1 0 1 "}, {"input": "10 10\r\n3 4 2 6 1 9 0 5 8 7\r\n5 7\r\n2 6\r\n0 1\r\n5 6\r\n3 4\r\n2 5\r\n2 10\r\n4 6\r\n3 6\r\n3 7\r\n", "output": "0 1 1 1 0 0 1 0 1 0 "}, {"input": "3 3\r\n50 51 52\r\n1 5\r\n6 10\r\n11 15\r\n", "output": "1 0 1 "}, {"input": "3 1\r\n1 2 3\r\n2 3\r\n", "output": "1 0 1 "}]
false
stdio
import sys def read_ints(file): return list(map(int, file.read().split())) def main(input_path, output_path, submission_path): with open(input_path) as f: n, m = map(int, f.readline().split()) points = list(map(int, f.readline().split())) segments = [tuple(map(int, f.readline().split())) for _ in range(m)] with open(output_path) as f: ref_output = f.read().strip().split() with open(submission_path) as f: sub_output = f.read().strip().split() # Check if submission is -1 if len(sub_output) == 1 and sub_output[0] == '-1': if len(ref_output) == 1 and ref_output[0] == '-1': print(100) return else: print(0) return # Check if reference is -1 (submission isn't) if len(ref_output) == 1 and ref_output[0] == '-1': print(0) return # Check submission has n elements if len(sub_output) != n: print(0) return # Check each element is 0 or 1 try: colors = list(map(int, sub_output)) except: print(0) return for c in colors: if c not in (0, 1): print(0) return # Create point to color mapping color_map = {p: c for p, c in zip(points, colors)} # Check each segment for l, r in segments: red = 0 blue = 0 for p in points: if l <= p <= r: if color_map[p] == 0: red += 1 else: blue += 1 if abs(red - blue) > 1: print(0) return # All checks passed print(100) if __name__ == '__main__': input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] main(input_path, output_path, submission_path)
true
430/A
430
A
Python 3
TESTS
2
46
0
6961224
n, m = map(int, input().split()) a = list(map(int, input().split())) for i in range(m): x, y = map(int, input().split()) for i in range(n): print(i % 2, end = ' ')
12
62
0
6612737
from operator import * R = lambda: map(int, input().split()) n, m = R() x = sorted(enumerate(R()), key=itemgetter(1)) y = [0] * n for i in range(n): y[x[i][0]] = i % 2 print(' '.join(map(str, y)))
Codeforces Round 245 (Div. 2)
CF
2,014
1
256
Points and Segments (easy)
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following. Iahub wants to draw n distinct points and m segments on the OX axis. He can draw each point with either red or blue. The drawing is good if and only if the following requirement is met: for each segment [li, ri] consider all the red points belong to it (ri points), and all the blue points belong to it (bi points); each segment i should satisfy the inequality |ri - bi| ≤ 1. Iahub thinks that point x belongs to segment [l, r], if inequality l ≤ x ≤ r holds. Iahub gives to you all coordinates of points and segments. Please, help him to find any good drawing.
The first line of input contains two integers: n (1 ≤ n ≤ 100) and m (1 ≤ m ≤ 100). The next line contains n space-separated integers x1, x2, ..., xn (0 ≤ xi ≤ 100) — the coordinates of the points. The following m lines contain the descriptions of the m segments. Each line contains two integers li and ri (0 ≤ li ≤ ri ≤ 100) — the borders of the i-th segment. It's guaranteed that all the points are distinct.
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers, each integer must be 0 or 1. The i-th number denotes the color of the i-th point (0 is red, and 1 is blue). If there are multiple good drawings you can output any of them.
null
null
[{"input": "3 3\n3 7 14\n1 5\n6 10\n11 15", "output": "0 0 0"}, {"input": "3 4\n1 2 3\n1 2\n2 3\n5 6\n2 2", "output": "1 0 1"}]
1,600
["constructive algorithms", "sortings"]
12
[{"input": "3 3\r\n3 7 14\r\n1 5\r\n6 10\r\n11 15\r\n", "output": "0 0 0"}, {"input": "3 4\r\n1 2 3\r\n1 2\r\n2 3\r\n5 6\r\n2 2\r\n", "output": "1 0 1 "}, {"input": "10 10\r\n3 4 2 6 1 9 0 5 8 7\r\n5 7\r\n2 6\r\n0 1\r\n5 6\r\n3 4\r\n2 5\r\n2 10\r\n4 6\r\n3 6\r\n3 7\r\n", "output": "0 1 1 1 0 0 1 0 1 0 "}, {"input": "3 3\r\n50 51 52\r\n1 5\r\n6 10\r\n11 15\r\n", "output": "1 0 1 "}, {"input": "3 1\r\n1 2 3\r\n2 3\r\n", "output": "1 0 1 "}]
false
stdio
import sys def read_ints(file): return list(map(int, file.read().split())) def main(input_path, output_path, submission_path): with open(input_path) as f: n, m = map(int, f.readline().split()) points = list(map(int, f.readline().split())) segments = [tuple(map(int, f.readline().split())) for _ in range(m)] with open(output_path) as f: ref_output = f.read().strip().split() with open(submission_path) as f: sub_output = f.read().strip().split() # Check if submission is -1 if len(sub_output) == 1 and sub_output[0] == '-1': if len(ref_output) == 1 and ref_output[0] == '-1': print(100) return else: print(0) return # Check if reference is -1 (submission isn't) if len(ref_output) == 1 and ref_output[0] == '-1': print(0) return # Check submission has n elements if len(sub_output) != n: print(0) return # Check each element is 0 or 1 try: colors = list(map(int, sub_output)) except: print(0) return for c in colors: if c not in (0, 1): print(0) return # Create point to color mapping color_map = {p: c for p, c in zip(points, colors)} # Check each segment for l, r in segments: red = 0 blue = 0 for p in points: if l <= p <= r: if color_map[p] == 0: red += 1 else: blue += 1 if abs(red - blue) > 1: print(0) return # All checks passed print(100) if __name__ == '__main__': input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] main(input_path, output_path, submission_path)
true
432/C
432
C
PyPy 3-64
TESTS
0
31
512,000
160994544
import sys from array import array def count_prime(n): prim[0] = prim[1] = 0 for i in range(2, n + 1): if prim[i]: for j in range(i * 2, n + 1, i): prim[j] = 0 input = lambda: sys.stdin.buffer.readline().decode().strip() n, a = int(input()), array('i', [int(x) - 1 for x in input().split()]) prim = array('b', [1] * (n + 1)) ixs = array('i', [-1] * n) out = [] count_prime(n) for i in range(n): ixs[a[i]] = i for i in range(n): while ixs[i] > i: cur = p = ixs[i] while not prim[p]: p -= 1 out.append(f'{ixs[i] + 1} {cur - p + 1}') ixs[i], ixs[a[cur - p]] = ixs[a[cur - p]], ixs[i] a[ixs[i]], a[cur - p] = a[cur - p], a[ixs[i]] print(len(out)) print(*out, sep='\n')
30
997
37,478,400
6630802
# from random import shuffle # f = open("out.txt", "w") # n = 1000 # test = list(range(1, n + 1)) # shuffle(test) # print(n, file=f) # print(' '.join(map(str, test)), file=f) from bisect import * from sys import stdin f = stdin # open("out.txt", "r") n = int(f.readline()) primes = [1] * (n + 1) primes[0] = 0 primes[1] = 0 for i in range(2, n + 1): if primes[i] != 0: for j in range(i + i, n + 1, i): primes[j] = 0 primes = [i - 1 for i in range(n + 1) if primes[i] != 0] a = list(map(int, f.readline().split())) p = [0] * n for i in range(n): p[a[i] - 1] = i swaps = [] for i in range(n): if p[i] != i: r = p[i] while r != i: j = bisect(primes, r - i) - 1 l = r - primes[j] p[a[l] - 1], p[a[r] - 1] = p[a[r] - 1], p[a[l] - 1] a[l], a[r] = a[r], a[l] swaps += [(l, r)] r = l print(len(swaps)) if len(swaps) > 0: print("\n".join("{} {}".format(l + 1, r + 1) for l, r in swaps))
Codeforces Round 246 (Div. 2)
CF
2,014
2
256
Prime Swaps
You have an array a[1], a[2], ..., a[n], containing distinct integers from 1 to n. Your task is to sort this array in increasing order with the following operation (you may need to apply it multiple times): - choose two indexes, i and j (1 ≤ i < j ≤ n; (j - i + 1) is a prime number); - swap the elements on positions i and j; in other words, you are allowed to apply the following sequence of assignments: tmp = a[i], a[i] = a[j], a[j] = tmp (tmp is a temporary variable). You do not need to minimize the number of used operations. However, you need to make sure that there are at most 5n operations.
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n distinct integers a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ n).
In the first line, print integer k (0 ≤ k ≤ 5n) — the number of used operations. Next, print the operations. Each operation must be printed as "i j" (1 ≤ i < j ≤ n; (j - i + 1) is a prime). If there are multiple answers, you can print any of them.
null
null
[{"input": "3\n3 2 1", "output": "1\n1 3"}, {"input": "2\n1 2", "output": "0"}, {"input": "4\n4 2 3 1", "output": "3\n2 4\n1 2\n2 4"}]
1,800
["greedy", "sortings"]
30
[{"input": "3\r\n3 2 1\r\n", "output": "1\r\n1 3\r\n"}, {"input": "2\r\n1 2\r\n", "output": "0\r\n"}, {"input": "4\r\n4 2 3 1\r\n", "output": "3\r\n2 4\r\n1 2\r\n2 4\r\n"}, {"input": "10\r\n10 9 8 7 6 5 4 3 2 1\r\n", "output": "15\r\n4 10\r\n2 4\r\n1 2\r\n3 9\r\n2 3\r\n4 8\r\n3 4\r\n5 7\r\n4 5\r\n5 6\r\n6 7\r\n8 10\r\n7 8\r\n8 9\r\n9 10\r\n"}, {"input": "1\r\n1\r\n", "output": "0\r\n"}, {"input": "5\r\n2 1 4 3 5\r\n", "output": "2\r\n1 2\r\n3 4\r\n"}, {"input": "8\r\n1 3 5 7 8 6 4 2\r\n", "output": "6\r\n2 8\r\n4 8\r\n3 4\r\n5 7\r\n4 5\r\n7 8\r\n"}, {"input": "3\r\n1 2 3\r\n", "output": "0\r\n"}, {"input": "4\r\n3 4 1 2\r\n", "output": "2\r\n1 3\r\n2 4\r\n"}, {"input": "5\r\n1 4 2 3 5\r\n", "output": "2\r\n2 3\r\n3 4\r\n"}, {"input": "6\r\n5 3 6 1 4 2\r\n", "output": "6\r\n2 4\r\n1 2\r\n2 6\r\n3 4\r\n4 5\r\n5 6\r\n"}, {"input": "2\r\n2 1\r\n", "output": "1\r\n1 2\r\n"}, {"input": "3\r\n2 1 3\r\n", "output": "1\r\n1 2\r\n"}, {"input": "3\r\n1 2 3\r\n", "output": "0\r\n"}, {"input": "3\r\n1 3 2\r\n", "output": "1\r\n2 3\r\n"}, {"input": "3\r\n3 1 2\r\n", "output": "2\r\n1 2\r\n2 3\r\n"}, {"input": "100\r\n100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1\r\n", "output": "198\n4 100\n2 4\n1 2\n3 99\n2 3\n10 98\n4 10\n3 4\n9 97\n5 9\n4 5\n8 96\n6 8\n5 6\n7 95\n6 7\n12 94\n8 12\n7 8\n11 93\n9 11\n8 9\n10 92\n9 10\n13 91\n11 13\n10 11\n12 90\n11 12\n17 89\n13 17\n12 13\n16 88\n14 16\n13 14\n15 87\n14 15\n16 86\n15 16\n19 85\n17 19\n16 17\n18 84\n17 18\n23 83\n19 23\n18 19\n22 82\n20 22\n19 20\n21 81\n20 21\n22 80\n21 22\n27 79\n23 27\n22 23\n26 78\n24 26\n23 24\n25 77\n24 25\n30 76\n26 30\n25 26\n29 75\n27 29\n26 27\n28 74\n27 28\n31 73\n29 31\n28 29\n30 72\n29 30\n31 71\n30 31\n34 70\n32 34\n31 32\n33 69\n32 33\n38 68\n34 38\n33 34\n37 67\n35 37\n34 35\n36 66\n35 36\n37 65\n36 37\n42 64\n38 42\n37 38\n41 63\n39 41\n38 39\n40 62\n39 40\n43 61\n41 43\n40 41\n42 60\n41 42\n43 59\n42 43\n46 58\n44 46\n43 44\n45 57\n44 45\n46 56\n45 46\n49 55\n47 49\n46 47\n48 54\n47 48\n49 53\n48 49\n50 52\n49 50\n50 51\n51 52\n53 55\n52 53\n53 54\n54 55\n56 58\n55 56\n56 57\n57 58\n59 61\n58 59\n60 64\n59 60\n61 63\n60 61\n61 62\n62 63\n64 68\n63 64\n65 67\n64 65\n65 66\n66 67\n68 70\n67 68\n68 69\n69 70\n71 73\n70 71\n72 76\n71 72\n73 75\n72 73\n73 74\n75 79\n74 75\n76 78\n75 76\n76 77\n77 78\n79 83\n78 79\n80 82\n79 80\n80 81\n81 82\n83 85\n82 83\n83 84\n85 89\n84 85\n86 88\n85 86\n86 87\n87 88\n89 91\n88 89\n90 94\n89 90\n91 93\n90 91\n92 98\n91 92\n93 97\n92 93\n94 96\n93 94\n94 95\n95 96\n96 97\n98 100\n97 98\n98 99\n99 100\n"}]
false
stdio
null
true
140/B
140
B
Python 3
TESTS
0
62
0
11600827
import sys from itertools import * from math import * def solve(): n = int(input()) tab = [list(map(int, input().split())) for _ in range(n)] alex = list(map(int, input().split())) willget = [[0]*n for _ in range(n)] has = list() for row in range(n): hasrow = list() for val in alex: if val <= row + 1: hasrow.append(val) has.append(hasrow) # print(has) # for col in range(n): # willget[row][col] = has[0] if has[0] != col + 1 else (-1 if len(has) == 1 else has[1]) # for row in willget: # print(row) # for row in has: print(row) for friend in range(1, n + 1): bestindex = -1 bestpriority = n + 1 for time in range(n): willsend = has[time][0] if has[time][0] != friend else (-1 if len(has) == 1 else has[1]) rating = n for i in range(n - 1, -1, -1): if tab[friend - 1][i] == willsend: rating = i if rating < bestpriority: bestpriority = rating bestindex = time print(bestindex + 1, end = ' ') if sys.hexversion == 50594544 : sys.stdin = open("test.txt") solve()
17
186
4,710,400
220623198
import sys input = lambda: sys.stdin.readline().rstrip() from collections import deque,defaultdict,Counter from itertools import permutations,combinations from bisect import * from heapq import * from math import ceil,gcd,lcm,floor,comb alph = 'abcdefghijklmnopqrstuvwxyz' #pow(x,mod-2,mod) N = int(input()) A = [list(map(int, input().split())) for _ in range(N+1)] l = [0]*N for i in range(N): p = [0]*(N+1) for j in range(N):p[A[i][j]] = j a,b,c = 0,float("inf"),float("inf") for x in A[N]: if x!=i+1 and x<c: if p[x]<b: a,b = x,p[x] c = x l[i] = a print(*l)
Codeforces Round 100
CF
2,012
2
256
New Year Cards
As meticulous Gerald sets the table, Alexander finished another post on Codeforces and begins to respond to New Year greetings from friends. Alexander has n friends, and each of them sends to Alexander exactly one e-card. Let us number his friends by numbers from 1 to n in the order in which they send the cards. Let's introduce the same numbering for the cards, that is, according to the numbering the i-th friend sent to Alexander a card number i. Alexander also sends cards to friends, but he doesn't look for the new cards on the Net. He simply uses the cards previously sent to him (sometimes, however, he does need to add some crucial details). Initially Alexander doesn't have any cards. Alexander always follows the two rules: 1. He will never send to a firend a card that this friend has sent to him. 2. Among the other cards available to him at the moment, Alexander always chooses one that Alexander himself likes most. Alexander plans to send to each friend exactly one card. Of course, Alexander can send the same card multiple times. Alexander and each his friend has the list of preferences, which is a permutation of integers from 1 to n. The first number in the list is the number of the favorite card, the second number shows the second favorite, and so on, the last number shows the least favorite card. Your task is to find a schedule of sending cards for Alexander. Determine at which moments of time Alexander must send cards to his friends, to please each of them as much as possible. In other words, so that as a result of applying two Alexander's rules, each friend receives the card that is preferred for him as much as possible. Note that Alexander doesn't choose freely what card to send, but he always strictly follows the two rules.
The first line contains an integer n (2 ≤ n ≤ 300) — the number of Alexander's friends, equal to the number of cards. Next n lines contain his friends' preference lists. Each list consists of n different integers from 1 to n. The last line contains Alexander's preference list in the same format.
Print n space-separated numbers: the i-th number should be the number of the friend, whose card Alexander receives right before he should send a card to the i-th friend. If there are several solutions, print any of them.
null
In the sample, the algorithm of actions Alexander and his friends perform is as follows: 1. Alexander receives card 1 from the first friend. 2. Alexander sends the card he has received (at the moment he only has one card, and therefore it is the most preferable for him) to friends with the numbers 2 and 3. 3. Alexander receives card 2 from the second friend, now he has two cards — 1 and 2. 4. Alexander sends a card to the first friend. Despite the fact that Alexander likes card 1 more, he sends card 2 as he cannot send a friend the card sent by that very friend. 5. Alexander receives card 3 from the third friend. 6. Alexander receives card 4 from the fourth friend. 7. Among the cards Alexander has number 3 is his favorite and he sends it to the fourth friend. Note that Alexander can send cards to multiple friends at a time (in this case the second and the third one). Alexander can send card 3 to the fourth friend after he receives the third card or after he receives the fourth card (both variants are correct).
[{"input": "4\n1 2 3 4\n4 1 3 2\n4 3 1 2\n3 4 2 1\n3 1 2 4", "output": "2 1 1 4"}]
1,800
["brute force", "greedy", "implementation"]
17
[{"input": "4\r\n1 2 3 4\r\n4 1 3 2\r\n4 3 1 2\r\n3 4 2 1\r\n3 1 2 4\r\n", "output": "2 1 1 3\r\n"}, {"input": "2\r\n1 2\r\n2 1\r\n2 1\r\n", "output": "2 1\r\n"}, {"input": "3\r\n1 2 3\r\n2 3 1\r\n1 3 2\r\n3 2 1\r\n", "output": "2 3 1\r\n"}, {"input": "5\r\n1 4 2 3 5\r\n5 1 3 4 2\r\n3 2 4 1 5\r\n1 4 5 3 2\r\n5 2 3 4 1\r\n5 4 2 1 3\r\n", "output": "4 5 2 1 2\r\n"}, {"input": "10\r\n5 1 6 2 8 3 4 10 9 7\r\n3 1 10 6 8 5 2 7 9 4\r\n2 9 1 4 10 6 8 7 3 5\r\n10 1 7 8 3 2 4 6 5 9\r\n3 2 10 4 7 8 5 6 1 9\r\n5 6 3 10 8 7 2 9 4 1\r\n6 5 1 3 2 7 9 10 8 4\r\n1 10 9 3 7 8 4 2 6 5\r\n6 8 4 5 9 1 2 10 7 3\r\n9 6 8 5 10 3 1 7 2 4\r\n5 7 4 8 9 6 1 10 3 2\r\n", "output": "5 1 1 1 4 5 5 1 4 5\r\n"}]
false
stdio
null
true
140/B
140
B
PyPy 3
TESTS
4
280
0
82171629
n = int(input()) frnd = [list(map(int, input().split() )) for _ in range(n)] ara = list(map(int, input().split() )) pref = [0]*(n+1) ans = [0]*(n) for i in range(n): pref[ara[i]] = i+1 def better(ro, x, y): if x == ro: return y if y == ro: return x if pref[x] < pref[y]: return x return min(x, y) # print(ara) for ro in range(n): tgt = frnd[ro][0] for i in range(1, n): # print(ro, tgt, frnd[ro][i] , end = ' ') tgt = better(ro+1, tgt, frnd[ro][i]) # print(tgt) ans[ro] = tgt for i in range(n): print(ans[i], end=' ') print()
17
248
4,300,800
166805289
n = int(input()) friends = [] friends_inverse = [] for _ in range(n): friend = list(map(int, input().split())) friends.append(friend) friends_inverse.append([friend.index(i + 1) for i in range(n)]) alex = list(map(int, input().split())) alex_inverse = [alex.index(i + 1) for i in range(n)] output = {k: -1 for k in range(1, n + 1)} for i, friend in enumerate(friends): for card in friend: #print(alex, alex_inverse, alex_inverse[card - 1], card, i + 1) if card != i + 1: candidates = alex[:alex_inverse[card - 1]] try: candidates.remove(i + 1) except: pass if len(candidates) == 0 or min(candidates) > card: #print("found for friend %d card %d" % (i + 1, card)) output[i + 1] = card break print(" ".join(map(str, output.values())))
Codeforces Round 100
CF
2,012
2
256
New Year Cards
As meticulous Gerald sets the table, Alexander finished another post on Codeforces and begins to respond to New Year greetings from friends. Alexander has n friends, and each of them sends to Alexander exactly one e-card. Let us number his friends by numbers from 1 to n in the order in which they send the cards. Let's introduce the same numbering for the cards, that is, according to the numbering the i-th friend sent to Alexander a card number i. Alexander also sends cards to friends, but he doesn't look for the new cards on the Net. He simply uses the cards previously sent to him (sometimes, however, he does need to add some crucial details). Initially Alexander doesn't have any cards. Alexander always follows the two rules: 1. He will never send to a firend a card that this friend has sent to him. 2. Among the other cards available to him at the moment, Alexander always chooses one that Alexander himself likes most. Alexander plans to send to each friend exactly one card. Of course, Alexander can send the same card multiple times. Alexander and each his friend has the list of preferences, which is a permutation of integers from 1 to n. The first number in the list is the number of the favorite card, the second number shows the second favorite, and so on, the last number shows the least favorite card. Your task is to find a schedule of sending cards for Alexander. Determine at which moments of time Alexander must send cards to his friends, to please each of them as much as possible. In other words, so that as a result of applying two Alexander's rules, each friend receives the card that is preferred for him as much as possible. Note that Alexander doesn't choose freely what card to send, but he always strictly follows the two rules.
The first line contains an integer n (2 ≤ n ≤ 300) — the number of Alexander's friends, equal to the number of cards. Next n lines contain his friends' preference lists. Each list consists of n different integers from 1 to n. The last line contains Alexander's preference list in the same format.
Print n space-separated numbers: the i-th number should be the number of the friend, whose card Alexander receives right before he should send a card to the i-th friend. If there are several solutions, print any of them.
null
In the sample, the algorithm of actions Alexander and his friends perform is as follows: 1. Alexander receives card 1 from the first friend. 2. Alexander sends the card he has received (at the moment he only has one card, and therefore it is the most preferable for him) to friends with the numbers 2 and 3. 3. Alexander receives card 2 from the second friend, now he has two cards — 1 and 2. 4. Alexander sends a card to the first friend. Despite the fact that Alexander likes card 1 more, he sends card 2 as he cannot send a friend the card sent by that very friend. 5. Alexander receives card 3 from the third friend. 6. Alexander receives card 4 from the fourth friend. 7. Among the cards Alexander has number 3 is his favorite and he sends it to the fourth friend. Note that Alexander can send cards to multiple friends at a time (in this case the second and the third one). Alexander can send card 3 to the fourth friend after he receives the third card or after he receives the fourth card (both variants are correct).
[{"input": "4\n1 2 3 4\n4 1 3 2\n4 3 1 2\n3 4 2 1\n3 1 2 4", "output": "2 1 1 4"}]
1,800
["brute force", "greedy", "implementation"]
17
[{"input": "4\r\n1 2 3 4\r\n4 1 3 2\r\n4 3 1 2\r\n3 4 2 1\r\n3 1 2 4\r\n", "output": "2 1 1 3\r\n"}, {"input": "2\r\n1 2\r\n2 1\r\n2 1\r\n", "output": "2 1\r\n"}, {"input": "3\r\n1 2 3\r\n2 3 1\r\n1 3 2\r\n3 2 1\r\n", "output": "2 3 1\r\n"}, {"input": "5\r\n1 4 2 3 5\r\n5 1 3 4 2\r\n3 2 4 1 5\r\n1 4 5 3 2\r\n5 2 3 4 1\r\n5 4 2 1 3\r\n", "output": "4 5 2 1 2\r\n"}, {"input": "10\r\n5 1 6 2 8 3 4 10 9 7\r\n3 1 10 6 8 5 2 7 9 4\r\n2 9 1 4 10 6 8 7 3 5\r\n10 1 7 8 3 2 4 6 5 9\r\n3 2 10 4 7 8 5 6 1 9\r\n5 6 3 10 8 7 2 9 4 1\r\n6 5 1 3 2 7 9 10 8 4\r\n1 10 9 3 7 8 4 2 6 5\r\n6 8 4 5 9 1 2 10 7 3\r\n9 6 8 5 10 3 1 7 2 4\r\n5 7 4 8 9 6 1 10 3 2\r\n", "output": "5 1 1 1 4 5 5 1 4 5\r\n"}]
false
stdio
null
true
908/F
908
F
Python 3
PRETESTS
3
46
5,529,600
33788830
n=int(input()) ar=[input().split() for x in range(n)] G,R,B=[],[],[] for x in ar: if x[1]=='G':G.append(int(x[0])) elif x[1]=='R':R.append(int(x[0])) elif x[1]=='B':B.append(int(x[0])) sum=0 for i in range(len(G)-1): x1,x2=G[i],G[i+1] while R and R[0] < x1: sum+=abs(R[0]-x1) del R[0] while R and abs(R[0]-x1) < abs(R[0]-x2): sum+=abs(R[0]-x1) del R[0] while R and R[0] < x2: sum+=abs(R[0]-x2) del R[0] while B and B[0] < x1: sum+=abs(B[0]-x1) del B[0] while B and abs(B[0]-x1) < abs(B[0]-x2): sum+=abs(B[0]-x1) del B[0] while B and B[0] < x2: sum+=abs(B[0]-x2) del B[0] sum+=x2-x1 print(sum)
44
842
83,660,800
33823019
import sys n = int(input()) pos = [] ind = 0 inp = [s for line in sys.stdin.readlines() for s in line.split()] color = [] costs = 0 reds = [] greens = [] blues = [] for i in range(n): a,b = inp[ind],inp[ind+1] ind+=2 pos.append(int(a)) color.append(b) if b=='R': reds.append(i) elif b=='G': greens.append(i) else: blues.append(i) if len(greens)==0: if len(reds)>0: a = reds[0] b = reds[-1] costs += pos[b] - pos[a] if len(blues)>0: a = blues[0] b = blues[-1] costs += pos[b] - pos[a] print(costs) sys.exit() first_g = greens[0] last_g = greens[-1] if len(blues)>0 and blues[0]<=first_g: costs += pos[first_g] - pos[blues[0]] if len(reds)>0 and reds[0]<=first_g: costs += pos[first_g] - pos[reds[0]] if len(blues)>0 and blues[-1]>= last_g: costs += pos[blues[-1]] - pos[last_g] if len(reds)>0 and reds[-1] >= last_g: costs += pos[reds[-1]] - pos[last_g] i = first_g while True: j = i+1 REDS = [i] BLUES = [i] while j<n and color[j]!='G': if color[j]=='R': REDS.append(j) else: BLUES.append(j) j+=1 if j>=n: break REDS.append(j) BLUES.append(j) best_r = 0 for ind in range(len(REDS)-1): a = REDS[ind] b = REDS[ind+1] best_r = max(best_r,pos[b]-pos[a]) best_b = 0 for ind in range(len(BLUES)-1): a = BLUES[ind] b = BLUES[ind+1] best_b = max(best_b,pos[b]-pos[a]) costs += min(2*(pos[j]-pos[i]), 3*(pos[j]-pos[i])-best_r-best_b) i = j print(costs)
Good Bye 2017
CF
2,017
2
256
New Year and Rainbow Roads
Roy and Biv have a set of n points on the infinite number line. Each point has one of 3 colors: red, green, or blue. Roy and Biv would like to connect all the points with some edges. Edges can be drawn between any of the two of the given points. The cost of an edge is equal to the distance between the two points it connects. They want to do this in such a way that they will both see that all the points are connected (either directly or indirectly). However, there is a catch: Roy cannot see the color red and Biv cannot see the color blue. Therefore, they have to choose the edges in such a way that if all the red points are removed, the remaining blue and green points are connected (and similarly, if all the blue points are removed, the remaining red and green points are connected). Help them compute the minimum cost way to choose edges to satisfy the above constraints.
The first line will contain an integer n (1 ≤ n ≤ 300 000), the number of points. The next n lines will contain two tokens pi and ci (pi is an integer, 1 ≤ pi ≤ 109, ci is a uppercase English letter 'R', 'G' or 'B'), denoting the position of the i-th point and the color of the i-th point. 'R' means red, 'G' denotes green, and 'B' means blue. The positions will be in strictly increasing order.
Print a single integer, the minimum cost way to solve the problem.
null
In the first sample, it is optimal to draw edges between the points (1,2), (1,4), (3,4). These have costs 4, 14, 5, respectively.
[{"input": "4\n1 G\n5 R\n10 B\n15 G", "output": "23"}, {"input": "4\n1 G\n2 R\n3 B\n10 G", "output": "12"}]
2,400
["graphs", "greedy", "implementation"]
44
[{"input": "4\r\n1 G\r\n5 R\r\n10 B\r\n15 G\r\n", "output": "23\r\n"}, {"input": "4\r\n1 G\r\n2 R\r\n3 B\r\n10 G\r\n", "output": "12\r\n"}, {"input": "4\r\n1 G\r\n123123 R\r\n987987987 B\r\n1000000000 G\r\n", "output": "1012135134\r\n"}, {"input": "1\r\n3 R\r\n", "output": "0\r\n"}]
false
stdio
null
true
898/C
898
C
PyPy 3
TESTS
4
124
20,172,800
80229575
# itne me hi thakk gaye? n = int(input()) phone_book = {} for i in range(n): inp = input().split() name = inp[0] count = int(inp[1]) phs = [i for i in inp[2:]] if name in phone_book: v, inp_arr = phone_book[name] else: v, inp_arr = set(), [] for phonenumber in phs: if phonenumber in v: pass else: inp_arr.append(phonenumber) for i in range(len(phonenumber)): v.add(phonenumber[i:]) phone_book[name] = [v, inp_arr] # print(phone_book[name]) print(len(phone_book.keys())) for i in phone_book: print('{} {} '.format(i, len(phone_book[i][1])), end='') print(*phone_book[i][1])
59
62
5,529,600
33297607
n = int(input()) p = {} o = {} for i in range(n): e = input().split() if not e[0] in p: p[e[0]] = [] for x in e[2:]: p[e[0]].append((len(x), x[::-1])) for i in p: for x in sorted(p[i])[::-1]: f = False if not i in o: o[i] = [] for z in o[i]: f |= x[1]==z or (x[0]<len(z) and x[1]==z[:x[0]]) if not f: o[i].append(x[1]) print(len(o)) for i in o: print(i + ' ' + str(len(o[i])) + ' ' + ' '.join(x[::-1] for x in o[i]))
Codeforces Round 451 (Div. 2)
CF
2,017
2
256
Phone Numbers
Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers. Vasya decided to organize information about the phone numbers of friends. You will be given n strings — all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record. Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account. The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once. Read the examples to understand statement and format of the output better.
First line contains the integer n (1 ≤ n ≤ 20) — number of entries in Vasya's phone books. The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.
Print out the ordered information about the phone numbers of Vasya's friends. First output m — number of friends that are found in Vasya's phone books. The following m lines must contain entries in the following format "name number_of_phone_numbers phone_numbers". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend. Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.
null
null
[{"input": "2\nivan 1 00123\nmasha 1 00123", "output": "2\nmasha 1 00123\nivan 1 00123"}, {"input": "3\nkarl 2 612 12\npetr 1 12\nkatya 1 612", "output": "3\nkatya 1 612\npetr 1 12\nkarl 1 612"}, {"input": "4\nivan 3 123 123 456\nivan 2 456 456\nivan 8 789 3 23 6 56 9 89 2\ndasha 2 23 789", "output": "2\ndasha 2 23 789\nivan 4 789 123 2 456"}]
1,400
["implementation", "strings"]
59
[{"input": "2\r\nivan 1 00123\r\nmasha 1 00123\r\n", "output": "2\r\nmasha 1 00123 \r\nivan 1 00123 \r\n"}, {"input": "3\r\nkarl 2 612 12\r\npetr 1 12\r\nkatya 1 612\r\n", "output": "3\r\nkatya 1 612 \r\npetr 1 12 \r\nkarl 1 612 \r\n"}, {"input": "4\r\nivan 3 123 123 456\r\nivan 2 456 456\r\nivan 8 789 3 23 6 56 9 89 2\r\ndasha 2 23 789\r\n", "output": "2\r\ndasha 2 789 23 \r\nivan 4 789 123 456 2 \r\n"}, {"input": "20\r\nnxj 6 7 6 6 7 7 7\r\nnxj 10 8 5 1 7 6 1 0 7 0 6\r\nnxj 2 6 5\r\nnxj 10 6 7 6 6 5 8 3 6 6 8\r\nnxj 10 6 1 7 6 7 1 8 7 8 6\r\nnxj 10 8 5 8 6 5 6 1 9 6 3\r\nnxj 10 8 1 6 4 8 0 4 6 0 1\r\nnxj 9 2 6 6 8 1 1 3 6 6\r\nnxj 10 8 9 0 9 1 3 2 3 2 3\r\nnxj 6 6 7 0 8 1 2\r\nnxj 7 7 7 8 1 3 6 9\r\nnxj 10 2 7 0 1 5 1 9 1 2 6\r\nnxj 6 9 6 9 6 3 7\r\nnxj 9 0 1 7 8 2 6 6 5 6\r\nnxj 4 0 2 3 7\r\nnxj 10 0 4 0 6 1 1 8 8 4 7\r\nnxj 8 4 6 2 6 6 1 2 7\r\nnxj 10 5 3 4 2 1 0 7 0 7 6\r\nnxj 10 9 6 0 6 1 6 2 1 9 6\r\nnxj 4 2 9 0 1\r\n", "output": "1\r\nnxj 10 0 3 2 1 4 7 8 5 9 6 \r\n"}, {"input": "20\r\nl 6 02 02 2 02 02 2\r\nl 8 8 8 8 2 62 13 31 3\r\ne 9 0 91 0 0 60 91 60 2 44\r\ne 9 69 2 1 44 2 91 66 1 70\r\nl 9 7 27 27 3 1 3 7 80 81\r\nl 9 2 1 13 7 2 10 02 3 92\r\ne 9 0 15 3 5 5 15 91 09 44\r\nl 7 2 50 4 5 98 31 98\r\nl 3 26 7 3\r\ne 6 7 5 0 62 65 91\r\nl 8 80 0 4 0 2 2 0 13\r\nl 9 19 13 02 2 1 4 19 26 02\r\nl 10 7 39 7 9 22 22 26 2 90 4\r\ne 7 65 2 36 0 34 57 9\r\ne 8 13 02 09 91 73 5 36 62\r\nl 9 75 0 10 8 76 7 82 8 34\r\nl 7 34 0 19 80 6 4 7\r\ne 5 4 2 5 7 2\r\ne 7 4 02 69 7 07 20 2\r\nl 4 8 2 1 63\r\n", "output": "2\r\ne 18 15 62 07 70 91 57 02 66 65 69 09 13 20 44 73 34 60 36 \r\nl 21 27 50 22 63 75 19 26 90 02 92 62 31 10 76 82 80 98 81 39 34 13 \r\n"}, {"input": "20\r\no 10 6 6 97 45 6 6 6 6 5 6\r\nl 8 5 5 5 19 59 5 8 5\r\nj 9 2 30 58 2 2 1 0 30 4\r\nc 10 1 1 7 51 7 7 51 1 1 1\r\no 9 7 97 87 70 2 19 2 14 6\r\ne 6 26 6 6 6 26 5\r\ng 9 3 3 3 3 3 78 69 8 9\r\nl 8 8 01 1 5 8 41 72 3\r\nz 10 1 2 2 2 9 1 9 1 6 7\r\ng 8 7 78 05 36 7 3 67 9\r\no 5 6 9 9 7 7\r\ne 10 30 2 1 1 2 5 04 0 6 6\r\ne 9 30 30 2 2 0 26 30 79 8\r\nt 10 2 2 9 29 7 7 7 9 2 9\r\nc 7 7 51 1 31 2 7 4\r\nc 9 83 1 6 78 94 74 54 8 32\r\ng 8 4 1 01 9 39 28 6 6\r\nt 7 9 2 01 4 4 9 58\r\nj 5 0 1 58 02 4\r\nw 10 80 0 91 91 06 91 9 9 27 7\r\n", "output": "9\r\nw 5 91 27 06 9 80 \r\nt 6 7 58 4 29 2 01 \r\ne 8 8 79 5 30 2 26 04 1 \r\nl 8 72 3 19 59 41 01 5 8 \r\nj 5 4 30 58 1 02 \r\nz 5 6 1 9 2 7 \r\ng 10 05 39 4 3 36 01 67 69 28 78 \r\no 8 14 87 97 6 19 70 45 2 \r\nc 10 83 51 31 54 74 32 7 94 78 6 \r\n"}, {"input": "1\r\negew 5 3 123 23 1234 134\r\n", "output": "1\r\negew 3 123 134 1234 \r\n"}]
false
stdio
import sys from collections import defaultdict def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] # Read input and build friends data friends = defaultdict(set) with open(input_path, 'r') as f: n = int(f.readline()) for _ in range(n): parts = f.readline().strip().split() name = parts[0] numbers = parts[2:] friends[name].update(numbers) # Add all numbers, duplicates in same line are handled via set # Read submission output submission_friends = set() try: with open(submission_path, 'r') as f: lines = f.read().splitlines() except: print(0) return if not lines: print(0) return # Check m line try: m_line = lines[0] m = int(m_line) except: print(0) return if m != len(friends): print(0) return submission_lines = lines[1:] if len(submission_lines) != m: print(0) return for line in submission_lines: parts = line.strip().split() if len(parts) < 2: print(0) return name = parts[0] if name not in friends: print(0) return submission_friends.add(name) try: k = int(parts[1]) except: print(0) return numbers = parts[2:] if len(numbers) != k: print(0) return # Check for duplicates in submission's numbers if len(set(numbers)) != k: print(0) return # Check all numbers are present in friends' merged set merged_numbers = friends[name] for num in numbers: if num not in merged_numbers: print(0) return # Check no two numbers in submission are suffix of each other for i in range(len(numbers)): for j in range(i+1, len(numbers)): a = numbers[i] b = numbers[j] if (len(a) >= len(b) and a.endswith(b)) or (len(b) >= len(a) and b.endswith(a)): print(0) return # Check every merged number is either in submission or is a suffix of some submission number for merged_num in merged_numbers: if merged_num in numbers: continue found = False for num in numbers: if len(merged_num) <= len(num) and num.endswith(merged_num): found = True break if not found: print(0) return # Check all friends are present in submission if submission_friends != set(friends.keys()): print(0) return # All checks passed print(100) if __name__ == "__main__": main()
true
617/C
617
C
PyPy 3
TESTS
3
77
0
228381968
import math import sys #input = sys.stdin.readline USE_FILE = False def calc_distance(x1, y1, x2, y2): return (x1 - x2) ** 2 + (y1 - y2) ** 2 def main(): n, x1, y1, x2, y2 = [int(i) for i in input().split()] points = [] d = [] for i in range(n): x, y = tuple(map(int, input().split())) d.append((calc_distance(x1, y1, x, y), calc_distance(x2, y2, x, y))) max_r1 = 0 max_r2 = 0 for dp1, dp2 in d: if dp1 < dp2 and max_r2 < dp2: max_r1 = max(max_r1, dp1) elif max_r1 < dp1: max_r2 = max(max_r2, dp2) return max_r1 + max_r2 if __name__=="__main__": if USE_FILE: import sys sys.stdin = open('/home/stefan/input.txt', 'r') print(main())
31
93
512,000
15525144
import math n, x1, y1, x2, y2 = [int(x) for x in input().split()] points = [] for i in range(n): points.append(tuple(int(x) for x in input().split())) d = lambda p1, p2: (p1[0] - p2[0])**2 + (p1[1] - p2[1])**2 first = (x1, y1) second = (x2, y2) distances = [(d(first, point), d(second, point)) for point in points] distances.sort() maxtaild = [0 for i in range(n+1)] for i in range(n-1, -1, -1): maxtaild[i] = max(maxtaild[i+1], distances[i][1]) print(min(maxtaild[0], min(distances[i][0] + maxtaild[i+1] for i in range(n))))
Codeforces Round 340 (Div. 2)
CF
2,016
2
256
Watering Flowers
A flowerbed has many flowers and two fountains. You can adjust the water pressure and set any values r1(r1 ≥ 0) and r2(r2 ≥ 0), giving the distances at which the water is spread from the first and second fountain respectively. You have to set such r1 and r2 that all the flowers are watered, that is, for each flower, the distance between the flower and the first fountain doesn't exceed r1, or the distance to the second fountain doesn't exceed r2. It's OK if some flowers are watered by both fountains. You need to decrease the amount of water you need, that is set such r1 and r2 that all the flowers are watered and the r12 + r22 is minimum possible. Find this minimum value.
The first line of the input contains integers n, x1, y1, x2, y2 (1 ≤ n ≤ 2000, - 107 ≤ x1, y1, x2, y2 ≤ 107) — the number of flowers, the coordinates of the first and the second fountain. Next follow n lines. The i-th of these lines contains integers xi and yi ( - 107 ≤ xi, yi ≤ 107) — the coordinates of the i-th flower. It is guaranteed that all n + 2 points in the input are distinct.
Print the minimum possible value r12 + r22. Note, that in this problem optimal answer is always integer.
null
The first sample is (r12 = 5, r22 = 1): The second sample is (r12 = 1, r22 = 32):
[{"input": "2 -1 0 5 3\n0 2\n5 2", "output": "6"}, {"input": "4 0 0 5 0\n9 4\n8 3\n-1 0\n1 4", "output": "33"}]
1,600
["implementation"]
31
[{"input": "2 -1 0 5 3\r\n0 2\r\n5 2\r\n", "output": "6\r\n"}, {"input": "4 0 0 5 0\r\n9 4\r\n8 3\r\n-1 0\r\n1 4\r\n", "output": "33\r\n"}, {"input": "5 -6 -4 0 10\r\n-7 6\r\n-9 7\r\n-5 -1\r\n-2 1\r\n-8 10\r\n", "output": "100\r\n"}, {"input": "10 -68 10 87 22\r\n30 89\r\n82 -97\r\n-52 25\r\n76 -22\r\n-20 95\r\n21 25\r\n2 -3\r\n45 -7\r\n-98 -56\r\n-15 16\r\n", "output": "22034\r\n"}, {"input": "1 -10000000 -10000000 -10000000 -9999999\r\n10000000 10000000\r\n", "output": "799999960000001\r\n"}]
false
stdio
null
true
908/F
908
F
PyPy 3-64
TESTS
12
1,964
87,347,200
203971619
def calculate_minimum_cost(n, points): total_cost = 0 partial_cost = 0 max_red = max_blue = 0 last_red = last_blue = last_green = -1 for i in range(n): pos, color = points[i] if color in ('R', 'G'): if last_red != -1: max_red = max(max_red, pos - last_red) partial_cost += pos - last_red last_red = pos if color in ('B', 'G'): if last_blue != -1: max_blue = max(max_blue, pos - last_blue) partial_cost += pos - last_blue last_blue = pos if color == 'G': total_cost += partial_cost + min(0, -max_red - max_blue + pos - last_green) last_red = last_blue = last_green = pos max_red = max_blue = partial_cost = 0 total_cost += partial_cost return total_cost if __name__ == '__main__': n = int(input()) points = [input().split() for _ in range(n)] points = [(int(p), c) for p, c in points] minimum_cost = calculate_minimum_cost(n, points) print(minimum_cost)
44
1,107
204,800
113280821
#collaborated with Prasoon Shakya n = int(input()) R,G,B=0,0,0 maxR,maxG,maxB=0,0,0 Cost = 0 for i in range(n): temp_input = input().split(" ") value = int(temp_input[0]) choice=temp_input[1] if choice == "R" or choice == "G": if R: Cost += value - R maxR = max(maxR, value - R) R = value if choice == "B" or choice == "G": if B: Cost += value - B maxB = max(maxB, value - B) B = value if choice == "G": if G: Cost += min(0, value - G - maxR - maxB) G = value maxR = 0 maxB = 0 print(Cost)
Good Bye 2017
CF
2,017
2
256
New Year and Rainbow Roads
Roy and Biv have a set of n points on the infinite number line. Each point has one of 3 colors: red, green, or blue. Roy and Biv would like to connect all the points with some edges. Edges can be drawn between any of the two of the given points. The cost of an edge is equal to the distance between the two points it connects. They want to do this in such a way that they will both see that all the points are connected (either directly or indirectly). However, there is a catch: Roy cannot see the color red and Biv cannot see the color blue. Therefore, they have to choose the edges in such a way that if all the red points are removed, the remaining blue and green points are connected (and similarly, if all the blue points are removed, the remaining red and green points are connected). Help them compute the minimum cost way to choose edges to satisfy the above constraints.
The first line will contain an integer n (1 ≤ n ≤ 300 000), the number of points. The next n lines will contain two tokens pi and ci (pi is an integer, 1 ≤ pi ≤ 109, ci is a uppercase English letter 'R', 'G' or 'B'), denoting the position of the i-th point and the color of the i-th point. 'R' means red, 'G' denotes green, and 'B' means blue. The positions will be in strictly increasing order.
Print a single integer, the minimum cost way to solve the problem.
null
In the first sample, it is optimal to draw edges between the points (1,2), (1,4), (3,4). These have costs 4, 14, 5, respectively.
[{"input": "4\n1 G\n5 R\n10 B\n15 G", "output": "23"}, {"input": "4\n1 G\n2 R\n3 B\n10 G", "output": "12"}]
2,400
["graphs", "greedy", "implementation"]
44
[{"input": "4\r\n1 G\r\n5 R\r\n10 B\r\n15 G\r\n", "output": "23\r\n"}, {"input": "4\r\n1 G\r\n2 R\r\n3 B\r\n10 G\r\n", "output": "12\r\n"}, {"input": "4\r\n1 G\r\n123123 R\r\n987987987 B\r\n1000000000 G\r\n", "output": "1012135134\r\n"}, {"input": "1\r\n3 R\r\n", "output": "0\r\n"}]
false
stdio
null
true
1006/D
1006
D
PyPy 3
TESTS
2
155
23,654,400
122371601
for _ in range(1): n=int(input()) st=input() rt=input() l=len(st) i=0 j=l-1 count=0 while i <=j: if i==j: count =count +0 if st[i] ==rt[j] else count +1 elif st[i] ==rt[i] and st[j] ==rt[j]: count +=0 elif st[i] ==rt[j] and rt[i] ==st[j]: count +=0 elif st[i] ==st[j] and rt[i] ==rt[j]: count +=0 else: s=set() s.add(st[i]) s.add(st[j]) s.add(rt[i]) s.add(rt[j]) if len(s) ==2: count +=1 elif len(s) ==3: if st[i] ==st[j] or rt[i] ==rt[j]: count +=2 else: count +=1 else: count +=2 i+=1 j-=1 print(count)
23
61
614,400
204964848
n=int(input()) a,b=input(),input() k=n//2 c=a[k]!=b[k]and n%2 for z,w,x,y in zip(a[:k],a[::-1],b,b[::-1]):c+=z!=w if x==y else len({x,y}-{z,w}) print(c)
Codeforces Round 498 (Div. 3)
ICPC
2,018
2
256
Two Strings Swaps
You are given two strings $$$a$$$ and $$$b$$$ consisting of lowercase English letters, both of length $$$n$$$. The characters of both strings have indices from $$$1$$$ to $$$n$$$, inclusive. You are allowed to do the following changes: - Choose any index $$$i$$$ ($$$1 \le i \le n$$$) and swap characters $$$a_i$$$ and $$$b_i$$$; - Choose any index $$$i$$$ ($$$1 \le i \le n$$$) and swap characters $$$a_i$$$ and $$$a_{n - i + 1}$$$; - Choose any index $$$i$$$ ($$$1 \le i \le n$$$) and swap characters $$$b_i$$$ and $$$b_{n - i + 1}$$$. Note that if $$$n$$$ is odd, you are formally allowed to swap $$$a_{\lceil\frac{n}{2}\rceil}$$$ with $$$a_{\lceil\frac{n}{2}\rceil}$$$ (and the same with the string $$$b$$$) but this move is useless. Also you can swap two equal characters but this operation is useless as well. You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps. In one preprocess move you can replace a character in $$$a$$$ with another character. In other words, in a single preprocess move you can choose any index $$$i$$$ ($$$1 \le i \le n$$$), any character $$$c$$$ and set $$$a_i := c$$$. Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings $$$a$$$ and $$$b$$$ equal by applying some number of changes described in the list above. Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string $$$b$$$ or make any preprocess moves after the first change is made.
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of strings $$$a$$$ and $$$b$$$. The second line contains the string $$$a$$$ consisting of exactly $$$n$$$ lowercase English letters. The third line contains the string $$$b$$$ consisting of exactly $$$n$$$ lowercase English letters.
Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string $$$a$$$ equal to string $$$b$$$ with a sequence of changes from the list above.
null
In the first example preprocess moves are as follows: $$$a_1 := $$$'b', $$$a_3 := $$$'c', $$$a_4 := $$$'a' and $$$a_5:=$$$'b'. Afterwards, $$$a = $$$"bbcabba". Then we can obtain equal strings by the following sequence of changes: $$$swap(a_2, b_2)$$$ and $$$swap(a_2, a_6)$$$. There is no way to use fewer than $$$4$$$ preprocess moves before a sequence of changes to make string equal, so the answer in this example is $$$4$$$. In the second example no preprocess moves are required. We can use the following sequence of changes to make $$$a$$$ and $$$b$$$ equal: $$$swap(b_1, b_5)$$$, $$$swap(a_2, a_4)$$$.
[{"input": "7\nabacaba\nbacabaa", "output": "4"}, {"input": "5\nzcabd\ndbacz", "output": "0"}]
1,700
["implementation"]
23
[{"input": "7\r\nabacaba\r\nbacabaa\r\n", "output": "4\r\n"}, {"input": "5\r\nzcabd\r\ndbacz\r\n", "output": "0\r\n"}, {"input": "1\r\na\r\nb\r\n", "output": "1\r\n"}, {"input": "5\r\nahmad\r\nyogaa\r\n", "output": "3\r\n"}]
false
stdio
null
true
1006/D
1006
D
Python 3
TESTS
2
171
819,200
105080961
n= int(input()) a=input() b=input() ans=0 if n%2==0: i2=n//2 else: i2=n//2+1 for i in range(i2): if i==i2-1: if n%2!=0: if a[i]!=b[i]: ans+=1 #print('a') else: if a[i]==a[n-i-1] or b[i]==b[n-i-1]: if not (a[i]==b[i] and a[i]==b[n-i-1] and a[i]==a[n-i-1]): ans+=1 #print('b') else: if not (a[i]==b[i] and a[n-i-1]==b[n-i-1])or (a[i]==b[n-i-1] and a[n-i-1]==b[i]): if a[i]==b[i] or a[n-i-1]==b[n-i-1] or a[i]==b[n-i-1] or b[i]==a[n-i-1]: ans+=1 #print('c') else: #print('d') ans+=2 else: if a[i]==a[n-i-1] or b[i]==b[n-i-1]: if not (a[i]==b[i] and a[i]==b[n-i-1] and a[i]==a[n-i-1]): ans+=1 #print('e') else: if not ((a[i]==b[i] and a[n-i-1]==b[n-i-1])or (a[i]==b[n-i-1] and a[n-i-1]==b[i])): if a[i]==b[i] or a[n-i-1]==b[n-i-1] or a[i]==b[n-i-1] or b[i]==a[n-i-1]: ans+=1 #print('f') else: ans+=2 #print('g') print(ans)
23
77
409,600
197554073
n=int(input()) a,b=input(),input() k=n//2 c=a[k]!=b[k]and n%2 for u,v,x,y in zip(a[:k],a[::-1],b,b[::-1]):c+=(len({x,y}-{u,v}),u!=v)[x==y] print(c)
Codeforces Round 498 (Div. 3)
ICPC
2,018
2
256
Two Strings Swaps
You are given two strings $$$a$$$ and $$$b$$$ consisting of lowercase English letters, both of length $$$n$$$. The characters of both strings have indices from $$$1$$$ to $$$n$$$, inclusive. You are allowed to do the following changes: - Choose any index $$$i$$$ ($$$1 \le i \le n$$$) and swap characters $$$a_i$$$ and $$$b_i$$$; - Choose any index $$$i$$$ ($$$1 \le i \le n$$$) and swap characters $$$a_i$$$ and $$$a_{n - i + 1}$$$; - Choose any index $$$i$$$ ($$$1 \le i \le n$$$) and swap characters $$$b_i$$$ and $$$b_{n - i + 1}$$$. Note that if $$$n$$$ is odd, you are formally allowed to swap $$$a_{\lceil\frac{n}{2}\rceil}$$$ with $$$a_{\lceil\frac{n}{2}\rceil}$$$ (and the same with the string $$$b$$$) but this move is useless. Also you can swap two equal characters but this operation is useless as well. You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps. In one preprocess move you can replace a character in $$$a$$$ with another character. In other words, in a single preprocess move you can choose any index $$$i$$$ ($$$1 \le i \le n$$$), any character $$$c$$$ and set $$$a_i := c$$$. Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings $$$a$$$ and $$$b$$$ equal by applying some number of changes described in the list above. Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string $$$b$$$ or make any preprocess moves after the first change is made.
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of strings $$$a$$$ and $$$b$$$. The second line contains the string $$$a$$$ consisting of exactly $$$n$$$ lowercase English letters. The third line contains the string $$$b$$$ consisting of exactly $$$n$$$ lowercase English letters.
Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string $$$a$$$ equal to string $$$b$$$ with a sequence of changes from the list above.
null
In the first example preprocess moves are as follows: $$$a_1 := $$$'b', $$$a_3 := $$$'c', $$$a_4 := $$$'a' and $$$a_5:=$$$'b'. Afterwards, $$$a = $$$"bbcabba". Then we can obtain equal strings by the following sequence of changes: $$$swap(a_2, b_2)$$$ and $$$swap(a_2, a_6)$$$. There is no way to use fewer than $$$4$$$ preprocess moves before a sequence of changes to make string equal, so the answer in this example is $$$4$$$. In the second example no preprocess moves are required. We can use the following sequence of changes to make $$$a$$$ and $$$b$$$ equal: $$$swap(b_1, b_5)$$$, $$$swap(a_2, a_4)$$$.
[{"input": "7\nabacaba\nbacabaa", "output": "4"}, {"input": "5\nzcabd\ndbacz", "output": "0"}]
1,700
["implementation"]
23
[{"input": "7\r\nabacaba\r\nbacabaa\r\n", "output": "4\r\n"}, {"input": "5\r\nzcabd\r\ndbacz\r\n", "output": "0\r\n"}, {"input": "1\r\na\r\nb\r\n", "output": "1\r\n"}, {"input": "5\r\nahmad\r\nyogaa\r\n", "output": "3\r\n"}]
false
stdio
null
true
1006/D
1006
D
PyPy 3
TESTS
2
171
28,364,800
86324973
n = int(input()) a = list(input()) b = list(input()) ans=0 for i in range(n//2): # print(i,n-i-1,a,b) l1 = [a[i],a[n-i-1]] l2 = [b[i],b[n-i-1]] # print(set(l1+l2)) if len(set(l1+l2))==4: ans+=2 elif len(set(l1+l2))==3: if l1[0]==l1[1]: ans+=2 elif l1[0]==l2[0] or l1[0]==l2[1]: ans+=1 elif l1[1]==l2[0] or l1[1]==l2[1]: ans+=1 elif len(set(l1+l2))==2: aa=l1+l2 if aa.count(l1[0])!=2: ans+=1 # print(ans) if n%2: if a[n//2]!=b[n//2]: ans+=1 print(ans)
23
77
512,000
214462893
n=int(input()) a=input() b=input() ans=0 if n%2!=0: if a[n//2]!=b[n//2]: ans+=1 for i in range(n//2): if b[i]==b[n-1-i]: if a[i]!=a[n-1-i]: ans+=1 continue if b[i]!=a[i] and b[i]!=a[n-1-i]: ans+=1 if b[n-1-i]!=a[i] and b[n-1-i]!=a[n-1-i]: ans+=1 print(ans)
Codeforces Round 498 (Div. 3)
ICPC
2,018
2
256
Two Strings Swaps
You are given two strings $$$a$$$ and $$$b$$$ consisting of lowercase English letters, both of length $$$n$$$. The characters of both strings have indices from $$$1$$$ to $$$n$$$, inclusive. You are allowed to do the following changes: - Choose any index $$$i$$$ ($$$1 \le i \le n$$$) and swap characters $$$a_i$$$ and $$$b_i$$$; - Choose any index $$$i$$$ ($$$1 \le i \le n$$$) and swap characters $$$a_i$$$ and $$$a_{n - i + 1}$$$; - Choose any index $$$i$$$ ($$$1 \le i \le n$$$) and swap characters $$$b_i$$$ and $$$b_{n - i + 1}$$$. Note that if $$$n$$$ is odd, you are formally allowed to swap $$$a_{\lceil\frac{n}{2}\rceil}$$$ with $$$a_{\lceil\frac{n}{2}\rceil}$$$ (and the same with the string $$$b$$$) but this move is useless. Also you can swap two equal characters but this operation is useless as well. You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps. In one preprocess move you can replace a character in $$$a$$$ with another character. In other words, in a single preprocess move you can choose any index $$$i$$$ ($$$1 \le i \le n$$$), any character $$$c$$$ and set $$$a_i := c$$$. Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings $$$a$$$ and $$$b$$$ equal by applying some number of changes described in the list above. Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string $$$b$$$ or make any preprocess moves after the first change is made.
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of strings $$$a$$$ and $$$b$$$. The second line contains the string $$$a$$$ consisting of exactly $$$n$$$ lowercase English letters. The third line contains the string $$$b$$$ consisting of exactly $$$n$$$ lowercase English letters.
Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string $$$a$$$ equal to string $$$b$$$ with a sequence of changes from the list above.
null
In the first example preprocess moves are as follows: $$$a_1 := $$$'b', $$$a_3 := $$$'c', $$$a_4 := $$$'a' and $$$a_5:=$$$'b'. Afterwards, $$$a = $$$"bbcabba". Then we can obtain equal strings by the following sequence of changes: $$$swap(a_2, b_2)$$$ and $$$swap(a_2, a_6)$$$. There is no way to use fewer than $$$4$$$ preprocess moves before a sequence of changes to make string equal, so the answer in this example is $$$4$$$. In the second example no preprocess moves are required. We can use the following sequence of changes to make $$$a$$$ and $$$b$$$ equal: $$$swap(b_1, b_5)$$$, $$$swap(a_2, a_4)$$$.
[{"input": "7\nabacaba\nbacabaa", "output": "4"}, {"input": "5\nzcabd\ndbacz", "output": "0"}]
1,700
["implementation"]
23
[{"input": "7\r\nabacaba\r\nbacabaa\r\n", "output": "4\r\n"}, {"input": "5\r\nzcabd\r\ndbacz\r\n", "output": "0\r\n"}, {"input": "1\r\na\r\nb\r\n", "output": "1\r\n"}, {"input": "5\r\nahmad\r\nyogaa\r\n", "output": "3\r\n"}]
false
stdio
null
true
1006/D
1006
D
Python 3
TESTS
2
187
716,800
105091812
n= int(input()) a=input() b=input() ans=0 if n%2==0: i2=n//2 else: i2=n//2+1 for i in range(i2): if i==i2-1: if n%2!=0: if a[i]!=b[i]: ans+=1 else: ans1=0 s1=[a[i],a[n-i-1],b[i],b[n-i-1]] d=dict() for j in s1: if j not in d: d[j]=1 else: d[j]+=1 for j in d: ans1+=d[j]//2 if ans1==0: ans+=2 elif ans1==1: ans+=1 else: ans1=0 s1=[a[i],a[n-i-1],b[i],b[n-i-1]] d=dict() ans2=0 for j in s1: if j not in d: d[j]=1 else: d[j]+=1 for j in d: ans1+=d[j]//2 if ans2<d[j]: ans2=d[j] if ans1==0: ans+=2 elif ans1==1 and (s1[0]==s1[1] or s1[2]==s1[3]) and ans2==2: ans+=2 elif ans1==1: ans+=1 print(ans)
23
77
2,867,200
178717549
n=int(input()) a=input() b=input() ans=0 if n%2==0: for i in range(n//2): temp=len(set([a[i],b[i],a[n-1-i],b[n-1-i]])) if temp==4 or (temp==3 and a[i]==a[n-1-i]): ans+=2 elif temp==3: ans+=1 elif temp==2 and [a[i],b[i],a[n-1-i],b[n-1-i]].count(a[i])!=2: ans+=1 print(ans) else: if a[(n-1)//2]!=b[(n-1)//2]: ans+=1 for i in range(n//2): temp=len(set([a[i],b[i],a[n-1-i],b[n-1-i]])) if temp==4 or (temp==3 and a[i]==a[n-1-i]): ans+=2 elif temp==3: ans+=1 elif temp==2 and [a[i],b[i],a[n-1-i],b[n-1-i]].count(a[i])!=2: ans+=1 print(ans)
Codeforces Round 498 (Div. 3)
ICPC
2,018
2
256
Two Strings Swaps
You are given two strings $$$a$$$ and $$$b$$$ consisting of lowercase English letters, both of length $$$n$$$. The characters of both strings have indices from $$$1$$$ to $$$n$$$, inclusive. You are allowed to do the following changes: - Choose any index $$$i$$$ ($$$1 \le i \le n$$$) and swap characters $$$a_i$$$ and $$$b_i$$$; - Choose any index $$$i$$$ ($$$1 \le i \le n$$$) and swap characters $$$a_i$$$ and $$$a_{n - i + 1}$$$; - Choose any index $$$i$$$ ($$$1 \le i \le n$$$) and swap characters $$$b_i$$$ and $$$b_{n - i + 1}$$$. Note that if $$$n$$$ is odd, you are formally allowed to swap $$$a_{\lceil\frac{n}{2}\rceil}$$$ with $$$a_{\lceil\frac{n}{2}\rceil}$$$ (and the same with the string $$$b$$$) but this move is useless. Also you can swap two equal characters but this operation is useless as well. You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps. In one preprocess move you can replace a character in $$$a$$$ with another character. In other words, in a single preprocess move you can choose any index $$$i$$$ ($$$1 \le i \le n$$$), any character $$$c$$$ and set $$$a_i := c$$$. Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings $$$a$$$ and $$$b$$$ equal by applying some number of changes described in the list above. Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string $$$b$$$ or make any preprocess moves after the first change is made.
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of strings $$$a$$$ and $$$b$$$. The second line contains the string $$$a$$$ consisting of exactly $$$n$$$ lowercase English letters. The third line contains the string $$$b$$$ consisting of exactly $$$n$$$ lowercase English letters.
Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string $$$a$$$ equal to string $$$b$$$ with a sequence of changes from the list above.
null
In the first example preprocess moves are as follows: $$$a_1 := $$$'b', $$$a_3 := $$$'c', $$$a_4 := $$$'a' and $$$a_5:=$$$'b'. Afterwards, $$$a = $$$"bbcabba". Then we can obtain equal strings by the following sequence of changes: $$$swap(a_2, b_2)$$$ and $$$swap(a_2, a_6)$$$. There is no way to use fewer than $$$4$$$ preprocess moves before a sequence of changes to make string equal, so the answer in this example is $$$4$$$. In the second example no preprocess moves are required. We can use the following sequence of changes to make $$$a$$$ and $$$b$$$ equal: $$$swap(b_1, b_5)$$$, $$$swap(a_2, a_4)$$$.
[{"input": "7\nabacaba\nbacabaa", "output": "4"}, {"input": "5\nzcabd\ndbacz", "output": "0"}]
1,700
["implementation"]
23
[{"input": "7\r\nabacaba\r\nbacabaa\r\n", "output": "4\r\n"}, {"input": "5\r\nzcabd\r\ndbacz\r\n", "output": "0\r\n"}, {"input": "1\r\na\r\nb\r\n", "output": "1\r\n"}, {"input": "5\r\nahmad\r\nyogaa\r\n", "output": "3\r\n"}]
false
stdio
null
true
267/A
267
A
Python 3
TESTS
5
62
307,200
103888044
n = int(input()) otv = 0 for i in range(n): a, b = map(int, input().split()) while (a > 0) and (b > 0): if abs(a - b) > 1000: otv = max(a,b) // min(a,b) if a > b: a = max(a,b) % min(a,b) else: b = max(a,b) % min(a,b) else: if a > b: a -= b otv += 1 else: b -= a otv += 1 print(otv) otv = 0
35
46
0
149466395
t = int(input()) while t > 0: t-=1 counter = int(0) a, b = map(int, input().split()) while a != 0 and b != 0 : if(a < b): a, b = b, a counter += (a // b) a %= b print(counter)
Codeforces Testing Round 5
CF
2,013
1
256
Subtractions
You've got two numbers. As long as they are both larger than zero, they go through the same operation: subtract the lesser number from the larger one. If they equal substract one number from the another. For example, one operation transforms pair (4,17) to pair (4,13), it transforms (5,5) to (0,5). You've got some number of pairs (ai, bi). How many operations will be performed for each of them?
The first line contains the number of pairs n (1 ≤ n ≤ 1000). Then follow n lines, each line contains a pair of positive integers ai, bi (1 ≤ ai, bi ≤ 109).
Print the sought number of operations for each pair on a single line.
null
null
[{"input": "2\n4 17\n7 987654321", "output": "8\n141093479"}]
900
["math", "number theory"]
35
[{"input": "2\r\n4 17\r\n7 987654321\r\n", "output": "8\r\n141093479\r\n"}, {"input": "10\r\n7 987654321\r\n7 987654321\r\n7 987654321\r\n7 987654321\r\n7 987654321\r\n7 987654321\r\n7 987654321\r\n7 987654321\r\n7 987654321\r\n7 987654321\r\n", "output": "141093479\r\n141093479\r\n141093479\r\n141093479\r\n141093479\r\n141093479\r\n141093479\r\n141093479\r\n141093479\r\n141093479\r\n"}, {"input": "1\r\n536870912 32\r\n", "output": "16777216\r\n"}, {"input": "20\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n1000000000 999999999\r\n", "output": "1000000000\r\n1000000000\r\n1000000000\r\n1000000000\r\n1000000000\r\n1000000000\r\n1000000000\r\n1000000000\r\n1000000000\r\n1000000000\r\n1000000000\r\n1000000000\r\n1000000000\r\n1000000000\r\n1000000000\r\n1000000000\r\n1000000000\r\n1000000000\r\n1000000000\r\n1000000000\r\n"}, {"input": "3\r\n1000000000 1\r\n1000000000 1\r\n1 100000000\r\n", "output": "1000000000\r\n1000000000\r\n100000000\r\n"}]
false
stdio
null
true
847/K
847
K
Python 3
TESTS
0
15
0
206245698
n, a, b, k, f = map(int, input().split()) stops = {} total_cost = 0 for _ in range(n): start_stop, finish_stop = input().split() if start_stop != finish_stop: stops[(start_stop, finish_stop)] = stops.get((start_stop, finish_stop), 0) + 1 total_cost += a else: total_cost += b sorted_stops = sorted(stops.items(), key=lambda x: x[1], reverse=True) for pair, frequency in sorted_stops: if k > 0 and frequency > 0: k -= 1 total_cost -= a stops[pair] -= 1 total_cost += f print(total_cost)
55
92
0
150973369
n,a,b,k,f = map(int,input().split()) poezd = {} sum = 0 prev = "" for i in range(n): ost =ost_key= input() ost_arr = ost.split() if ost not in poezd.keys(): if ost_arr[1]+" "+ost_arr[0] in poezd.keys(): ost_key = ost_arr[1]+" "+ost_arr[0] else: poezd[ost_key] = {"a":0,"b":0} if prev != ost.split()[0]: sum+=a poezd[ost_key]["a"]+=1 else: sum+=b poezd[ost_key]["b"] += 1 prev = ost.split()[1] sorted_cities = sorted(poezd.items(), key=lambda x: (x[1]['a'],x[1]['b'])) sorted_cities.reverse() min_sum = sum min_arr = [] countofk = 0 for i in range(len(sorted_cities)): min_arr.append(sorted_cities[i][1]['a']*a+sorted_cities[i][1]['b']*b-f) min_arr.sort() min_arr.reverse() for i in range(min(k,len(min_arr))): if min_sum-min_arr[i]<min_sum: min_sum-=min_arr[i] else: break print(min_sum)
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (Online Mirror, ACM-ICPC Rules, Teams Preferred)
ICPC
2,017
4
256
Travel Cards
In the evening Polycarp decided to analyze his today's travel expenses on public transport. The bus system in the capital of Berland is arranged in such a way that each bus runs along the route between two stops. Each bus has no intermediate stops. So each of the buses continuously runs along the route from one stop to the other and back. There is at most one bus running between a pair of stops. Polycarp made n trips on buses. About each trip the stop where he started the trip and the the stop where he finished are known. The trips follow in the chronological order in Polycarp's notes. It is known that one trip on any bus costs a burles. In case when passenger makes a transshipment the cost of trip decreases to b burles (b < a). A passenger makes a transshipment if the stop on which he boards the bus coincides with the stop where he left the previous bus. Obviously, the first trip can not be made with transshipment. For example, if Polycarp made three consecutive trips: "BerBank" $$\rightarrow$$ "University", "University" $$\rightarrow$$ "BerMall", "University" $$\rightarrow$$ "BerBank", then he payed a + b + a = 2a + b burles. From the BerBank he arrived to the University, where he made transshipment to the other bus and departed to the BerMall. Then he walked to the University and returned to the BerBank by bus. Also Polycarp can buy no more than k travel cards. Each travel card costs f burles. The travel card for a single bus route makes free of charge any trip by this route (in both directions). Once purchased, a travel card can be used any number of times in any direction. What is the smallest amount of money Polycarp could have spent today if he can buy no more than k travel cards?
The first line contains five integers n, a, b, k, f (1 ≤ n ≤ 300, 1 ≤ b < a ≤ 100, 0 ≤ k ≤ 300, 1 ≤ f ≤ 1000) where: - n — the number of Polycarp trips, - a — the cost of a regualar single trip, - b — the cost of a trip after a transshipment, - k — the maximum number of travel cards Polycarp can buy, - f — the cost of a single travel card. The following n lines describe the trips in the chronological order. Each line contains exactly two different words separated by a single space — the name of the start stop and the name of the finish stop of the trip. All names consist of uppercase and lowercase English letters and have lengths between 1 to 20 letters inclusive. Uppercase and lowercase letters should be considered different.
Print the smallest amount of money Polycarp could have spent today, if he can purchase no more than k travel cards.
null
In the first example Polycarp can buy travel card for the route "BerBank $$\leftarrow$$ University" and spend 8 burles. Note that his second trip "University" $$\rightarrow$$ "BerMall" was made after transshipment, so for this trip Polycarp payed 3 burles. So the minimum total sum equals to 8 + 3 = 11 burles. In the second example it doesn't make sense to buy travel cards. Note that each of Polycarp trip (except the first) was made with transshipment. So the minimum total sum equals to 2 + 1 + 1 + 1 = 5 burles.
[{"input": "3 5 3 1 8\nBerBank University\nUniversity BerMall\nUniversity BerBank", "output": "11"}, {"input": "4 2 1 300 1000\na A\nA aa\naa AA\nAA a", "output": "5"}]
1,800
["greedy", "implementation", "sortings"]
55
[{"input": "3 5 3 1 8\r\nBerBank University\r\nUniversity BerMall\r\nUniversity BerBank\r\n", "output": "11\r\n"}, {"input": "4 2 1 300 1000\r\na A\r\nA aa\r\naa AA\r\nAA a\r\n", "output": "5\r\n"}, {"input": "2 2 1 0 1\r\naca BCBA\r\nBCBA aca\r\n", "output": "3\r\n"}, {"input": "2 2 1 2 1\r\nBDDB C\r\nC BDDB\r\n", "output": "1\r\n"}, {"input": "2 3 1 1 9\r\nbacAB aAdb\r\nbacAB aAdb\r\n", "output": "6\r\n"}, {"input": "2 3 1 4 6\r\nAaCdC CdD\r\naBACc CdD\r\n", "output": "6\r\n"}, {"input": "1 2 1 2 1\r\nC BA\r\n", "output": "1\r\n"}, {"input": "1 3 1 1 4\r\nbCCCC BC\r\n", "output": "3\r\n"}, {"input": "1 4 3 1 1\r\nC bC\r\n", "output": "1\r\n"}, {"input": "1 3 2 1 1\r\nBBC B\r\n", "output": "1\r\n"}, {"input": "3 2 1 5 1\r\nBCA cBBBd\r\ncBBBd CdC\r\nCdC bbdAb\r\n", "output": "3\r\n"}, {"input": "5 3 2 1 1\r\nC CB\r\nCB C\r\nC d\r\nCB d\r\nCB C\r\n", "output": "6\r\n"}, {"input": "3 3 1 0 1\r\ncbcC A\r\nA aA\r\nA cbcC\r\n", "output": "7\r\n"}, {"input": "3 3 1 4 3\r\nc CADC\r\nCADC ABaD\r\nABaD c\r\n", "output": "5\r\n"}, {"input": "8 2 1 11 1\r\ndacdD cdDAA\r\ncdDAA dacdD\r\ndacdD bcCA\r\nbcCA B\r\nDDAA B\r\nDDAA daC\r\nAbCAc B\r\ndacdD daC\r\n", "output": "7\r\n"}, {"input": "12 4 1 2 8\r\nDA b\r\nDA dC\r\ndC b\r\nb DA\r\nb dC\r\nDA b\r\ndC b\r\nb dC\r\ndC DA\r\nDA dC\r\nDA b\r\nb dC\r\n", "output": "22\r\n"}, {"input": "27 8 1 0 1\r\nBcd A\r\nA b\r\nb BcDc\r\ndc dbaC\r\ndbaC dcCB\r\nB d\r\nd BbAc\r\nCBC b\r\nDBDca d\r\ncAbb AA\r\nAA Ba\r\ncAccb DBDca\r\ncb DdaB\r\nAAcBc Ba\r\nBa dc\r\ndc DDCd\r\nbcBDA da\r\nbDD ADD\r\nAA b\r\nb cb\r\ncb CCBbd\r\nCCBbd bcDdb\r\nbcDdb ddc\r\nddc C\r\nC Adc\r\nAdc BbAc\r\nBbAc DD\r\n", "output": "111\r\n"}, {"input": "22 85 1 36 1\r\ncdAd cBbCa\r\ncBbCa abBBc\r\nabBBc dddb\r\ndddb BBDA\r\nBBDA abBBc\r\nabBBc ADCad\r\naDaC cdAd\r\ncdAd D\r\nD acCbD\r\nAd DB\r\nDB C\r\nACb ca\r\nca ACb\r\nACb D\r\nD BBDA\r\nBBDA d\r\nd C\r\nC A\r\nA B\r\nB Ad\r\nAd cDD\r\ncDD ACb\r\n", "output": "21\r\n"}, {"input": "8 8 2 4 5\r\naBBba C\r\nCc CcBd\r\nd C\r\ndD dDba\r\ndDba c\r\nCc d\r\nd dD\r\ndD dDba\r\n", "output": "32\r\n"}, {"input": "4 10 6 2 7\r\nbbCc c\r\nd Bdccd\r\nBdccd c\r\nc Bdccd\r\n", "output": "24\r\n"}, {"input": "10 3 2 6 2\r\nbCbA a\r\naDA Bba\r\nbCbA aDA\r\nBba caa\r\nbCbA Bba\r\ncaa aDA\r\naDA bCbA\r\nbCbA dBba\r\ndBba bCbA\r\ndBba a\r\n", "output": "18\r\n"}, {"input": "7 5 4 3 2\r\nDBBCa BdC\r\nCDBcb BdC\r\ncA BdC\r\nBD CDBcb\r\nBD BdC\r\nDDd cacD\r\nBdC cA\r\n", "output": "21\r\n"}, {"input": "9 10 5 4 3\r\nDcAaa caCBc\r\ncaCBc B\r\nB b\r\nb cdddb\r\ncdddb aDBAb\r\naDBAb B\r\nB aDBAb\r\naDBAb cdddb\r\ncdddb aDCda\r\n", "output": "27\r\n"}, {"input": "10 20 10 0 11\r\nudgbkX Xe\r\nXe udgbkX\r\nudgbkX Xe\r\nXe udgbkX\r\nudgbkX Xe\r\nXe udgbkX\r\nudgbkX Xe\r\nXe udgbkX\r\nudgbkX Xe\r\nXe udgbkX\r\n", "output": "110\r\n"}, {"input": "10 20 10 1 11\r\nkeJKdQ HS\r\nHS keJKdQ\r\nkeJKdQ HS\r\nHS keJKdQ\r\nkeJKdQ HS\r\nHS keJKdQ\r\nkeJKdQ HS\r\nHS keJKdQ\r\nkeJKdQ HS\r\nHS keJKdQ\r\n", "output": "11\r\n"}]
false
stdio
null
true
172/C
172
C
Python 3
TESTS
0
30
0
155527034
def solution_task3(): data = input().split(" ") n = int(data[0]) m = int(data[1]) students = {} for i in range(n): data = input().split(" ") students[int(data[0])] = int(data[1]) total_time = 0 bus = [] result_dict = {} for key, value in students.items(): bus.append((key, value)) if len(bus) == m: bus.sort(key=lambda x: x[1]) total_time = max(total_time, bus[-1][0]) total_time += bus[0][1] current_stop = bus[0][1] current_out = 0 for i in range(m): if bus[i][1] == current_stop: current_out += 1 result_dict[bus[i][0]] = total_time else: total_time += bus[i][1] - bus[i - 1][1] current_stop = bus[i][1] total_time += 1 + (current_out // 2) current_out = 1 if current_out == 1: result_dict[bus[-1][0]] = total_time total_time += 1 else: for i in range(m - current_out - 1, m): result_dict[bus[i][0]] = total_time total_time += 1 + (current_out // 2) total_time += bus[-1][1] bus = [] if len(bus) > 0: bus.sort(key=lambda x: x[1]) total_time += bus[-1][0] + bus[0][1] current_stop = bus[0][1] current_out = 0 for i in range(len(bus)): if bus[i][1] == current_stop: current_out += 1 print(total_time) result_dict[bus[i][0]] = total_time else: total_time += bus[i][1] - bus[i - 1][1] current_stop = bus[i][1] total_time += 1 + (current_out // 2) current_out = 1 if current_out == 1: result_dict[bus[-1][0]] = total_time total_time += 1 else: for i in range(len(bus) - current_out - 1, len(bus)): result_dict[bus[i][0]] = total_time total_time += 1 + (current_out // 2) return " ".join([str(el) for el in result_dict.values()]) result = solution_task3() print(result)
66
857
5,120,000
193001159
n,m=map(int,input().split()) ti=[] xi=[] for _ in range(n): t,x=map(int,input().split()) ti.append(t) xi.append(x) t=0 for k in range(0,n,m): maxt=max(ti[min(n,k+m)-1],t) dicti={} for j in range(k,min(n,k+m)): dicti[xi[j]]=dicti.get(xi[j],0)+1 dicti=dict(sorted(dicti.items())) t=maxt last=0 for i in dicti: t=t+(i-last) val=dicti[i] dicti[i]=t t=t+1+val//2 last=i t=t+last for j in range(k,min(n,k+m)): print(dicti[xi[j]],end=" ")
Croc Champ 2012 - Qualification Round
CF
2,012
1
256
Bus
There is a bus stop near the university. The lessons are over, and n students come to the stop. The i-th student will appear at the bus stop at time ti (all ti's are distinct). We shall assume that the stop is located on the coordinate axis Ox, at point x = 0, and the bus goes along the ray Ox, that is, towards the positive direction of the coordinate axis, and back. The i-th student needs to get to the point with coordinate xi (xi > 0). The bus moves by the following algorithm. Initially it is at point 0. The students consistently come to the stop and get on it. The bus has a seating capacity which is equal to m passengers. At the moment when m students get on the bus, it starts moving in the positive direction of the coordinate axis. Also it starts moving when the last (n-th) student gets on the bus. The bus is moving at a speed of 1 unit of distance per 1 unit of time, i.e. it covers distance y in time y. Every time the bus passes the point at which at least one student needs to get off, it stops and these students get off the bus. The students need 1 + [k / 2] units of time to get off the bus, where k is the number of students who leave at this point. Expression [k / 2] denotes rounded down k / 2. As soon as the last student leaves the bus, the bus turns around and goes back to the point x = 0. It doesn't make any stops until it reaches the point. At the given point the bus fills with students once more, and everything is repeated. If students come to the stop when there's no bus, they form a line (queue) and get on the bus in the order in which they came. Any number of students get on the bus in negligible time, you should assume that it doesn't take any time. Any other actions also take no time. The bus has no other passengers apart from the students. Write a program that will determine for each student the time when he got off the bus. The moment a student got off the bus is the moment the bus stopped at the student's destination stop (despite the fact that the group of students need some time to get off).
The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of students and the number of passengers the bus can transport, correspondingly. Next n lines contain descriptions of the students, one per line. Each line contains a pair of integers ti, xi (1 ≤ ti ≤ 105, 1 ≤ xi ≤ 104). The lines are given in the order of strict increasing of ti. Values of xi can coincide.
Print n numbers w1, w2, ..., wn, wi — the moment of time when the i-th student got off the bus. Print the numbers on one line and separate them with single spaces.
null
In the first sample the bus waits for the first student for 3 units of time and drives him to his destination in additional 5 units of time. So the student leaves the bus at the moment of time 3 + 5 = 8. In the second sample the capacity of the bus equals 1, that's why it will drive the first student alone. This student is the same as the student from the first sample. So the bus arrives to his destination at the moment of time 8, spends 1 + [1 / 2] = 1 units of time on getting him off, and returns back to 0 in additional 5 units of time. That is, the bus returns to the bus stop at the moment of time 14. By this moment the second student has already came to the bus stop. So he immediately gets in the bus, and is driven to his destination in additional 5 units of time. He gets there at the moment 14 + 5 = 19. In the third sample the bus waits for the fourth student for 6 units of time, then drives for 5 units of time, then gets the passengers off for 1 + [4 / 2] = 3 units of time, then returns for 5 units of time, and then drives the fifth student for 1 unit of time.
[{"input": "1 10\n3 5", "output": "8"}, {"input": "2 1\n3 5\n4 5", "output": "8 19"}, {"input": "5 4\n3 5\n4 5\n5 5\n6 5\n7 1", "output": "11 11 11 11 20"}, {"input": "20 4\n28 13\n31 13\n35 6\n36 4\n52 6\n53 4\n83 2\n84 4\n87 1\n93 6\n108 4\n113 6\n116 1\n125 2\n130 2\n136 13\n162 2\n166 4\n184 1\n192 2", "output": "51 51 43 40 93 89 86 89 114 121 118 121 137 139 139 152 195 199 193 195"}]
1,500
["*special", "implementation", "sortings"]
66
[{"input": "1 10\r\n3 5\r\n", "output": "8\r\n"}, {"input": "2 1\r\n3 5\r\n4 5\r\n", "output": "8 19\r\n"}, {"input": "5 4\r\n3 5\r\n4 5\r\n5 5\r\n6 5\r\n7 1\r\n", "output": "11 11 11 11 20\r\n"}, {"input": "20 4\r\n28 13\r\n31 13\r\n35 6\r\n36 4\r\n52 6\r\n53 4\r\n83 2\r\n84 4\r\n87 1\r\n93 6\r\n108 4\r\n113 6\r\n116 1\r\n125 2\r\n130 2\r\n136 13\r\n162 2\r\n166 4\r\n184 1\r\n192 2\r\n", "output": "51 51 43 40 93 89 86 89 114 121 118 121 137 139 139 152 195 199 193 195\r\n"}, {"input": "1 1\r\n109 15\r\n", "output": "124\r\n"}, {"input": "2 1\r\n43 5\r\n102 1\r\n", "output": "48 103\r\n"}, {"input": "4 2\r\n7 1\r\n12 14\r\n90 15\r\n176 1\r\n", "output": "13 27 192 177\r\n"}, {"input": "8 8\r\n48 14\r\n74 12\r\n94 4\r\n127 14\r\n151 11\r\n173 4\r\n190 14\r\n191 9\r\n", "output": "210 207 195 210 205 195 210 202\r\n"}, {"input": "16 1\r\n29 10\r\n48 13\r\n53 10\r\n54 5\r\n59 6\r\n67 9\r\n68 10\r\n95 13\r\n132 5\r\n148 6\r\n150 6\r\n154 6\r\n169 10\r\n171 10\r\n185 6\r\n198 6\r\n", "output": "39 63 87 103 115 131 151 175 194 206 219 232 249 270 287 300\r\n"}, {"input": "32 3\r\n9 2\r\n12 4\r\n13 7\r\n14 7\r\n15 4\r\n19 10\r\n20 10\r\n29 2\r\n38 7\r\n58 4\r\n59 1\r\n61 4\r\n73 4\r\n90 1\r\n92 4\r\n95 7\r\n103 4\r\n107 7\r\n119 4\r\n121 4\r\n122 10\r\n123 10\r\n127 2\r\n134 10\r\n142 7\r\n144 7\r\n151 10\r\n160 7\r\n165 10\r\n191 1\r\n197 1\r\n199 7\r\n", "output": "15 18 22 38 34 42 65 55 61 81 77 81 97 93 97 115 111 115 128 128 136 158 149 158 177 177 182 201 205 194 217 224\r\n"}, {"input": "32 4\r\n4 6\r\n7 5\r\n13 6\r\n27 6\r\n39 5\r\n48 5\r\n57 11\r\n62 13\r\n64 11\r\n68 11\r\n84 9\r\n86 5\r\n89 6\r\n91 6\r\n107 13\r\n108 13\r\n113 11\r\n120 13\r\n126 5\r\n130 6\r\n134 9\r\n136 6\r\n137 5\r\n139 9\r\n143 5\r\n154 9\r\n155 5\r\n157 13\r\n171 11\r\n179 11\r\n185 13\r\n190 5\r\n", "output": "34 32 34 34 67 67 75 78 105 105 102 97 124 124 133 133 161 164 153 155 189 185 183 189 205 211 205 216 242 242 246 235\r\n"}, {"input": "32 5\r\n12 11\r\n17 14\r\n21 2\r\n24 2\r\n35 7\r\n41 15\r\n51 11\r\n52 2\r\n53 2\r\n61 14\r\n62 14\r\n75 2\r\n89 15\r\n90 14\r\n95 7\r\n102 7\r\n104 2\r\n105 14\r\n106 14\r\n109 2\r\n133 2\r\n135 2\r\n143 14\r\n151 11\r\n155 14\r\n168 15\r\n169 15\r\n179 14\r\n180 7\r\n181 15\r\n186 7\r\n198 14\r\n", "output": "49 53 37 37 44 87 81 70 70 85 119 105 122 119 111 147 140 155 155 140 173 173 188 184 188 221 221 219 211 221 245 253\r\n"}, {"input": "32 6\r\n15 12\r\n24 6\r\n30 13\r\n35 6\r\n38 6\r\n46 6\r\n47 12\r\n60 6\r\n66 9\r\n71 15\r\n74 6\r\n76 15\r\n104 6\r\n105 6\r\n110 15\r\n124 12\r\n126 12\r\n129 9\r\n131 12\r\n134 15\r\n135 15\r\n141 12\r\n154 13\r\n167 9\r\n171 9\r\n179 15\r\n181 15\r\n185 12\r\n189 12\r\n191 6\r\n192 6\r\n196 12\r\n", "output": "61 52 63 52 52 52 92 83 88 96 83 96 135 135 149 144 144 140 180 186 186 180 183 176 213 222 222 217 217 209 245 252\r\n"}, {"input": "32 7\r\n4 14\r\n6 14\r\n17 4\r\n22 3\r\n29 4\r\n32 4\r\n39 10\r\n40 11\r\n44 11\r\n51 11\r\n57 10\r\n76 4\r\n82 4\r\n87 14\r\n88 10\r\n118 10\r\n121 10\r\n136 14\r\n141 3\r\n143 4\r\n159 10\r\n162 10\r\n163 11\r\n165 10\r\n171 4\r\n172 10\r\n175 4\r\n176 3\r\n179 10\r\n196 10\r\n197 3\r\n198 10\r\n", "output": "57 57 44 42 44 44 52 101 101 101 99 91 91 106 171 171 171 178 162 164 171 206 209 206 198 206 198 196 232 232 224 232\r\n"}, {"input": "32 8\r\n12 9\r\n26 8\r\n27 8\r\n29 9\r\n43 11\r\n44 9\r\n45 5\r\n48 5\r\n50 8\r\n53 8\r\n57 9\r\n69 8\r\n76 11\r\n86 1\r\n88 9\r\n103 5\r\n116 9\r\n131 8\r\n139 8\r\n142 5\r\n148 1\r\n152 8\r\n154 8\r\n167 1\r\n170 5\r\n172 5\r\n173 5\r\n181 8\r\n183 1\r\n185 1\r\n190 1\r\n200 5\r\n", "output": "61 58 58 61 65 61 53 53 113 113 116 113 120 104 116 109 182 178 178 174 168 178 178 168 207 207 207 213 201 201 201 207\r\n"}]
false
stdio
null
true
401/B
401
B
PyPy 3-64
TESTS
0
30
512,000
145737187
import math def main_function(): x, k = [int(u) for u in input().split(" ")] x -= 1 dif = x - k hashy = [0 for i in range(x + 1)] for i in range(k): l = [int(j) for j in input().split(" ")] print(hashy) print(l) if len(l) == 2: hashy[l[1]] = 1 else: hashy[l[1]] = 1 hashy[l[2]] = 1 max_val = x - sum(hashy) #print(max_val) min_val = 0 for i in range(1, len(hashy)): if i < len(hashy) - 1 and hashy[i] == 0 and hashy[i + 1] == 0: hashy[i] = 1 hashy[i + 1] = 1 min_val += 1 elif hashy[i] == 0: hashy[i] = 1 min_val += 1 print(min_val, max_val) if __name__ == '__main__': main_function()
47
77
0
5984004
#!/usr/bin/env python3 def main(): x, t = map(int, input().split()) markers = [1] * (x - 1) for c in range(t): r = [int(n) for n in input().split()] markers[r[1] - 1] = 0 if r[0] == 1: markers[r[2] - 1] = 0 c = 0 max_num = 0 min_num = 0 for i in markers: if i: c += 1 continue if c: max_num += c min_num += ((c + 1) >> 1) c = 0 if c: max_num += c min_num += ((c + 1) >> 1) print(min_num, max_num) if __name__ == "__main__": main()
Codeforces Round 235 (Div. 2)
CF
2,014
1
256
Sereja and Contests
Sereja is a coder and he likes to take part in Codesorfes rounds. However, Uzhland doesn't have good internet connection, so Sereja sometimes skips rounds. Codesorfes has rounds of two types: Div1 (for advanced coders) and Div2 (for beginner coders). Two rounds, Div1 and Div2, can go simultaneously, (Div1 round cannot be held without Div2) in all other cases the rounds don't overlap in time. Each round has a unique identifier — a positive integer. The rounds are sequentially (without gaps) numbered with identifiers by the starting time of the round. The identifiers of rounds that are run simultaneously are different by one, also the identifier of the Div1 round is always greater. Sereja is a beginner coder, so he can take part only in rounds of Div2 type. At the moment he is taking part in a Div2 round, its identifier equals to x. Sereja remembers very well that he has taken part in exactly k rounds before this round. Also, he remembers all identifiers of the rounds he has taken part in and all identifiers of the rounds that went simultaneously with them. Sereja doesn't remember anything about the rounds he missed. Sereja is wondering: what minimum and what maximum number of Div2 rounds could he have missed? Help him find these two numbers.
The first line contains two integers: x (1 ≤ x ≤ 4000) — the round Sereja is taking part in today, and k (0 ≤ k < 4000) — the number of rounds he took part in. Next k lines contain the descriptions of the rounds that Sereja took part in before. If Sereja took part in one of two simultaneous rounds, the corresponding line looks like: "1 num2 num1" (where num2 is the identifier of this Div2 round, num1 is the identifier of the Div1 round). It is guaranteed that num1 - num2 = 1. If Sereja took part in a usual Div2 round, then the corresponding line looks like: "2 num" (where num is the identifier of this Div2 round). It is guaranteed that the identifiers of all given rounds are less than x.
Print in a single line two integers — the minimum and the maximum number of rounds that Sereja could have missed.
null
In the second sample we have unused identifiers of rounds 1, 6, 7. The minimum number of rounds Sereja could have missed equals to 2. In this case, the round with the identifier 1 will be a usual Div2 round and the round with identifier 6 will be synchronous with the Div1 round. The maximum number of rounds equals 3. In this case all unused identifiers belong to usual Div2 rounds.
[{"input": "3 2\n2 1\n2 2", "output": "0 0"}, {"input": "9 3\n1 2 3\n2 8\n1 4 5", "output": "2 3"}, {"input": "10 0", "output": "5 9"}]
1,200
["greedy", "implementation", "math"]
47
[{"input": "3 2\r\n2 1\r\n2 2\r\n", "output": "0 0"}, {"input": "9 3\r\n1 2 3\r\n2 8\r\n1 4 5\r\n", "output": "2 3"}, {"input": "10 0\r\n", "output": "5 9"}, {"input": "10 2\r\n1 1 2\r\n1 8 9\r\n", "output": "3 5"}, {"input": "9 3\r\n1 4 5\r\n1 1 2\r\n1 6 7\r\n", "output": "2 2"}, {"input": "7 2\r\n2 3\r\n1 5 6\r\n", "output": "2 3"}, {"input": "81 28\r\n1 77 78\r\n1 50 51\r\n2 9\r\n1 66 67\r\n1 12 13\r\n1 20 21\r\n1 28 29\r\n1 34 35\r\n1 54 55\r\n2 19\r\n1 70 71\r\n1 45 46\r\n1 36 37\r\n2 47\r\n2 7\r\n2 76\r\n2 6\r\n2 31\r\n1 16 17\r\n1 4 5\r\n1 73 74\r\n1 64 65\r\n2 62\r\n2 22\r\n2 1\r\n1 48 49\r\n2 24\r\n2 40\r\n", "output": "22 36"}, {"input": "12 8\r\n1 4 5\r\n1 9 10\r\n2 3\r\n1 6 7\r\n2 1\r\n2 2\r\n2 8\r\n2 11\r\n", "output": "0 0"}, {"input": "54 25\r\n1 40 41\r\n2 46\r\n2 32\r\n2 8\r\n1 51 52\r\n2 39\r\n1 30 31\r\n2 53\r\n1 33 34\r\n1 42 43\r\n1 17 18\r\n1 21 22\r\n1 44 45\r\n2 50\r\n2 49\r\n2 15\r\n1 3 4\r\n1 27 28\r\n1 19 20\r\n1 47 48\r\n2 13\r\n1 37 38\r\n1 6 7\r\n2 35\r\n2 26\r\n", "output": "10 14"}, {"input": "90 35\r\n2 83\r\n2 86\r\n2 46\r\n1 61 62\r\n2 11\r\n1 76 77\r\n2 37\r\n2 9\r\n1 18 19\r\n2 79\r\n1 35 36\r\n1 3 4\r\n2 78\r\n2 72\r\n1 44 45\r\n2 31\r\n2 38\r\n2 65\r\n1 32 33\r\n1 13 14\r\n2 75\r\n2 42\r\n2 51\r\n2 80\r\n2 29\r\n1 22 23\r\n1 5 6\r\n2 53\r\n1 7 8\r\n1 24 25\r\n1 54 55\r\n2 84\r\n1 27 28\r\n2 26\r\n2 12\r\n", "output": "25 40"}, {"input": "98 47\r\n1 48 49\r\n2 47\r\n1 25 26\r\n2 29\r\n1 38 39\r\n1 20 21\r\n2 75\r\n2 68\r\n2 95\r\n2 6\r\n1 1 2\r\n1 84 85\r\n2 66\r\n1 88 89\r\n2 19\r\n2 32\r\n1 93 94\r\n1 45 46\r\n2 50\r\n1 15 16\r\n1 63 64\r\n1 23 24\r\n1 53 54\r\n1 43 44\r\n2 97\r\n1 12 13\r\n2 86\r\n2 74\r\n2 42\r\n1 40 41\r\n1 30 31\r\n1 34 35\r\n1 27 28\r\n2 81\r\n1 8 9\r\n2 73\r\n1 70 71\r\n2 67\r\n2 60\r\n2 72\r\n1 76 77\r\n1 90 91\r\n1 17 18\r\n2 11\r\n1 82 83\r\n1 58 59\r\n2 55\r\n", "output": "18 24"}, {"input": "56 34\r\n2 22\r\n2 27\r\n1 18 19\r\n1 38 39\r\n2 49\r\n1 10 11\r\n1 14 15\r\n2 40\r\n2 34\r\n1 32 33\r\n2 17\r\n1 24 25\r\n2 23\r\n2 52\r\n1 45 46\r\n2 28\r\n2 7\r\n1 4 5\r\n1 30 31\r\n2 21\r\n2 6\r\n1 47 48\r\n1 43 44\r\n1 54 55\r\n2 13\r\n1 8 9\r\n1 2 3\r\n2 41\r\n1 35 36\r\n1 50 51\r\n2 1\r\n2 29\r\n2 16\r\n2 53\r\n", "output": "5 5"}, {"input": "43 27\r\n1 40 41\r\n1 2 3\r\n1 32 33\r\n1 35 36\r\n1 27 28\r\n1 30 31\r\n1 7 8\r\n2 11\r\n1 5 6\r\n2 1\r\n1 15 16\r\n1 38 39\r\n2 12\r\n1 20 21\r\n1 22 23\r\n1 24 25\r\n1 9 10\r\n2 26\r\n2 14\r\n1 18 19\r\n2 17\r\n2 4\r\n2 34\r\n2 37\r\n2 29\r\n2 42\r\n2 13\r\n", "output": "0 0"}, {"input": "21 13\r\n1 6 7\r\n2 12\r\n1 8 9\r\n2 19\r\n1 4 5\r\n1 17 18\r\n2 3\r\n2 20\r\n1 10 11\r\n2 14\r\n1 15 16\r\n1 1 2\r\n2 13\r\n", "output": "0 0"}, {"input": "66 1\r\n1 50 51\r\n", "output": "32 63"}, {"input": "62 21\r\n2 34\r\n1 39 40\r\n1 52 53\r\n1 35 36\r\n2 27\r\n1 56 57\r\n2 43\r\n1 7 8\r\n2 28\r\n1 44 45\r\n1 41 42\r\n1 32 33\r\n2 58\r\n1 47 48\r\n2 10\r\n1 21 22\r\n2 51\r\n1 15 16\r\n1 19 20\r\n1 3 4\r\n2 25\r\n", "output": "16 27"}, {"input": "83 56\r\n2 24\r\n2 30\r\n1 76 77\r\n1 26 27\r\n1 73 74\r\n1 52 53\r\n2 82\r\n1 36 37\r\n2 13\r\n2 4\r\n2 68\r\n1 31 32\r\n1 65 66\r\n1 16 17\r\n1 56 57\r\n2 60\r\n1 44 45\r\n1 80 81\r\n1 28 29\r\n2 23\r\n1 54 55\r\n2 9\r\n2 1\r\n1 34 35\r\n2 5\r\n1 78 79\r\n2 40\r\n2 42\r\n1 61 62\r\n2 49\r\n2 22\r\n2 25\r\n1 7 8\r\n1 20 21\r\n1 38 39\r\n2 43\r\n2 12\r\n1 46 47\r\n2 70\r\n1 71 72\r\n2 3\r\n1 10 11\r\n2 75\r\n2 59\r\n1 18 19\r\n2 69\r\n2 48\r\n1 63 64\r\n2 33\r\n1 14 15\r\n1 50 51\r\n2 6\r\n2 41\r\n2 2\r\n2 67\r\n2 58\r\n", "output": "0 0"}, {"input": "229 27\r\n2 7\r\n1 64 65\r\n1 12 13\r\n2 110\r\n1 145 146\r\n2 92\r\n2 28\r\n2 39\r\n1 16 17\r\n2 164\r\n2 137\r\n1 95 96\r\n2 125\r\n1 48 49\r\n1 115 116\r\n1 198 199\r\n1 148 149\r\n1 225 226\r\n1 1 2\r\n2 24\r\n2 103\r\n1 87 88\r\n2 124\r\n2 89\r\n1 178 179\r\n1 160 161\r\n2 184\r\n", "output": "98 187"}, {"input": "293 49\r\n2 286\r\n2 66\r\n2 98\r\n1 237 238\r\n1 136 137\r\n1 275 276\r\n2 152\r\n1 36 37\r\n2 26\r\n2 40\r\n2 79\r\n2 274\r\n1 205 206\r\n1 141 142\r\n1 243 244\r\n2 201\r\n1 12 13\r\n1 123 124\r\n1 165 166\r\n1 6 7\r\n2 64\r\n1 22 23\r\n2 120\r\n1 138 139\r\n1 50 51\r\n2 15\r\n2 67\r\n2 45\r\n1 288 289\r\n1 261 262\r\n1 103 104\r\n2 249\r\n2 32\r\n2 153\r\n2 248\r\n1 162 163\r\n2 89\r\n1 94 95\r\n2 21\r\n1 48 49\r\n1 56 57\r\n2 102\r\n1 271 272\r\n2 269\r\n1 232 233\r\n1 70 71\r\n1 42 43\r\n1 267 268\r\n2 292\r\n", "output": "121 217"}, {"input": "181 57\r\n1 10 11\r\n1 4 5\r\n1 170 171\r\n2 86\r\n2 97\r\n1 91 92\r\n2 162\r\n2 115\r\n1 98 99\r\n2 134\r\n1 100 101\r\n2 168\r\n1 113 114\r\n1 37 38\r\n2 81\r\n2 169\r\n1 173 174\r\n1 165 166\r\n2 108\r\n2 121\r\n1 31 32\r\n2 67\r\n2 13\r\n2 50\r\n2 157\r\n1 27 28\r\n1 19 20\r\n2 109\r\n1 104 105\r\n2 46\r\n1 126 127\r\n1 102 103\r\n2 158\r\n2 133\r\n2 93\r\n2 68\r\n1 70 71\r\n2 125\r\n2 36\r\n1 48 49\r\n2 117\r\n1 131 132\r\n2 79\r\n2 23\r\n1 75 76\r\n2 107\r\n2 138\r\n1 94 95\r\n2 54\r\n1 87 88\r\n2 41\r\n1 153 154\r\n1 14 15\r\n2 60\r\n2 148\r\n1 159 160\r\n2 58\r\n", "output": "61 98"}, {"input": "432 5\r\n1 130 131\r\n2 108\r\n1 76 77\r\n1 147 148\r\n2 137\r\n", "output": "214 423"}, {"input": "125 45\r\n2 70\r\n2 111\r\n2 52\r\n2 3\r\n2 97\r\n2 104\r\n1 47 48\r\n2 44\r\n2 88\r\n1 117 118\r\n2 82\r\n1 22 23\r\n1 53 54\r\n1 38 39\r\n1 114 115\r\n2 93\r\n2 113\r\n2 102\r\n2 30\r\n2 95\r\n2 36\r\n2 73\r\n2 51\r\n2 87\r\n1 15 16\r\n2 55\r\n2 80\r\n2 121\r\n2 26\r\n1 31 32\r\n1 105 106\r\n1 1 2\r\n1 10 11\r\n2 91\r\n1 78 79\r\n1 7 8\r\n2 120\r\n2 75\r\n1 45 46\r\n2 94\r\n2 72\r\n2 25\r\n1 34 35\r\n1 17 18\r\n1 20 21\r\n", "output": "40 62"}, {"input": "48 35\r\n1 17 18\r\n2 20\r\n1 7 8\r\n2 13\r\n1 1 2\r\n2 23\r\n1 25 26\r\n1 14 15\r\n2 3\r\n1 45 46\r\n1 35 36\r\n2 47\r\n1 27 28\r\n2 30\r\n1 5 6\r\n2 11\r\n2 9\r\n1 32 33\r\n2 19\r\n2 24\r\n2 16\r\n1 42 43\r\n1 21 22\r\n2 37\r\n2 34\r\n2 40\r\n2 31\r\n2 10\r\n2 44\r\n2 39\r\n2 12\r\n2 29\r\n2 38\r\n2 4\r\n2 41\r\n", "output": "0 0"}, {"input": "203 55\r\n2 81\r\n2 65\r\n1 38 39\r\n1 121 122\r\n2 48\r\n2 83\r\n1 23 24\r\n2 165\r\n1 132 133\r\n1 143 144\r\n2 35\r\n2 85\r\n2 187\r\n1 19 20\r\n2 137\r\n2 150\r\n2 202\r\n2 156\r\n2 178\r\n1 93 94\r\n2 73\r\n2 167\r\n1 56 57\r\n1 100 101\r\n1 26 27\r\n1 51 52\r\n2 74\r\n2 4\r\n2 79\r\n2 113\r\n1 181 182\r\n2 75\r\n2 157\r\n2 25\r\n2 124\r\n1 68 69\r\n1 135 136\r\n1 110 111\r\n1 153 154\r\n2 123\r\n2 134\r\n1 36 37\r\n1 145 146\r\n1 141 142\r\n1 86 87\r\n2 10\r\n1 5 6\r\n2 131\r\n2 116\r\n2 70\r\n1 95 96\r\n1 174 175\r\n2 108\r\n1 91 92\r\n2 152\r\n", "output": "71 123"}, {"input": "51 38\r\n2 48\r\n2 42\r\n2 20\r\n2 4\r\n2 37\r\n2 22\r\n2 9\r\n2 13\r\n1 44 45\r\n1 33 34\r\n2 8\r\n1 18 19\r\n1 2 3\r\n2 27\r\n1 5 6\r\n1 40 41\r\n1 24 25\r\n2 16\r\n2 39\r\n2 50\r\n1 31 32\r\n1 46 47\r\n2 15\r\n1 29 30\r\n1 10 11\r\n2 49\r\n2 14\r\n1 35 36\r\n2 23\r\n2 7\r\n2 38\r\n2 26\r\n2 1\r\n2 17\r\n2 43\r\n2 21\r\n2 12\r\n2 28\r\n", "output": "0 0"}, {"input": "401 40\r\n1 104 105\r\n2 368\r\n1 350 351\r\n1 107 108\r\n1 4 5\r\n1 143 144\r\n2 369\r\n1 337 338\r\n2 360\r\n2 384\r\n2 145\r\n1 102 103\r\n1 88 89\r\n1 179 180\r\n2 202\r\n1 234 235\r\n2 154\r\n1 9 10\r\n1 113 114\r\n2 398\r\n1 46 47\r\n1 35 36\r\n1 174 175\r\n1 273 274\r\n1 237 238\r\n2 209\r\n1 138 139\r\n1 33 34\r\n1 243 244\r\n1 266 267\r\n1 294 295\r\n2 219\r\n2 75\r\n2 340\r\n1 260 261\r\n1 245 246\r\n2 210\r\n1 221 222\r\n1 328 329\r\n1 164 165\r\n", "output": "177 333"}, {"input": "24 16\r\n1 16 17\r\n1 1 2\r\n1 8 9\r\n1 18 19\r\n1 22 23\r\n1 13 14\r\n2 15\r\n2 6\r\n2 11\r\n2 20\r\n2 3\r\n1 4 5\r\n2 10\r\n2 7\r\n2 21\r\n2 12\r\n", "output": "0 0"}, {"input": "137 37\r\n2 108\r\n1 55 56\r\n2 20\r\n1 33 34\r\n2 112\r\n2 48\r\n2 120\r\n2 38\r\n2 74\r\n2 119\r\n2 27\r\n1 13 14\r\n2 8\r\n1 88 89\r\n1 44 45\r\n2 124\r\n2 76\r\n2 123\r\n2 104\r\n1 58 59\r\n2 52\r\n2 47\r\n1 3 4\r\n1 65 66\r\n2 28\r\n1 102 103\r\n2 81\r\n2 86\r\n2 116\r\n1 69 70\r\n1 11 12\r\n2 84\r\n1 25 26\r\n2 100\r\n2 90\r\n2 83\r\n1 95 96\r\n", "output": "52 86"}, {"input": "1155 50\r\n1 636 637\r\n1 448 449\r\n2 631\r\n2 247\r\n1 1049 1050\r\n1 1103 1104\r\n1 816 817\r\n1 1127 1128\r\n2 441\r\n2 982\r\n1 863 864\r\n2 186\r\n1 774 775\r\n2 793\r\n2 173\r\n2 800\r\n1 952 953\r\n1 492 493\r\n1 796 797\r\n2 907\r\n2 856\r\n2 786\r\n2 921\r\n1 558 559\r\n2 1090\r\n1 307 308\r\n1 1152 1153\r\n1 578 579\r\n1 944 945\r\n1 707 708\r\n2 968\r\n1 1005 1006\r\n1 1100 1101\r\n2 402\r\n1 917 918\r\n1 237 238\r\n1 191 192\r\n2 460\r\n1 1010 1011\r\n2 960\r\n1 1018 1019\r\n2 296\r\n1 958 959\r\n2 650\r\n2 395\r\n1 1124 1125\r\n2 539\r\n2 152\r\n1 385 386\r\n2 464\r\n", "output": "548 1077"}, {"input": "1122 54\r\n2 1031\r\n1 363 364\r\n1 14 15\r\n1 902 903\r\n1 1052 1053\r\n2 170\r\n2 36\r\n2 194\r\n1 340 341\r\n1 1018 1019\r\n1 670 671\r\n1 558 559\r\n2 431\r\n2 351\r\n2 201\r\n1 1104 1105\r\n2 1056\r\n2 823\r\n1 274 275\r\n2 980\r\n1 542 543\r\n1 807 808\r\n2 157\r\n2 895\r\n1 505 506\r\n2 658\r\n1 484 485\r\n1 533 534\r\n1 384 385\r\n2 779\r\n2 888\r\n1 137 138\r\n1 198 199\r\n2 762\r\n1 451 452\r\n1 248 249\r\n2 294\r\n2 123\r\n2 948\r\n2 1024\r\n2 771\r\n2 922\r\n1 566 567\r\n1 707 708\r\n1 1037 1038\r\n2 63\r\n1 208 209\r\n1 738 739\r\n2 648\r\n1 491 492\r\n1 440 441\r\n2 651\r\n1 971 972\r\n1 93 94\r\n", "output": "532 1038"}, {"input": "2938 48\r\n2 1519\r\n2 1828\r\n1 252 253\r\n1 2275 2276\r\n1 1479 1480\r\n2 751\r\n2 972\r\n2 175\r\n2 255\r\n1 1837 1838\r\n1 1914 1915\r\n2 198\r\n1 1686 1687\r\n1 950 951\r\n2 61\r\n1 840 841\r\n2 277\r\n1 52 53\r\n1 76 77\r\n2 795\r\n2 1680\r\n1 2601 2602\r\n2 2286\r\n2 2188\r\n2 2521\r\n2 1166\r\n2 1171\r\n2 2421\r\n1 1297 1298\r\n1 1736 1737\r\n1 991 992\r\n1 1048 1049\r\n2 756\r\n2 2054\r\n1 2878 2879\r\n1 1445 1446\r\n1 2539 2540\r\n2 1334\r\n2 2233\r\n2 494\r\n2 506\r\n1 1942 1943\r\n2 2617\r\n1 1991 1992\r\n2 1501\r\n1 2488 2489\r\n1 752 753\r\n2 2623\r\n", "output": "1444 2867"}, {"input": "2698 39\r\n2 710\r\n1 260 261\r\n2 174\r\n2 1697\r\n2 915\r\n1 2029 2030\r\n2 916\r\n2 2419\r\n2 323\r\n1 2130 2131\r\n2 1350\r\n1 64 65\r\n1 763 764\r\n1 939 940\r\n2 1693\r\n2 659\r\n2 2281\r\n2 761\r\n2 909\r\n1 1873 1874\r\n1 1164 1165\r\n2 2308\r\n2 504\r\n1 1035 1036\r\n1 2271 2272\r\n1 1085 1086\r\n1 1757 1758\r\n2 1818\r\n1 1604 1605\r\n1 517 518\r\n1 2206 2207\r\n2 636\r\n1 519 520\r\n2 1928\r\n1 1894 1895\r\n2 573\r\n2 2313\r\n1 42 43\r\n2 1529\r\n", "output": "1327 2640"}, {"input": "3999 0\r\n", "output": "1999 3998"}, {"input": "1 0\r\n", "output": "0 0"}, {"input": "10 5\r\n1 1 2\r\n2 3\r\n2 8\r\n1 4 5\r\n1 6 7\r\n", "output": "1 1"}, {"input": "4000 0\r\n", "output": "2000 3999"}]
false
stdio
null
true
401/B
401
B
Python 3
TESTS
0
109
204,800
89823657
x,k=list(map(int,input().split())) r=[0] for i in range(k): l=list(map(int,input().split())) for i in l[1:]: r.append(i) r.sort() f=[] j=0 for i in range(1,x): if i==r[j]: j=j+1 if j+1<=len(r)-1 else j else: f.append(i) m1,m2=0,len(f) i=0 while i<len(f): if i+1<=len(f)-1 and f[i+1]-f[i]==1: i=i+2 m1=m1+1 else: m1=m1+1 i=i+1 print(m1,m2)
47
77
102,400
5980805
#!/usr/bin/python -SOO x,n = map(int,input().strip().split()) s = set(range(1,x)) for _ in range(n): xs = list(map(int,input().strip().split())) if xs[0] == 2: s.remove(xs[1]) else: s.remove(xs[1]) s.remove(xs[2]) m,i= 0,0 s = sorted(s) while i < len(s): if i==len(s)-1: m+=1 break elif s[i] == s[i+1]-1: m += 1 i += 2 else: m += 1 i += 1 print(m,len(s))
Codeforces Round 235 (Div. 2)
CF
2,014
1
256
Sereja and Contests
Sereja is a coder and he likes to take part in Codesorfes rounds. However, Uzhland doesn't have good internet connection, so Sereja sometimes skips rounds. Codesorfes has rounds of two types: Div1 (for advanced coders) and Div2 (for beginner coders). Two rounds, Div1 and Div2, can go simultaneously, (Div1 round cannot be held without Div2) in all other cases the rounds don't overlap in time. Each round has a unique identifier — a positive integer. The rounds are sequentially (without gaps) numbered with identifiers by the starting time of the round. The identifiers of rounds that are run simultaneously are different by one, also the identifier of the Div1 round is always greater. Sereja is a beginner coder, so he can take part only in rounds of Div2 type. At the moment he is taking part in a Div2 round, its identifier equals to x. Sereja remembers very well that he has taken part in exactly k rounds before this round. Also, he remembers all identifiers of the rounds he has taken part in and all identifiers of the rounds that went simultaneously with them. Sereja doesn't remember anything about the rounds he missed. Sereja is wondering: what minimum and what maximum number of Div2 rounds could he have missed? Help him find these two numbers.
The first line contains two integers: x (1 ≤ x ≤ 4000) — the round Sereja is taking part in today, and k (0 ≤ k < 4000) — the number of rounds he took part in. Next k lines contain the descriptions of the rounds that Sereja took part in before. If Sereja took part in one of two simultaneous rounds, the corresponding line looks like: "1 num2 num1" (where num2 is the identifier of this Div2 round, num1 is the identifier of the Div1 round). It is guaranteed that num1 - num2 = 1. If Sereja took part in a usual Div2 round, then the corresponding line looks like: "2 num" (where num is the identifier of this Div2 round). It is guaranteed that the identifiers of all given rounds are less than x.
Print in a single line two integers — the minimum and the maximum number of rounds that Sereja could have missed.
null
In the second sample we have unused identifiers of rounds 1, 6, 7. The minimum number of rounds Sereja could have missed equals to 2. In this case, the round with the identifier 1 will be a usual Div2 round and the round with identifier 6 will be synchronous with the Div1 round. The maximum number of rounds equals 3. In this case all unused identifiers belong to usual Div2 rounds.
[{"input": "3 2\n2 1\n2 2", "output": "0 0"}, {"input": "9 3\n1 2 3\n2 8\n1 4 5", "output": "2 3"}, {"input": "10 0", "output": "5 9"}]
1,200
["greedy", "implementation", "math"]
47
[{"input": "3 2\r\n2 1\r\n2 2\r\n", "output": "0 0"}, {"input": "9 3\r\n1 2 3\r\n2 8\r\n1 4 5\r\n", "output": "2 3"}, {"input": "10 0\r\n", "output": "5 9"}, {"input": "10 2\r\n1 1 2\r\n1 8 9\r\n", "output": "3 5"}, {"input": "9 3\r\n1 4 5\r\n1 1 2\r\n1 6 7\r\n", "output": "2 2"}, {"input": "7 2\r\n2 3\r\n1 5 6\r\n", "output": "2 3"}, {"input": "81 28\r\n1 77 78\r\n1 50 51\r\n2 9\r\n1 66 67\r\n1 12 13\r\n1 20 21\r\n1 28 29\r\n1 34 35\r\n1 54 55\r\n2 19\r\n1 70 71\r\n1 45 46\r\n1 36 37\r\n2 47\r\n2 7\r\n2 76\r\n2 6\r\n2 31\r\n1 16 17\r\n1 4 5\r\n1 73 74\r\n1 64 65\r\n2 62\r\n2 22\r\n2 1\r\n1 48 49\r\n2 24\r\n2 40\r\n", "output": "22 36"}, {"input": "12 8\r\n1 4 5\r\n1 9 10\r\n2 3\r\n1 6 7\r\n2 1\r\n2 2\r\n2 8\r\n2 11\r\n", "output": "0 0"}, {"input": "54 25\r\n1 40 41\r\n2 46\r\n2 32\r\n2 8\r\n1 51 52\r\n2 39\r\n1 30 31\r\n2 53\r\n1 33 34\r\n1 42 43\r\n1 17 18\r\n1 21 22\r\n1 44 45\r\n2 50\r\n2 49\r\n2 15\r\n1 3 4\r\n1 27 28\r\n1 19 20\r\n1 47 48\r\n2 13\r\n1 37 38\r\n1 6 7\r\n2 35\r\n2 26\r\n", "output": "10 14"}, {"input": "90 35\r\n2 83\r\n2 86\r\n2 46\r\n1 61 62\r\n2 11\r\n1 76 77\r\n2 37\r\n2 9\r\n1 18 19\r\n2 79\r\n1 35 36\r\n1 3 4\r\n2 78\r\n2 72\r\n1 44 45\r\n2 31\r\n2 38\r\n2 65\r\n1 32 33\r\n1 13 14\r\n2 75\r\n2 42\r\n2 51\r\n2 80\r\n2 29\r\n1 22 23\r\n1 5 6\r\n2 53\r\n1 7 8\r\n1 24 25\r\n1 54 55\r\n2 84\r\n1 27 28\r\n2 26\r\n2 12\r\n", "output": "25 40"}, {"input": "98 47\r\n1 48 49\r\n2 47\r\n1 25 26\r\n2 29\r\n1 38 39\r\n1 20 21\r\n2 75\r\n2 68\r\n2 95\r\n2 6\r\n1 1 2\r\n1 84 85\r\n2 66\r\n1 88 89\r\n2 19\r\n2 32\r\n1 93 94\r\n1 45 46\r\n2 50\r\n1 15 16\r\n1 63 64\r\n1 23 24\r\n1 53 54\r\n1 43 44\r\n2 97\r\n1 12 13\r\n2 86\r\n2 74\r\n2 42\r\n1 40 41\r\n1 30 31\r\n1 34 35\r\n1 27 28\r\n2 81\r\n1 8 9\r\n2 73\r\n1 70 71\r\n2 67\r\n2 60\r\n2 72\r\n1 76 77\r\n1 90 91\r\n1 17 18\r\n2 11\r\n1 82 83\r\n1 58 59\r\n2 55\r\n", "output": "18 24"}, {"input": "56 34\r\n2 22\r\n2 27\r\n1 18 19\r\n1 38 39\r\n2 49\r\n1 10 11\r\n1 14 15\r\n2 40\r\n2 34\r\n1 32 33\r\n2 17\r\n1 24 25\r\n2 23\r\n2 52\r\n1 45 46\r\n2 28\r\n2 7\r\n1 4 5\r\n1 30 31\r\n2 21\r\n2 6\r\n1 47 48\r\n1 43 44\r\n1 54 55\r\n2 13\r\n1 8 9\r\n1 2 3\r\n2 41\r\n1 35 36\r\n1 50 51\r\n2 1\r\n2 29\r\n2 16\r\n2 53\r\n", "output": "5 5"}, {"input": "43 27\r\n1 40 41\r\n1 2 3\r\n1 32 33\r\n1 35 36\r\n1 27 28\r\n1 30 31\r\n1 7 8\r\n2 11\r\n1 5 6\r\n2 1\r\n1 15 16\r\n1 38 39\r\n2 12\r\n1 20 21\r\n1 22 23\r\n1 24 25\r\n1 9 10\r\n2 26\r\n2 14\r\n1 18 19\r\n2 17\r\n2 4\r\n2 34\r\n2 37\r\n2 29\r\n2 42\r\n2 13\r\n", "output": "0 0"}, {"input": "21 13\r\n1 6 7\r\n2 12\r\n1 8 9\r\n2 19\r\n1 4 5\r\n1 17 18\r\n2 3\r\n2 20\r\n1 10 11\r\n2 14\r\n1 15 16\r\n1 1 2\r\n2 13\r\n", "output": "0 0"}, {"input": "66 1\r\n1 50 51\r\n", "output": "32 63"}, {"input": "62 21\r\n2 34\r\n1 39 40\r\n1 52 53\r\n1 35 36\r\n2 27\r\n1 56 57\r\n2 43\r\n1 7 8\r\n2 28\r\n1 44 45\r\n1 41 42\r\n1 32 33\r\n2 58\r\n1 47 48\r\n2 10\r\n1 21 22\r\n2 51\r\n1 15 16\r\n1 19 20\r\n1 3 4\r\n2 25\r\n", "output": "16 27"}, {"input": "83 56\r\n2 24\r\n2 30\r\n1 76 77\r\n1 26 27\r\n1 73 74\r\n1 52 53\r\n2 82\r\n1 36 37\r\n2 13\r\n2 4\r\n2 68\r\n1 31 32\r\n1 65 66\r\n1 16 17\r\n1 56 57\r\n2 60\r\n1 44 45\r\n1 80 81\r\n1 28 29\r\n2 23\r\n1 54 55\r\n2 9\r\n2 1\r\n1 34 35\r\n2 5\r\n1 78 79\r\n2 40\r\n2 42\r\n1 61 62\r\n2 49\r\n2 22\r\n2 25\r\n1 7 8\r\n1 20 21\r\n1 38 39\r\n2 43\r\n2 12\r\n1 46 47\r\n2 70\r\n1 71 72\r\n2 3\r\n1 10 11\r\n2 75\r\n2 59\r\n1 18 19\r\n2 69\r\n2 48\r\n1 63 64\r\n2 33\r\n1 14 15\r\n1 50 51\r\n2 6\r\n2 41\r\n2 2\r\n2 67\r\n2 58\r\n", "output": "0 0"}, {"input": "229 27\r\n2 7\r\n1 64 65\r\n1 12 13\r\n2 110\r\n1 145 146\r\n2 92\r\n2 28\r\n2 39\r\n1 16 17\r\n2 164\r\n2 137\r\n1 95 96\r\n2 125\r\n1 48 49\r\n1 115 116\r\n1 198 199\r\n1 148 149\r\n1 225 226\r\n1 1 2\r\n2 24\r\n2 103\r\n1 87 88\r\n2 124\r\n2 89\r\n1 178 179\r\n1 160 161\r\n2 184\r\n", "output": "98 187"}, {"input": "293 49\r\n2 286\r\n2 66\r\n2 98\r\n1 237 238\r\n1 136 137\r\n1 275 276\r\n2 152\r\n1 36 37\r\n2 26\r\n2 40\r\n2 79\r\n2 274\r\n1 205 206\r\n1 141 142\r\n1 243 244\r\n2 201\r\n1 12 13\r\n1 123 124\r\n1 165 166\r\n1 6 7\r\n2 64\r\n1 22 23\r\n2 120\r\n1 138 139\r\n1 50 51\r\n2 15\r\n2 67\r\n2 45\r\n1 288 289\r\n1 261 262\r\n1 103 104\r\n2 249\r\n2 32\r\n2 153\r\n2 248\r\n1 162 163\r\n2 89\r\n1 94 95\r\n2 21\r\n1 48 49\r\n1 56 57\r\n2 102\r\n1 271 272\r\n2 269\r\n1 232 233\r\n1 70 71\r\n1 42 43\r\n1 267 268\r\n2 292\r\n", "output": "121 217"}, {"input": "181 57\r\n1 10 11\r\n1 4 5\r\n1 170 171\r\n2 86\r\n2 97\r\n1 91 92\r\n2 162\r\n2 115\r\n1 98 99\r\n2 134\r\n1 100 101\r\n2 168\r\n1 113 114\r\n1 37 38\r\n2 81\r\n2 169\r\n1 173 174\r\n1 165 166\r\n2 108\r\n2 121\r\n1 31 32\r\n2 67\r\n2 13\r\n2 50\r\n2 157\r\n1 27 28\r\n1 19 20\r\n2 109\r\n1 104 105\r\n2 46\r\n1 126 127\r\n1 102 103\r\n2 158\r\n2 133\r\n2 93\r\n2 68\r\n1 70 71\r\n2 125\r\n2 36\r\n1 48 49\r\n2 117\r\n1 131 132\r\n2 79\r\n2 23\r\n1 75 76\r\n2 107\r\n2 138\r\n1 94 95\r\n2 54\r\n1 87 88\r\n2 41\r\n1 153 154\r\n1 14 15\r\n2 60\r\n2 148\r\n1 159 160\r\n2 58\r\n", "output": "61 98"}, {"input": "432 5\r\n1 130 131\r\n2 108\r\n1 76 77\r\n1 147 148\r\n2 137\r\n", "output": "214 423"}, {"input": "125 45\r\n2 70\r\n2 111\r\n2 52\r\n2 3\r\n2 97\r\n2 104\r\n1 47 48\r\n2 44\r\n2 88\r\n1 117 118\r\n2 82\r\n1 22 23\r\n1 53 54\r\n1 38 39\r\n1 114 115\r\n2 93\r\n2 113\r\n2 102\r\n2 30\r\n2 95\r\n2 36\r\n2 73\r\n2 51\r\n2 87\r\n1 15 16\r\n2 55\r\n2 80\r\n2 121\r\n2 26\r\n1 31 32\r\n1 105 106\r\n1 1 2\r\n1 10 11\r\n2 91\r\n1 78 79\r\n1 7 8\r\n2 120\r\n2 75\r\n1 45 46\r\n2 94\r\n2 72\r\n2 25\r\n1 34 35\r\n1 17 18\r\n1 20 21\r\n", "output": "40 62"}, {"input": "48 35\r\n1 17 18\r\n2 20\r\n1 7 8\r\n2 13\r\n1 1 2\r\n2 23\r\n1 25 26\r\n1 14 15\r\n2 3\r\n1 45 46\r\n1 35 36\r\n2 47\r\n1 27 28\r\n2 30\r\n1 5 6\r\n2 11\r\n2 9\r\n1 32 33\r\n2 19\r\n2 24\r\n2 16\r\n1 42 43\r\n1 21 22\r\n2 37\r\n2 34\r\n2 40\r\n2 31\r\n2 10\r\n2 44\r\n2 39\r\n2 12\r\n2 29\r\n2 38\r\n2 4\r\n2 41\r\n", "output": "0 0"}, {"input": "203 55\r\n2 81\r\n2 65\r\n1 38 39\r\n1 121 122\r\n2 48\r\n2 83\r\n1 23 24\r\n2 165\r\n1 132 133\r\n1 143 144\r\n2 35\r\n2 85\r\n2 187\r\n1 19 20\r\n2 137\r\n2 150\r\n2 202\r\n2 156\r\n2 178\r\n1 93 94\r\n2 73\r\n2 167\r\n1 56 57\r\n1 100 101\r\n1 26 27\r\n1 51 52\r\n2 74\r\n2 4\r\n2 79\r\n2 113\r\n1 181 182\r\n2 75\r\n2 157\r\n2 25\r\n2 124\r\n1 68 69\r\n1 135 136\r\n1 110 111\r\n1 153 154\r\n2 123\r\n2 134\r\n1 36 37\r\n1 145 146\r\n1 141 142\r\n1 86 87\r\n2 10\r\n1 5 6\r\n2 131\r\n2 116\r\n2 70\r\n1 95 96\r\n1 174 175\r\n2 108\r\n1 91 92\r\n2 152\r\n", "output": "71 123"}, {"input": "51 38\r\n2 48\r\n2 42\r\n2 20\r\n2 4\r\n2 37\r\n2 22\r\n2 9\r\n2 13\r\n1 44 45\r\n1 33 34\r\n2 8\r\n1 18 19\r\n1 2 3\r\n2 27\r\n1 5 6\r\n1 40 41\r\n1 24 25\r\n2 16\r\n2 39\r\n2 50\r\n1 31 32\r\n1 46 47\r\n2 15\r\n1 29 30\r\n1 10 11\r\n2 49\r\n2 14\r\n1 35 36\r\n2 23\r\n2 7\r\n2 38\r\n2 26\r\n2 1\r\n2 17\r\n2 43\r\n2 21\r\n2 12\r\n2 28\r\n", "output": "0 0"}, {"input": "401 40\r\n1 104 105\r\n2 368\r\n1 350 351\r\n1 107 108\r\n1 4 5\r\n1 143 144\r\n2 369\r\n1 337 338\r\n2 360\r\n2 384\r\n2 145\r\n1 102 103\r\n1 88 89\r\n1 179 180\r\n2 202\r\n1 234 235\r\n2 154\r\n1 9 10\r\n1 113 114\r\n2 398\r\n1 46 47\r\n1 35 36\r\n1 174 175\r\n1 273 274\r\n1 237 238\r\n2 209\r\n1 138 139\r\n1 33 34\r\n1 243 244\r\n1 266 267\r\n1 294 295\r\n2 219\r\n2 75\r\n2 340\r\n1 260 261\r\n1 245 246\r\n2 210\r\n1 221 222\r\n1 328 329\r\n1 164 165\r\n", "output": "177 333"}, {"input": "24 16\r\n1 16 17\r\n1 1 2\r\n1 8 9\r\n1 18 19\r\n1 22 23\r\n1 13 14\r\n2 15\r\n2 6\r\n2 11\r\n2 20\r\n2 3\r\n1 4 5\r\n2 10\r\n2 7\r\n2 21\r\n2 12\r\n", "output": "0 0"}, {"input": "137 37\r\n2 108\r\n1 55 56\r\n2 20\r\n1 33 34\r\n2 112\r\n2 48\r\n2 120\r\n2 38\r\n2 74\r\n2 119\r\n2 27\r\n1 13 14\r\n2 8\r\n1 88 89\r\n1 44 45\r\n2 124\r\n2 76\r\n2 123\r\n2 104\r\n1 58 59\r\n2 52\r\n2 47\r\n1 3 4\r\n1 65 66\r\n2 28\r\n1 102 103\r\n2 81\r\n2 86\r\n2 116\r\n1 69 70\r\n1 11 12\r\n2 84\r\n1 25 26\r\n2 100\r\n2 90\r\n2 83\r\n1 95 96\r\n", "output": "52 86"}, {"input": "1155 50\r\n1 636 637\r\n1 448 449\r\n2 631\r\n2 247\r\n1 1049 1050\r\n1 1103 1104\r\n1 816 817\r\n1 1127 1128\r\n2 441\r\n2 982\r\n1 863 864\r\n2 186\r\n1 774 775\r\n2 793\r\n2 173\r\n2 800\r\n1 952 953\r\n1 492 493\r\n1 796 797\r\n2 907\r\n2 856\r\n2 786\r\n2 921\r\n1 558 559\r\n2 1090\r\n1 307 308\r\n1 1152 1153\r\n1 578 579\r\n1 944 945\r\n1 707 708\r\n2 968\r\n1 1005 1006\r\n1 1100 1101\r\n2 402\r\n1 917 918\r\n1 237 238\r\n1 191 192\r\n2 460\r\n1 1010 1011\r\n2 960\r\n1 1018 1019\r\n2 296\r\n1 958 959\r\n2 650\r\n2 395\r\n1 1124 1125\r\n2 539\r\n2 152\r\n1 385 386\r\n2 464\r\n", "output": "548 1077"}, {"input": "1122 54\r\n2 1031\r\n1 363 364\r\n1 14 15\r\n1 902 903\r\n1 1052 1053\r\n2 170\r\n2 36\r\n2 194\r\n1 340 341\r\n1 1018 1019\r\n1 670 671\r\n1 558 559\r\n2 431\r\n2 351\r\n2 201\r\n1 1104 1105\r\n2 1056\r\n2 823\r\n1 274 275\r\n2 980\r\n1 542 543\r\n1 807 808\r\n2 157\r\n2 895\r\n1 505 506\r\n2 658\r\n1 484 485\r\n1 533 534\r\n1 384 385\r\n2 779\r\n2 888\r\n1 137 138\r\n1 198 199\r\n2 762\r\n1 451 452\r\n1 248 249\r\n2 294\r\n2 123\r\n2 948\r\n2 1024\r\n2 771\r\n2 922\r\n1 566 567\r\n1 707 708\r\n1 1037 1038\r\n2 63\r\n1 208 209\r\n1 738 739\r\n2 648\r\n1 491 492\r\n1 440 441\r\n2 651\r\n1 971 972\r\n1 93 94\r\n", "output": "532 1038"}, {"input": "2938 48\r\n2 1519\r\n2 1828\r\n1 252 253\r\n1 2275 2276\r\n1 1479 1480\r\n2 751\r\n2 972\r\n2 175\r\n2 255\r\n1 1837 1838\r\n1 1914 1915\r\n2 198\r\n1 1686 1687\r\n1 950 951\r\n2 61\r\n1 840 841\r\n2 277\r\n1 52 53\r\n1 76 77\r\n2 795\r\n2 1680\r\n1 2601 2602\r\n2 2286\r\n2 2188\r\n2 2521\r\n2 1166\r\n2 1171\r\n2 2421\r\n1 1297 1298\r\n1 1736 1737\r\n1 991 992\r\n1 1048 1049\r\n2 756\r\n2 2054\r\n1 2878 2879\r\n1 1445 1446\r\n1 2539 2540\r\n2 1334\r\n2 2233\r\n2 494\r\n2 506\r\n1 1942 1943\r\n2 2617\r\n1 1991 1992\r\n2 1501\r\n1 2488 2489\r\n1 752 753\r\n2 2623\r\n", "output": "1444 2867"}, {"input": "2698 39\r\n2 710\r\n1 260 261\r\n2 174\r\n2 1697\r\n2 915\r\n1 2029 2030\r\n2 916\r\n2 2419\r\n2 323\r\n1 2130 2131\r\n2 1350\r\n1 64 65\r\n1 763 764\r\n1 939 940\r\n2 1693\r\n2 659\r\n2 2281\r\n2 761\r\n2 909\r\n1 1873 1874\r\n1 1164 1165\r\n2 2308\r\n2 504\r\n1 1035 1036\r\n1 2271 2272\r\n1 1085 1086\r\n1 1757 1758\r\n2 1818\r\n1 1604 1605\r\n1 517 518\r\n1 2206 2207\r\n2 636\r\n1 519 520\r\n2 1928\r\n1 1894 1895\r\n2 573\r\n2 2313\r\n1 42 43\r\n2 1529\r\n", "output": "1327 2640"}, {"input": "3999 0\r\n", "output": "1999 3998"}, {"input": "1 0\r\n", "output": "0 0"}, {"input": "10 5\r\n1 1 2\r\n2 3\r\n2 8\r\n1 4 5\r\n1 6 7\r\n", "output": "1 1"}, {"input": "4000 0\r\n", "output": "2000 3999"}]
false
stdio
null
true
723/D
723
D
PyPy 3-64
TESTS
2
62
0
211993901
import sys input = lambda: sys.stdin.readline().rstrip() from collections import deque,defaultdict,Counter from itertools import permutations,combinations from bisect import * from heapq import * from math import ceil,gcd,lcm,floor,comb #pow(x,mod-2,mod) def check(N,M,x,y): global vis,P if 0<x<M-1 and 0<y<N-1 and vis[y][x]==0 and P[y][x]==".": return True return False def dfs(x,y): global N,M,P l = [(x,y)] ans = [] while l: a,b = l.pop() if vis[b][a]==1:continue vis[b][a]=1 if not (0<x<M-1 and 0<y<N-1):return [float('inf'),[]] ans.append((a,b)) for x1,y1 in [[1,0],[-1,0],[0,1],[0,-1]]: if check(N,M,a+x1,b+y1): l.append((a+x1,b+y1)) return [len(ans),ans] N,M,K = map(int,input().split()) P = [[x for x in input()] for _ in range(N)] vis = [[0]*M for _ in range(N)] ans = [] for x in range(M): for y in range(N): if vis[y][x]==1 or P[y][x]=="*":continue p,m = dfs(x,y) if p==float("inf"):continue ans.append([p,m]) num = 0 ans.sort() for i in range(len(ans)-K): num+=ans[i][0] for x,y in ans[i][1]: P[y][x] = "*" print(num) for i in P: print(''.join(i))
26
77
716,800
21172235
def find_neigh(t,x,y): return [(i,j) for (i,j) in [(x-1,y),(x+1,y),(x,y-1),(x,y+1),(x,y+1)] if t[i][j]=='.'] def lake(table): return [(i,j) for i in range(0,len(table)-1) for j in range(0, len(table[i])-1) if table[i][j] == '.'] stack=set() def the_lake(table,x,y): queue=[(x,y)] counted=set() counted.add((x,y)) stack.add((x,y)) while queue: start=queue.pop() for (i,j) in find_neigh(table,*start): if (i,j) in counted: continue stack.add((i,j)) queue.append((i,j)) counted.add((i,j)) for (i,j) in counted: if table[i+1][j]=='0' or table[i-1][j]=='0': return if table[i][j+1]=='0' or table[i][j-1]=='0': return return counted def island(table,n,m,k): lakes=[] count=0 for (i,j) in lake(table): if (i,j) in stack: continue tlake=the_lake(table,i,j) if tlake==None: continue lakes.append(tlake) lakes=sorted(lakes,key=len) for p in range(0,len(lakes)-k): for i,j in lakes[p]: row=list(table[i]) row[j]='*' count+=1 table[i]=''.join(row) print(count) for i in range(1,n+1): print(table[i][1:m+1]) n,m,k=input().split(' ') carta=[] for i in range(int(n)): row='0'+input()+'0' carta.append(row) carta=['0'*(int(m)+2)]+carta+['0'*(int(m)+2)] island(carta,int(n),int(m),int(k))
Codeforces Round 375 (Div. 2)
CF
2,016
2
256
Lakes in Berland
The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × 1. Each cell is either land or water. The map is surrounded by the ocean. Lakes are the maximal regions of water cells, connected by sides, which are not connected with the ocean. Formally, lake is a set of water cells, such that it's possible to get from any cell of the set to any other without leaving the set and moving only to cells adjacent by the side, none of them is located on the border of the rectangle, and it's impossible to add one more water cell to the set such that it will be connected with any other cell. You task is to fill up with the earth the minimum number of water cells so that there will be exactly k lakes in Berland. Note that the initial number of lakes on the map is not less than k.
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 0 ≤ k ≤ 50) — the sizes of the map and the number of lakes which should be left on the map. The next n lines contain m characters each — the description of the map. Each of the characters is either '.' (it means that the corresponding cell is water) or '*' (it means that the corresponding cell is land). It is guaranteed that the map contain at least k lakes.
In the first line print the minimum number of cells which should be transformed from water to land. In the next n lines print m symbols — the map after the changes. The format must strictly follow the format of the map in the input data (there is no need to print the size of the map). If there are several answers, print any of them. It is guaranteed that the answer exists on the given data.
null
In the first example there are only two lakes — the first consists of the cells (2, 2) and (2, 3), the second consists of the cell (4, 3). It is profitable to cover the second lake because it is smaller. Pay attention that the area of water in the lower left corner is not a lake because this area share a border with the ocean.
[{"input": "5 4 1\n****\n*..*\n****\n**.*\n..**", "output": "1\n****\n*..*\n****\n****\n..**"}, {"input": "3 3 0\n***\n*.*\n***", "output": "1\n***\n***\n***"}]
1,600
["dfs and similar", "dsu", "graphs", "greedy", "implementation"]
26
[{"input": "5 4 1\r\n****\r\n*..*\r\n****\r\n**.*\r\n..**\r\n", "output": "1\r\n****\r\n*..*\r\n****\r\n****\r\n..**\r\n"}, {"input": "3 3 0\r\n***\r\n*.*\r\n***\r\n", "output": "1\r\n***\r\n***\r\n***\r\n"}, {"input": "3 5 1\r\n.**.*\r\n*.*.*\r\n***..\r\n", "output": "0\r\n.**.*\r\n*.*.*\r\n***..\r\n"}, {"input": "3 5 0\r\n.**.*\r\n*.*.*\r\n***..\r\n", "output": "1\r\n.**.*\r\n***.*\r\n***..\r\n"}, {"input": "3 50 7\r\n***.********.*********************.**********.****\r\n*...**..*.**.*.*.*.*.*.*.*..*.*.*.*.*.*.*.*.*.*..*\r\n****************.*.********.**********************\r\n", "output": "8\r\n***.********.*********************.**********.****\r\n*...**..****.***.*.*******..*******.*.*.*.*.*.*..*\r\n****************.*.********.**********************\r\n"}, {"input": "50 3 4\r\n***\r\n*.*\r\n*.*\r\n*.*\r\n***\r\n***\r\n*.*\r\n***\r\n.**\r\n***\r\n..*\r\n***\r\n***\r\n*.*\r\n***\r\n*.*\r\n***\r\n***\r\n*.*\r\n***\r\n*.*\r\n*.*\r\n*.*\r\n*.*\r\n***\r\n*.*\r\n*.*\r\n*.*\r\n*.*\r\n***\r\n***\r\n*.*\r\n*.*\r\n*.*\r\n*.*\r\n*.*\r\n***\r\n***\r\n***\r\n*.*\r\n***\r\n***\r\n***\r\n*.*\r\n*.*\r\n*.*\r\n***\r\n***\r\n***\r\n***\r\n", "output": "8\r\n***\r\n***\r\n***\r\n***\r\n***\r\n***\r\n***\r\n***\r\n.**\r\n***\r\n..*\r\n***\r\n***\r\n***\r\n***\r\n***\r\n***\r\n***\r\n***\r\n***\r\n*.*\r\n*.*\r\n*.*\r\n*.*\r\n***\r\n*.*\r\n*.*\r\n*.*\r\n*.*\r\n***\r\n***\r\n*.*\r\n*.*\r\n*.*\r\n*.*\r\n*.*\r\n***\r\n***\r\n***\r\n***\r\n***\r\n***\r\n***\r\n*.*\r\n*.*\r\n*.*\r\n***\r\n***\r\n***\r\n***\r\n"}, {"input": "1 1 0\r\n.\r\n", "output": "0\r\n.\r\n"}, {"input": "1 1 0\r\n*\r\n", "output": "0\r\n*\r\n"}]
false
stdio
import sys from sys import argv def main(): input_path = argv[1] output_path = argv[2] submission_path = argv[3] with open(input_path, 'r') as f: input_lines = f.read().splitlines() n, m, k = map(int, input_lines[0].split()) original_map = input_lines[1:n+1] with open(output_path, 'r') as f: ref_lines = f.read().splitlines() if not ref_lines: print(0) return ref_min = int(ref_lines[0]) with open(submission_path, 'r') as f: sub_lines = f.read().splitlines() if not sub_lines: print(0) return # Check first line try: sub_min = int(sub_lines[0]) except: print(0) return if sub_min != ref_min: print(0) return # Check map lines if len(sub_lines) -1 != n: print(0) return sub_map = sub_lines[1:n+1] for line in sub_map: if len(line) != m: print(0) return # Check original land preserved and count transformed transformed = 0 for i in range(n): orig_line = original_map[i] sub_line = sub_map[i] for j in range(m): oc = orig_line[j] sc = sub_line[j] if oc == '*' and sc != '*': print(0) return if oc == '.' and sc == '*': transformed += 1 if transformed != sub_min: print(0) return # Check number of lakes visited = [[False]*m for _ in range(n)] lakes = 0 dirs = [(-1,0), (1,0), (0,-1), (0,1)] def is_border(x, y): return x == 0 or x == n-1 or y ==0 or y == m-1 for i in range(n): for j in range(m): if sub_map[i][j] == '.' and not visited[i][j]: stack = [(i,j)] visited[i][j] = True has_border = False for x, y in stack: if is_border(x, y): has_border = True for dx, dy in dirs: nx, ny = x + dx, y + dy if 0 <= nx < n and 0 <= ny < m and sub_map[nx][ny] == '.' and not visited[nx][ny]: visited[nx][ny] = True stack.append((nx, ny)) if not has_border: lakes += 1 if lakes != k: print(0) return print(1) if __name__ == '__main__': main()
true
722/F
722
F
PyPy 3-64
TESTS
3
108
4,096,000
218252901
from io import BytesIO, IOBase import sys import os # import time import bisect # import functools import math import random # import re from collections import Counter, defaultdict, deque from copy import deepcopy from functools import cmp_to_key, lru_cache, reduce from heapq import heapify, heappop, heappush, heappushpop, nlargest, nsmallest from itertools import accumulate, combinations, permutations, count, product from operator import add, iand, ior, itemgetter, mul, xor from string import ascii_lowercase, ascii_uppercase from typing import * class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") BUFSIZE = 5096 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() sys.stdin = IOWrapper(sys.stdin) sys.stdout = IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") def I(): return input() def II(): return int(input()) def MII(): return map(int, input().split()) def LI(): return list(input().split()) def LII(): return list(map(int, input().split())) def GMI(): return map(lambda x: int(x) - 1, input().split()) def LGMI(): return list(map(lambda x: int(x) - 1, input().split())) inf = float('inf') # from types import GeneratorType # def bootstrap(f, stack=[]): # def wrappedfunc(*args, **kwargs): # if stack: # return f(*args, **kwargs) # else: # to = f(*args, **kwargs) # while True: # if type(to) is GeneratorType: # stack.append(to) # to = next(to) # else: # stack.pop() # if not stack: # break # to = stack[-1].send(to) # return to # return wrappedfunc # RANDOM = random.getrandbits(32) # class Wrapper_str(str): # def __init__(self, x): # str.__init__(x) # def __hash__(self): # return super(Wrapper_str, self).__hash__() ^ RANDOM # class Wrapper_tuple(tuple): # def __init__(self, x): # tuple.__init__(x) # def __hash__(self): # return super(Wrapper_tuple, self).__hash__() ^ RANDOM # class Wrapper_int(int): # def __init__(self, x): # int.__init__(x) # def __hash__(self): # return super(Wrapper_int, self).__hash__() ^ RANDOM class SparseTable: def __init__(self, data, merge_method): self.note = [0] * (len(data) + 1) self.merge_method = merge_method l, r, v = 1, 2, 0 while True: for i in range(l, r): if i >= len(self.note): break self.note[i] = v else: l *= 2 r *= 2 v += 1 continue break self.ST = [[0] * len(data) for _ in range(self.note[-1]+1)] self.ST[0] = data for i in range(1, len(self.ST)): for j in range(len(data) - (1 << i) + 1): self.ST[i][j] = merge_method(self.ST[i-1][j], self.ST[i-1][j + (1 << (i-1))]) def query(self, l, r): if l > r: return 1 pos = self.note[r-l+1] return self.merge_method(self.ST[pos][l], self.ST[pos][r - (1 << pos) + 1]) def ext_gcd(a, b): if b == 0: return a, 1, 0 gcd, x, y = ext_gcd(b, a % b) return gcd, y, x - (a // b) * y def mod_inv(a, p): gcd, x, y = ext_gcd(a, p) return x % p n, m = MII() notes = [[] for _ in range(m + 1)] lengths = [] for i in range(n): k, *nums = MII() for j in range(k): notes[nums[j]].append((i, j)) lengths.append(k) for v in range(1, m + 1): tmp = notes[v] l = 0 ans = 0 st = SparseTable([lengths[i] for i, _ in tmp], math.lcm) while l < len(tmp): r = l while r < len(tmp) - 1 and tmp[r][0] + 1 == tmp[r+1][0]: r += 1 pt1, pt2 = l, l curr, curr_mod = 0, 1 while pt1 <= r: while pt2 <= r: length = lengths[pt2] val = tmp[pt2][1] g = math.gcd(curr_mod, length) if (val - curr) % g: break curr = (val - curr) // g * mod_inv(curr_mod // g, length // g) * curr_mod + curr curr_mod = st.query(pt1, pt2) curr %= curr_mod pt2 += 1 ans = max(ans, pt2 - pt1) pt1 += 1 curr_mod = st.query(pt1, pt2 - 1) curr %= curr_mod l = r + 1 print(ans)
73
1,575
43,315,200
189445525
import sys, random input = lambda : sys.stdin.readline().rstrip() write = lambda x: sys.stdout.write(x+"\n"); writef = lambda x: print("{:.12f}".format(x)) debug = lambda x: sys.stderr.write(x+"\n") YES="Yes"; NO="No"; pans = lambda v: print(YES if v else NO); INF=10**18 LI = lambda : list(map(int, input().split())); II=lambda : int(input()); SI=lambda : [ord(c)-ord("a") for c in input()] def debug(_l_): for s in _l_.split(): print(f"{s}={eval(s)}", end=" ") print() def dlist(*l, fill=0): if len(l)==1: return [fill]*l[0] ll = l[1:] return [dlist(*ll, fill=fill) for _ in range(l[0])] def gcd2(a, b): """a*x + b*y = gcd(a,b)なるx,yも求める """ l = [] while b: l.append(divmod(a,b)) a, b = b, a%b x, y = 1, 0 for aa,bb in l[::-1]: x, y = y, x - aa*y return a, x, y def modinv(x, M): """素数ではないM、Mと互いに素なxに対し x * y == 1 mod M なるyを求める """ a,xx,yy = gcd2(x,M) return a,xx%M def solve(a,b,n): """a*x = b mod nを解く """ g,xx,yy = gcd2(a,n) if b%g!=0: return None a //= g n //= g b //= g # aとnが互いに素になっているので ainv = modinv(a, n)[1] x = ainv*b%n return x def crt(rs, ms): """中国剰余定理 x == rs[i] mod ms[i] をみたすxをを求め、(x, l(=ms))を返す (そのようなxは x + i * lとして書ける) 存在しない場合はNoneを返す 長さが0の配列を渡すと(0,1)を返す """ r0 = 0 m0 = 1 for r1,m1 in zip(rs, ms): if m0<m1: m0, m1 = m1, m0 r0, r1 = r1, r0 if m0%m1==0: if r0%m1 != r1: return None,None else: continue # print(m0,m1) g,im = modinv(m0, m1) u1 = m1//g if (r1-r0)%g!=0: return None,None x = (r1-r0) // g % u1 * im % u1 r0 += x * m0 m0 *= u1 if r0<0: r0 += m0 return r0,m0 ### セグメント木(はやい) class SG: def __init__(self, n, v=None): self._n = n self.geta = 0 x = 0 while (1 << x) < n: x += 1 self._log = x self._size = 1 << self._log self._d = [ninf] * (2 * self._size) if v is not None: for i in range(self._n): self._d[self._size + i] = v[i] for i in range(self._size - 1, 0, -1): self._update(i) def _update(self, k): self._d[k] = op(self._d[2 * k], self._d[2 * k + 1]) def update(self, p, x): assert 0 <= p < self._n # x -= self.geta p += self._size self._d[p] = x for i in range(1, self._log + 1): # self._update(p >> i) k = p>>i self._d[k] = op(self._d[2 * k], self._d[2 * k + 1]) def get(self, p): assert 0 <= p < self._n return self._d[p + self._size] # + self.geta def check(self): return [self.get(p) for p in range(self._n)] def query(self, left, right): # [l,r)の総和 assert 0 <= left <= right <= self._n sml = ninf smr = ninf left += self._size right += self._size # 外側から計算していく(lは小さい側から, rは大きい側から) while left < right: if left & 1: sml = op(sml, self._d[left]) left += 1 if right & 1: right -= 1 smr = op(self._d[right], smr) left >>= 1 right >>= 1 return op(sml, smr) # + self.geta # def update_all(self, v): # # 全体加算 # self.geta += v def query_all(self): return self._d[1] # + self.geta def max_right(self, left, f): """f(op(a[l], a[l + 1], ..., a[r - 1])) = true となる最大の r -> rはf(op(a[l], ..., a[r]))がFalseになる最小のr """ # assert 0 <= left <= self._n # assert f(ninf) if left == self._n: return self._n left += self._size sm = ninf first = True while first or (left & -left) != left: first = False while left % 2 == 0: left >>= 1 if not f(op(sm, self._d[left])): while left < self._size: left *= 2 if f(op(sm, self._d[left])): sm = op(sm, self._d[left]) left += 1 return left - self._size sm = op(sm, self._d[left]) left += 1 return self._n def min_left(self, right, f): """f(op(a[l], a[l + 1], ..., a[r - 1])) = true となる最小の l -> l は f(op(a[l-1] ,..., a[r-1])) が false になる最大の l """ # assert 0 <= right <= self._n # assert f(ninf) if right == 0: return 0 right += self._size sm = ninf first = True while first or (right & -right) != right: first = False right -= 1 while right > 1 and right % 2: right >>= 1 if not f(op(self._d[right], sm)): while right < self._size: right = 2 * right + 1 if f(op(self._d[right], sm)): sm = op(self._d[right], sm) right -= 1 return right + 1 - self._size sm = op(self._d[right], sm) return 0 # [左よせの(l, m, r), 右よせの(l, m, r), 長さの max, 区間の長さ] def op(l,r): if l[0] is None or r[0] is None: return (None, None) return crt([l[0], r[0]], [l[1], r[1]]) ninf = (0,1) n,m = list(map(int, input().split())) index = [[] for _ in range(m)] ls = [0]*n for i in range(n): k,*a = LI() ls[i] = k for j in range(k): index[a[j]-1].append((i,j)) sg = SG(n, [(None, None) for _ in range(n)]) for v in range(m): for i,j in index[v]: sg.update(i, (j, ls[i])) # sg.update(n+i, ((j-1)%ls[i], ls[i])) val = 0 for i,j in index[v]: res = sg.max_right(i, lambda item: item[0] is not None) val = max(val, res-i) print(val) # if v==0: # break for i,j in index[v]: sg.update(i, (None,None)) # sg.update(n+i, (None,None))
Intel Code Challenge Elimination Round (Div. 1 + Div. 2, combined)
CF
2,016
2
256
Cyclic Cipher
You are given n sequences. Each sequence consists of positive integers, not exceeding m. All integers in one sequence are distinct, but the same integer may appear in multiple sequences. The length of the i-th sequence is ki. Each second integers in each of the sequences are shifted by one to the left, i.e. integers at positions i > 1 go to positions i - 1, while the first integers becomes the last. Each second we take the first integer of each sequence and write it down to a new array. Then, for each value x from 1 to m we compute the longest segment of the array consisting of element x only. The above operation is performed for 10100 seconds. For each integer from 1 to m find out the longest segment found at this time.
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100 000) — the number of sequences and the maximum integer that can appear in the sequences. Then follow n lines providing the sequences. Each of them starts with an integer ki (1 ≤ ki ≤ 40) — the number of integers in the sequence, proceeded by ki positive integers — elements of the sequence. It's guaranteed that all integers in each sequence are pairwise distinct and do not exceed m. The total length of all sequences doesn't exceed 200 000.
Print m integers, the i-th of them should be equal to the length of the longest segment of the array with all its values equal to i during the first 10100 seconds.
null
null
[{"input": "3 4\n3 3 4 1\n4 1 3 4 2\n3 3 1 4", "output": "2\n1\n3\n2"}, {"input": "5 5\n2 3 1\n4 5 1 3 2\n4 2 1 3 5\n1 3\n2 5 3", "output": "3\n1\n4\n0\n1"}, {"input": "4 6\n3 4 5 3\n2 6 3\n2 3 6\n3 3 6 5", "output": "0\n0\n2\n1\n1\n2"}]
2,800
["chinese remainder theorem", "data structures", "implementation", "number theory", "two pointers"]
73
[{"input": "3 4\r\n3 3 4 1\r\n4 1 3 4 2\r\n3 3 1 4\r\n", "output": "2\r\n1\r\n3\r\n2\r\n"}, {"input": "5 5\r\n2 3 1\r\n4 5 1 3 2\r\n4 2 1 3 5\r\n1 3\r\n2 5 3\r\n", "output": "3\r\n1\r\n4\r\n0\r\n1\r\n"}, {"input": "4 6\r\n3 4 5 3\r\n2 6 3\r\n2 3 6\r\n3 3 6 5\r\n", "output": "0\r\n0\r\n2\r\n1\r\n1\r\n2\r\n"}, {"input": "10 5\r\n2 2 4\r\n2 4 5\r\n2 1 2\r\n4 3 1 5 2\r\n4 1 3 2 5\r\n5 3 4 5 2 1\r\n3 3 5 4\r\n1 1\r\n2 4 1\r\n1 5\r\n", "output": "2\r\n2\r\n3\r\n2\r\n3\r\n"}, {"input": "10 10\r\n2 1 5\r\n2 3 4\r\n4 7 1 10 4\r\n4 6 3 9 7\r\n2 3 5\r\n3 4 9 7\r\n3 6 5 9\r\n5 8 5 4 10 6\r\n3 5 7 1\r\n1 5\r\n", "output": "1\r\n0\r\n1\r\n2\r\n3\r\n2\r\n1\r\n1\r\n1\r\n1\r\n"}, {"input": "10 100\r\n1 76\r\n3 86 1 85\r\n1 96\r\n5 48 54 32 71 90\r\n2 27 18\r\n1 38\r\n1 73\r\n3 60 40 4\r\n4 92 77 37 80\r\n4 61 24 67 82\r\n", "output": "1\r\n0\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n0\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n1\r\n0\r\n0\r\n0\r\n0\r\n0\r\n1\r\n0\r\n0\r\n0\r\n1\r\n0\r\n1\r\n0\r\n0\r\n1\r\n1\r\n0\r\n0\r\n1\r\n0\r\n1\r\n0\r\n0\r\n1\r\n1\r\n0\r\n0\r\n0\r\n1\r\n0\r\n1\r\n0\r\n0\r\n0\r\n1\r\n0\r\n0\r\n0\r\n0\r\n"}]
false
stdio
null
true
908/F
908
F
Python 3
TESTS
3
46
204,800
181818000
class Aresta: def __init__(self,vertice_1,vertice_2,peso): self.vertice_1 = vertice_1 self.vertice_2 = vertice_2 self.peso = peso class Vertice: def __init__(self,cor,posicao): self.cor = cor self.posicao = posicao class Oferta: def __init__(self,x,y,z) : self.x = x self.y = y self.z = z class Grafo: def __init__(self) -> None: self.vertices = [] self.arestas = [] self.ofertas = [] self.arestas_verde_verde= [] self.arestas_vermelho_vermelho= [] self.arestas_azul_azul= [] self.qnt_verde = 0 self.qnt_azul = 0 self.qnt_vermelho = 0 def inicializar(self): for i in range(0,len(self.vertices)): for k in range(i,len(self.vertices)): if( i != k): menor = i if self.vertices[k].posicao > self.vertices[i].posicao else k maior = i if self.vertices[k].posicao < self.vertices[i].posicao else k self.arestas.append(Aresta(i,k,self.vertices[maior].posicao - self.vertices[menor].posicao)) def find(self, conjuntos, vertice): for i in range(len(conjuntos)): for y in conjuntos[i]: if(vertice == y): return i def kruskall_cor(self,cor): result = [] i = 0 e = 0 arestas_ordenadas = sorted(self.arestas, key=lambda aresta: aresta.peso) arestas_selecionadas = [] for a in arestas_ordenadas: if(self.vertices[a.vertice_1].cor == cor and self.vertices[a.vertice_2].cor == cor): arestas_selecionadas.append(a) conjuntos = [] conj_final= [] qnt =0 #cria conjuntos com os vertices for k in range(len(self.vertices)): if(self.vertices[k].cor == cor): conjuntos.append([k]) qnt += 1 while e < qnt-1: aresta = arestas_selecionadas[i] i = i + 1 x = self.find(conjuntos, aresta.vertice_1) y = self.find(conjuntos, aresta.vertice_2) if x != y: e = e + 1 result.append(aresta) #union if len(conjuntos[x]) < len(conjuntos[y]): conjuntos[y] = conjuntos[y] + conjuntos[x] conjuntos[x] = [] conj_final = conjuntos[y] else: conjuntos[x] = conjuntos[x] + conjuntos[y] conjuntos[y] = [] conj_final = conjuntos[x] minimumCost = 0 for a in result: minimumCost += a.peso return (conj_final,minimumCost) def kruskall(self): conj_verde = self.kruskall_cor('G') conj_vermelho = self.kruskall_cor('R') conj_azul = self.kruskall_cor('B') conjunto_final = conj_verde minimumCost = conj_verde[1] + conj_vermelho[1] + conj_azul[1] #pegar menor aresta verde_vermelha arestas_ordenadas = sorted(self.arestas, key=lambda aresta: aresta.peso) for a in arestas_ordenadas: if((self.vertices[a.vertice_1].cor == 'R' and self.vertices[a.vertice_2].cor == 'G') or (self.vertices[a.vertice_1].cor == 'G' and self.vertices[a.vertice_2].cor == 'R')): conjunto_final = (conjunto_final[0]+conj_vermelho[0],conjunto_final[1]) minimumCost += a.peso break #pegar menor aresta verde_azul for a in arestas_ordenadas: if((self.vertices[a.vertice_1].cor == 'B' and self.vertices[a.vertice_2].cor == 'G') or (self.vertices[a.vertice_1].cor == 'G' and self.vertices[a.vertice_2].cor == 'B')): conjunto_final = (conjunto_final[0]+conj_azul[0],conjunto_final[1]) minimumCost += a.peso break # o custo minimo será a soma de todos os conjuntos mais as arestas de ligacao return minimumCost def adicionar_vertice(self,cor,posicao): self.vertices.append(Vertice(cor,posicao)) if(cor == 'R'): self.qnt_vermelho += 1 if(cor == 'B'): self.qnt_azul += 1 if(cor == 'G'): self.qnt_verde += 1 """ def adicionar_aresta(self,vertice_1,vertice_2): # checa se o grafo possui os dois vertices if self.grafo.get(vertice_1) != None and self.grafo.get(vertice_2) != None: self.grafo[vertice_1].insert(len(self.grafo[vertice_1]),vertice_2) self.grafo[vertice_2].insert(len(self.grafo[vertice_2]),vertice_1) self.arestas.append(Aresta(vertice_1,vertice_2)) def remover_aresta(self,vertice_1,vertice_2): # checa se o grafo possui os dois vertices if(self.grafo.get(vertice_1) != None and self.grafo.get(vertice_2) != None): if(vertice_1 in self.grafo[vertice_2]): self.grafo[vertice_2].remove(vertice_1) if(vertice_2 in self.grafo[vertice_1]): self.grafo[vertice_1].remove(vertice_2) for a in self.arestas: if((a.vertice_1 == vertice_1 and a.vertice_2 == vertice_2) or (a.vertice_1 == vertice_2 and a.vertice_2 == vertice_1)): self.arestas.remove(a) break def remover_vertice(self,vertice): if self.grafo.get(vertice) != None: for v in self.grafo[vertice]: self.remover_aresta(v,vertice) self.grafo.pop(vertice) def vizinhos(self,vertice_1,vertice_2): return vertice_2 in self.grafo[vertice_1] """ nvert = int(input()) # inicializa os vertices grf = Grafo() # coloca os pesos e cores nos vertices for i in range(0,nvert): entrada = input() pos = int(entrada.split()[0]) cor = entrada.split()[1] grf.adicionar_vertice(cor,pos) grf.inicializar() print(grf.kruskall())
44
1,185
307,200
113258056
#Problem Set F: Collaborated with no one n = int(input()) result = 0 temp = 0 b_red = 0 b_blue = 0 final_red = -1 final_blue = -1 final_green = -(1<<60) for i in range(n): cost_colorList = input().split() cost = int(cost_colorList[0]) color = cost_colorList[1] if color == 'R' or color == 'G': if final_red != -1: b_red = max(b_red, cost - final_red) temp += cost - final_red final_red = cost if color == 'B' or color == 'G': if final_blue != -1: b_blue = max(b_blue, cost - final_blue) temp += cost - final_blue final_blue = cost if color == 'G': result += temp + min(0, - b_red - b_blue + cost - final_green) final_red = final_blue = final_green = cost b_red = b_blue = temp = 0 result += temp print(result)
Good Bye 2017
CF
2,017
2
256
New Year and Rainbow Roads
Roy and Biv have a set of n points on the infinite number line. Each point has one of 3 colors: red, green, or blue. Roy and Biv would like to connect all the points with some edges. Edges can be drawn between any of the two of the given points. The cost of an edge is equal to the distance between the two points it connects. They want to do this in such a way that they will both see that all the points are connected (either directly or indirectly). However, there is a catch: Roy cannot see the color red and Biv cannot see the color blue. Therefore, they have to choose the edges in such a way that if all the red points are removed, the remaining blue and green points are connected (and similarly, if all the blue points are removed, the remaining red and green points are connected). Help them compute the minimum cost way to choose edges to satisfy the above constraints.
The first line will contain an integer n (1 ≤ n ≤ 300 000), the number of points. The next n lines will contain two tokens pi and ci (pi is an integer, 1 ≤ pi ≤ 109, ci is a uppercase English letter 'R', 'G' or 'B'), denoting the position of the i-th point and the color of the i-th point. 'R' means red, 'G' denotes green, and 'B' means blue. The positions will be in strictly increasing order.
Print a single integer, the minimum cost way to solve the problem.
null
In the first sample, it is optimal to draw edges between the points (1,2), (1,4), (3,4). These have costs 4, 14, 5, respectively.
[{"input": "4\n1 G\n5 R\n10 B\n15 G", "output": "23"}, {"input": "4\n1 G\n2 R\n3 B\n10 G", "output": "12"}]
2,400
["graphs", "greedy", "implementation"]
44
[{"input": "4\r\n1 G\r\n5 R\r\n10 B\r\n15 G\r\n", "output": "23\r\n"}, {"input": "4\r\n1 G\r\n2 R\r\n3 B\r\n10 G\r\n", "output": "12\r\n"}, {"input": "4\r\n1 G\r\n123123 R\r\n987987987 B\r\n1000000000 G\r\n", "output": "1012135134\r\n"}, {"input": "1\r\n3 R\r\n", "output": "0\r\n"}]
false
stdio
null
true
908/F
908
F
Python 3
TESTS
4
62
5,529,600
33905656
n=int(input()) prer=preg=preb=0 ans=ansb=ansr=0 for i in range(n): inp=input().split() pla=int(inp[0]) col=inp[1] if col=='R': if prer<=preg: ansr=pla-preg prer=pla elif col=='B': if preb<=preg: ansb=pla-preg preb=pla else: if preg==0: if preb>preg: ans+=pla-ansb if prer>preg: ans+=pla-ansr else: if (prer<=preg): if(preb<=preg): ans+=pla-preg else: ans+=(pla-preg)*2-max(ansb,pla-preb) elif preb<=preg: ans+=(pla-preg)*2-max(ansr,pla-prer) else: ans+=min((pla-preg)*2,(pla-preg)*3-max(ansr,pla-prer)-max(ansb,pla-preb)) preg=pla #print(ans,preb,ansb,prer,ansr) if preg>0: if preb>=preg: ans+=preb-preg if prer>=preg: ans+=prer-preg else: if preb>0: ans+=preb-ansb if prer>0: ans+=prer-ansr print(ans)
44
1,388
56,320,000
202729801
""" F. New Year and Rainbow Roads (https://codeforces.com/problemset/problem/908/F) 1. n points, each point has one color: RED, GREEN or BLUE. 2. Roy and Biv wanna connect all points with some edges. 3. The cost of an edge is its distance between another point. 4. All points needs to be connected. (directly or indirectly) 5. Roy cannot see RED and Biv cannot see BLUE. """ def calcular_custo_minimo(n, pontos): # Inicializar variáveis custo_total = 0 custo_parcial = 0 maior_distancia_r = 0 maior_distancia_b = 0 ultima_posicao_r = -1 ultima_posicao_b = -1 ultima_posicao_g = float('-inf') # Iterar sobre todos os pontos for i in range(n): posicao, cor = pontos[i] if cor == 'R' or cor == 'G': if ultima_posicao_r != -1: maior_distancia_r = max(maior_distancia_r, posicao - ultima_posicao_r) custo_parcial += posicao - ultima_posicao_r ultima_posicao_r = posicao if cor == 'B' or cor == 'G': if ultima_posicao_b != -1: maior_distancia_b = max(maior_distancia_b, posicao - ultima_posicao_b) custo_parcial += posicao - ultima_posicao_b ultima_posicao_b = posicao if cor == 'G': custo_total += custo_parcial + min(0, -maior_distancia_r - maior_distancia_b + posicao - ultima_posicao_g) ultima_posicao_r = ultima_posicao_b = ultima_posicao_g = posicao maior_distancia_r = maior_distancia_b = custo_parcial = 0 custo_total += custo_parcial return custo_total if __name__ == '__main__': n = int(input()) pontos = [input().split() for _ in range(n)] pontos = [(int(p), c) for p, c in pontos] custo_minimo = calcular_custo_minimo(n, pontos) print(custo_minimo)
Good Bye 2017
CF
2,017
2
256
New Year and Rainbow Roads
Roy and Biv have a set of n points on the infinite number line. Each point has one of 3 colors: red, green, or blue. Roy and Biv would like to connect all the points with some edges. Edges can be drawn between any of the two of the given points. The cost of an edge is equal to the distance between the two points it connects. They want to do this in such a way that they will both see that all the points are connected (either directly or indirectly). However, there is a catch: Roy cannot see the color red and Biv cannot see the color blue. Therefore, they have to choose the edges in such a way that if all the red points are removed, the remaining blue and green points are connected (and similarly, if all the blue points are removed, the remaining red and green points are connected). Help them compute the minimum cost way to choose edges to satisfy the above constraints.
The first line will contain an integer n (1 ≤ n ≤ 300 000), the number of points. The next n lines will contain two tokens pi and ci (pi is an integer, 1 ≤ pi ≤ 109, ci is a uppercase English letter 'R', 'G' or 'B'), denoting the position of the i-th point and the color of the i-th point. 'R' means red, 'G' denotes green, and 'B' means blue. The positions will be in strictly increasing order.
Print a single integer, the minimum cost way to solve the problem.
null
In the first sample, it is optimal to draw edges between the points (1,2), (1,4), (3,4). These have costs 4, 14, 5, respectively.
[{"input": "4\n1 G\n5 R\n10 B\n15 G", "output": "23"}, {"input": "4\n1 G\n2 R\n3 B\n10 G", "output": "12"}]
2,400
["graphs", "greedy", "implementation"]
44
[{"input": "4\r\n1 G\r\n5 R\r\n10 B\r\n15 G\r\n", "output": "23\r\n"}, {"input": "4\r\n1 G\r\n2 R\r\n3 B\r\n10 G\r\n", "output": "12\r\n"}, {"input": "4\r\n1 G\r\n123123 R\r\n987987987 B\r\n1000000000 G\r\n", "output": "1012135134\r\n"}, {"input": "1\r\n3 R\r\n", "output": "0\r\n"}]
false
stdio
null
true
1006/D
1006
D
PyPy 3-64
TESTS
2
140
7,168,000
218217374
import sys input = lambda: sys.stdin.readline().rstrip() import math from heapq import heappush , heappop from collections import defaultdict,deque,Counter from bisect import * N = int(input()) S = input() T = input() def check(C): keys = list(C.keys()) if len(keys)>2: return False if len(keys)==1: return True if C[keys[0]]==C[keys[1]]: return True return False def cal(A): t = int(A[0]!=A[2]) + int(A[1]!=A[3]) t = min(t, int(A[0]!=A[3]) + int(A[1]!=A[2])) return t ans = 0 for i in range(N//2): j = N-i-1 #print(i,j,S[i],S[j]) tmp = [S[i],S[j],T[i],T[j]] C = Counter(tmp) if check(C): continue ans += cal(tmp) if N%2: #print(N//2+1) if S[N//2]!=T[N//2]: ans+=1 print(ans)
23
77
2,969,600
183641682
n = int(input()) a = input() b = input() ans = 0 for i in range(n//2): sa = [a[i], a[n-i-1]] sb = [b[i], b[n-i-1]] sa.sort() sb.sort() #print(sa, sb) if sb[0] == sb[1]: if not sa[0] == sa[1]: ans += 1 else: if sa[0] == sa[1]: if sa[0] == sb[0] or sa[0] == sb[1]: ans += 1 else: ans += 2 else: ans += len(set([a[i], a[n-i-1], b[i], b[n-i-1]]))-2 #print(ans) if n%2 == 1: if a[n//2] != b[n//2]: ans += 1 print(ans)
Codeforces Round 498 (Div. 3)
ICPC
2,018
2
256
Two Strings Swaps
You are given two strings $$$a$$$ and $$$b$$$ consisting of lowercase English letters, both of length $$$n$$$. The characters of both strings have indices from $$$1$$$ to $$$n$$$, inclusive. You are allowed to do the following changes: - Choose any index $$$i$$$ ($$$1 \le i \le n$$$) and swap characters $$$a_i$$$ and $$$b_i$$$; - Choose any index $$$i$$$ ($$$1 \le i \le n$$$) and swap characters $$$a_i$$$ and $$$a_{n - i + 1}$$$; - Choose any index $$$i$$$ ($$$1 \le i \le n$$$) and swap characters $$$b_i$$$ and $$$b_{n - i + 1}$$$. Note that if $$$n$$$ is odd, you are formally allowed to swap $$$a_{\lceil\frac{n}{2}\rceil}$$$ with $$$a_{\lceil\frac{n}{2}\rceil}$$$ (and the same with the string $$$b$$$) but this move is useless. Also you can swap two equal characters but this operation is useless as well. You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps. In one preprocess move you can replace a character in $$$a$$$ with another character. In other words, in a single preprocess move you can choose any index $$$i$$$ ($$$1 \le i \le n$$$), any character $$$c$$$ and set $$$a_i := c$$$. Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings $$$a$$$ and $$$b$$$ equal by applying some number of changes described in the list above. Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string $$$b$$$ or make any preprocess moves after the first change is made.
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of strings $$$a$$$ and $$$b$$$. The second line contains the string $$$a$$$ consisting of exactly $$$n$$$ lowercase English letters. The third line contains the string $$$b$$$ consisting of exactly $$$n$$$ lowercase English letters.
Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string $$$a$$$ equal to string $$$b$$$ with a sequence of changes from the list above.
null
In the first example preprocess moves are as follows: $$$a_1 := $$$'b', $$$a_3 := $$$'c', $$$a_4 := $$$'a' and $$$a_5:=$$$'b'. Afterwards, $$$a = $$$"bbcabba". Then we can obtain equal strings by the following sequence of changes: $$$swap(a_2, b_2)$$$ and $$$swap(a_2, a_6)$$$. There is no way to use fewer than $$$4$$$ preprocess moves before a sequence of changes to make string equal, so the answer in this example is $$$4$$$. In the second example no preprocess moves are required. We can use the following sequence of changes to make $$$a$$$ and $$$b$$$ equal: $$$swap(b_1, b_5)$$$, $$$swap(a_2, a_4)$$$.
[{"input": "7\nabacaba\nbacabaa", "output": "4"}, {"input": "5\nzcabd\ndbacz", "output": "0"}]
1,700
["implementation"]
23
[{"input": "7\r\nabacaba\r\nbacabaa\r\n", "output": "4\r\n"}, {"input": "5\r\nzcabd\r\ndbacz\r\n", "output": "0\r\n"}, {"input": "1\r\na\r\nb\r\n", "output": "1\r\n"}, {"input": "5\r\nahmad\r\nyogaa\r\n", "output": "3\r\n"}]
false
stdio
null
true
1006/D
1006
D
PyPy 3
TESTS
2
155
2,662,400
83916243
""" 616C """ """ 1152B """ # import math # import sys def main(): # n ,m= map(int,input().split()) # arr = list(map(int,input().split())) # b = list(map(int,input().split())) # n = int(input()) # string = str(input()) # TODO: # 1> LEETCODE FIRST PROBLEM WRITE # 2> VALERYINE AND DEQUEUE n = int(input()) str1 = str(input()) str2 = str(input()) ans = 0 for i in range(n//2): if (str1[i]==str2[i] and str1[n-1-i]==str2[n-i-1]) or (str1[i]==str2[n-1-i] and str1[n-1-i]==str2[i]): continue else: ans+=1 if n%2 and str1[n//2]!=str2[n//2]: ans+=1 print(ans) return main() # def test(): # t = int(input()) # while t: # main() # t-=1 # test()
23
78
3,584,000
154192240
n=int(input()) a=input() b=input() ans=0 for i in range(n//2): seen=set() if b[i]==b[n-i-1]: if a[i]==a[n-i-1]: ans+=0 else: ans+=1 else : seen.add(b[i]) if b[n-i-1] in seen: seen.remove(b[n-i-1]) else : seen.add(b[n-i-1]) if a[i] in seen: seen.remove(a[i]) else : ans+=1 if a[n-1-i] in seen: seen.remove(a[n-i-1]) else : ans+=1 if n%2: if a[n//2]!=b[n//2]: ans+=1 print(ans)
Codeforces Round 498 (Div. 3)
ICPC
2,018
2
256
Two Strings Swaps
You are given two strings $$$a$$$ and $$$b$$$ consisting of lowercase English letters, both of length $$$n$$$. The characters of both strings have indices from $$$1$$$ to $$$n$$$, inclusive. You are allowed to do the following changes: - Choose any index $$$i$$$ ($$$1 \le i \le n$$$) and swap characters $$$a_i$$$ and $$$b_i$$$; - Choose any index $$$i$$$ ($$$1 \le i \le n$$$) and swap characters $$$a_i$$$ and $$$a_{n - i + 1}$$$; - Choose any index $$$i$$$ ($$$1 \le i \le n$$$) and swap characters $$$b_i$$$ and $$$b_{n - i + 1}$$$. Note that if $$$n$$$ is odd, you are formally allowed to swap $$$a_{\lceil\frac{n}{2}\rceil}$$$ with $$$a_{\lceil\frac{n}{2}\rceil}$$$ (and the same with the string $$$b$$$) but this move is useless. Also you can swap two equal characters but this operation is useless as well. You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps. In one preprocess move you can replace a character in $$$a$$$ with another character. In other words, in a single preprocess move you can choose any index $$$i$$$ ($$$1 \le i \le n$$$), any character $$$c$$$ and set $$$a_i := c$$$. Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings $$$a$$$ and $$$b$$$ equal by applying some number of changes described in the list above. Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string $$$b$$$ or make any preprocess moves after the first change is made.
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of strings $$$a$$$ and $$$b$$$. The second line contains the string $$$a$$$ consisting of exactly $$$n$$$ lowercase English letters. The third line contains the string $$$b$$$ consisting of exactly $$$n$$$ lowercase English letters.
Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string $$$a$$$ equal to string $$$b$$$ with a sequence of changes from the list above.
null
In the first example preprocess moves are as follows: $$$a_1 := $$$'b', $$$a_3 := $$$'c', $$$a_4 := $$$'a' and $$$a_5:=$$$'b'. Afterwards, $$$a = $$$"bbcabba". Then we can obtain equal strings by the following sequence of changes: $$$swap(a_2, b_2)$$$ and $$$swap(a_2, a_6)$$$. There is no way to use fewer than $$$4$$$ preprocess moves before a sequence of changes to make string equal, so the answer in this example is $$$4$$$. In the second example no preprocess moves are required. We can use the following sequence of changes to make $$$a$$$ and $$$b$$$ equal: $$$swap(b_1, b_5)$$$, $$$swap(a_2, a_4)$$$.
[{"input": "7\nabacaba\nbacabaa", "output": "4"}, {"input": "5\nzcabd\ndbacz", "output": "0"}]
1,700
["implementation"]
23
[{"input": "7\r\nabacaba\r\nbacabaa\r\n", "output": "4\r\n"}, {"input": "5\r\nzcabd\r\ndbacz\r\n", "output": "0\r\n"}, {"input": "1\r\na\r\nb\r\n", "output": "1\r\n"}, {"input": "5\r\nahmad\r\nyogaa\r\n", "output": "3\r\n"}]
false
stdio
null
true
1006/D
1006
D
PyPy 3
TESTS
2
155
4,812,800
120343576
from sys import stdin, stdout from itertools import accumulate nmbr = lambda: int(input()) lst = lambda: list(map(int, input().split())) def fn(x,y,c1,c2): return int(x!=c1)+int(y!=c2) for _ in range(1):#nmbr()): n=nmbr() # n,k=lst() s1=input() s2=input() ans=0 l,r=0,n-1 while l<=r: if l==r: if s1[l]!=s2[l]:ans+=1 l+=1 r-=1 else: c1,c2=s2[l],s2[r] cha,chb=s1[l],s1[r] if len(set([c1,c2,cha,chb]))==4:ans+=2 else: if min(fn(cha,chb,c1,c2), fn(chb,cha,c1,c2))!=0: ans+=1 # elif len(set([c1,c2,cha,chb]))==3: # ans+=1 # print(c1,c2,cha,chb,ans) l+=1 r-=1 print(ans)
23
93
512,000
214462808
# LUOGU_RID: 116435796 n=int(input()) a=input() b=input() ans=0 if n%2!=0: if a[n//2]!=b[n//2]: ans+=1 for i in range(n//2): if b[i]==b[n-1-i]: if a[i]!=a[n-1-i]: ans+=1 continue if b[i]!=a[i] and b[i]!=a[n-1-i]: ans+=1 if b[n-1-i]!=a[i] and b[n-1-i]!=a[n-1-i]: ans+=1 print(ans)
Codeforces Round 498 (Div. 3)
ICPC
2,018
2
256
Two Strings Swaps
You are given two strings $$$a$$$ and $$$b$$$ consisting of lowercase English letters, both of length $$$n$$$. The characters of both strings have indices from $$$1$$$ to $$$n$$$, inclusive. You are allowed to do the following changes: - Choose any index $$$i$$$ ($$$1 \le i \le n$$$) and swap characters $$$a_i$$$ and $$$b_i$$$; - Choose any index $$$i$$$ ($$$1 \le i \le n$$$) and swap characters $$$a_i$$$ and $$$a_{n - i + 1}$$$; - Choose any index $$$i$$$ ($$$1 \le i \le n$$$) and swap characters $$$b_i$$$ and $$$b_{n - i + 1}$$$. Note that if $$$n$$$ is odd, you are formally allowed to swap $$$a_{\lceil\frac{n}{2}\rceil}$$$ with $$$a_{\lceil\frac{n}{2}\rceil}$$$ (and the same with the string $$$b$$$) but this move is useless. Also you can swap two equal characters but this operation is useless as well. You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps. In one preprocess move you can replace a character in $$$a$$$ with another character. In other words, in a single preprocess move you can choose any index $$$i$$$ ($$$1 \le i \le n$$$), any character $$$c$$$ and set $$$a_i := c$$$. Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings $$$a$$$ and $$$b$$$ equal by applying some number of changes described in the list above. Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string $$$b$$$ or make any preprocess moves after the first change is made.
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of strings $$$a$$$ and $$$b$$$. The second line contains the string $$$a$$$ consisting of exactly $$$n$$$ lowercase English letters. The third line contains the string $$$b$$$ consisting of exactly $$$n$$$ lowercase English letters.
Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string $$$a$$$ equal to string $$$b$$$ with a sequence of changes from the list above.
null
In the first example preprocess moves are as follows: $$$a_1 := $$$'b', $$$a_3 := $$$'c', $$$a_4 := $$$'a' and $$$a_5:=$$$'b'. Afterwards, $$$a = $$$"bbcabba". Then we can obtain equal strings by the following sequence of changes: $$$swap(a_2, b_2)$$$ and $$$swap(a_2, a_6)$$$. There is no way to use fewer than $$$4$$$ preprocess moves before a sequence of changes to make string equal, so the answer in this example is $$$4$$$. In the second example no preprocess moves are required. We can use the following sequence of changes to make $$$a$$$ and $$$b$$$ equal: $$$swap(b_1, b_5)$$$, $$$swap(a_2, a_4)$$$.
[{"input": "7\nabacaba\nbacabaa", "output": "4"}, {"input": "5\nzcabd\ndbacz", "output": "0"}]
1,700
["implementation"]
23
[{"input": "7\r\nabacaba\r\nbacabaa\r\n", "output": "4\r\n"}, {"input": "5\r\nzcabd\r\ndbacz\r\n", "output": "0\r\n"}, {"input": "1\r\na\r\nb\r\n", "output": "1\r\n"}, {"input": "5\r\nahmad\r\nyogaa\r\n", "output": "3\r\n"}]
false
stdio
null
true
351/E
351
E
Python 3
TESTS
1
62
0
228196194
n = int(input()) sequence = list(map(int, input().split())) positive_count = 0 negative_count = 0 for num in sequence: if num > 0: positive_count += 1 elif num < 0: negative_count += 1 inversions = min(positive_count, negative_count) print(inversions)
36
280
3,686,400
150804902
import sys input = sys.stdin.buffer.readline def process(A): n = len(A) S = [1 for i in range(n)] d = {} for i in range(n): ai = abs(A[i]) if ai not in d: d[ai] = [] d[ai].append(i) L = sorted(d) answer = 0 while len(L) > 0: ai = L.pop() for i in d[ai]: S[i] = 0 for i in d[ai]: answer+=min(sum(S[:i]), sum(S[i+1:])) if sum(S[:i]) < sum(S[i+1:]): A[i] = -1*ai else: A[i] = ai return answer n = int(input()) A = [int(x) for x in input().split()] print(process(A))
Codeforces Round 204 (Div. 1)
CF
2,013
2
256
Jeff and Permutation
Jeff's friends know full well that the boy likes to get sequences and arrays for his birthday. Thus, Jeff got sequence p1, p2, ..., pn for his birthday. Jeff hates inversions in sequences. An inversion in sequence a1, a2, ..., an is a pair of indexes i, j (1 ≤ i < j ≤ n), such that an inequality ai > aj holds. Jeff can multiply some numbers of the sequence p by -1. At that, he wants the number of inversions in the sequence to be minimum. Help Jeff and find the minimum number of inversions he manages to get.
The first line contains integer n (1 ≤ n ≤ 2000). The next line contains n integers — sequence p1, p2, ..., pn (|pi| ≤ 105). The numbers are separated by spaces.
In a single line print the answer to the problem — the minimum number of inversions Jeff can get.
null
null
[{"input": "2\n2 1", "output": "0"}, {"input": "9\n-2 0 -1 0 -1 2 1 0 -1", "output": "6"}]
2,200
["greedy"]
36
[{"input": "2\r\n2 1\r\n", "output": "0\r\n"}, {"input": "9\r\n-2 0 -1 0 -1 2 1 0 -1\r\n", "output": "6\r\n"}, {"input": "9\r\n0 0 1 1 0 0 1 0 1\r\n", "output": "5\r\n"}, {"input": "8\r\n0 1 2 -1 -2 1 -2 2\r\n", "output": "3\r\n"}, {"input": "24\r\n-1 -1 2 2 0 -2 2 -1 0 0 2 -2 3 0 2 -3 0 -3 -1 1 0 0 -1 -2\r\n", "output": "55\r\n"}, {"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "31\r\n-2 2 -2 -1 0 0 1 2 1 1 -1 -2 1 -1 -2 2 0 1 -1 -2 -1 -2 -1 2 2 2 2 1 1 0 1\r\n", "output": "74\r\n"}, {"input": "9\r\n1 -1 -1 0 -1 0 1 1 1\r\n", "output": "1\r\n"}, {"input": "5\r\n1 0 1 -2 1\r\n", "output": "1\r\n"}, {"input": "31\r\n-5 -5 5 3 -1 3 1 -3 -3 -1 -5 -3 -2 -4 -3 3 5 -2 1 0 -1 1 -3 1 -1 1 3 3 2 1 0\r\n", "output": "70\r\n"}, {"input": "53\r\n-3 2 -3 -5 -2 7 0 -2 1 6 -1 2 5 -3 3 -6 -2 -5 -3 -6 4 -4 -2 6 1 -7 -6 -4 0 2 -5 -1 -2 -6 2 2 7 -2 -3 1 0 -4 3 4 -2 7 -3 7 7 3 -5 -5 3\r\n", "output": "289\r\n"}, {"input": "24\r\n-3 -4 3 -3 3 2 -1 -3 -4 0 -4 0 2 3 3 -1 2 1 2 -2 3 -2 1 0\r\n", "output": "46\r\n"}, {"input": "50\r\n-6 1 -3 7 -5 -5 4 0 3 -5 1 2 -1 0 7 0 6 3 -5 4 4 3 -7 -1 4 4 -5 3 7 1 4 2 6 -4 0 3 -3 -2 -3 1 -5 3 -4 2 -2 7 -1 3 -7 4\r\n", "output": "260\r\n"}, {"input": "17\r\n-56007 -97423 -66458 -17041 49374 60662 42188 56222 28689 -4117 -1712 11034 17161 43908 -65064 -76642 -73934\r\n", "output": "13\r\n"}, {"input": "12\r\n0 1 0 1 1 -1 1 -1 0 1 0 -1\r\n", "output": "12\r\n"}]
false
stdio
null
true
68/B
68
B
Python 3
TESTS
2
154
5,529,600
33962531
n, k = map(int, input().split()) a = list(map(int, input().split())) result = sum(a)/n eps = 0.0000000001 while True: transferred = (100-k)/100 * sum(map(lambda x: x-result if x-result>0 else 0, a)) missing = sum(map(lambda x: result-x if result-x>0 else 0, a)) if abs(transferred - missing) < eps: print(result) break result = result + (transferred - missing)/2
42
154
3,072,000
194515194
import bisect I = lambda: list(map(int, input().split())) n, k = I() k /= 100 a = I() l = 0 r = 1000 sumie = sum(a) while r - l > 1e-7: mid = (l+r) / 2 sum_transfer = 0 for x in a: if x > mid: sum_transfer += x-mid if n*mid < sumie - sum_transfer*k: l = mid else: r = mid print(l)
Codeforces Beta Round 62
CF
2,011
2
256
Energy exchange
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by $$x - \frac{xk}{100}$$ units. Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers. Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy. The absolute or relative error in the answer should not exceed 10 - 6.
null
null
[{"input": "3 50\n4 2 1", "output": "2.000000000"}, {"input": "2 90\n1 11", "output": "1.909090909"}]
1,600
["binary search"]
42
[{"input": "3 50\r\n4 2 1\r\n", "output": "2.000000000\r\n"}, {"input": "2 90\r\n1 11\r\n", "output": "1.909090909\r\n"}, {"input": "5 26\r\n42 65 23 43 64\r\n", "output": "45.415178571\r\n"}, {"input": "5 45\r\n964 515 454 623 594\r\n", "output": "594.109756098\r\n"}, {"input": "1 20\r\n784\r\n", "output": "784.000000000\r\n"}, {"input": "10 20\r\n812 896 36 596 709 641 679 778 738 302\r\n", "output": "597.255813953\r\n"}, {"input": "10 83\r\n689 759 779 927 15 231 976 943 604 917\r\n", "output": "406.839285714\r\n"}, {"input": "11 1\r\n235 280 196 670 495 379 391 280 847 875 506\r\n", "output": "467.586301370\r\n"}, {"input": "12 71\r\n472 111 924 103 975 527 807 618 400 523 607 424\r\n", "output": "413.249554367\r\n"}, {"input": "13 89\r\n19 944 341 846 764 676 222 957 953 481 708 920 950\r\n", "output": "361.924390244\r\n"}, {"input": "14 6\r\n256 465 759 589 242 824 638 985 506 128 809 105 301 827\r\n", "output": "523.427098675\r\n"}]
false
stdio
import sys def main(): input_path = sys.argv[1] output_path = sys.argv[2] submission_path = sys.argv[3] # Read reference output with open(output_path, 'r') as f: ref_line = f.readline().strip() try: E_ref = float(ref_line) except: print(0) return # Read submission output with open(submission_path, 'r') as f: sub_line = f.readline().strip() try: E_sub = float(sub_line) except: print(0) return abs_err = abs(E_sub - E_ref) if abs_err <= 1e-6: print(1) return if E_ref == 0: print(0) return rel_err = abs_err / E_ref if rel_err <= 1e-6: print(1) return print(0) if __name__ == "__main__": main()
true
509/C
509
C
Python 3
TESTS
3
93
307,200
89816479
import math n = int(input()) last=0 for i in range(n): a = int(input()) dig = math.ceil(a/9) s='' if i==0: while a>9: s += '9' a-=9 s += str(a) last = int(s) print(last) else: while a>9: s += '9' a-=9 s += str(a) val = int(s) while val<=last: val*=10 last = val print(last)
21
93
5,427,200
153762868
import sys def get_max(su, le): div, mod = divmod(su, 9) ret = ['9'] * div if mod: ret = [str(mod)] + ret if le - len(ret) > 0: x = str(int(ret.pop(0)) - 1) ret = ['1', x] + ret if le - len(ret) > 0: ret = [ret[0]] + ['0'] * (le - len(ret)) + ret[1:] return ret def get_max2(su, le): div, mod = divmod(su, 9) ret = ['9'] * div if mod: ret = [str(mod)] + ret return ['0'] * (le - len(ret)) + ret input = lambda: sys.stdin.buffer.readline().decode().strip() lst = ['0'] for _ in range(int(input())): su = int(input()) le = su // 9 + bool(su % 9) if le > len(lst): lst = get_max(su, le) else: rem, ix = su, -1 for i in range(len(lst)): if int(lst[i]) > rem: break if lst[i] != '9' and rem > int(lst[i]) and 9 * (len(lst) - i) >= rem: ix = i rem -= int(lst[i]) if ix == -1: lst = get_max(su, len(lst) + 1) else: rem = su - sum([int(lst[i]) for i in range(ix + 1)]) cur = 1 for i in range(int(lst[ix]) + 1, 10): rem -= 1 if (len(lst) - ix - 1) * 9 >= rem: break cur += 1 lst = lst[:ix] + [str(int(lst[ix]) + cur)] + get_max2(rem, len(lst) - ix - 1) print(''.join(lst))
Codeforces Round 289 (Div. 2, ACM ICPC Rules)
ICPC
2,015
2
256
Sums of Digits
Vasya had a strictly increasing sequence of positive integers a1, ..., an. Vasya used it to build a new sequence b1, ..., bn, where bi is the sum of digits of ai's decimal representation. Then sequence ai got lost and all that remained is sequence bi. Vasya wonders what the numbers ai could be like. Of all the possible options he likes the one sequence with the minimum possible last number an. Help Vasya restore the initial sequence. It is guaranteed that such a sequence always exists.
The first line contains a single integer number n (1 ≤ n ≤ 300). Next n lines contain integer numbers b1, ..., bn  — the required sums of digits. All bi belong to the range 1 ≤ bi ≤ 300.
Print n integer numbers, one per line — the correct option for numbers ai, in order of following in sequence. The sequence should be strictly increasing. The sum of digits of the i-th number should be equal to bi. If there are multiple sequences with least possible number an, print any of them. Print the numbers without leading zeroes.
null
null
[{"input": "3\n1\n2\n3", "output": "1\n2\n3"}, {"input": "3\n3\n2\n1", "output": "3\n11\n100"}]
2,000
["dp", "greedy", "implementation"]
21
[{"input": "3\r\n1\r\n2\r\n3\r\n", "output": "1\r\n2\r\n3\r\n"}, {"input": "3\r\n3\r\n2\r\n1\r\n", "output": "3\r\n11\r\n100\r\n"}, {"input": "10\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n1\r\n", "output": "1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n"}, {"input": "10\r\n8\r\n8\r\n5\r\n1\r\n2\r\n7\r\n3\r\n8\r\n9\r\n4\r\n", "output": "8\r\n17\r\n23\r\n100\r\n101\r\n106\r\n111\r\n116\r\n117\r\n121\r\n"}, {"input": "10\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n1\r\n", "output": "1\r\n10\r\n100\r\n1000\r\n10000\r\n100000\r\n1000000\r\n10000000\r\n100000000\r\n1000000000\r\n"}, {"input": "100\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n1\r\n", "output": "1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n38\r\n39\r\n40\r\n41\r\n42\r\n43\r\n44\r\n45\r\n46\r\n47\r\n48\r\n49\r\n50\r\n51\r\n52\r\n53\r\n54\r\n55\r\n56\r\n57\r\n58\r\n59\r\n60\r\n61\r\n62\r\n63\r\n64\r\n65\r\n66\r\n67\r\n68\r\n69\r\n70\r\n71\r\n72\r\n73\r\n74\r\n75\r\n76\r\n77\r\n78\r\n79\r\n80\r\n81\r\n82\r\n83\r\n84\r\n85\r\n86\r\n87\r\n88\r\n89\r\n90\r\n91\r\n92\r\n93\r\n94\r\n95\r\n96\r\n97\r\n98\r\n99\r\n100\r\n"}, {"input": "100\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n18\r\n", "output": "99\n189\n198\n279\n288\n297\n369\n378\n387\n396\n459\n468\n477\n486\n495\n549\n558\n567\n576\n585\n594\n639\n648\n657\n666\n675\n684\n693\n729\n738\n747\n756\n765\n774\n783\n792\n819\n828\n837\n846\n855\n864\n873\n882\n891\n909\n918\n927\n936\n945\n954\n963\n972\n981\n990\n1089\n1098\n1179\n1188\n1197\n1269\n1278\n1287\n1296\n1359\n1368\n1377\n1386\n1395\n1449\n1458\n1467\n1476\n1485\n1494\n1539\n1548\n1557\n1566\n1575\n1584\n1593\n1629\n1638\n1647\n1656\n1665\n1674\n1683\n1692\n1719\n1728\n1737\n1746\n1755\n1764\n1773\n1782\n1791\n1809\n"}, {"input": "1\r\n139\r\n", "output": "4999999999999999\r\n"}, {"input": "1\r\n6\r\n", "output": "6\r\n"}]
false
stdio
null
true
1006/D
1006
D
PyPy 3
TESTS
2
124
3,993,600
116210236
from sys import stdin from collections import Counter input = stdin.readline n = int(input()) a = input().rstrip() b = input().rstrip() ans = 0 for i in range(n // 2): x = [a[i], a[n-i-1], b[i], b[n-i-1]] c = Counter(x) y = c.values() if len(y) == 4: ans += 2 elif len(y) == 3: if x[0] == x[1] or x[2] == x[3]: ans += 2 else: ans += 1 elif max(y) == 3: ans += 1 if n % 2 > 0: ans += a[n // 2] != b[n // 2] print(ans)
23
93
2,457,600
197431902
# https://codeforces.com/contest/1006 import sys input = lambda: sys.stdin.readline().rstrip() # faster! def solve_case(): n = int(input()) a = input() b = input() ans = 0 for i in range(n // 2): l = len({a[i], a[n - 1 - i], b[i], b[n - 1 - i]}) if l == 1: ans += 0 elif l == 2: if a[i] == a[n - 1 - i] and b[i] != b[n - 1 - i]: ans += 1 elif a[i] != a[n - 1 - i] and b[i] == b[n - 1 - i]: ans += 1 else: ans += 0 elif l == 3: if a[i] == a[n - 1 - i]: ans += 2 else: ans += 1 elif l == 4: ans += 2 if n & 1 and a[n // 2] != b[n // 2]: ans += 1 print(ans) solve_case()
Codeforces Round 498 (Div. 3)
ICPC
2,018
2
256
Two Strings Swaps
You are given two strings $$$a$$$ and $$$b$$$ consisting of lowercase English letters, both of length $$$n$$$. The characters of both strings have indices from $$$1$$$ to $$$n$$$, inclusive. You are allowed to do the following changes: - Choose any index $$$i$$$ ($$$1 \le i \le n$$$) and swap characters $$$a_i$$$ and $$$b_i$$$; - Choose any index $$$i$$$ ($$$1 \le i \le n$$$) and swap characters $$$a_i$$$ and $$$a_{n - i + 1}$$$; - Choose any index $$$i$$$ ($$$1 \le i \le n$$$) and swap characters $$$b_i$$$ and $$$b_{n - i + 1}$$$. Note that if $$$n$$$ is odd, you are formally allowed to swap $$$a_{\lceil\frac{n}{2}\rceil}$$$ with $$$a_{\lceil\frac{n}{2}\rceil}$$$ (and the same with the string $$$b$$$) but this move is useless. Also you can swap two equal characters but this operation is useless as well. You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps. In one preprocess move you can replace a character in $$$a$$$ with another character. In other words, in a single preprocess move you can choose any index $$$i$$$ ($$$1 \le i \le n$$$), any character $$$c$$$ and set $$$a_i := c$$$. Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings $$$a$$$ and $$$b$$$ equal by applying some number of changes described in the list above. Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string $$$b$$$ or make any preprocess moves after the first change is made.
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of strings $$$a$$$ and $$$b$$$. The second line contains the string $$$a$$$ consisting of exactly $$$n$$$ lowercase English letters. The third line contains the string $$$b$$$ consisting of exactly $$$n$$$ lowercase English letters.
Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string $$$a$$$ equal to string $$$b$$$ with a sequence of changes from the list above.
null
In the first example preprocess moves are as follows: $$$a_1 := $$$'b', $$$a_3 := $$$'c', $$$a_4 := $$$'a' and $$$a_5:=$$$'b'. Afterwards, $$$a = $$$"bbcabba". Then we can obtain equal strings by the following sequence of changes: $$$swap(a_2, b_2)$$$ and $$$swap(a_2, a_6)$$$. There is no way to use fewer than $$$4$$$ preprocess moves before a sequence of changes to make string equal, so the answer in this example is $$$4$$$. In the second example no preprocess moves are required. We can use the following sequence of changes to make $$$a$$$ and $$$b$$$ equal: $$$swap(b_1, b_5)$$$, $$$swap(a_2, a_4)$$$.
[{"input": "7\nabacaba\nbacabaa", "output": "4"}, {"input": "5\nzcabd\ndbacz", "output": "0"}]
1,700
["implementation"]
23
[{"input": "7\r\nabacaba\r\nbacabaa\r\n", "output": "4\r\n"}, {"input": "5\r\nzcabd\r\ndbacz\r\n", "output": "0\r\n"}, {"input": "1\r\na\r\nb\r\n", "output": "1\r\n"}, {"input": "5\r\nahmad\r\nyogaa\r\n", "output": "3\r\n"}]
false
stdio
null
true
436/C
436
C
Python 3
TESTS
2
108
307,200
48892505
import heapq import math import sys def find_parent(a): if parents[a] != a: parents[a] = find_parent(parents[a]) return parents[a] def make_union(a, b): pa = find_parent(a) pb = find_parent(b) if pa == pb: return False ra = ranks[pa] rb = ranks[pb] if ra <= rb: parents[pa] = pb if ra == rb: ranks[pb] += 1 else: parents[pb] = pa return True N, M, K, W = map(int, sys.stdin.readline().split()) parents = [j for j in range(K+1)] ranks = [1 for j in range(K+1)] def difference(A, B): s = 0 for i in range(len(A)): for j in range(len(A[i])): s += A[i][j] != B[i][j] return s*W levels = [[['-' for j in range(M)] for j in range(N)]] for lvl in range(K): levl = [sys.stdin.readline().strip() for j in range(N)] levels.append(levl) edges = [] for j in range(1,K+1): for i in range(j+1, K+1): edges.append( (difference(levels[j], levels[i]), j, i) ) for j in range(1,K+1): edges.append((N*M, 0, j)) edges.sort() ww = 0 res = [] for w, start, stop in edges: if make_union(start, stop): ww += w res.append([stop, start]) print(ww) res.sort() for u,j in res: print(u, j)
30
1,964
62,873,600
232798881
import heapq total_bytes = 0 messages_sent = [] def send_message(messages, weight, start_ind): global total_bytes global messages_sent num_messages = len(messages) num_rows = len(messages[1]) num_cols = len(messages[1][0]) sent = [False] * num_messages # Send the first message total_bytes += num_rows * num_cols mst = ["1 0"] sent[start_ind] = True queue = [] start_message = messages[start_ind] max_bytes = num_rows * num_cols for curr_ind in range(1, num_messages): curr_message = messages[curr_ind] diff = 0 if curr_ind != start_ind: for i in range(num_rows): for j in range(num_cols): if curr_message[i][j] != start_message[i][j]: diff += 1 if max_bytes > diff * weight: heapq.heappush(queue, (diff * weight, (start_ind, curr_ind))) else: heapq.heappush(queue, (max_bytes, (0, curr_ind))) while queue: curr_bytes, (prev_ind, curr_ind) = heapq.heappop(queue) if sent[curr_ind]: continue mst.append(f"{curr_ind} {prev_ind}") sent[curr_ind] = True total_bytes += curr_bytes curr_message = messages[curr_ind] for next_ind in range(1, num_messages): if next_ind != curr_ind and not sent[next_ind]: next_message = messages[next_ind] diff = 0 for i in range(num_rows): for j in range(num_cols): if curr_message[i][j] != next_message[i][j]: diff += 1 if max_bytes > diff * weight: heapq.heappush(queue, (diff * weight, (curr_ind, next_ind))) else: heapq.heappush(queue, (max_bytes, (0, next_ind))) return mst def main(): global total_bytes global messages_sent num_rows, num_cols, num_messages, weight = map(int, input().split()) messages = [[]] for _ in range(num_messages): message = [input() for _ in range(num_rows)] messages.append(message) result = send_message(messages, weight, 1) print(total_bytes) for line in result: print(line) if __name__ == "__main__": main()
Zepto Code Rush 2014
CF
2,014
2
256
Dungeons and Candies
During the loading of the game "Dungeons and Candies" you are required to get descriptions of k levels from the server. Each description is a map of an n × m checkered rectangular field. Some cells of the field contain candies (each cell has at most one candy). An empty cell is denoted as "." on the map, but if a cell has a candy, it is denoted as a letter of the English alphabet. A level may contain identical candies, in this case the letters in the corresponding cells of the map will be the same. When you transmit information via a network, you want to minimize traffic — the total size of the transferred data. The levels can be transmitted in any order. There are two ways to transmit the current level A: 1. You can transmit the whole level A. Then you need to transmit n·m bytes via the network. 2. You can transmit the difference between level A and some previously transmitted level B (if it exists); this operation requires to transmit dA, B·w bytes, where dA, B is the number of cells of the field that are different for A and B, and w is a constant. Note, that you should compare only the corresponding cells of levels A and B to calculate dA, B. You cannot transform the maps of levels, i.e. rotate or shift them relatively to each other. Your task is to find a way to transfer all the k levels and minimize the traffic.
The first line contains four integers n, m, k, w (1 ≤ n, m ≤ 10; 1 ≤ k, w ≤ 1000). Then follows the description of k levels. Each level is described by n lines, each line contains m characters. Each character is either a letter of the English alphabet or a dot ("."). Please note that the case of the letters matters.
In the first line print the required minimum number of transferred bytes. Then print k pairs of integers x1, y1, x2, y2, ..., xk, yk, describing the way to transfer levels. Pair xi, yi means that level xi needs to be transferred by way yi. If yi equals 0, that means that the level must be transferred using the first way, otherwise yi must be equal to the number of a previously transferred level. It means that you will transfer the difference between levels yi and xi to transfer level xi. Print the pairs in the order of transferring levels. The levels are numbered 1 through k in the order they follow in the input. If there are multiple optimal solutions, you can print any of them.
null
null
[{"input": "2 3 3 2\nA.A\n...\nA.a\n..C\nX.Y\n...", "output": "14\n1 0\n2 1\n3 1"}, {"input": "1 1 4 1\nA\n.\nB\n.", "output": "3\n1 0\n2 0\n4 2\n3 0"}, {"input": "1 3 5 2\nABA\nBBB\nBBA\nBAB\nABB", "output": "11\n1 0\n3 1\n2 3\n4 2\n5 1"}]
1,800
["dsu", "graphs", "greedy", "trees"]
30
[{"input": "2 3 3 2\r\nA.A\r\n...\r\nA.a\r\n..C\r\nX.Y\r\n...\r\n", "output": "14\r\n1 0\r\n2 1\r\n3 1\r\n"}, {"input": "1 1 4 1\r\nA\r\n.\r\nB\r\n.\r\n", "output": "3\r\n1 0\r\n2 0\r\n4 2\r\n3 0\r\n"}, {"input": "1 3 5 2\r\nABA\r\nBBB\r\nBBA\r\nBAB\r\nABB\r\n", "output": "11\r\n1 0\r\n3 1\r\n2 3\r\n4 2\r\n5 1\r\n"}, {"input": "2 2 5 1\r\n..\r\nBA\r\n.A\r\nB.\r\n..\r\nA.\r\nAB\r\n.B\r\n..\r\n..\r\n", "output": "12\r\n1 0\r\n2 1\r\n3 1\r\n5 3\r\n4 5\r\n"}, {"input": "3 3 10 2\r\nBA.\r\n..A\r\n.BB\r\nB..\r\n..B\r\n.AA\r\nB..\r\nAB.\r\n..A\r\nBAB\r\n.A.\r\n.B.\r\n..B\r\nA..\r\n...\r\n...\r\n.B.\r\nBA.\r\n..B\r\n.AB\r\n.B.\r\nB.A\r\n.A.\r\n.BA\r\n..B\r\n...\r\n.A.\r\n.AA\r\n..A\r\n.B.\r\n", "output": "67\r\n1 0\r\n10 1\r\n2 1\r\n3 2\r\n4 1\r\n7 4\r\n9 7\r\n5 9\r\n6 9\r\n8 4\r\n"}, {"input": "3 1 5 1\r\nB\r\nA\r\nB\r\nA\r\nA\r\nB\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\n", "output": "5\r\n1 0\r\n2 1\r\n3 2\r\n4 3\r\n5 3\r\n"}, {"input": "3 2 10 1\r\nAB\r\nBA\r\nAB\r\nAA\r\nAA\r\nBA\r\nAA\r\nAA\r\nAB\r\nAB\r\nAB\r\nBA\r\nBA\r\nAB\r\nAA\r\nBB\r\nAB\r\nBA\r\nBB\r\nBB\r\nBA\r\nAA\r\nAA\r\nAB\r\nAB\r\nAB\r\nBA\r\nBB\r\nAB\r\nAA\r\n", "output": "16\r\n1 0\r\n3 1\r\n8 3\r\n2 3\r\n4 2\r\n9 4\r\n6 4\r\n7 6\r\n10 6\r\n5 10\r\n"}, {"input": "2 3 10 2\r\nABB\r\nABA\r\nAAB\r\nBAB\r\nAAA\r\nBBA\r\nBBB\r\nBAA\r\nBBB\r\nABB\r\nABA\r\nBBA\r\nBBB\r\nAAB\r\nABA\r\nABB\r\nBBA\r\nBAB\r\nBBB\r\nBBB\r\n", "output": "38\r\n1 0\r\n5 1\r\n7 5\r\n4 7\r\n9 4\r\n10 5\r\n6 1\r\n3 6\r\n8 1\r\n2 0\r\n"}, {"input": "1 1 1 1\r\n.\r\n", "output": "1\r\n1 0\r\n"}]
false
stdio
import sys def read_levels(input_path): with open(input_path) as f: lines = [line.strip() for line in f.readlines() if line.strip()] first_line = lines[0].split() n, m, k, w = map(int, first_line) levels = [None] # 1-based indexing idx = 1 for _ in range(k): level = [] for _ in range(n): if idx >= len(lines): return None, None, None, None, None level.append(lines[idx].strip()) idx += 1 levels.append(level) return n, m, k, w, levels def main(input_path, output_path, submission_path): n, m, k, w, levels = read_levels(input_path) if levels is None: return 0 # Read reference output's first line with open(output_path) as f_ref: ref_lines = f_ref.read().splitlines() if not ref_lines: return 0 ref_total = int(ref_lines[0]) # Read submission output with open(submission_path) as f_sub: sub_lines = f_sub.read().splitlines() if not sub_lines: return 0 try: sub_total = int(sub_lines[0]) except: return 0 # Check sub_total matches reference if sub_total != ref_total: return 0 # Parse pairs tokens = [] for line in sub_lines[1:]: tokens.extend(line.strip().split()) if len(tokens) != 2 * k: return 0 pairs = [] try: for i in range(0, len(tokens), 2): xi = int(tokens[i]) yi = int(tokens[i+1]) pairs.append((xi, yi)) except: return 0 # Check all xi are unique and 1..k seen_xi = set() for xi, _ in pairs: if xi < 1 or xi > k or xi in seen_xi: return 0 seen_xi.add(xi) if len(seen_xi) != k: return 0 # Check pairs order and calculate total transmitted = set() total = 0 for xi, yi in pairs: if yi != 0 and yi not in transmitted: return 0 if yi == 0: cost = n * m else: # Compute d between xi and yi d = 0 for row in range(n): for col in range(m): if levels[xi][row][col] != levels[yi][row][col]: d += 1 cost = d * w total += cost transmitted.add(xi) if total != sub_total: return 0 return 1 if __name__ == "__main__": input_path, output_path, submission_path = sys.argv[1:] score = main(input_path, output_path, submission_path) print(score)
true