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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
66/C
|
66
|
C
|
Python 3
|
TESTS
| 6
| 216
| 0
|
66515077
|
import sys
filePaths = sys.stdin.readlines()
foldercount,filecount = 0,0
directory = {}
for line in filePaths:
path = list(map(str,line.split('\\')))
newFolder = False
for i in range(1,len(path)-1):
if path[i] not in directory.keys():
if i == 1:
foldercount -= 1
directory.update({path[i]:0})
foldercount+=1
newFolder = True
elif newFolder:
foldercount+=1
filecount += 1 if newFolder else 0
print(foldercount,filecount)
| 100
| 340
| 15,564,800
|
180697838
|
import sys
sys.setrecursionlimit(int(1e4))
class TrieVertex:
def __init__(self, folder):
self.child = dict()
self.files = set()
self.folder = folder
def add(v, path, k):
if k==len(path)-1:
v.files.add(path[k])
return
if path[k] not in v.child:
v.child[path[k]] = TrieVertex(path[k])
add(v.child[path[k]], path, k+1)
def dfs(v, cntSubs, cntFiles, cur):
s = str(cur) + '-' + v.folder
cntSubs[s] = 0
cntFiles[s] = len(v.files)
for subFolder in v.child:
subs, files = dfs(v.child[subFolder], cntSubs, cntFiles, cur+1)
cntSubs[s] += subs
cntFiles[s] += files
return cntSubs[s]+1, cntFiles[s]
roots = dict()
for line in sys.stdin:
path = line.split('\\')
if path[0] in roots:
add(roots[path[0]], path, 1)
else:
v = TrieVertex(path[0])
add(v, path, 1)
roots[path[0]] = v
ans = [0 for _ in range(2)]
for disk in roots:
cntSubs, cntFiles = dict(), dict()
subs, files = dfs(roots[disk], cntSubs, cntFiles, 0)
for folder in cntSubs:
if folder[len(folder)-1] == ':': continue
ans[0] = max(ans[0], cntSubs[folder])
for folder in cntFiles:
if folder[len(folder)-1] == ':': continue
ans[1] = max(ans[1], cntFiles[folder])
print(ans[0], ans[1])
|
Codeforces Beta Round 61 (Div. 2)
|
CF
| 2,011
| 3
| 256
|
Petya and File System
|
Recently, on a programming lesson little Petya showed how quickly he can create files and folders on the computer. But he got soon fed up with this activity, and he decided to do a much more useful thing. He decided to calculate what folder contains most subfolders (including nested folders, nested folders of nested folders, and so on) and what folder contains most files (including the files in the subfolders).
More formally, the subfolders of the folder are all its directly nested folders and the subfolders of these nested folders. The given folder is not considered the subfolder of itself. A file is regarded as lying in a folder, if and only if it either lies directly in this folder, or lies in some subfolder of the folder.
For a better understanding of how to count subfolders and files for calculating the answer, see notes and answers to the samples.
You are given a few files that Petya has managed to create. The path to each file looks as follows:
diskName:\folder1\folder2\...\ foldern\fileName
- diskName is single capital letter from the set {C,D,E,F,G}.
- folder1, ..., foldern are folder names. Each folder name is nonempty sequence of lowercase Latin letters and digits from 0 to 9. (n ≥ 1)
- fileName is a file name in the form of name.extension, where the name and the extension are nonempty sequences of lowercase Latin letters and digits from 0 to 9.
It is also known that there is no file whose path looks like diskName:\fileName. That is, each file is stored in some folder, but there are no files directly in the root. Also let us assume that the disk root is not a folder.
Help Petya to find the largest number of subfolders, which can be in some folder, and the largest number of files that can be in some folder, counting all its subfolders.
|
Each line of input data contains the description of one file path. The length of each line does not exceed 100, and overall there are no more than 100 lines. It is guaranteed, that all the paths are correct and meet the above rules. It is also guaranteed, that there are no two completely equal lines. That is, each file is described exactly once.
There is at least one line in the input data.
|
Print two space-separated numbers. The first one is the maximal number of possible subfolders in a folder (including nested folders, nested folders of nested folders, and so on). The second one is the maximal number of files in a folder (including nested files in subfolders). Note that the disks are not regarded as folders.
| null |
In the first sample we have one folder on the "C" disk. It has no subfolders, which is why the first number in the answer is 0. But this folder contains one file, so the second number of the answer is 1.
In the second sample we have several different folders. Consider the "folder1" folder on the "C" disk. This folder directly contains one folder, "folder2". The "folder2" folder contains two more folders — "folder3" and "folder4". Thus, the "folder1" folder on the "C" drive has exactly 3 subfolders. Also this folder contains two files, even though they do not lie directly in the folder, but they are located in subfolders of "folder1".
In the third example we see that the names of some folders and some subfolders are identical. Consider the "file" folder, which lies directly on the "C" disk. That folder contains another "file" folder, which in turn contains another "file" folder, which contains two more folders, "file" and "file2". Thus, the "file" folder, which lies directly on the "C" disk, contains 4 subfolders.
|
[{"input": "C:\n\\\nfolder1\n\\\nfile1.txt", "output": "0 1"}, {"input": "C:\n\\\nfolder1\n\\\nfolder2\n\\\nfolder3\n\\\nfile1.txt\nC:\n\\\nfolder1\n\\\nfolder2\n\\\nfolder4\n\\\nfile1.txt\nD:\n\\\nfolder1\n\\\nfile1.txt", "output": "3 2"}, {"input": "C:\n\\\nfile\n\\\nfile\n\\\nfile\n\\\nfile\n\\\nfile.txt\nC:\n\\\nfile\n\\\nfile\n\\\nfile\n\\\nfile2\n\\\nfile.txt", "output": "4 2"}]
| 1,800
|
["data structures", "implementation"]
| 100
|
[{"input": "C:\\folder1\\file1.txt\r\n", "output": "0 1\r\n"}, {"input": "C:\\folder1\\folder2\\folder3\\file1.txt\r\nC:\\folder1\\folder2\\folder4\\file1.txt\r\nD:\\folder1\\file1.txt\r\n", "output": "3 2\r\n"}, {"input": "C:\\file\\file\\file\\file\\file.txt\r\nC:\\file\\file\\file\\file2\\file.txt\r\n", "output": "4 2\r\n"}, {"input": "C:\\file\\file.txt\r\nD:\\file\\file.txt\r\nE:\\file\\file.txt\r\nF:\\file\\file.txt\r\nG:\\file\\file.txt\r\n", "output": "0 1\r\n"}, {"input": "C:\\a\\b\\c\\d\\d.txt\r\nC:\\a\\b\\c\\e\\f.txt\r\n", "output": "4 2\r\n"}, {"input": "C:\\z\\z.txt\r\nD:\\1\\1.txt\r\nD:\\1\\2.txt\r\n", "output": "0 2\r\n"}, {"input": "D:\\0000\\1.txt\r\nE:\\00000\\1.txt\r\n", "output": "0 1\r\n"}, {"input": "C:\\a\\b\\c\\d.txt\r\nC:\\a\\e\\c\\d.txt\r\n", "output": "4 2\r\n"}, {"input": "C:\\test1\\test2\\test3\\test.txt\r\nC:\\test1\\test3\\test3\\test4\\test.txt\r\nC:\\test1\\test2\\test3\\test2.txt\r\nD:\\test1\\test2\\test.txt\r\nD:\\test1\\test3\\test4.txt\r\n", "output": "5 3\r\n"}, {"input": "C:\\test1\\test2\\test.txt\r\nC:\\test1\\test2\\test2.txt\r\n", "output": "1 2\r\n"}]
| false
|
stdio
| null | true
|
358/C
|
358
|
C
|
Python 3
|
TESTS
| 0
| 31
| 0
|
208016548
|
n = int(input())
stack, queue, deck = 0, 0, 0
for i in range(n):
inna_command = int(input())
if inna_command > 0:
if stack == 0:
print('pushStack')
stack = 1
elif queue == 0:
print('pushQueue')
queue = 1
else:
print('pushFront')
deck = 1
else:
words = []
if stack > 0:
words.append('popStack')
if queue > 0:
words.append('popQueue')
if deck > 0:
words.append('popBack')
stack, queue, deck = 0, 0, 0
print(len(words), end=' ')
print(' '.join(words))
| 26
| 187
| 4,300,800
|
231249531
|
i, n = 0, int(input())
s = ['pushQueue'] * n
a, b, c = ' popQueue', ' popStack', ' popBack'
p = ['0', '1' + a, '2' + a + b, '3' + a + b + c]
t = []
for j in range(n):
x = int(input())
if x:
t.append((x, j))
continue
t = sorted(k for x, k in sorted(t)[-3:])
k = len(t)
if k > 0: s[i: t[0]] = ['pushStack'] * (t[0] - i)
if k > 1: s[t[1]] = 'pushStack'
if k > 2: s[t[2]] = 'pushBack'
i, t, s[j] = j + 1, [], p[k]
print('\n'.join(s))
|
Codeforces Round 208 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Dima and Containers
|
Dima has a birthday soon! It's a big day! Saryozha's present to Dima is that Seryozha won't be in the room and won't disturb Dima and Inna as they celebrate the birthday. Inna's present to Dima is a stack, a queue and a deck.
Inna wants her present to show Dima how great a programmer he is. For that, she is going to give Dima commands one by one. There are two types of commands:
1. Add a given number into one of containers. For the queue and the stack, you can add elements only to the end. For the deck, you can add elements to the beginning and to the end.
2. Extract a number from each of at most three distinct containers. Tell all extracted numbers to Inna and then empty all containers. In the queue container you can extract numbers only from the beginning. In the stack container you can extract numbers only from the end. In the deck number you can extract numbers from the beginning and from the end. You cannot extract numbers from empty containers.
Every time Dima makes a command of the second type, Inna kisses Dima some (possibly zero) number of times. Dima knows Inna perfectly well, he is sure that this number equals the sum of numbers he extracts from containers during this operation.
As we've said before, Dima knows Inna perfectly well and he knows which commands Inna will give to Dima and the order of the commands. Help Dima find the strategy that lets him give as more kisses as possible for his birthday!
|
The first line contains integer n (1 ≤ n ≤ 105) — the number of Inna's commands. Then n lines follow, describing Inna's commands. Each line consists an integer:
1. Integer a (1 ≤ a ≤ 105) means that Inna gives Dima a command to add number a into one of containers.
2. Integer 0 shows that Inna asks Dima to make at most three extractions from different containers.
|
Each command of the input must correspond to one line of the output — Dima's action.
For the command of the first type (adding) print one word that corresponds to Dima's choice:
- pushStack — add to the end of the stack;
- pushQueue — add to the end of the queue;
- pushFront — add to the beginning of the deck;
- pushBack — add to the end of the deck.
For a command of the second type first print an integer k (0 ≤ k ≤ 3), that shows the number of extract operations, then print k words separated by space. The words can be:
- popStack — extract from the end of the stack;
- popQueue — extract from the beginning of the line;
- popFront — extract from the beginning from the deck;
- popBack — extract from the end of the deck.
The printed operations mustn't extract numbers from empty containers. Also, they must extract numbers from distinct containers.
The printed sequence of actions must lead to the maximum number of kisses. If there are multiple sequences of actions leading to the maximum number of kisses, you are allowed to print any of them.
| null | null |
[{"input": "10\n0\n1\n0\n1\n2\n0\n1\n2\n3\n0", "output": "0\npushStack\n1 popStack\npushStack\npushQueue\n2 popStack popQueue\npushStack\npushQueue\npushFront\n3 popStack popQueue popFront"}, {"input": "4\n1\n2\n3\n0", "output": "pushStack\npushQueue\npushFront\n3 popStack popQueue popFront"}]
| 2,000
|
["constructive algorithms", "greedy", "implementation"]
| 26
|
[{"input": "10\r\n0\r\n1\r\n0\r\n1\r\n2\r\n0\r\n1\r\n2\r\n3\r\n0\r\n", "output": "0\r\npushStack\r\n1 popStack\r\npushStack\r\npushQueue\r\n2 popStack popQueue\r\npushStack\r\npushQueue\r\npushFront\r\n3 popStack popQueue popFront\r\n"}, {"input": "4\r\n1\r\n2\r\n3\r\n0\r\n", "output": "pushStack\r\npushQueue\r\npushFront\r\n3 popStack popQueue popFront\r\n"}, {"input": "2\r\n0\r\n1\r\n", "output": "0\r\npushQueue\r\n"}, {"input": "5\r\n1\r\n1\r\n1\r\n2\r\n1\r\n", "output": "pushQueue\r\npushQueue\r\npushQueue\r\npushQueue\r\npushQueue\r\n"}, {"input": "5\r\n3\r\n2\r\n3\r\n1\r\n0\r\n", "output": "pushStack\r\npushQueue\r\npushFront\r\npushBack\r\n3 popStack popQueue popFront\r\n"}, {"input": "49\r\n8735\r\n95244\r\n50563\r\n33648\r\n10711\r\n30217\r\n49166\r\n28240\r\n0\r\n97232\r\n12428\r\n16180\r\n58610\r\n61112\r\n74423\r\n56323\r\n43327\r\n0\r\n12549\r\n48493\r\n43086\r\n69266\r\n27033\r\n37338\r\n43900\r\n5570\r\n25293\r\n44517\r\n7183\r\n41969\r\n31944\r\n32247\r\n96959\r\n44890\r\n98237\r\n52601\r\n29081\r\n93641\r\n14980\r\n29539\r\n84672\r\n57310\r\n91014\r\n31721\r\n6944\r\n67672\r\n22040\r\n86269\r\n86709\r\n", "output": "pushBack\npushStack\npushQueue\npushBack\npushBack\npushBack\npushFront\npushBack\n3 popStack popQueue popFront\npushStack\npushBack\npushBack\npushBack\npushFront\npushQueue\npushBack\npushBack\n3 popStack popQueue popFront\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushStack\npushBack\npushBack\npushFront\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushQueue\npushBack\npushBack\npushBack\npushBack\n"}, {"input": "55\r\n73792\r\n39309\r\n73808\r\n47389\r\n34803\r\n87947\r\n32460\r\n14649\r\n70151\r\n35816\r\n8272\r\n78886\r\n71345\r\n61907\r\n16977\r\n85362\r\n0\r\n43792\r\n8118\r\n83254\r\n89459\r\n32230\r\n87068\r\n82617\r\n94847\r\n83528\r\n37629\r\n31438\r\n97413\r\n62260\r\n13651\r\n47564\r\n43543\r\n61292\r\n51025\r\n64106\r\n0\r\n19282\r\n35422\r\n19657\r\n95170\r\n10266\r\n43771\r\n3190\r\n93962\r\n11747\r\n43021\r\n91531\r\n88370\r\n1760\r\n10950\r\n77059\r\n61741\r\n52965\r\n10445\r\n", "output": "pushBack\npushBack\npushBack\npushBack\npushBack\npushStack\npushBack\npushBack\npushBack\npushBack\npushBack\npushFront\npushBack\npushBack\npushBack\npushQueue\n3 popStack popQueue popFront\npushBack\npushBack\npushBack\npushFront\npushBack\npushBack\npushBack\npushQueue\npushBack\npushBack\npushBack\npushStack\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\npushBack\n3 popStack popQueue popFront\npushBack\npushBack\npushBack\npushBack\npushFront\npushBack\npushQueue\npushBack\npushBack\npushBack\npushBack\npushBack\npushStack\npushBack\npushBack\npushBack\npushBack\npushBack\n"}, {"input": "10\r\n1\r\n2\r\n3\r\n5\r\n4\r\n9\r\n8\r\n6\r\n7\r\n0\r\n", "output": "pushBack\r\npushBack\r\npushBack\r\npushBack\r\npushBack\r\npushStack\r\npushQueue\r\npushBack\r\npushFront\r\n3 popStack popQueue popFront\r\n"}, {"input": "10\r\n1\r\n3\r\n4\r\n2\r\n6\r\n8\r\n5\r\n7\r\n10\r\n9\r\n", "output": "pushQueue\r\npushQueue\r\npushQueue\r\npushQueue\r\npushQueue\r\npushQueue\r\npushQueue\r\npushQueue\r\npushQueue\r\npushQueue\r\n"}, {"input": "1\r\n0\r\n", "output": "0\r\n"}]
| false
|
stdio
|
import sys
from collections import deque
def main(input_path, output_path, submission_path):
with open(input_path) as f:
input_lines = f.read().splitlines()
n = int(input_lines[0])
input_commands = input_lines[1:n+1]
with open(submission_path) as f:
submission_lines = [line.strip() for line in f]
if len(submission_lines) != len(input_commands):
print(0)
return
stack = []
queue = []
deck = deque()
sub_idx = 0
for cmd in input_commands:
if cmd == '0':
if sub_idx >= len(submission_lines):
print(0)
return
line = submission_lines[sub_idx]
sub_idx += 1
parts = line.split()
if not parts:
print(0)
return
try:
k = int(parts[0])
except:
print(0)
return
if k < 0 or k > 3 or len(parts) != k + 1:
print(0)
return
ops = parts[1:]
# Compute max possible sum using copies
stack_copy = list(stack)
queue_copy = list(queue)
deck_copy = deque(deck)
contributions = []
if stack_copy:
contributions.append(stack_copy[-1])
if queue_copy:
contributions.append(queue_copy[0])
if deck_copy:
deck_val = max(deck_copy[0], deck_copy[-1]) if deck_copy else 0
contributions.append(deck_val)
contributions.sort(reverse=True)
max_sum = sum(contributions[:3])
# Validate submission's ops
temp_stack = list(stack)
temp_queue = list(queue)
temp_deck = deque(deck)
sum_sub = 0
used = set()
valid = True
for op in ops:
container = None
if op == 'popStack':
if not temp_stack:
valid = False
break
sum_sub += temp_stack.pop()
container = 'stack'
elif op == 'popQueue':
if not temp_queue:
valid = False
break
sum_sub += temp_queue.pop(0)
container = 'queue'
elif op == 'popFront':
if not temp_deck:
valid = False
break
sum_sub += temp_deck.popleft()
container = 'deck'
elif op == 'popBack':
if not temp_deck:
valid = False
break
sum_sub += temp_deck.pop()
container = 'deck'
else:
valid = False
break
if container in used:
valid = False
break
used.add(container)
if not valid or sum_sub != max_sum:
print(0)
return
# Clear containers
stack.clear()
queue.clear()
deck.clear()
else:
a = int(cmd)
if sub_idx >= len(submission_lines):
print(0)
return
line = submission_lines[sub_idx]
sub_idx += 1
if line == 'pushStack':
stack.append(a)
elif line == 'pushQueue':
queue.append(a)
elif line == 'pushFront':
deck.appendleft(a)
elif line == 'pushBack':
deck.append(a)
else:
print(0)
return
print(1)
if __name__ == '__main__':
main(sys.argv[1], sys.argv[2], sys.argv[3])
| true
|
653/D
|
653
|
D
|
PyPy 3-64
|
TESTS
| 43
| 311
| 11,161,600
|
206974008
|
import sys
input = sys.stdin.buffer.readline
INF = float("inf")
class Dinic:
def __init__(self, n):
self.lvl = [0] * n
self.ptr = [0] * n
self.q = [0] * n
self.adj = [[] for _ in range(n)]
def add_edge(self, a, b, c, rcap=0):
self.adj[a].append([b, len(self.adj[b]), c, 0])
self.adj[b].append([a, len(self.adj[a]) - 1, rcap, 0])
def dfs(self, v, t, f):
if v == t or not f:
return f
for i in range(self.ptr[v], len(self.adj[v])):
e = self.adj[v][i]
if self.lvl[e[0]] == self.lvl[v] + 1:
p = self.dfs(e[0], t, min(f, e[2] - e[3]))
if p:
self.adj[v][i][3] += p
self.adj[e[0]][e[1]][3] -= p
return p
self.ptr[v] += 1
return 0
def calc(self, s, t):
flow, self.q[0] = 0, s
for l in range(31): # l = 30 maybe faster for random data
while True:
self.lvl, self.ptr = [0] * len(self.q), [0] * len(self.q)
qi, qe, self.lvl[s] = 0, 1, 1
while qi < qe and not self.lvl[t]:
v = self.q[qi]
qi += 1
for e in self.adj[v]:
if not self.lvl[e[0]] and (e[2] - e[3]) >> (30 - l):
self.q[qe] = e[0]
qe += 1
self.lvl[e[0]] = self.lvl[v] + 1
p = self.dfs(s, t, INF)
while p:
flow += p
p = self.dfs(s, t, INF)
if not self.lvl[t]:
break
return flow
def check_path(n, G, a):
D = Dinic(n+1)
for u, v, w in G:
D.add_edge(u, v, int(w//a))
return D.calc(1, n)
def process(n, x, G):
s = 0
e = 10**6
old_m = None
while True:
m = (s+e)/2
if old_m is not None and abs(x*old_m-x*m) < 1/10**6:
break
old_m = m
if check_path(n, G, m) >= x:
s, e = m, e
else:
s, e = s, m
print(s*x)
return
n, m, x = [int(x) for x in input().split()]
G = []
for i in range(m):
u, v, w = [int(x) for x in input().split()]
G.append([u, v, w])
process(n, x, G)
| 47
| 1,637
| 10,342,400
|
201896750
|
from collections import defaultdict, deque
def bfs(graph, start, end, parent):
parent.clear()
queue = deque()
queue.append([start, float("Inf")])
parent[start] = -2
while queue:
current, flow = queue.popleft()
for neighbor in graph[current]:
if parent[neighbor] == -1 and graph[current][neighbor] > 0:
parent[neighbor] = current
flow = min(flow, graph[current][neighbor])
if neighbor== end:
return flow
queue.append((neighbor, flow))
return 0
def maxflow(graph, start, end):
flow = 0
parent = defaultdict(lambda: -1)
while True:
bt = bfs(graph, start, end, parent)
if bt:
flow += bt
current = end
while current != start:
prev = parent[current]
graph[prev][current] -= bt
graph[current][prev] += bt
current = prev
else:
break
return flow
n, m, x = map(int, input().split())
graph = defaultdict(lambda: defaultdict(lambda: 0))
for _ in range(m):
u, v, w = map(int, input().split())
graph[u][v] = w
def check(k):
meh = defaultdict(lambda: defaultdict(lambda: 0))
for i in graph:
for j in graph[i]:
ww = graph[i][j] // k
meh[i][j] = ww
flow = maxflow(meh, 1, n)
return flow
lo = 1 / x
hi = check(1)
for _ in range(70):
mid = round((hi + lo) / 2,9)
if check(mid)>=x:
lo = round(mid,9)
else:
hi = mid
print(format(lo * x, '.9f'))
|
IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2)
|
CF
| 2,016
| 2
| 256
|
Delivery Bears
|
Niwel is a little golden bear. As everyone knows, bears live in forests, but Niwel got tired of seeing all the trees so he decided to move to the city.
In the city, Niwel took on a job managing bears to deliver goods. The city that he lives in can be represented as a directed graph with n nodes and m edges. Each edge has a weight capacity. A delivery consists of a bear carrying weights with their bear hands on a simple path from node 1 to node n. The total weight that travels across a particular edge must not exceed the weight capacity of that edge.
Niwel has exactly x bears. In the interest of fairness, no bear can rest, and the weight that each bear carries must be exactly the same. However, each bear may take different paths if they like.
Niwel would like to determine, what is the maximum amount of weight he can deliver (it's the sum of weights carried by bears). Find the maximum weight.
|
The first line contains three integers n, m and x (2 ≤ n ≤ 50, 1 ≤ m ≤ 500, 1 ≤ x ≤ 100 000) — the number of nodes, the number of directed edges and the number of bears, respectively.
Each of the following m lines contains three integers ai, bi and ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 1 000 000). This represents a directed edge from node ai to bi with weight capacity ci. There are no self loops and no multiple edges from one city to the other city. More formally, for each i and j that i ≠ j it's guaranteed that ai ≠ aj or bi ≠ bj. It is also guaranteed that there is at least one path from node 1 to node n.
|
Print one real value on a single line — the maximum amount of weight Niwel can deliver if he uses exactly x bears. 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, Niwel has three bears. Two bears can choose the path $$1 \rightarrow 3 \rightarrow 4$$, while one bear can choose the path $$1 \rightarrow 2 \rightarrow 4$$. Even though the bear that goes on the path $$1 \rightarrow 2 \rightarrow 4$$ can carry one unit of weight, in the interest of fairness, he is restricted to carry 0.5 units of weight. Thus, the total weight is 1.5 units overall. Note that even though Niwel can deliver more weight with just 2 bears, he must use exactly 3 bears on this day.
|
[{"input": "4 4 3\n1 2 2\n2 4 1\n1 3 1\n3 4 2", "output": "1.5000000000"}, {"input": "5 11 23\n1 2 3\n2 3 4\n3 4 5\n4 5 6\n1 3 4\n2 4 5\n3 5 6\n1 4 2\n2 5 3\n1 5 2\n3 2 30", "output": "10.2222222222"}]
| 2,200
|
["binary search", "flows", "graphs"]
| 47
|
[{"input": "4 4 3\r\n1 2 2\r\n2 4 1\r\n1 3 1\r\n3 4 2\r\n", "output": "1.5000000000\n"}, {"input": "5 11 23\r\n1 2 3\r\n2 3 4\r\n3 4 5\r\n4 5 6\r\n1 3 4\r\n2 4 5\r\n3 5 6\r\n1 4 2\r\n2 5 3\r\n1 5 2\r\n3 2 30\r\n", "output": "10.2222222222\n"}, {"input": "10 16 63\r\n1 2 1\r\n2 10 1\r\n1 3 1\r\n3 10 1\r\n1 4 1\r\n4 10 1\r\n1 5 1\r\n5 10 1\r\n1 6 1\r\n6 10 1\r\n1 7 1\r\n7 10 1\r\n1 8 1\r\n8 10 1\r\n1 9 1\r\n9 10 1\r\n", "output": "7.8750000000\n"}, {"input": "2 1 3\r\n1 2 301\r\n", "output": "301.0000000000\n"}, {"input": "2 2 1\r\n1 2 48\r\n2 1 39\r\n", "output": "48.0000000000\n"}, {"input": "5 9 5\r\n3 2 188619\r\n4 2 834845\r\n2 4 996667\r\n1 2 946392\r\n2 5 920935\r\n2 3 916558\r\n1 5 433923\r\n4 5 355150\r\n3 5 609814\r\n", "output": "1182990.0000000000\n"}, {"input": "7 15 10\r\n1 3 776124\r\n6 7 769968\r\n2 1 797048\r\n4 3 53774\r\n2 7 305724\r\n4 1 963904\r\n4 6 877656\r\n4 5 971901\r\n1 4 803781\r\n3 1 457050\r\n3 7 915891\r\n1 7 8626\r\n5 7 961155\r\n3 4 891456\r\n5 4 756977\r\n", "output": "1552248.0000000000\n"}, {"input": "3 2 100000\r\n1 2 1\r\n2 3 1\r\n", "output": "1.0000000000\n"}, {"input": "3 2 100000\r\n1 2 1\r\n2 3 1000000\r\n", "output": "1.0000000000\n"}, {"input": "2 1 100000\r\n1 2 1\r\n", "output": "1.0000000000\n"}, {"input": "3 2 100000\r\n1 2 1\r\n2 3 100000\r\n", "output": "1.0000000000\n"}]
| false
|
stdio
|
import sys
def main():
input_path = sys.argv[1]
correct_output_path = sys.argv[2]
submission_output_path = sys.argv[3]
# Read correct output
with open(correct_output_path, 'r') as f:
correct_line = f.readline().strip()
correct = float(correct_line.split()[0])
# Read submission output
with open(submission_output_path, 'r') as f:
sub_line = f.readline().strip()
try:
sub = float(sub_line)
except:
print(0)
return
# Compute the error
abs_diff = abs(sub - correct)
denominator = max(1, correct)
if abs_diff <= 1e-6 * denominator:
print(1)
else:
print(0)
if __name__ == '__main__':
main()
| true
|
877/E
|
877
|
E
|
PyPy 3
|
TESTS
| 3
| 108
| 20,480,000
|
123887692
|
# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase
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")
# ------------------- fast io --------------------
from math import ceil
def prod(a, mod=10 ** 9 + 7):
ans = 1
for each in a:
ans = (ans * each) % mod
return ans
def gcd(x, y):
while y:
x, y = y, x % y
return x
def lcm(a, b): return a * b // gcd(a, b)
def binary(x, length=16):
y = bin(x)[2:]
return y if len(y) >= length else "0" * (length - len(y)) + y
from math import inf, log2
class LazySegmentTree:
def __init__(self, array, func=max):
self.n = len(array)
self.size = 2 ** (int(log2(self.n - 1)) + 1) if self.n != 1 else 1
self.func = func
self.default = 0
self.data = [self.default] * (2 * self.size)
self.segsize = [0] * (2 * self.size)
self.lazy = [0] * (2 * self.size)
self.process(array)
def process(self, array):
self.data[self.size: self.size + self.n] = array
self.segsize[self.size: self.size + self.n] = [1] * len(array)
for i in range(self.size - 1, -1, -1):
self.data[i] = self.data[2 * i] + self.data[2 * i + 1]
self.segsize[i] = self.segsize[2 * i] + self.segsize[2 * i + 1]
def push(self, index):
"""Push the information of the root to it's children!"""
self.lazy[2 * index] += self.lazy[index]
self.lazy[2 * index + 1] += self.lazy[index]
if self.lazy[index] % 2:
self.data[index] = self.segsize[index] - self.data[index]
self.lazy[index] = 0
def build(self, index):
"""Build data with the new changes!"""
index >>= 1
while index:
left, right = self.data[2 * index], self.data[2 * index + 1]
if self.lazy[2 * index] % 2:
left = self.segsize[2 * index] - left
if self.lazy[2 * index + 1] % 2:
right = self.segsize[2 * index + 1] - right
self.data[index] = left + right
index >>= 1
def query(self, alpha, omega):
"""Returns the result of function over the range (inclusive)!"""
res = self.default
alpha += self.size
omega += self.size + 1
for i in range(len(bin(alpha)[2:]) - 1, 0, -1):
self.push(alpha >> i)
for i in range(len(bin(omega - 1)[2:]) - 1, 0, -1):
self.push((omega - 1) >> i)
while alpha < omega:
if alpha & 1:
if self.lazy[alpha] % 2:
res += self.segsize[alpha] - self.data[alpha]
else:
res += self.data[alpha]
alpha += 1
if omega & 1:
omega -= 1
if self.lazy[omega] % 2:
res += self.segsize[omega] - self.data[omega]
else:
res += self.data[omega]
alpha >>= 1
omega >>= 1
return res
def update(self, alpha, omega):
"""Increases all elements in the range (inclusive) by given value!"""
alpha += self.size
omega += self.size + 1
l, r = alpha, omega
for i in range(len(bin(alpha)[2:]) - 1, 0, -1):
self.push(alpha >> i)
for i in range(len(bin(omega - 1)[2:]) - 1, 0, -1):
self.push((omega - 1) >> i)
while alpha < omega:
if alpha & 1:
self.lazy[alpha] += 1
alpha += 1
if omega & 1:
omega -= 1
self.lazy[omega] += 1
alpha >>= 1
omega >>= 1
self.build(l)
self.build(r - 1)
def dfs(graph, alpha):
"""Depth First Search on a graph!"""
n = len(graph)
visited = [False] * n
finished = [False] * n
dp = [0] * n
subtree = [0] * n
order = []
stack = [alpha]
while stack:
v = stack[-1]
if not visited[v]:
visited[v] = True
for u in graph[v]:
if not visited[u]:
stack.append(u)
else:
order += [stack.pop()]
dp[v] = (switch[v] == 1)
subtree[v] = 1
for child in graph[v]:
if finished[child]:
dp[v] += dp[child]
subtree[v] += subtree[child]
finished[v] = True
return dp, order[::-1], subtree
for _ in range(int(input()) if not True else 1):
n = int(input())
# n, k = map(int, input().split())
# a, b = map(int, input().split())
# c, d = map(int, input().split())
a = list(map(int, input().split()))
switch = [0] + list(map(int, input().split()))
# s = input()
graph = [[] for i in range(n + 1)]
for i in range(len(a)):
x, y = i + 2, a[i]
graph[x] += [y]
graph[y] += [x]
dp, order, subtree = dfs(graph, 1)
index = [0] * (n + 1)
for i in range(len(order)):
index[order[i]] = i + 1
st = LazySegmentTree(switch, func=lambda a, b: a + b)
#for i in range(1, n + 1):
# if switch[i]:
# st.update(index[i], i)
for __ in range(int(input())):
s = input().split()
x = int(s[1])
count = subtree[x]
if s[0] == 'get':
print(st.query(index[x], index[x] + count - 1))
else:
st.update(index[x], index[x] + count - 1)
| 57
| 1,544
| 90,316,800
|
209145397
|
import math
import sys
from bisect import *
from collections import *
from functools import *
from heapq import *
from itertools import *
from random import *
from string import *
from types import GeneratorType
# region fastio
input = lambda: sys.stdin.readline().rstrip()
sint = lambda: int(input())
mint = lambda: map(int, input().split())
ints = lambda: list(map(int, input().split()))
# print = lambda d: sys.stdout.write(str(d) + "\n")
# endregion fastio
# # region interactive
# def printQry(a, b) -> None:
# sa = str(a)
# sb = str(b)
# print(f"? {sa} {sb}", flush = True)
# def printAns(ans) -> None:
# s = str(ans)
# print(f"! {s}", flush = True)
# # endregion interactive
# # region dfsconvert
# 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
# # endregion dfsconvert
# MOD = 998244353
# MOD = 10 ** 9 + 7
# DIR = ((-1, 0), (0, 1), (1, 0), (0, -1))
def dfs(graph, start=0):
n = len(graph)
dfn = [-1] * n
sz = [1] * n
visited, finished = [False] * n, [False] * n
time = n - 1
stack = [start]
while stack:
start = stack[-1]
# push unvisited children into stack
if not visited[start]:
visited[start] = True
for child in graph[start]:
if not visited[child]:
stack.append(child)
else:
stack.pop()
dfn[start] = time
time -= 1
# update with finished children
for child in graph[start]:
if finished[child]:
sz[start] += sz[child]
finished[start] = True
return dfn, sz
class SegTree:
def __init__(self, nums: list) -> None:
n = len(nums)
self.n = n
self.tree = [0] * (4 * n)
self.lazy = [0] * (4 * n)
self.build(nums, 1, 1, n)
def build(self, nums: list, o: int, l: int, r: int) -> None:
if l == r:
self.tree[o] = nums[l - 1]
return
m = (l + r) >> 1
self.build(nums, o * 2, l, m)
self.build(nums, o * 2 + 1, m + 1, r)
self.pushUp(o)
def xor(self, o: int, l: int, r: int) -> None:
self.tree[o] = r - l + 1 - self.tree[o] # 异或反转
self.lazy[o] ^= 1
def do(self, o: int, l: int, r: int, val: int) -> None:
self.tree[o] = r - l + 1 - self.tree[o] # 异或反转
self.lazy[o] ^= 1
def op(self, a: int, b: int) -> int:
# + - * / max min
return a + b
def maintain(self, o: int, l: int, r: int) -> None:
self.tree[o] = self.op(self.tree[l], self.tree[r])
self.lazy[o] ^= 1
def pushUp(self, o: int) -> None:
self.tree[o] = self.tree[o * 2] + self.tree[o * 2 + 1]
def pushDown(self, o: int, l: int, r: int) -> None:
m = (l + r) >> 1
self.do(o * 2, l, m, self.lazy[o]) # 调用相应的更新操作方法
self.do(o * 2 + 1, m + 1, r, self.lazy[o])
self.lazy[o] = 0
def update(self, o: int, l: int, r: int, L: int, R: int, val: int) -> None:
if L <= l and r <= R: # 当前区间已完全包含在更新区间,不需要继续往下更新,存在lazy
self.xor(o, l, r)
return
if self.lazy[o]: # 当前lazyd存在更新,往下传递
self.pushDown(o, l, r)
m = (l + r) >> 1
if m >= L: # 左节点在更新区间
self.update(o * 2, l, m, L, R, val)
if m < R: # 右节点在更新区间
self.update(o * 2 + 1, m + 1, r, L, R, val)
self.pushUp(o) # 从左右节点更新当前节点值
def query(self, o: int, l: int, r: int, L: int, R: int) -> int:
if R < l or L > r:
return 0
if L <= l and r <= R:
return self.tree[o]
if self.lazy[o]: # 当前lazyd存在更新,往下传递
self.pushDown(o, l, r)
m = (l + r) >> 1
res = 0
if m >= L: # 左节点在查询区间
res += self.query(o * 2, l, m, L, R)
if m < R: # 右节点在查询区间
res += self.query(o * 2 + 1, m + 1, r, L, R)
return res
def solve() -> None:
n = sint()
p = ints()
# 建图
g = [[] for _ in range(n)]
for i, u in enumerate(p, 1):
g[u - 1].append(i)
# DFN序
dfn, sz = dfs(g)
# print(dfn)
# print(sz)
t = ints()
nums = [0] * n
for i, v in enumerate(t):
nums[dfn[i]] = v
# print(nums)
st = SegTree(nums)
for _ in range(sint()):
qry = input().split()
u = int(qry[1]) - 1
if qry[0] == "get":
print(st.query(1, 1, n, dfn[u] + 1, dfn[u] + sz[u]))
else:
st.update(1, 1, n, dfn[u] + 1, dfn[u] + sz[u], 1)
# for _ in range(int(input())):
solve()
|
Codeforces Round 442 (Div. 2)
|
CF
| 2,017
| 2
| 256
|
Danil and a Part-time Job
|
Danil decided to earn some money, so he had found a part-time job. The interview have went well, so now he is a light switcher.
Danil works in a rooted tree (undirected connected acyclic graph) with n vertices, vertex 1 is the root of the tree. There is a room in each vertex, light can be switched on or off in each room. Danil's duties include switching light in all rooms of the subtree of the vertex. It means that if light is switched on in some room of the subtree, he should switch it off. Otherwise, he should switch it on.
Unfortunately (or fortunately), Danil is very lazy. He knows that his boss is not going to personally check the work. Instead, he will send Danil tasks using Workforces personal messages.
There are two types of tasks:
1. pow v describes a task to switch lights in the subtree of vertex v.
2. get v describes a task to count the number of rooms in the subtree of v, in which the light is turned on. Danil should send the answer to his boss using Workforces messages.
A subtree of vertex v is a set of vertices for which the shortest path from them to the root passes through v. In particular, the vertex v is in the subtree of v.
Danil is not going to perform his duties. He asks you to write a program, which answers the boss instead of him.
|
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the number of vertices in the tree.
The second line contains n - 1 space-separated integers p2, p3, ..., pn (1 ≤ pi < i), where pi is the ancestor of vertex i.
The third line contains n space-separated integers t1, t2, ..., tn (0 ≤ ti ≤ 1), where ti is 1, if the light is turned on in vertex i and 0 otherwise.
The fourth line contains a single integer q (1 ≤ q ≤ 200 000) — the number of tasks.
The next q lines are get v or pow v (1 ≤ v ≤ n) — the tasks described above.
|
For each task get v print the number of rooms in the subtree of v, in which the light is turned on.
| null |
The tree before the task pow 1. The tree after the task pow 1.
|
[{"input": "4\n1 1 1\n1 0 0 1\n9\nget 1\nget 2\nget 3\nget 4\npow 1\nget 1\nget 2\nget 3\nget 4", "output": "2\n0\n0\n1\n2\n1\n1\n0"}]
| 2,000
|
["bitmasks", "data structures", "trees"]
| 57
|
[{"input": "4\r\n1 1 1\r\n1 0 0 1\r\n9\r\nget 1\r\nget 2\r\nget 3\r\nget 4\r\npow 1\r\nget 1\r\nget 2\r\nget 3\r\nget 4\r\n", "output": "2\r\n0\r\n0\r\n1\r\n2\r\n1\r\n1\r\n0\r\n"}, {"input": "1\r\n\r\n1\r\n4\r\npow 1\r\nget 1\r\npow 1\r\nget 1\r\n", "output": "0\r\n1\r\n"}, {"input": "10\r\n1 2 1 3 4 5 6 8 5\r\n1 0 0 0 1 0 0 0 1 0\r\n10\r\npow 10\r\npow 4\r\npow 10\r\npow 7\r\npow 10\r\npow 10\r\npow 4\r\npow 8\r\npow 7\r\npow 1\r\n", "output": ""}, {"input": "10\r\n1 2 3 4 2 4 1 7 8\r\n1 1 0 1 1 0 0 0 1 1\r\n10\r\npow 1\r\nget 2\r\npow 2\r\npow 8\r\nget 6\r\npow 6\r\npow 10\r\nget 6\r\npow 8\r\npow 3\r\n", "output": "3\r\n0\r\n1\r\n"}, {"input": "10\r\n1 1 1 4 5 3 5 6 3\r\n0 0 0 0 0 0 1 0 0 0\r\n10\r\nget 2\r\nget 4\r\nget 7\r\nget 3\r\npow 2\r\npow 5\r\npow 2\r\nget 7\r\npow 6\r\nget 10\r\n", "output": "0\r\n0\r\n1\r\n1\r\n1\r\n0\r\n"}, {"input": "10\r\n1 1 3 1 3 1 4 6 3\r\n0 1 1 1 1 1 1 1 0 0\r\n10\r\nget 9\r\nget 10\r\nget 4\r\nget 5\r\nget 5\r\nget 5\r\nget 10\r\nget 7\r\nget 5\r\nget 2\r\n", "output": "0\r\n0\r\n2\r\n1\r\n1\r\n1\r\n0\r\n1\r\n1\r\n1\r\n"}, {"input": "10\r\n1 2 3 3 5 5 7 7 8\r\n0 0 0 0 1 1 1 1 0 0\r\n10\r\npow 3\r\nget 1\r\npow 9\r\nget 1\r\nget 1\r\nget 8\r\npow 8\r\npow 4\r\nget 10\r\npow 2\r\n", "output": "4\r\n3\r\n3\r\n1\r\n0\r\n"}, {"input": "10\r\n1 2 3 3 5 5 7 7 9\r\n1 1 0 1 0 0 1 0 0 0\r\n10\r\nget 2\r\nget 6\r\nget 4\r\nget 2\r\nget 1\r\nget 2\r\nget 6\r\nget 9\r\nget 10\r\nget 7\r\n", "output": "3\r\n0\r\n1\r\n3\r\n4\r\n3\r\n0\r\n0\r\n0\r\n1\r\n"}, {"input": "10\r\n1 1 3 3 5 5 7 7 9\r\n0 0 1 1 0 0 1 1 0 0\r\n10\r\npow 8\r\npow 4\r\npow 8\r\npow 5\r\npow 1\r\npow 2\r\npow 6\r\npow 6\r\npow 3\r\npow 2\r\n", "output": ""}, {"input": "10\r\n1 1 2 2 3 3 5 5 6\r\n1 1 1 1 0 0 1 1 0 0\r\n10\r\nget 2\r\nget 8\r\nget 10\r\nget 5\r\nget 5\r\npow 10\r\nget 10\r\nget 1\r\nget 7\r\npow 4\r\n", "output": "3\r\n1\r\n0\r\n1\r\n1\r\n1\r\n7\r\n1\r\n"}, {"input": "10\r\n1 1 2 2 3 3 4 4 5\r\n1 1 0 1 0 0 0 0 0 0\r\n10\r\nget 2\r\nget 5\r\npow 2\r\npow 4\r\nget 2\r\nget 4\r\npow 7\r\nget 10\r\npow 5\r\nget 6\r\n", "output": "2\r\n0\r\n3\r\n1\r\n1\r\n0\r\n"}]
| false
|
stdio
| null | true
|
10/A
|
10
|
A
|
Python 3
|
TESTS
| 5
| 216
| 0
|
55691746
|
"""
Tom is interested in power consumption of his favourite laptop. His laptop has three modes. In normal mode laptop consumes P1 watt per minute. T1 minutes after Tom moved the mouse or touched the keyboard for the last time, a screensaver starts and power consumption changes to P2 watt per minute. Finally, after T2 minutes from the start of the screensaver, laptop switches to the "sleep" mode and consumes P3 watt per minute. If Tom moves the mouse or touches the keyboard when the laptop is in the second or in the third mode, it switches to the first (normal) mode. Tom's work with the laptop can be divided into n time periods [l1, r1], [l2, r2], ..., [ln, rn]. During each interval Tom continuously moves the mouse and presses buttons on the keyboard. Between the periods Tom stays away from the laptop. Find out the total amount of power consumed by the laptop during the period [l1, rn].
Input
The first line contains 6 integer numbers n, P1, P2, P3, T1, T2 (1 ≤ n ≤ 100, 0 ≤ P1, P2, P3 ≤ 100, 1 ≤ T1, T2 ≤ 60). The following n lines contain description of Tom's work. Each i-th of these lines contains two space-separated integers li and ri (0 ≤ li < ri ≤ 1440, ri < li + 1 for i < n), which stand for the start and the end of the i-th period of work.
Output
Output the answer to the problem.
"""
n, P1, P2, P3, T1, T2 = list(map(int, input().split()))
e = 0 # e for energy
time_periods = []
for i in range(n):
li, ri = list(map(int, input().split()))
time_periods += [li, ri]
e += (ri - li) * P1
# take out both first and last element
# time_periods.pop(0)
# time_periods.pop(-1)
for j in range(1, 2*n - 1, 2):
y = time_periods[j+1] - time_periods[j]
if y > T1:
e += T1 * P1
if (y-T1) > T2:
e+= T2 * P2 + (y - T1 - T2) * P3
else:
e += (y - T1) * P1
else:
e += y * P1
print(e)
| 30
| 92
| 0
|
14124795
|
# import sys
# n = int(input())
# s = input().strip()
# a = [int(tmp) for tmp in input().split()]
# for i in range(n):
n, p1, p2, p3, t1, t2 = [int(tmp) for tmp in input().split()]
l = [0] * n
r = [0] * n
for i in range(n):
l[i], r[i] = [int(tmp) for tmp in input().split()]
all = 0
for i in range(n - 1):
all += (r[i] - l[i]) * p1
all += min(t1, l[i + 1] - r[i]) * p1
all += min(t2, max(l[i + 1] - r[i] - t1, 0)) * p2
all += max(l[i + 1] - r[i] - t1 - t2, 0) * p3
all += (r[-1] - l[-1]) * p1
print(all)
|
Codeforces Beta Round 10
|
ICPC
| 2,010
| 1
| 256
|
Power Consumption Calculation
|
Tom is interested in power consumption of his favourite laptop. His laptop has three modes. In normal mode laptop consumes P1 watt per minute. T1 minutes after Tom moved the mouse or touched the keyboard for the last time, a screensaver starts and power consumption changes to P2 watt per minute. Finally, after T2 minutes from the start of the screensaver, laptop switches to the "sleep" mode and consumes P3 watt per minute. If Tom moves the mouse or touches the keyboard when the laptop is in the second or in the third mode, it switches to the first (normal) mode. Tom's work with the laptop can be divided into n time periods [l1, r1], [l2, r2], ..., [ln, rn]. During each interval Tom continuously moves the mouse and presses buttons on the keyboard. Between the periods Tom stays away from the laptop. Find out the total amount of power consumed by the laptop during the period [l1, rn].
|
The first line contains 6 integer numbers n, P1, P2, P3, T1, T2 (1 ≤ n ≤ 100, 0 ≤ P1, P2, P3 ≤ 100, 1 ≤ T1, T2 ≤ 60). The following n lines contain description of Tom's work. Each i-th of these lines contains two space-separated integers li and ri (0 ≤ li < ri ≤ 1440, ri < li + 1 for i < n), which stand for the start and the end of the i-th period of work.
|
Output the answer to the problem.
| null | null |
[{"input": "1 3 2 1 5 10\n0 10", "output": "30"}, {"input": "2 8 4 2 5 10\n20 30\n50 100", "output": "570"}]
| 900
|
["implementation"]
| 30
|
[{"input": "1 3 2 1 5 10\r\n0 10\r\n", "output": "30"}, {"input": "2 8 4 2 5 10\r\n20 30\r\n50 100\r\n", "output": "570"}, {"input": "3 15 9 95 39 19\r\n873 989\r\n1003 1137\r\n1172 1436\r\n", "output": "8445"}, {"input": "4 73 2 53 58 16\r\n51 52\r\n209 242\r\n281 407\r\n904 945\r\n", "output": "52870"}, {"input": "5 41 20 33 43 4\r\n46 465\r\n598 875\r\n967 980\r\n1135 1151\r\n1194 1245\r\n", "output": "46995"}, {"input": "6 88 28 100 53 36\r\n440 445\r\n525 614\r\n644 844\r\n1238 1261\r\n1305 1307\r\n1425 1434\r\n", "output": "85540"}, {"input": "7 46 61 55 28 59\r\n24 26\r\n31 61\r\n66 133\r\n161 612\r\n741 746\r\n771 849\r\n1345 1357\r\n", "output": "67147"}, {"input": "8 83 18 30 28 5\r\n196 249\r\n313 544\r\n585 630\r\n718 843\r\n1040 1194\r\n1207 1246\r\n1268 1370\r\n1414 1422\r\n", "output": "85876"}, {"input": "9 31 65 27 53 54\r\n164 176\r\n194 210\r\n485 538\r\n617 690\r\n875 886\r\n888 902\r\n955 957\r\n1020 1200\r\n1205 1282\r\n", "output": "38570"}]
| false
|
stdio
| null | true
|
10/A
|
10
|
A
|
PyPy 3
|
TESTS
| 5
| 186
| 1,228,800
|
106342722
|
import sys
# import logging
# logging.root.setLevel(logging.INFO)
n,p1,p2,p3,t1,t2 = list(map(int,sys.stdin.readline().strip().split()))
cost = [p3] * 2000
periods = []
for _ in range(n):
period = list(map(int,sys.stdin.readline().strip().split()))
periods.append(period)
normal_end = period[1]+t1
for t in range(period[0],normal_end):
cost[t] = p1
screensaver_begin = normal_end+1
for t in range(screensaver_begin,screensaver_begin+t2):
cost[t] = p2
begin = periods[0][0]
end = periods[-1][1]
# for i,p in enumerate(cost[:end]):
# logging.info(f"{i} {p}")
print(sum(cost[begin : end]))
# Input
# 1
# 1
# Participant's output
# 0 1
# Jury's answer
# 1 0
# Checker comment
# wrong answer 1st numbers differ - expected: '1', found: '0'
| 30
| 92
| 0
|
136566367
|
if __name__ == "__main__":
n, P1, P2, P3, T1, T2 = map(int, input().split())
work_periods = []
for _ in range(n):
work_periods.append(tuple(map(int, input().split())))
result = 0
for ((start1, end1), (start2, end2)) in zip(work_periods, work_periods[1:]):
result += (end1 - start1) * P1
gap_time = start2 - end1
result += min(gap_time, T1) * P1
if gap_time > T1:
result += min(gap_time - T1, T2) * P2
if gap_time > T1 + T2:
result += (gap_time - T1 - T2) * P3
result += (work_periods[-1][1] - work_periods[-1][0]) * P1
print(result)
|
Codeforces Beta Round 10
|
ICPC
| 2,010
| 1
| 256
|
Power Consumption Calculation
|
Tom is interested in power consumption of his favourite laptop. His laptop has three modes. In normal mode laptop consumes P1 watt per minute. T1 minutes after Tom moved the mouse or touched the keyboard for the last time, a screensaver starts and power consumption changes to P2 watt per minute. Finally, after T2 minutes from the start of the screensaver, laptop switches to the "sleep" mode and consumes P3 watt per minute. If Tom moves the mouse or touches the keyboard when the laptop is in the second or in the third mode, it switches to the first (normal) mode. Tom's work with the laptop can be divided into n time periods [l1, r1], [l2, r2], ..., [ln, rn]. During each interval Tom continuously moves the mouse and presses buttons on the keyboard. Between the periods Tom stays away from the laptop. Find out the total amount of power consumed by the laptop during the period [l1, rn].
|
The first line contains 6 integer numbers n, P1, P2, P3, T1, T2 (1 ≤ n ≤ 100, 0 ≤ P1, P2, P3 ≤ 100, 1 ≤ T1, T2 ≤ 60). The following n lines contain description of Tom's work. Each i-th of these lines contains two space-separated integers li and ri (0 ≤ li < ri ≤ 1440, ri < li + 1 for i < n), which stand for the start and the end of the i-th period of work.
|
Output the answer to the problem.
| null | null |
[{"input": "1 3 2 1 5 10\n0 10", "output": "30"}, {"input": "2 8 4 2 5 10\n20 30\n50 100", "output": "570"}]
| 900
|
["implementation"]
| 30
|
[{"input": "1 3 2 1 5 10\r\n0 10\r\n", "output": "30"}, {"input": "2 8 4 2 5 10\r\n20 30\r\n50 100\r\n", "output": "570"}, {"input": "3 15 9 95 39 19\r\n873 989\r\n1003 1137\r\n1172 1436\r\n", "output": "8445"}, {"input": "4 73 2 53 58 16\r\n51 52\r\n209 242\r\n281 407\r\n904 945\r\n", "output": "52870"}, {"input": "5 41 20 33 43 4\r\n46 465\r\n598 875\r\n967 980\r\n1135 1151\r\n1194 1245\r\n", "output": "46995"}, {"input": "6 88 28 100 53 36\r\n440 445\r\n525 614\r\n644 844\r\n1238 1261\r\n1305 1307\r\n1425 1434\r\n", "output": "85540"}, {"input": "7 46 61 55 28 59\r\n24 26\r\n31 61\r\n66 133\r\n161 612\r\n741 746\r\n771 849\r\n1345 1357\r\n", "output": "67147"}, {"input": "8 83 18 30 28 5\r\n196 249\r\n313 544\r\n585 630\r\n718 843\r\n1040 1194\r\n1207 1246\r\n1268 1370\r\n1414 1422\r\n", "output": "85876"}, {"input": "9 31 65 27 53 54\r\n164 176\r\n194 210\r\n485 538\r\n617 690\r\n875 886\r\n888 902\r\n955 957\r\n1020 1200\r\n1205 1282\r\n", "output": "38570"}]
| false
|
stdio
| null | true
|
10/A
|
10
|
A
|
Python 3
|
TESTS
| 5
| 186
| 6,963,200
|
87614808
|
# -*- coding: utf-8 -*-
lista,power = [],0
n,p1,p2,p3,t1,t2 = map(int,input().split())
for i in range(n):
work = list(map(int,input().split()))
lista.append(work); power += (work[1]-work[0])*p1
if len(lista) > 1:
descanso = lista[i][0]-lista[i-1][1]
if descanso <= t1: power += p1*descanso
elif descanso <= t1+t2: power += p1*t1 + p2*(descanso-p1)
else: power += p1*t1 + p2*t2 + (descanso-t1-t2)*p3
print(power)
| 30
| 92
| 0
|
136613965
|
n,p1,p2,p3,t1,t2=[int(x) for x in input().split()]
lst=[]
for i in range(n):
lst.append([int(x) for x in input().split()])
res=(lst[0][1]-lst[0][0])*p1
for i in range(1,n):
if lst[i][0]-lst[i-1][1]<=t1:
res+=(lst[i][0]-lst[i-1][1])*p1
else:
if lst[i][0]-lst[i-1][1]<t1+t2:
res+=t1*p1+(lst[i][0]-lst[i-1][1]-t1)*p2
else:
res+=t1*p1+t2*p2+(lst[i][0]-lst[i-1][1]-t1-t2)*p3
res+=(lst[i][1]-lst[i][0])*p1
print(res)
|
Codeforces Beta Round 10
|
ICPC
| 2,010
| 1
| 256
|
Power Consumption Calculation
|
Tom is interested in power consumption of his favourite laptop. His laptop has three modes. In normal mode laptop consumes P1 watt per minute. T1 minutes after Tom moved the mouse or touched the keyboard for the last time, a screensaver starts and power consumption changes to P2 watt per minute. Finally, after T2 minutes from the start of the screensaver, laptop switches to the "sleep" mode and consumes P3 watt per minute. If Tom moves the mouse or touches the keyboard when the laptop is in the second or in the third mode, it switches to the first (normal) mode. Tom's work with the laptop can be divided into n time periods [l1, r1], [l2, r2], ..., [ln, rn]. During each interval Tom continuously moves the mouse and presses buttons on the keyboard. Between the periods Tom stays away from the laptop. Find out the total amount of power consumed by the laptop during the period [l1, rn].
|
The first line contains 6 integer numbers n, P1, P2, P3, T1, T2 (1 ≤ n ≤ 100, 0 ≤ P1, P2, P3 ≤ 100, 1 ≤ T1, T2 ≤ 60). The following n lines contain description of Tom's work. Each i-th of these lines contains two space-separated integers li and ri (0 ≤ li < ri ≤ 1440, ri < li + 1 for i < n), which stand for the start and the end of the i-th period of work.
|
Output the answer to the problem.
| null | null |
[{"input": "1 3 2 1 5 10\n0 10", "output": "30"}, {"input": "2 8 4 2 5 10\n20 30\n50 100", "output": "570"}]
| 900
|
["implementation"]
| 30
|
[{"input": "1 3 2 1 5 10\r\n0 10\r\n", "output": "30"}, {"input": "2 8 4 2 5 10\r\n20 30\r\n50 100\r\n", "output": "570"}, {"input": "3 15 9 95 39 19\r\n873 989\r\n1003 1137\r\n1172 1436\r\n", "output": "8445"}, {"input": "4 73 2 53 58 16\r\n51 52\r\n209 242\r\n281 407\r\n904 945\r\n", "output": "52870"}, {"input": "5 41 20 33 43 4\r\n46 465\r\n598 875\r\n967 980\r\n1135 1151\r\n1194 1245\r\n", "output": "46995"}, {"input": "6 88 28 100 53 36\r\n440 445\r\n525 614\r\n644 844\r\n1238 1261\r\n1305 1307\r\n1425 1434\r\n", "output": "85540"}, {"input": "7 46 61 55 28 59\r\n24 26\r\n31 61\r\n66 133\r\n161 612\r\n741 746\r\n771 849\r\n1345 1357\r\n", "output": "67147"}, {"input": "8 83 18 30 28 5\r\n196 249\r\n313 544\r\n585 630\r\n718 843\r\n1040 1194\r\n1207 1246\r\n1268 1370\r\n1414 1422\r\n", "output": "85876"}, {"input": "9 31 65 27 53 54\r\n164 176\r\n194 210\r\n485 538\r\n617 690\r\n875 886\r\n888 902\r\n955 957\r\n1020 1200\r\n1205 1282\r\n", "output": "38570"}]
| false
|
stdio
| null | true
|
298/A
|
298
|
A
|
Python 3
|
TESTS
| 9
| 62
| 0
|
149564768
|
n = int(input())
data = input()
first_index = 0
last_index = 0
#get first move
for i, ch in enumerate(data):
if ch != '.':
first_index = i
break
#get last move
for j in range(n-1, 1, -1):
#print(data[j], j)
if data[j] != '.':
last_index = j
break
#print(first_index, last_index)
if data[first_index] == 'R' and data[last_index] == 'R':
print(first_index + 1, last_index + 2)
elif data[first_index] == 'L' and data[last_index] == 'L':
print(last_index +1 , first_index )
else:
for k in range(first_index, n):
last_index = k
if data[last_index] == 'L':
break
print(first_index +1, last_index)
| 23
| 62
| 0
|
216285938
|
input()
s = ' ' + input()
if 'R' in s:
if 'L' in s:
print(s.find('R'), s.find('RL'))
else:
print(s.find('R'), s.find('R.') + 1)
else:
print(s.find('L.'), s.find('.L'))
|
Codeforces Round 180 (Div. 2)
|
CF
| 2,013
| 1
| 256
|
Snow Footprints
|
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.
At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
|
The first line of the input contains integer n (3 ≤ n ≤ 1000).
The second line contains the description of the road — the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
|
Print two space-separated integers — the values of s and t. If there are several possible solutions you can print any of them.
| null |
The first test sample is the one in the picture.
|
[{"input": "9\n..RRLL...", "output": "3 4"}, {"input": "11\n.RRRLLLLL..", "output": "7 5"}]
| 1,300
|
["greedy", "implementation"]
| 23
|
[{"input": "9\r\n..RRLL...\r\n", "output": "3 4\r\n"}, {"input": "11\r\n.RRRLLLLL..\r\n", "output": "7 5\r\n"}, {"input": "17\r\n.......RRRRR.....\r\n", "output": "12 13\r\n"}, {"input": "13\r\n....LLLLLL...\r\n", "output": "10 4\r\n"}, {"input": "4\r\n.RL.\r\n", "output": "3 2\r\n"}, {"input": "3\r\n.L.\r\n", "output": "2 1\r\n"}, {"input": "3\r\n.R.\r\n", "output": "2 3\r\n"}]
| false
|
stdio
|
import sys
from collections import deque
def main(input_path, output_path, submission_path):
with open(input_path) as f:
n = int(f.readline().strip())
road = f.readline().strip()
with open(submission_path) as f:
line = f.readline().strip()
try:
s, t = map(int, line.split())
except:
print(0)
return
if s < 1 or s > n or t < 1 or t > n:
print(0)
return
adj = [[] for _ in range(n+1)]
for i in range(1, n+1):
c = road[i-1]
if c == 'R':
if i+1 <= n:
adj[i].append(i+1)
elif c == 'L':
if i-1 >= 1:
adj[i].append(i-1)
visited = [False] * (n+1)
q = deque([s])
visited[s] = True
while q:
u = q.popleft()
if u == t:
print(1)
return
for v in adj[u]:
if not visited[v]:
visited[v] = True
q.append(v)
print(0)
if __name__ == "__main__":
input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3]
main(input_path, output_path, submission_path)
| true
|
298/A
|
298
|
A
|
Python 3
|
TESTS
| 10
| 92
| 0
|
177886782
|
non=int(input())
tx=input()
if len(tx)==3:
if tx[1]=='R':
print(f'2 3')
if tx[1]=='L':
print('2 1')
else:
if 'RL' in tx:
print(tx.find('R')+1,end=' ')
print(tx.find('RL')+1)
elif "R" in tx:
print(tx.find('RR.')+2,tx.find('R.')+2)
elif "L" in tx:
print(tx.find('.L')+2,tx.find('.LL')+1)
| 23
| 92
| 0
|
4611320
|
n=int(input())
s=input()
if 'R' in s and 'L' in s:
print(s.index('R')+1,s.index('L'))
elif 'R' in s:
print(s.index('R')+1,n-s[::-1].index('R')+1)
else:
print(s.index('L')+1,s.index('L'))
|
Codeforces Round 180 (Div. 2)
|
CF
| 2,013
| 1
| 256
|
Snow Footprints
|
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.
At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
|
The first line of the input contains integer n (3 ≤ n ≤ 1000).
The second line contains the description of the road — the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
|
Print two space-separated integers — the values of s and t. If there are several possible solutions you can print any of them.
| null |
The first test sample is the one in the picture.
|
[{"input": "9\n..RRLL...", "output": "3 4"}, {"input": "11\n.RRRLLLLL..", "output": "7 5"}]
| 1,300
|
["greedy", "implementation"]
| 23
|
[{"input": "9\r\n..RRLL...\r\n", "output": "3 4\r\n"}, {"input": "11\r\n.RRRLLLLL..\r\n", "output": "7 5\r\n"}, {"input": "17\r\n.......RRRRR.....\r\n", "output": "12 13\r\n"}, {"input": "13\r\n....LLLLLL...\r\n", "output": "10 4\r\n"}, {"input": "4\r\n.RL.\r\n", "output": "3 2\r\n"}, {"input": "3\r\n.L.\r\n", "output": "2 1\r\n"}, {"input": "3\r\n.R.\r\n", "output": "2 3\r\n"}]
| false
|
stdio
|
import sys
from collections import deque
def main(input_path, output_path, submission_path):
with open(input_path) as f:
n = int(f.readline().strip())
road = f.readline().strip()
with open(submission_path) as f:
line = f.readline().strip()
try:
s, t = map(int, line.split())
except:
print(0)
return
if s < 1 or s > n or t < 1 or t > n:
print(0)
return
adj = [[] for _ in range(n+1)]
for i in range(1, n+1):
c = road[i-1]
if c == 'R':
if i+1 <= n:
adj[i].append(i+1)
elif c == 'L':
if i-1 >= 1:
adj[i].append(i-1)
visited = [False] * (n+1)
q = deque([s])
visited[s] = True
while q:
u = q.popleft()
if u == t:
print(1)
return
for v in adj[u]:
if not visited[v]:
visited[v] = True
q.append(v)
print(0)
if __name__ == "__main__":
input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3]
main(input_path, output_path, submission_path)
| true
|
731/C
|
731
|
C
|
PyPy 3
|
TESTS
| 5
| 1,918
| 35,635,200
|
127828826
|
class dsu():
def __init__(self,n):
self.parent=[0]*(n)
self.sz=[0]*(n)
def make_set(self,v):
self.parent[v]=v
self.sz[v]=1
def find_set(self,v):
if v==self.parent[v]:
return v
self.parent[v]=self.find_set(self.parent[v])
return self.parent[v]
def union(self,a,b):
a=self.find_set(a)
b=self.find_set(b)
if a==b:
return
if self.sz[a]<self.sz[b]:
a,b=b,a
self.parent[b]=a
self.sz[a]+=self.sz[b]
def getsize(self,v):
return self.sz[self.find_set(v)]
n,m,k=map(int,input().strip().split())
ds=dsu(n)
for i in range(n):
ds.make_set(i)
color=[*map(int,input().strip().split())]
ans=0
for i in range(m):
x,y=map(int,input().strip().split())
x-=1
y-=1
if color[x]==color[y] or ds.find_set(x)==ds.find_set(y):
pass
else:
ans+=1
ds.union(x,y)
print(ans)
| 70
| 1,107
| 31,948,800
|
132050753
|
n, m, k = map(int, input().split())
lst = list(input().split())
lsta = [[] for i in range(n)]
for i in range(m):
li, ri = map(int, input().split())
lsta[li - 1].append(ri - 1)
lsta[ri - 1].append(li - 1)
visited = [False for i in range(n)]
ans = 0
for i in range(n):
if visited[i]:
continue
s = [i]
visited[i] = True
qeue = {}
while s:
x = s.pop()
if lst[x] not in qeue:
qeue[lst[x]] = 0
qeue[lst[x]] += 1
for j in lsta[x]:
if visited[j]:
continue
visited[j] = True
s.append(j)
neu, maxi = 0, 0
for e in qeue:
maxi = max(maxi, qeue[e])
neu += qeue[e]
ans += neu - maxi
print(ans)
|
Codeforces Round 376 (Div. 2)
|
CF
| 2,016
| 2
| 256
|
Socks
|
Arseniy is already grown-up and independent. His mother decided to leave him alone for m days and left on a vacation. She have prepared a lot of food, left some money and washed all Arseniy's clothes.
Ten minutes before her leave she realized that it would be also useful to prepare instruction of which particular clothes to wear on each of the days she will be absent. Arseniy's family is a bit weird so all the clothes is enumerated. For example, each of Arseniy's n socks is assigned a unique integer from 1 to n. Thus, the only thing his mother had to do was to write down two integers li and ri for each of the days — the indices of socks to wear on the day i (obviously, li stands for the left foot and ri for the right). Each sock is painted in one of k colors.
When mother already left Arseniy noticed that according to instruction he would wear the socks of different colors on some days. Of course, that is a terrible mistake cause by a rush. Arseniy is a smart boy, and, by some magical coincidence, he posses k jars with the paint — one for each of k colors.
Arseniy wants to repaint some of the socks in such a way, that for each of m days he can follow the mother's instructions and wear the socks of the same color. As he is going to be very busy these days he will have no time to change the colors of any socks so he has to finalize the colors now.
The new computer game Bota-3 was just realised and Arseniy can't wait to play it. What is the minimum number of socks that need their color to be changed in order to make it possible to follow mother's instructions and wear the socks of the same color during each of m days.
|
The first line of input contains three integers n, m and k (2 ≤ n ≤ 200 000, 0 ≤ m ≤ 200 000, 1 ≤ k ≤ 200 000) — the number of socks, the number of days and the number of available colors respectively.
The second line contain n integers c1, c2, ..., cn (1 ≤ ci ≤ k) — current colors of Arseniy's socks.
Each of the following m lines contains two integers li and ri (1 ≤ li, ri ≤ n, li ≠ ri) — indices of socks which Arseniy should wear during the i-th day.
|
Print one integer — the minimum number of socks that should have their colors changed in order to be able to obey the instructions and not make people laugh from watching the socks of different colors.
| null |
In the first sample, Arseniy can repaint the first and the third socks to the second color.
In the second sample, there is no need to change any colors.
|
[{"input": "3 2 3\n1 2 3\n1 2\n2 3", "output": "2"}, {"input": "3 2 2\n1 1 2\n1 2\n2 1", "output": "0"}]
| 1,600
|
["dfs and similar", "dsu", "graphs", "greedy"]
| 70
|
[{"input": "3 2 3\r\n1 2 3\r\n1 2\r\n2 3\r\n", "output": "2\r\n"}, {"input": "3 2 2\r\n1 1 2\r\n1 2\r\n2 1\r\n", "output": "0\r\n"}, {"input": "3 3 3\r\n1 2 3\r\n1 2\r\n2 3\r\n3 1\r\n", "output": "2\r\n"}, {"input": "4 2 4\r\n1 2 3 4\r\n1 2\r\n3 4\r\n", "output": "2\r\n"}, {"input": "10 3 2\r\n2 1 1 2 1 1 2 1 2 2\r\n4 10\r\n9 3\r\n5 7\r\n", "output": "2\r\n"}, {"input": "10 3 3\r\n2 2 1 3 1 2 1 2 2 2\r\n10 8\r\n9 6\r\n8 10\r\n", "output": "0\r\n"}, {"input": "4 3 2\r\n1 1 2 2\r\n1 2\r\n3 4\r\n2 3\r\n", "output": "2\r\n"}, {"input": "4 3 4\r\n1 2 3 4\r\n1 2\r\n3 4\r\n4 1\r\n", "output": "3\r\n"}]
| false
|
stdio
| null | true
|
731/C
|
731
|
C
|
Python 3
|
TESTS
| 5
| 873
| 24,371,200
|
127332714
|
n,m,k=map(int,input().split())
c=[*map(int,input().split())]
q={}
z=[*c]
l=[]
for i in range(m):
x,y=map(int,input().split())
l+=[[x,y]]
if c[x-1]!=c[y-1]:
q[c[x-1]]=q.get(c[x-1],0)+1
q[c[y-1]]=q.get(c[y-1],0)+1
if q:
for i in l:
if q[c[i[0]-1]]>q[c[i[1]-1]]:
q[c[i[0]-1]]+=1
c[i[1]-1]=c[i[0]-1]
else:
q[c[i[1]-1]]+=1
c[i[0]-1]=c[i[1]-1]
w=sum(1 for i in range(n) if c[i]!=z[i])
print(w)
| 70
| 1,138
| 46,489,600
|
162030654
|
import collections
n, m, k = map(int, input().split())
c = list(map(int, input().split()))
g = [set() for _ in range(n)]
for _ in range(m):
l, r = map(int, input().split())
l -= 1
r -= 1
g[l].add(r)
g[r].add(l)
total = 0
visited = [False] * n
counter = collections.Counter()
for i in range(n):
if visited[i] or not g[i]:
continue
counter.clear()
visited[i] = True
stack = [i]
while stack:
i = stack.pop()
counter[c[i]] += 1
for j in g[i]:
if not visited[j]:
visited[j] = True
stack.append(j)
total += sum(sorted(counter.values())[:-1])
print(total)
|
Codeforces Round 376 (Div. 2)
|
CF
| 2,016
| 2
| 256
|
Socks
|
Arseniy is already grown-up and independent. His mother decided to leave him alone for m days and left on a vacation. She have prepared a lot of food, left some money and washed all Arseniy's clothes.
Ten minutes before her leave she realized that it would be also useful to prepare instruction of which particular clothes to wear on each of the days she will be absent. Arseniy's family is a bit weird so all the clothes is enumerated. For example, each of Arseniy's n socks is assigned a unique integer from 1 to n. Thus, the only thing his mother had to do was to write down two integers li and ri for each of the days — the indices of socks to wear on the day i (obviously, li stands for the left foot and ri for the right). Each sock is painted in one of k colors.
When mother already left Arseniy noticed that according to instruction he would wear the socks of different colors on some days. Of course, that is a terrible mistake cause by a rush. Arseniy is a smart boy, and, by some magical coincidence, he posses k jars with the paint — one for each of k colors.
Arseniy wants to repaint some of the socks in such a way, that for each of m days he can follow the mother's instructions and wear the socks of the same color. As he is going to be very busy these days he will have no time to change the colors of any socks so he has to finalize the colors now.
The new computer game Bota-3 was just realised and Arseniy can't wait to play it. What is the minimum number of socks that need their color to be changed in order to make it possible to follow mother's instructions and wear the socks of the same color during each of m days.
|
The first line of input contains three integers n, m and k (2 ≤ n ≤ 200 000, 0 ≤ m ≤ 200 000, 1 ≤ k ≤ 200 000) — the number of socks, the number of days and the number of available colors respectively.
The second line contain n integers c1, c2, ..., cn (1 ≤ ci ≤ k) — current colors of Arseniy's socks.
Each of the following m lines contains two integers li and ri (1 ≤ li, ri ≤ n, li ≠ ri) — indices of socks which Arseniy should wear during the i-th day.
|
Print one integer — the minimum number of socks that should have their colors changed in order to be able to obey the instructions and not make people laugh from watching the socks of different colors.
| null |
In the first sample, Arseniy can repaint the first and the third socks to the second color.
In the second sample, there is no need to change any colors.
|
[{"input": "3 2 3\n1 2 3\n1 2\n2 3", "output": "2"}, {"input": "3 2 2\n1 1 2\n1 2\n2 1", "output": "0"}]
| 1,600
|
["dfs and similar", "dsu", "graphs", "greedy"]
| 70
|
[{"input": "3 2 3\r\n1 2 3\r\n1 2\r\n2 3\r\n", "output": "2\r\n"}, {"input": "3 2 2\r\n1 1 2\r\n1 2\r\n2 1\r\n", "output": "0\r\n"}, {"input": "3 3 3\r\n1 2 3\r\n1 2\r\n2 3\r\n3 1\r\n", "output": "2\r\n"}, {"input": "4 2 4\r\n1 2 3 4\r\n1 2\r\n3 4\r\n", "output": "2\r\n"}, {"input": "10 3 2\r\n2 1 1 2 1 1 2 1 2 2\r\n4 10\r\n9 3\r\n5 7\r\n", "output": "2\r\n"}, {"input": "10 3 3\r\n2 2 1 3 1 2 1 2 2 2\r\n10 8\r\n9 6\r\n8 10\r\n", "output": "0\r\n"}, {"input": "4 3 2\r\n1 1 2 2\r\n1 2\r\n3 4\r\n2 3\r\n", "output": "2\r\n"}, {"input": "4 3 4\r\n1 2 3 4\r\n1 2\r\n3 4\r\n4 1\r\n", "output": "3\r\n"}]
| false
|
stdio
| null | true
|
10/A
|
10
|
A
|
PyPy 3
|
TESTS
| 5
| 154
| 0
|
139704512
|
n,p1,p2,p3,t1,t2=list(map(int,input().split()))
l,r=list(map(int,input().split()))
x=r
s=(r-l)*p1
for i in range(n-1):
x=r
l,r=list(map(int,input().split()))
s=s+(r-l)*p1
t=l-x
if t>=t1+t2:
s=s+(t1*p1)+(t2*p2)+(t-(t1+t2))*p3
elif t>t1 and t<t2:
s=s+(t1*p1)+(t-t1)*p2
else:
s=s+t*p1
print(s)
| 30
| 92
| 0
|
137928771
|
I=lambda:map(int,input().split())
n,a,b,c,u,v=I()
exec('x=['+n*'[*I()],'+']')
r=0
for i in range(n):
r+=a*(x[i][1]-x[i][0])
if i:
t=x[i][0]-x[i-1][1]
s=min(t,u);r+=s*a;t-=s
s=min(t,v);r+=s*b;t-=s
r+=t*c
print(r)
|
Codeforces Beta Round 10
|
ICPC
| 2,010
| 1
| 256
|
Power Consumption Calculation
|
Tom is interested in power consumption of his favourite laptop. His laptop has three modes. In normal mode laptop consumes P1 watt per minute. T1 minutes after Tom moved the mouse or touched the keyboard for the last time, a screensaver starts and power consumption changes to P2 watt per minute. Finally, after T2 minutes from the start of the screensaver, laptop switches to the "sleep" mode and consumes P3 watt per minute. If Tom moves the mouse or touches the keyboard when the laptop is in the second or in the third mode, it switches to the first (normal) mode. Tom's work with the laptop can be divided into n time periods [l1, r1], [l2, r2], ..., [ln, rn]. During each interval Tom continuously moves the mouse and presses buttons on the keyboard. Between the periods Tom stays away from the laptop. Find out the total amount of power consumed by the laptop during the period [l1, rn].
|
The first line contains 6 integer numbers n, P1, P2, P3, T1, T2 (1 ≤ n ≤ 100, 0 ≤ P1, P2, P3 ≤ 100, 1 ≤ T1, T2 ≤ 60). The following n lines contain description of Tom's work. Each i-th of these lines contains two space-separated integers li and ri (0 ≤ li < ri ≤ 1440, ri < li + 1 for i < n), which stand for the start and the end of the i-th period of work.
|
Output the answer to the problem.
| null | null |
[{"input": "1 3 2 1 5 10\n0 10", "output": "30"}, {"input": "2 8 4 2 5 10\n20 30\n50 100", "output": "570"}]
| 900
|
["implementation"]
| 30
|
[{"input": "1 3 2 1 5 10\r\n0 10\r\n", "output": "30"}, {"input": "2 8 4 2 5 10\r\n20 30\r\n50 100\r\n", "output": "570"}, {"input": "3 15 9 95 39 19\r\n873 989\r\n1003 1137\r\n1172 1436\r\n", "output": "8445"}, {"input": "4 73 2 53 58 16\r\n51 52\r\n209 242\r\n281 407\r\n904 945\r\n", "output": "52870"}, {"input": "5 41 20 33 43 4\r\n46 465\r\n598 875\r\n967 980\r\n1135 1151\r\n1194 1245\r\n", "output": "46995"}, {"input": "6 88 28 100 53 36\r\n440 445\r\n525 614\r\n644 844\r\n1238 1261\r\n1305 1307\r\n1425 1434\r\n", "output": "85540"}, {"input": "7 46 61 55 28 59\r\n24 26\r\n31 61\r\n66 133\r\n161 612\r\n741 746\r\n771 849\r\n1345 1357\r\n", "output": "67147"}, {"input": "8 83 18 30 28 5\r\n196 249\r\n313 544\r\n585 630\r\n718 843\r\n1040 1194\r\n1207 1246\r\n1268 1370\r\n1414 1422\r\n", "output": "85876"}, {"input": "9 31 65 27 53 54\r\n164 176\r\n194 210\r\n485 538\r\n617 690\r\n875 886\r\n888 902\r\n955 957\r\n1020 1200\r\n1205 1282\r\n", "output": "38570"}]
| false
|
stdio
| null | true
|
24/A
|
24
|
A
|
PyPy 3-64
|
TESTS
| 5
| 92
| 0
|
165467439
|
n = int(input())
f = [0]*(n)
b = [0]*(n)
def find_cost(n, forward: list, backward:list):
cost = 0
node = forward[0][0]
visited = [0]*(n+1)
# visited[node] = 1
for i in range(n):
for f in range(n):
if forward[f][0] == node and not visited[forward[f][1]]:
visited[forward[f][1]] = 1
node = forward[f][1]
break
else:
for f in range(n):
if backward[f][0] == node and not visited[backward[f][1]]:
visited[backward[f][1]] = 1
node = backward[f][1]
cost += backward[f][2]
return cost
for i in range(n):
u, v, w = map(int, input().split())
f[i] = (u, v, w)
b[i] = (v, u, w)
print(min(find_cost(n, f, b,), find_cost(n, b, f)))
| 21
| 92
| 0
|
142011358
|
def dfs(road):
global visited, pos, neg, roads
visited[road[0]] = True
if road[1] >= 0:
pos += road[1]
else:
neg -= road[1]
for childRoad in roads[road[0]]:
if not visited[childRoad[0]]:
dfs(childRoad)
n = int(input())
dic = {}
roads = [[] for _ in range(n)]
visited = [False] * n
for _ in range(n):
a, b, cost = map(int, input().split())
roads[a-1].append((b-1, cost))
roads[b-1].append((a-1, -cost))
pos, neg = 0, 0
visited[0] = True
dfs(roads[0][0])
if roads[0][1][1] >= 0:
neg += roads[0][1][1]
else:
pos -= roads[0][1][1]
print(min(pos, neg))
|
Codeforces Beta Round 24
|
ICPC
| 2,010
| 2
| 256
|
Ring road
|
Nowadays the one-way traffic is introduced all over the world in order to improve driving safety and reduce traffic jams. The government of Berland decided to keep up with new trends. Formerly all n cities of Berland were connected by n two-way roads in the ring, i. e. each city was connected directly to exactly two other cities, and from each city it was possible to get to any other city. Government of Berland introduced one-way traffic on all n roads, but it soon became clear that it's impossible to get from some of the cities to some others. Now for each road is known in which direction the traffic is directed at it, and the cost of redirecting the traffic. What is the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other?
|
The first line contains integer n (3 ≤ n ≤ 100) — amount of cities (and roads) in Berland. Next n lines contain description of roads. Each road is described by three integers ai, bi, ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 100) — road is directed from city ai to city bi, redirecting the traffic costs ci.
|
Output single integer — the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other.
| null | null |
[{"input": "3\n1 3 1\n1 2 1\n3 2 1", "output": "1"}, {"input": "3\n1 3 1\n1 2 5\n3 2 1", "output": "2"}, {"input": "6\n1 5 4\n5 3 8\n2 4 15\n1 6 16\n2 3 23\n4 6 42", "output": "39"}, {"input": "4\n1 2 9\n2 3 8\n3 4 7\n4 1 5", "output": "0"}]
| 1,400
|
["graphs"]
| 21
|
[{"input": "3\r\n1 3 1\r\n1 2 1\r\n3 2 1\r\n", "output": "1\r\n"}, {"input": "3\r\n1 3 1\r\n1 2 5\r\n3 2 1\r\n", "output": "2\r\n"}, {"input": "6\r\n1 5 4\r\n5 3 8\r\n2 4 15\r\n1 6 16\r\n2 3 23\r\n4 6 42\r\n", "output": "39\r\n"}, {"input": "4\r\n1 2 9\r\n2 3 8\r\n3 4 7\r\n4 1 5\r\n", "output": "0\r\n"}, {"input": "5\r\n5 3 89\r\n2 3 43\r\n4 2 50\r\n1 4 69\r\n1 5 54\r\n", "output": "143\r\n"}, {"input": "10\r\n1 8 16\r\n6 1 80\r\n6 5 27\r\n5 7 86\r\n7 9 72\r\n4 9 20\r\n4 3 54\r\n3 2 57\r\n10 2 61\r\n8 10 90\r\n", "output": "267\r\n"}, {"input": "17\r\n8 12 43\r\n13 12 70\r\n7 13 68\r\n11 7 19\r\n5 11 24\r\n5 1 100\r\n4 1 10\r\n3 4 68\r\n2 3 46\r\n15 2 58\r\n15 6 38\r\n6 9 91\r\n9 10 72\r\n14 10 32\r\n14 17 97\r\n17 16 67\r\n8 16 40\r\n", "output": "435\r\n"}, {"input": "22\r\n18 22 46\r\n18 21 87\r\n5 21 17\r\n5 10 82\r\n10 12 81\r\n17 12 98\r\n16 17 17\r\n16 13 93\r\n4 13 64\r\n4 11 65\r\n15 11 18\r\n6 15 35\r\n6 7 61\r\n7 19 12\r\n19 1 65\r\n8 1 32\r\n8 2 46\r\n9 2 19\r\n9 3 58\r\n3 14 65\r\n20 14 67\r\n20 22 2\r\n", "output": "413\r\n"}, {"input": "3\r\n3 1 1\r\n2 1 1\r\n2 3 1\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
825/F
|
825
|
F
|
Python 3
|
TESTS
| 4
| 62
| 4,608,000
|
28700153
|
# D
# s = input()
# t = input()
#
# count_q = s.count('?')
# set_t = set(t)
#
# c_s = {i: s.count(i) for i in set_t}
# c_s['?'] = s.count('?')
#
# c_t = {i: t.count(i) for i in set_t}
#
# max_n = sum(c_s.values()) // sum(c_t.values())
# for i in set_t:
# c_s[i] -= (c_t[i] * max_n)
#
# while True:
# ss = c_s['?']
# for i in c_s.values():
# if i < 0:
# ss += i
#
# if ss >= 0:
# break
# elif ss < 0:
# for i in set_t:
# c_s[i] += c_t[i]
#
# for i in set_t:
# if c_s[i] < 0:
# s = s.replace('?', i, -c_s[i])
# s = s.replace('?', 'a')
#
# print(s)
# n, m = map(int, input().split())
# n, m = 3, 3
# a = ['1 2',
# '1 3',
# '3 2']
# N = [set() for _ in range(n+1)]
#
# for i in range(m):
# # v, u = map(int, input().split())
# v, u = map(int, a[i].split())
# N[v].add(u)
#
# print(N)
strs = input()
res = []
while len(strs) > 0:
n = 1
# print('s ', strs)
for i in range(1, len(strs) // 2 + 1):
# print(strs[i:], strs[:i], i)
p = strs[:i]
if strs[i:].startswith(p):
while strs[i:].startswith(p):
i += len(p)
n += 1
strs = strs[i:]
# print(res)
res.append((n, p))
break
else:
if len(res) > 0 and res[-1][0] == 1:
pp = res.pop()
res.append((1, pp[1] + strs[0]))
else:
res.append((1, strs[0]))
strs = strs[1:]
size_res = 0
for i in res:
size_res += len(str(i[0]))
size_res += len(i[1])
# print(res)
print(size_res)
| 63
| 1,778
| 12,288,000
|
65988119
|
def prefix(s):
p = [0]
for i in range(1, len(s)):
j = p[-1]
while j > 0 and s[j] != s[i]:
j = p[j - 1]
if s[i] == s[j]:
j += 1
p.append(j)
return p
s = input()
n = len(s)
ans = [0] * (n + 1)
i = n - 1
while i >= 0:
p = prefix(s[i:])
ans[i] = 2 + ans[i + 1]
for j in range(len(p)):
z = 1
if (j + 1) % (j + 1 - p[j]) == 0:
z = (j + 1) // (j + 1 - p[j])
res = len(str(z)) + (j + 1) // z + ans[i + j + 1]
ans[i] = min(ans[i], res)
i -= 1
print(ans[0])
|
Educational Codeforces Round 25
|
ICPC
| 2,017
| 2
| 512
|
String Compression
|
Ivan wants to write a letter to his friend. The letter is a string s consisting of lowercase Latin letters.
Unfortunately, when Ivan started writing the letter, he realised that it is very long and writing the whole letter may take extremely long time. So he wants to write the compressed version of string s instead of the string itself.
The compressed version of string s is a sequence of strings c1, s1, c2, s2, ..., ck, sk, where ci is the decimal representation of number ai (without any leading zeroes) and si is some string consisting of lowercase Latin letters. If Ivan writes string s1 exactly a1 times, then string s2 exactly a2 times, and so on, the result will be string s.
The length of a compressed version is |c1| + |s1| + |c2| + |s2|... |ck| + |sk|. Among all compressed versions Ivan wants to choose a version such that its length is minimum possible. Help Ivan to determine minimum possible length.
|
The only line of input contains one string s consisting of lowercase Latin letters (1 ≤ |s| ≤ 8000).
|
Output one integer number — the minimum possible length of a compressed version of s.
| null |
In the first example Ivan will choose this compressed version: c1 is 10, s1 is a.
In the second example Ivan will choose this compressed version: c1 is 1, s1 is abcab.
In the third example Ivan will choose this compressed version: c1 is 2, s1 is c, c2 is 1, s2 is z, c3 is 4, s3 is ab.
|
[{"input": "aaaaaaaaaa", "output": "3"}, {"input": "abcab", "output": "6"}, {"input": "cczabababab", "output": "7"}]
| 2,400
|
["dp", "hashing", "string suffix structures", "strings"]
| 63
|
[{"input": "aaaaaaaaaa\r\n", "output": "3\r\n"}, {"input": "abcab\r\n", "output": "6\r\n"}, {"input": "cczabababab\r\n", "output": "7\r\n"}, {"input": "kbyjorwqjk\r\n", "output": "11\r\n"}, {"input": "baaabbbaba\r\n", "output": "9\r\n"}, {"input": "aaaaaaaaaa\r\n", "output": "3\r\n"}, {"input": "cbbbcccbbc\r\n", "output": "10\r\n"}, {"input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n", "output": "4\r\n"}, {"input": "hltcdvuobkormkxkbmpfjniilublkrckmvvxemcyietgxcyjgrjwsdsgsfmoqnmbxozfavxopklhldhnsjpxhejxaxuctxeifglx\r\n", "output": "101\r\n"}, {"input": "agdmdjkbfnleldamiiedfheefgaimecnllgkjdkdcfejainklmhaklcjkgkimgfiiajiiihhdngjedgmefnjmbglghjjejfjkaha\r\n", "output": "101\r\n"}, {"input": "aaaaaaabaaaabbbbaaaaaaabbaaaaaaaaaabbabaaaaaabaaaaabaaaaaaaabaaaaaaaaaaaaaaaabaaaaaabaaaaaaaaabbaaabaaaaabbaaabaaaaabaaabaaaaaabaaaaaaaaaaabaabaaabaaaaabbbbaaaaaaaaaaaaaaabaaaaaaaaababaaabaaaaaaaaaabaaaaaaaabaaaabbbbaaaaaaabbaaaaaaaaaabbabaaaaaabaaaaabaaaaaaaabaaaaaaaaaaaaaaaabaaaaaabaaaaaaaaabbaaabaaaaabbaaabaaaaabaaabaaaaaabaaaaaaaaaaabaabaaabaaaaabbbbaaaaaaaaaaaaaaabaaaaaaaaababaaabaaaaaaaaaaba\r\n", "output": "191\r\n"}, {"input": "mulzibhhlxawrjqunzww\r\n", "output": "21\r\n"}]
| false
|
stdio
| null | true
|
10/A
|
10
|
A
|
Python 3
|
TESTS
| 5
| 154
| 5,120,000
|
17563218
|
a = list(map(int, input().split()))
n,p1,p2,p3,t1,t2 = map(int, a)
output = 0
prev = 0
for index in range(n):
safe_mode = 0
line = list(map(int, input().split()))
output += (line[1] - line[0]) * p1
if prev != 0:
safe_mode = line[0] - prev
if safe_mode > t1:
output += p1 * t1
if safe_mode > t1 + t2:
output += p2 * t2
output += p3 * (safe_mode - t1 - t2)
else:
output += t2 * (safe_mode - t1)
else:
output += safe_mode * p1
prev = line[1]
print(output)
| 30
| 92
| 0
|
143546211
|
n, p1, p2, p3, t1, t2 = map(int, input().split())
nums = []
for i in range(n):
l = list(map(int, input().split()))
nums.append(l)
ans = 0
start = 1
for i in range(n):
if start == 0:
idle = nums[i][0] - prev
if idle > t1:
ans += t1 * p1
idle -= t1
else:
ans += idle*p1
idle = 0
if idle > t2:
ans += t2*p2
idle -= t2
else:
ans += idle*p2
idle = 0
if idle > 0:
ans += idle*p3
idle = 0
diff = nums[i][1] - nums[i][0]
ans += diff * p1
prev = nums[i][1]
start = 0
print(ans)
|
Codeforces Beta Round 10
|
ICPC
| 2,010
| 1
| 256
|
Power Consumption Calculation
|
Tom is interested in power consumption of his favourite laptop. His laptop has three modes. In normal mode laptop consumes P1 watt per minute. T1 minutes after Tom moved the mouse or touched the keyboard for the last time, a screensaver starts and power consumption changes to P2 watt per minute. Finally, after T2 minutes from the start of the screensaver, laptop switches to the "sleep" mode and consumes P3 watt per minute. If Tom moves the mouse or touches the keyboard when the laptop is in the second or in the third mode, it switches to the first (normal) mode. Tom's work with the laptop can be divided into n time periods [l1, r1], [l2, r2], ..., [ln, rn]. During each interval Tom continuously moves the mouse and presses buttons on the keyboard. Between the periods Tom stays away from the laptop. Find out the total amount of power consumed by the laptop during the period [l1, rn].
|
The first line contains 6 integer numbers n, P1, P2, P3, T1, T2 (1 ≤ n ≤ 100, 0 ≤ P1, P2, P3 ≤ 100, 1 ≤ T1, T2 ≤ 60). The following n lines contain description of Tom's work. Each i-th of these lines contains two space-separated integers li and ri (0 ≤ li < ri ≤ 1440, ri < li + 1 for i < n), which stand for the start and the end of the i-th period of work.
|
Output the answer to the problem.
| null | null |
[{"input": "1 3 2 1 5 10\n0 10", "output": "30"}, {"input": "2 8 4 2 5 10\n20 30\n50 100", "output": "570"}]
| 900
|
["implementation"]
| 30
|
[{"input": "1 3 2 1 5 10\r\n0 10\r\n", "output": "30"}, {"input": "2 8 4 2 5 10\r\n20 30\r\n50 100\r\n", "output": "570"}, {"input": "3 15 9 95 39 19\r\n873 989\r\n1003 1137\r\n1172 1436\r\n", "output": "8445"}, {"input": "4 73 2 53 58 16\r\n51 52\r\n209 242\r\n281 407\r\n904 945\r\n", "output": "52870"}, {"input": "5 41 20 33 43 4\r\n46 465\r\n598 875\r\n967 980\r\n1135 1151\r\n1194 1245\r\n", "output": "46995"}, {"input": "6 88 28 100 53 36\r\n440 445\r\n525 614\r\n644 844\r\n1238 1261\r\n1305 1307\r\n1425 1434\r\n", "output": "85540"}, {"input": "7 46 61 55 28 59\r\n24 26\r\n31 61\r\n66 133\r\n161 612\r\n741 746\r\n771 849\r\n1345 1357\r\n", "output": "67147"}, {"input": "8 83 18 30 28 5\r\n196 249\r\n313 544\r\n585 630\r\n718 843\r\n1040 1194\r\n1207 1246\r\n1268 1370\r\n1414 1422\r\n", "output": "85876"}, {"input": "9 31 65 27 53 54\r\n164 176\r\n194 210\r\n485 538\r\n617 690\r\n875 886\r\n888 902\r\n955 957\r\n1020 1200\r\n1205 1282\r\n", "output": "38570"}]
| false
|
stdio
| null | true
|
732/D
|
732
|
D
|
PyPy 3-64
|
TESTS
| 3
| 78
| 13,721,600
|
218601791
|
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,M = map(int, input().split())
D = list(map(int, input().split()))
A = list(map(int, input().split()))
def check(m):
seen = set()
for i in range(m,-1,-1):
if D[i] and D[i] not in seen:
seen.add(D[i])
if i<A[D[i]-1]:
return False
return True
l,r = sum(A)+M-1,N+1
while l+1<r:
m = (l+r)//2
if check(m):
#print('m',m,'yes')
r = m
else:
#print('m',m,'no')
l = m
if r>N:
print(-1)
else:
print(r)
| 53
| 155
| 8,601,600
|
199733993
|
n,m = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
sums = sum(B)+len(B)
last = [-1] * m
cou = 0
ans = 0
per = 0
for j in range(n):
if A[j] == 0:
cou +=1
else:
if last[A[j]-1] == -1:
if j >= B[A[j]-1]:
ans += 1
last[A[j]-1] = 0
if ans == m:
if (j+1) >= sums:
per=1
print(j+1)
break
if per == 0:
print(-1)
|
Codeforces Round 377 (Div. 2)
|
CF
| 2,016
| 1
| 256
|
Exams
|
Vasiliy has an exam period which will continue for n days. He has to pass exams on m subjects. Subjects are numbered from 1 to m.
About every day we know exam for which one of m subjects can be passed on that day. Perhaps, some day you can't pass any exam. It is not allowed to pass more than one exam on any day.
On each day Vasiliy can either pass the exam of that day (it takes the whole day) or prepare all day for some exam or have a rest.
About each subject Vasiliy know a number ai — the number of days he should prepare to pass the exam number i. Vasiliy can switch subjects while preparing for exams, it is not necessary to prepare continuously during ai days for the exam number i. He can mix the order of preparation for exams in any way.
Your task is to determine the minimum number of days in which Vasiliy can pass all exams, or determine that it is impossible. Each exam should be passed exactly one time.
|
The first line contains two integers n and m (1 ≤ n, m ≤ 105) — the number of days in the exam period and the number of subjects.
The second line contains n integers d1, d2, ..., dn (0 ≤ di ≤ m), where di is the number of subject, the exam of which can be passed on the day number i. If di equals 0, it is not allowed to pass any exams on the day number i.
The third line contains m positive integers a1, a2, ..., am (1 ≤ ai ≤ 105), where ai is the number of days that are needed to prepare before passing the exam on the subject i.
|
Print one integer — the minimum number of days in which Vasiliy can pass all exams. If it is impossible, print -1.
| null |
In the first example Vasiliy can behave as follows. On the first and the second day he can prepare for the exam number 1 and pass it on the fifth day, prepare for the exam number 2 on the third day and pass it on the fourth day.
In the second example Vasiliy should prepare for the exam number 3 during the first four days and pass it on the fifth day. Then on the sixth day he should prepare for the exam number 2 and then pass it on the seventh day. After that he needs to prepare for the exam number 1 on the eighth day and pass it on the ninth day.
In the third example Vasiliy can't pass the only exam because he hasn't anough time to prepare for it.
|
[{"input": "7 2\n0 1 0 2 1 0 2\n2 1", "output": "5"}, {"input": "10 3\n0 0 1 2 3 0 2 0 1 2\n1 1 4", "output": "9"}, {"input": "5 1\n1 1 1 1 1\n5", "output": "-1"}]
| 1,700
|
["binary search", "greedy", "sortings"]
| 53
|
[{"input": "7 2\r\n0 1 0 2 1 0 2\r\n2 1\r\n", "output": "5\r\n"}, {"input": "10 3\r\n0 0 1 2 3 0 2 0 1 2\r\n1 1 4\r\n", "output": "9\r\n"}, {"input": "5 1\r\n1 1 1 1 1\r\n5\r\n", "output": "-1\r\n"}, {"input": "100 10\r\n1 1 6 6 6 2 5 7 6 5 3 7 10 10 8 9 7 6 9 2 6 7 8 6 7 5 2 5 10 1 10 1 8 10 2 9 7 1 6 8 3 10 9 4 4 8 8 6 6 1 5 5 6 5 6 6 6 9 4 7 5 4 6 6 1 1 2 1 8 10 6 2 1 7 2 1 8 10 9 2 7 3 1 5 10 2 8 10 10 10 8 9 5 4 6 10 8 9 6 6\r\n2 4 10 11 5 2 6 7 2 15\r\n", "output": "74\r\n"}, {"input": "1 1\r\n1\r\n1\r\n", "output": "-1\r\n"}, {"input": "3 2\r\n0 0 0\r\n2 1\r\n", "output": "-1\r\n"}, {"input": "4 2\r\n0 1 0 2\r\n1 1\r\n", "output": "4\r\n"}, {"input": "10 1\r\n0 1 0 0 0 0 0 0 0 1\r\n1\r\n", "output": "2\r\n"}, {"input": "5 1\r\n0 0 0 0 1\r\n1\r\n", "output": "5\r\n"}, {"input": "7 2\r\n0 0 0 0 0 1 2\r\n1 1\r\n", "output": "7\r\n"}, {"input": "10 3\r\n0 0 1 2 2 0 2 0 1 3\r\n1 1 4\r\n", "output": "10\r\n"}, {"input": "6 2\r\n1 1 1 1 1 2\r\n1 1\r\n", "output": "6\r\n"}, {"input": "6 2\r\n1 0 0 0 0 2\r\n1 1\r\n", "output": "-1\r\n"}]
| false
|
stdio
| null | true
|
731/F
|
731
|
F
|
PyPy 3-64
|
TESTS
| 3
| 62
| 3,379,200
|
188223710
|
n = int(input())
mas = [int(i) for i in input().split()]
nmax = 200001
p = [0] * (nmax + 1)
for i in range(n):
p[mas[i]] += 1
for i in range(1, nmax):
p[i] += p[i - 1]
ans = 0
for i in range(1, nmax + 1):
ma = 0
if p[i] - p[i - 1] == 0:
continue
for j in range(i, nmax - i + 1, i):
ma += j * (p[j + i - 1] - p[j - 1])
ans = max(ans, ma)
print(ans)
| 51
| 764
| 21,299,200
|
230899484
|
from bisect import bisect_left
n = int(input())
a = sorted(list(map(int, input().split())))
ans = 0
for k in set(a):
r = sum(n - bisect_left(a, j) for j in range(k, a[-1] + 1, k))
ans = max(ans, k * r)
print(ans)
|
Codeforces Round 376 (Div. 2)
|
CF
| 2,016
| 1
| 256
|
Video Cards
|
Little Vlad is fond of popular computer game Bota-2. Recently, the developers announced the new add-on named Bota-3. Of course, Vlad immediately bought only to find out his computer is too old for the new game and needs to be updated.
There are n video cards in the shop, the power of the i-th video card is equal to integer value ai. As Vlad wants to be sure the new game will work he wants to buy not one, but several video cards and unite their powers using the cutting-edge technology. To use this technology one of the cards is chosen as the leading one and other video cards are attached to it as secondary. For this new technology to work it's required that the power of each of the secondary video cards is divisible by the power of the leading video card. In order to achieve that the power of any secondary video card can be reduced to any integer value less or equal than the current power. However, the power of the leading video card should remain unchanged, i.e. it can't be reduced.
Vlad has an infinite amount of money so he can buy any set of video cards. Help him determine which video cards he should buy such that after picking the leading video card and may be reducing some powers of others to make them work together he will get the maximum total value of video power.
|
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of video cards in the shop.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 200 000) — powers of video cards.
|
The only line of the output should contain one integer value — the maximum possible total power of video cards working together.
| null |
In the first sample, it would be optimal to buy video cards with powers 3, 15 and 9. The video card with power 3 should be chosen as the leading one and all other video cards will be compatible with it. Thus, the total power would be 3 + 15 + 9 = 27. If he buys all the video cards and pick the one with the power 2 as the leading, the powers of all other video cards should be reduced by 1, thus the total power would be 2 + 2 + 14 + 8 = 26, that is less than 27. Please note, that it's not allowed to reduce the power of the leading video card, i.e. one can't get the total power 3 + 1 + 15 + 9 = 28.
In the second sample, the optimal answer is to buy all video cards and pick the one with the power 2 as the leading. The video card with the power 7 needs it power to be reduced down to 6. The total power would be 8 + 2 + 2 + 6 = 18.
|
[{"input": "4\n3 2 15 9", "output": "27"}, {"input": "4\n8 2 2 7", "output": "18"}]
| 1,900
|
["brute force", "data structures", "implementation", "math", "number theory"]
| 51
|
[{"input": "4\r\n3 2 15 9\r\n", "output": "27\r\n"}, {"input": "4\r\n8 2 2 7\r\n", "output": "18\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n123819\r\n", "output": "123819\r\n"}, {"input": "10\r\n9 6 8 5 5 2 8 9 2 2\r\n", "output": "52\r\n"}, {"input": "100\r\n17 23 71 25 50 71 85 46 78 72 89 26 23 70 40 59 23 43 86 81 70 89 92 98 85 88 16 10 26 91 61 58 23 13 75 39 48 15 73 79 59 29 48 32 45 44 25 37 58 54 45 67 27 77 20 64 95 41 80 53 69 24 38 97 59 94 50 88 92 47 95 31 66 48 48 56 37 76 42 74 55 34 43 79 65 82 70 52 48 56 36 17 14 65 77 81 88 18 33 40\r\n", "output": "5030\r\n"}, {"input": "100\r\n881 479 355 759 257 497 690 598 275 446 439 787 257 326 584 713 322 5 253 781 434 307 164 154 241 381 38 942 680 906 240 11 431 478 628 959 346 74 493 964 455 746 950 41 585 549 892 687 264 41 487 676 63 453 861 980 477 901 80 907 285 506 619 748 773 743 56 925 651 685 845 313 419 504 770 324 2 559 405 851 919 128 318 698 820 409 547 43 777 496 925 918 162 725 481 83 220 203 609 617\r\n", "output": "50692\r\n"}, {"input": "12\r\n2 3 5 5 5 5 5 5 5 5 5 5\r\n", "output": "50\r\n"}]
| false
|
stdio
| null | true
|
491/B
|
491
|
B
|
Python 3
|
TESTS
| 0
| 15
| 0
|
227285235
|
# LUOGU_RID: 128400103
a=[];b=[];c=0;d=1<<63;input()
for i in range(int(input())):e,f=map(int,input().split());a.append(e);b.append(f)
for i in range(int(input())):
e,f=map(int,input().split())
if sum(abs(j-e)for j in a)+sum(abs(k-f)for k in b)<d:d=sum(abs(j-e)for j in a)+sum(abs(k-f)for k in b);c=i+1
print(d);print(c)
| 28
| 1,747
| 10,035,200
|
72031483
|
N, M = input().split()
a, b, c, d = [int(1e10) for _ in range(4)]
for i in range(int(input())):
x, y = list(map(int, input().split()))
a, b, c, d = min(a, x + y), min(b, x - y), min(c, - x + y), min(d, - x - y)
res, pos = int(1e10), 0
for i in range(int(input())):
x, y = list(map(int, input().split()))
ans = max(max(x + y - a, x - y - b), max( - x + y - c, - x - y - d))
if ans < res:
pos = i + 1
res = ans
print(res, pos, sep = '\n')
|
Testing Round 11
|
CF
| 2,014
| 2
| 256
|
New York Hotel
|
Think of New York as a rectangular grid consisting of N vertical avenues numerated from 1 to N and M horizontal streets numerated 1 to M. C friends are staying at C hotels located at some street-avenue crossings. They are going to celebrate birthday of one of them in the one of H restaurants also located at some street-avenue crossings. They also want that the maximum distance covered by one of them while traveling to the restaurant to be minimum possible. Help friends choose optimal restaurant for a celebration.
Suppose that the distance between neighboring crossings are all the same equal to one kilometer.
|
The first line contains two integers N и M — size of the city (1 ≤ N, M ≤ 109). In the next line there is a single integer C (1 ≤ C ≤ 105) — the number of hotels friends stayed at. Following C lines contain descriptions of hotels, each consisting of two coordinates x and y (1 ≤ x ≤ N, 1 ≤ y ≤ M). The next line contains an integer H — the number of restaurants (1 ≤ H ≤ 105). Following H lines contain descriptions of restaurants in the same format.
Several restaurants and hotels may be located near the same crossing.
|
In the first line output the optimal distance. In the next line output index of a restaurant that produces this optimal distance. If there are several possibilities, you are allowed to output any of them.
| null | null |
[{"input": "10 10\n2\n1 1\n3 3\n2\n1 10\n4 4", "output": "6\n2"}]
| 2,100
|
["greedy", "math"]
| 28
|
[{"input": "10 10\r\n2\r\n1 1\r\n3 3\r\n2\r\n1 10\r\n4 4\r\n", "output": "6\r\n2\r\n"}, {"input": "100 100\r\n10\r\n53 20\r\n97 6\r\n12 74\r\n48 92\r\n97 13\r\n47 96\r\n75 32\r\n69 21\r\n95 75\r\n1 54\r\n10\r\n36 97\r\n41 1\r\n1 87\r\n39 23\r\n27 44\r\n73 97\r\n1 1\r\n6 26\r\n48 3\r\n5 69\r\n", "output": "108\r\n4\r\n"}, {"input": "100 100\r\n10\r\n86 72\r\n25 73\r\n29 84\r\n34 33\r\n29 20\r\n84 83\r\n41 80\r\n22 22\r\n16 89\r\n77 49\r\n1\r\n4 23\r\n", "output": "140\r\n1\r\n"}, {"input": "100 100\r\n1\r\n66 77\r\n10\r\n70 11\r\n76 69\r\n79 39\r\n90 3\r\n38 87\r\n61 81\r\n98 66\r\n63 68\r\n62 93\r\n53 36\r\n", "output": "9\r\n6\r\n"}, {"input": "1000000000 1000000000\r\n1\r\n1 1\r\n1\r\n1000000000 1000000000\r\n", "output": "1999999998\r\n1\r\n"}, {"input": "123456789 987654321\r\n1\r\n312 987654321\r\n1\r\n123456789 213\r\n", "output": "1111110585\r\n1\r\n"}, {"input": "453456789 987654321\r\n1\r\n443943901 1\r\n1\r\n1354 213389832\r\n", "output": "657332378\r\n1\r\n"}, {"input": "923456789 987654321\r\n1\r\n443943901 132319791\r\n1\r\n1354 560\r\n", "output": "576261778\r\n1\r\n"}, {"input": "100 100\r\n1\r\n1 100\r\n1\r\n1 100\r\n", "output": "0\r\n1\r\n"}, {"input": "1 1\r\n1\r\n1 1\r\n1\r\n1 1\r\n", "output": "0\r\n1\r\n"}, {"input": "1000000000 1000000000\r\n2\r\n1 1\r\n3 3\r\n2\r\n1 10\r\n4 4\r\n", "output": "6\r\n2\r\n"}]
| false
|
stdio
| null | true
|
731/F
|
731
|
F
|
PyPy 3
|
TESTS
| 5
| 93
| 3,072,000
|
193084428
|
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
a = list(map(int, input().split()))
m = max(a) + 5
cnt = [0] * (m + 1)
for i in a:
cnt[i] += 1
for i in range(1, m + 1):
cnt[i] += cnt[i - 1]
ans = 0
for i in range(1, m + 1):
if not cnt[min(2 * i - 1, m)] - cnt[i - 1]:
continue
c = 0
for j in range(i, m + 1, i):
c += j * (cnt[min(i + j - 1, m)] - cnt[j - 1])
ans = max(ans, c)
print(ans)
| 51
| 155
| 32,358,400
|
215746255
|
import sys
from itertools import accumulate
def main():
n = int(sys.stdin.readline().strip())
a = list(map(int, sys.stdin.readline().split()))
max_val = max(a)
C = [0] * (max_val + 1)
for x in a:
C[x] += 1
ans=0
C_sum = list(accumulate(C))
for x in set(a):
cnt = 0
for i in range(x, max_val + 1, x):
l = C_sum[i - 1] if i - 1 >= 0 else 0
r = C_sum[min(i + x - 1, max_val)]
cnt += i * (r - l)
ans = max(ans, cnt)
print(ans)
if __name__ == "__main__":
main()# 1690380579.9107695
|
Codeforces Round 376 (Div. 2)
|
CF
| 2,016
| 1
| 256
|
Video Cards
|
Little Vlad is fond of popular computer game Bota-2. Recently, the developers announced the new add-on named Bota-3. Of course, Vlad immediately bought only to find out his computer is too old for the new game and needs to be updated.
There are n video cards in the shop, the power of the i-th video card is equal to integer value ai. As Vlad wants to be sure the new game will work he wants to buy not one, but several video cards and unite their powers using the cutting-edge technology. To use this technology one of the cards is chosen as the leading one and other video cards are attached to it as secondary. For this new technology to work it's required that the power of each of the secondary video cards is divisible by the power of the leading video card. In order to achieve that the power of any secondary video card can be reduced to any integer value less or equal than the current power. However, the power of the leading video card should remain unchanged, i.e. it can't be reduced.
Vlad has an infinite amount of money so he can buy any set of video cards. Help him determine which video cards he should buy such that after picking the leading video card and may be reducing some powers of others to make them work together he will get the maximum total value of video power.
|
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of video cards in the shop.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 200 000) — powers of video cards.
|
The only line of the output should contain one integer value — the maximum possible total power of video cards working together.
| null |
In the first sample, it would be optimal to buy video cards with powers 3, 15 and 9. The video card with power 3 should be chosen as the leading one and all other video cards will be compatible with it. Thus, the total power would be 3 + 15 + 9 = 27. If he buys all the video cards and pick the one with the power 2 as the leading, the powers of all other video cards should be reduced by 1, thus the total power would be 2 + 2 + 14 + 8 = 26, that is less than 27. Please note, that it's not allowed to reduce the power of the leading video card, i.e. one can't get the total power 3 + 1 + 15 + 9 = 28.
In the second sample, the optimal answer is to buy all video cards and pick the one with the power 2 as the leading. The video card with the power 7 needs it power to be reduced down to 6. The total power would be 8 + 2 + 2 + 6 = 18.
|
[{"input": "4\n3 2 15 9", "output": "27"}, {"input": "4\n8 2 2 7", "output": "18"}]
| 1,900
|
["brute force", "data structures", "implementation", "math", "number theory"]
| 51
|
[{"input": "4\r\n3 2 15 9\r\n", "output": "27\r\n"}, {"input": "4\r\n8 2 2 7\r\n", "output": "18\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}, {"input": "1\r\n123819\r\n", "output": "123819\r\n"}, {"input": "10\r\n9 6 8 5 5 2 8 9 2 2\r\n", "output": "52\r\n"}, {"input": "100\r\n17 23 71 25 50 71 85 46 78 72 89 26 23 70 40 59 23 43 86 81 70 89 92 98 85 88 16 10 26 91 61 58 23 13 75 39 48 15 73 79 59 29 48 32 45 44 25 37 58 54 45 67 27 77 20 64 95 41 80 53 69 24 38 97 59 94 50 88 92 47 95 31 66 48 48 56 37 76 42 74 55 34 43 79 65 82 70 52 48 56 36 17 14 65 77 81 88 18 33 40\r\n", "output": "5030\r\n"}, {"input": "100\r\n881 479 355 759 257 497 690 598 275 446 439 787 257 326 584 713 322 5 253 781 434 307 164 154 241 381 38 942 680 906 240 11 431 478 628 959 346 74 493 964 455 746 950 41 585 549 892 687 264 41 487 676 63 453 861 980 477 901 80 907 285 506 619 748 773 743 56 925 651 685 845 313 419 504 770 324 2 559 405 851 919 128 318 698 820 409 547 43 777 496 925 918 162 725 481 83 220 203 609 617\r\n", "output": "50692\r\n"}, {"input": "12\r\n2 3 5 5 5 5 5 5 5 5 5 5\r\n", "output": "50\r\n"}]
| false
|
stdio
| null | true
|
743/E
|
743
|
E
|
Python 3
|
TESTS
| 5
| 46
| 0
|
23007595
|
in1 = input()
in2 = input()
cards = in2.split(" ")
case = int(in1)
current = int(cards[0])
count = 1
dls = False
for i in range(0, len(cards), 1) :
if i == 0 :
continue
elif int(cards[i]) == current :
if int(cards[i-1]) == current:
if dls :
count = count + 1
dls = False
continue
else:
count = count + 1
continue
elif (int(cards[i]) == current + 1) or (int(cards[i]) == current - 1) :
count = count + 1
dls = True
current = int(cards[i])
print(count)
| 41
| 233
| 24,780,800
|
192171052
|
import bisect
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def f(u, v):
return (m + 1) * u + v
n = int(input())
a = list(map(int, input().split()))
m = 8
x = [[] for _ in range(m)]
for i in range(n):
x[a[i] - 1].append(i)
s = 0
for y in x:
s += min(len(y), 1)
if s < m:
ans = s
print(ans)
exit()
pow2 = [1]
for _ in range(m):
pow2.append(2 * pow2[-1])
pm = pow2[m]
inf = pow(10, 9) + 1
ans = 8
ok = 1
for c in range(1, n // 8 + 3):
dp = [inf] * ((m + 1) * pm)
dp[0] = -1
for i in range(pm):
for j in range(m):
u = f(i, j)
if dp[u] == inf:
break
dpu = dp[u]
for k in range(m):
if i & pow2[k]:
continue
l = i ^ pow2[k]
xk = x[k]
z = bisect.bisect_left(xk, dpu) + c
for y in range(2):
if y + z - 1 < len(xk):
v = f(l, j + y)
dp[v] = min(dp[v], xk[y + z - 1])
for i in range(1, m + 1):
if dp[f(pm - 1, i)] == inf:
ok = 0
break
ans += 1
if not ok:
break
print(ans)
|
Codeforces Round 384 (Div. 2)
|
CF
| 2,016
| 2
| 256
|
Vladik and cards
|
Vladik was bored on his way home and decided to play the following game. He took n cards and put them in a row in front of himself. Every card has a positive integer number not exceeding 8 written on it. He decided to find the longest subsequence of cards which satisfies the following conditions:
- the number of occurrences of each number from 1 to 8 in the subsequence doesn't differ by more then 1 from the number of occurrences of any other number. Formally, if there are ck cards with number k on them in the subsequence, than for all pairs of integers $$i \in [1,8], j \in [1,8]$$ the condition |ci - cj| ≤ 1 must hold.
- if there is at least one card with number x on it in the subsequence, then all cards with number x in this subsequence must form a continuous segment in it (but not necessarily a continuous segment in the original sequence). For example, the subsequence [1, 1, 2, 2] satisfies this condition while the subsequence [1, 2, 2, 1] doesn't. Note that [1, 1, 2, 2] doesn't satisfy the first condition.
Please help Vladik to find the length of the longest subsequence that satisfies both conditions.
|
The first line contains single integer n (1 ≤ n ≤ 1000) — the number of cards in Vladik's sequence.
The second line contains the sequence of n positive integers not exceeding 8 — the description of Vladik's sequence.
|
Print single integer — the length of the longest subsequence of Vladik's sequence that satisfies both conditions.
| null |
In the first sample all the numbers written on the cards are equal, so you can't take more than one card, otherwise you'll violate the first condition.
|
[{"input": "3\n1 1 1", "output": "1"}, {"input": "8\n8 7 6 5 4 3 2 1", "output": "8"}, {"input": "24\n1 8 1 2 8 2 3 8 3 4 8 4 5 8 5 6 8 6 7 8 7 8 8 8", "output": "17"}]
| 2,200
|
["binary search", "bitmasks", "brute force", "dp"]
| 41
|
[{"input": "3\r\n1 1 1\r\n", "output": "1"}, {"input": "8\r\n8 7 6 5 4 3 2 1\r\n", "output": "8"}, {"input": "24\r\n1 8 1 2 8 2 3 8 3 4 8 4 5 8 5 6 8 6 7 8 7 8 8 8\r\n", "output": "17"}, {"input": "1\r\n8\r\n", "output": "1"}, {"input": "2\r\n5 4\r\n", "output": "2"}, {"input": "3\r\n3 3 2\r\n", "output": "2"}, {"input": "18\r\n3 6 6 8 8 1 1 4 4 3 3 5 5 7 7 2 2 3\r\n", "output": "16"}, {"input": "5\r\n2 6 1 2 6\r\n", "output": "3"}, {"input": "6\r\n4 3 1 6 7 4\r\n", "output": "5"}, {"input": "7\r\n8 8 2 6 1 8 5\r\n", "output": "5"}, {"input": "8\r\n2 8 4 7 5 3 6 1\r\n", "output": "8"}, {"input": "8\r\n8 6 3 6 7 5 5 3\r\n", "output": "5"}, {"input": "15\r\n5 2 2 7 5 2 6 4 3 8 1 8 4 2 7\r\n", "output": "9"}, {"input": "15\r\n8 8 1 6 2 2 4 5 4 2 4 8 2 5 2\r\n", "output": "6"}, {"input": "16\r\n8 2 1 5 7 6 2 5 4 4 8 2 2 6 3 8\r\n", "output": "10"}, {"input": "16\r\n2 2 8 8 5 5 3 3 7 7 1 1 6 6 4 4\r\n", "output": "16"}, {"input": "18\r\n4 3 3 3 7 7 5 2 1 1 3 3 6 1 2 4 1 8\r\n", "output": "11"}, {"input": "30\r\n5 5 4 8 6 6 7 7 8 2 2 2 1 4 4 4 8 8 6 3 5 7 7 3 7 1 6 1 1 8\r\n", "output": "19"}, {"input": "30\r\n1 7 2 2 2 3 1 1 1 3 7 3 7 3 7 7 1 7 6 6 6 5 5 5 4 4 4 8 8 8\r\n", "output": "24"}, {"input": "120\r\n6 7 8 5 2 8 5 4 6 4 3 2 5 6 5 7 5 7 1 7 4 6 4 1 4 1 1 7 6 7 3 7 4 7 4 6 4 7 6 6 6 5 5 7 3 5 3 7 2 2 4 2 5 6 8 4 1 2 2 8 3 3 2 5 6 4 3 6 2 4 1 4 2 8 8 3 7 6 4 7 2 7 3 3 8 8 6 8 7 7 6 8 3 2 5 2 6 5 7 5 7 5 3 2 6 2 6 5 7 8 7 7 2 6 5 4 2 3 1 8\r\n", "output": "34"}, {"input": "120\r\n5 4 1 4 1 7 7 1 1 1 8 2 3 3 6 3 6 2 7 3 7 3 2 8 1 6 6 1 8 3 4 6 4 7 5 8 1 4 3 5 7 6 1 5 8 5 8 5 6 5 7 4 3 4 5 2 6 3 2 4 4 4 4 7 4 5 2 7 2 6 2 2 7 2 4 7 2 1 6 4 2 8 6 2 3 4 4 8 1 6 7 6 2 7 5 6 7 6 2 3 7 8 5 2 7 7 7 7 2 7 8 8 7 5 5 6 8 8 8 3\r\n", "output": "46"}, {"input": "120\r\n6 6 6 6 3 6 6 6 6 6 6 8 2 8 8 8 8 8 4 8 8 8 8 2 1 6 1 3 1 1 1 1 1 5 1 1 1 5 2 1 7 7 7 1 7 7 3 7 7 7 7 7 7 3 7 5 6 2 1 5 4 5 4 5 5 5 6 4 5 5 5 3 5 5 4 2 4 3 2 4 4 4 4 7 4 2 4 4 3 8 4 3 3 4 7 3 3 3 3 3 3 3 3 2 2 2 1 2 7 1 2 2 2 6 2 8 2 2 3 2\r\n", "output": "84"}, {"input": "120\r\n8 8 8 8 8 8 8 8 8 8 8 8 1 8 8 4 6 4 4 4 4 4 4 4 4 3 4 4 4 4 4 6 6 6 6 6 6 6 6 6 6 6 6 6 6 3 3 3 3 3 3 3 3 3 3 3 3 3 3 7 7 7 7 7 8 7 7 7 7 7 7 4 7 7 7 3 5 5 5 1 5 5 5 5 5 5 5 5 5 5 5 5 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1\r\n", "output": "113"}, {"input": "120\r\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5\r\n", "output": "120"}]
| false
|
stdio
| null | true
|
612/F
|
612
|
F
|
PyPy 3
|
TESTS
| 7
| 77
| 409,600
|
167715885
|
from collections import defaultdict
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n, s = map(int, input().split())
a = list(map(int, input().split()))
x = defaultdict(lambda : [])
for i in range(n):
x[a[i]].append(i)
y = list(x.keys())
m = len(y)
dp = []
inf = pow(10, 9) + 1
dp0 = [inf] * n
dp0[s - 1] = 0
dp.append(dp0)
p = [[-1] * n]
for i in y:
dp1 = [inf] * n
p0 = [-1] * n
xi = x[i]
l = len(xi)
for j in range(n):
if dp0[j] == inf:
continue
dpj = dp0[j]
for k in range(l):
xik, xu, xv = xi[k], xi[(k - 1) % l], xi[(k + 1) % l]
u = (xu - xik) % n + min(abs(j - xu), n - abs(j - xu))
v = (xik - xv) % n + min(abs(j - xv), n - abs(j - xv))
d = dpj + min(u, v)
if dp1[xik] > d:
dp1[xik], p0[xik] = d, j
dp0 = dp1
dp.append(dp0)
p.append(p0)
m = len(y)
t = min(dp[m])
j = dp[m].index(t)
ans = []
for i in range(m, 0, -1):
pij = p[i][j]
d = dp[i][j] - dp[i - 1][pij]
xi = x[y[i - 1]]
k, l = xi.index(j), len(xi)
xu, xv = xi[(k + 1) % l], xi[(k - 1) % l]
if (j - xu) % n + min(abs(pij - xu), n - abs(pij - xu)) == d:
for _ in range(l - 1):
ans.append("+" + str((xi[k] - xi[(k - 1) % l]) % n))
k = (k - 1) % l
if (xu - pij) % n <= (pij - xu) % n:
ans.append("+" + str((xu - pij) % n))
else:
ans.append("-" + str((pij - xu) % n))
else:
for _ in range(l - 1):
ans.append("-" + str((xi[(k + 1) % l] - xi[k]) % n))
k = (k + 1) % l
if (xv - pij) % n <= (pij - xv) % n:
ans.append("+" + str((xv - pij) % n))
else:
ans.append("-" + str((pij - xv) % n))
j = pij
ans.reverse()
print(t)
sys.stdout.write("\n".join(ans))
| 27
| 140
| 8,704,000
|
165028661
|
import os
from re import M
import sys
from io import BytesIO, IOBase
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")
def solve():
n, s = map(int, input().split())
A = list(map(int, input().split()))
s -= 1
se = set(A)
dic = {l:i for i, l in enumerate(sorted(se))}
le = len(se)
A = [dic[a] for a in A]
lst = [[] for _ in range(le + 1)]
for i, a in enumerate(A):
lst[a + 1].append(i)
lst[0] = [s]
dp = [[] for _ in range(le + 1)]
bef = [[] for _ in range(le + 1)]
dp[0] = [0]
inf = 1 << 30
for ii in range(le):
l1 = lst[ii]
l2 = lst[ii + 1]
le1 = len(l1)
le2 = len(l2)
bdp = dp[ii]
ndp = [inf] * le2
bef_ = [None] * le2
if le2 == 1:
for i in range(le1):
tmp = abs(l1[i] - l2[0])
tmp = min(tmp, n - tmp)
d = bdp[i] + tmp
if d < ndp[0]:
ndp[0] = d
bef_[0] = i
else:
for i in range(le1):
for j in range(le2):
tmp = abs(l1[i] - l2[j])
tmp = min(tmp, n - tmp)
d = bdp[i] + tmp
if j == 0:
dd = d + l2[le2 - 1] - l2[j]
if dd < ndp[le2 - 1]:
ndp[le2 - 1] = dd
bef_[le2 - 1] = (i, 0)
else:
dd = d + n - (l2[j] - l2[j - 1])
if dd < ndp[j - 1]:
ndp[j - 1] = dd
bef_[j - 1] = (i, 0)
if j == le2 - 1:
dd = d + l2[j] - l2[0]
if dd < ndp[0]:
ndp[0] = dd
bef_[0] = (i, 1)
else:
dd = d + n - (l2[j + 1] - l2[j])
if dd < ndp[j + 1]:
ndp[j + 1] = dd
bef_[j + 1] = (i, 1)
dp[ii + 1] = ndp
bef[ii + 1] = bef_
min_ = 1 << 30
t = -1
for i, d in enumerate(dp[-1]):
if d < min_:
min_ = d
t = i
print(min_)
ans = []
for i in range(le, 0, -1):
if type(bef[i][t]) == int:
j = bef[i][t]
tmp = lst[i][t] - lst[i - 1][j]
if tmp < 0:
tmp += n
if tmp <= n - tmp:
ans.append(f"+{tmp}")
else:
ans.append(f"-{n - tmp}")
t = j
else:
j, k = bef[i][t]
l = len(lst[i])
if k == 1:
r = t + 1
if r == l:
r = 0
for _ in range(l - 1):
d = lst[i][r] - lst[i][t]
if d < 0:
d += n
ans.append(f"-{d}")
t = r
r = t + 1
if r == l:
r = 0
else:
r = t - 1
if r == -1:
r = l - 1
for _ in range(l - 1):
d = lst[i][t] - lst[i][r]
if d < 0:
d += n
ans.append(f"+{d}")
t = r
r = t - 1
if r == -1:
r = l - 1
tmp = lst[i][t] - lst[i - 1][j]
if tmp < 0:
tmp += n
if tmp <= n - tmp:
ans.append(f"+{tmp}")
else:
ans.append(f"-{n - tmp}")
t = j
print(*ans[::-1], sep="\n")
for _ in range(1):
solve()
|
Educational Codeforces Round 4
|
ICPC
| 2,015
| 1
| 256
|
Simba on the Circle
|
You are given a circular array with n elements. The elements are numbered from some element with values from 1 to n in clockwise order. The i-th cell contains the value ai. The robot Simba is in cell s.
Each moment of time the robot is in some of the n cells (at the begin he is in s). In one turn the robot can write out the number written in current cell or move to the adjacent cell in clockwise or counterclockwise direction. To write out the number from the cell Simba doesn't spend any time, but to move to adjacent cell Simba spends one unit of time.
Simba wants to write the number from each cell one time, so the numbers will be written in a non decreasing order. Find the least number of time units to write out all numbers.
|
The first line contains two integers n and s (1 ≤ s ≤ n ≤ 2000) — the number of cells in the circular array and the starting position of Simba.
The second line contains n integers ai ( - 109 ≤ ai ≤ 109) — the number written in the i-th cell. The numbers are given for cells in order from 1 to n. Some of numbers ai can be equal.
|
In the first line print the number t — the least number of time units.
Each of the next n lines should contain the direction of robot movement and the number of cells to move in that direction. After that movement the robot writes out the number from the cell in which it turns out. The direction and the number of cells should be printed in the form of +x in case of clockwise movement and -x in case of counterclockwise movement to x cells (0 ≤ x ≤ n - 1).
Note that the sum of absolute values of x should be equal to t.
| null | null |
[{"input": "9 1\n0 1 2 2 2 1 0 1 1", "output": "12\n+0\n-3\n-1\n+2\n+1\n+2\n+1\n+1\n+1"}, {"input": "8 1\n0 1 0 1 0 1 0 1", "output": "13\n+0\n+2\n+2\n+2\n-1\n+2\n+2\n+2"}, {"input": "8 1\n1 2 3 4 5 6 7 8", "output": "7\n+0\n+1\n+1\n+1\n+1\n+1\n+1\n+1"}, {"input": "8 1\n0 0 0 0 0 0 0 0", "output": "7\n+0\n+1\n+1\n+1\n+1\n+1\n+1\n+1"}]
| 2,600
|
["dp"]
| 27
|
[{"input": "9 1\r\n0 1 2 2 2 1 0 1 1\r\n", "output": "12\r\n+0\r\n-3\r\n-1\r\n+2\r\n+1\r\n+2\r\n+1\r\n+1\r\n+1\r\n"}, {"input": "8 1\r\n0 1 0 1 0 1 0 1\r\n", "output": "13\r\n+0\r\n+2\r\n+2\r\n+2\r\n-1\r\n+2\r\n+2\r\n+2\r\n"}, {"input": "8 1\r\n1 2 3 4 5 6 7 8\r\n", "output": "7\r\n+0\r\n+1\r\n+1\r\n+1\r\n+1\r\n+1\r\n+1\r\n+1\r\n"}, {"input": "8 1\r\n0 0 0 0 0 0 0 0\r\n", "output": "7\r\n+0\r\n+1\r\n+1\r\n+1\r\n+1\r\n+1\r\n+1\r\n+1\r\n"}, {"input": "8 1\r\n0 1 2 2 1 0 1 1\r\n", "output": "11\r\n+0\r\n-3\r\n-1\r\n+2\r\n+1\r\n+2\r\n+1\r\n+1\r\n"}, {"input": "1 1\r\n4\r\n", "output": "0\r\n+0\r\n"}, {"input": "10 1\r\n-1 0 1 0 -1 1 0 0 1 -1\r\n", "output": "22\r\n+0\r\n-1\r\n-5\r\n-3\r\n+2\r\n+3\r\n+1\r\n+1\r\n-3\r\n-3\r\n"}, {"input": "20 7\r\n0 6 0 0 0 -7 -8 9 -7 4 7 2 -4 4 -5 2 6 8 -2 -7\r\n", "output": "83\r\n+0\r\n+2\r\n-3\r\n-6\r\n-5\r\n-2\r\n+6\r\n+2\r\n+2\r\n+1\r\n+1\r\n+7\r\n+4\r\n-2\r\n-4\r\n-8\r\n-5\r\n-6\r\n+7\r\n+10\r\n"}, {"input": "30 13\r\n68 50 99 23 84 23 24 -42 82 36 -10 -51 -96 96 19 -4 4 -41 74 92 13 58 26 79 -11 38 -80 -38 73 -21\r\n", "output": "238\r\n+0\r\n+14\r\n+15\r\n-4\r\n+10\r\n+10\r\n+2\r\n-5\r\n-14\r\n+5\r\n+1\r\n+4\r\n-6\r\n-11\r\n+2\r\n+1\r\n-14\r\n-13\r\n-14\r\n+6\r\n-10\r\n+9\r\n-2\r\n-10\r\n+5\r\n+15\r\n-4\r\n+15\r\n-6\r\n-11\r\n"}, {"input": "40 1\r\n886 -661 499 -14 -101 660 -259 -499 -766 155 -120 -112 -922 979 36 528 593 653 409 -476 -125 183 -817 59 353 16 525 -43 -388 989 306 -145 935 -712 -243 460 -861 339 347 -445\r\n", "output": "437\r\n+12\r\n-16\r\n-14\r\n-14\r\n-15\r\n+8\r\n+6\r\n+12\r\n+20\r\n-11\r\n+18\r\n-12\r\n-3\r\n-11\r\n-10\r\n+1\r\n-7\r\n-17\r\n+16\r\n-18\r\n-11\r\n+9\r\n-14\r\n+12\r\n+9\r\n+7\r\n+1\r\n-14\r\n-6\r\n+17\r\n+7\r\n-16\r\n-11\r\n+1\r\n+1\r\n-12\r\n-5\r\n-8\r\n-19\r\n+16\r\n"}, {"input": "50 32\r\n2624 -8355 -5993 -1 8197 382 -9197 -5078 -7 -1021 -4419 8918 -7114 5016 1912 -8436 -1217 2178 -6513 -9910 -1695 7501 7028 -6171 9063 9112 9063 -1886 9156 -7256 8871 -6855 7059 -5209 2308 5964 -4283 2248 1790 -6658 2906 -478 -5663 -9250 4355 1099 1468 -3051 -9353 -5717\r\n", "output": "601\r\n-12\r\n-21\r\n-5\r\n+13\r\n+9\r\n-14\r\n-22\r\n-17\r\n+19\r\n+8\r\n-21\r\n+5\r\n-21\r\n-3\r\n-7\r\n-9\r\n+24\r\n+3\r\n-24\r\n+11\r\n-20\r\n-7\r\n-4\r\n-7\r\n-18\r\n+17\r\n-5\r\n+2\r\n-10\r\n+1\r\n-8\r\n-24\r\n+3\r\n+20\r\n-3\r\n+16\r\n-10\r\n+4\r\n+19\r\n+22\r\n-13\r\n+10\r\n-11\r\n-17\r\n-24\r\n-19\r\n+13\r\n+2\r\n-1\r\n+3\r\n"}, {"input": "60 32\r\n58726 58267 -31806 44691 -52713 -11475 61179 83630 93772 48048 -64921 -16810 -16172 -30820 30109 -81876 -27921 -69676 -28393 -45495 6588 -30154 21312 50563 22336 -37995 -31034 -30980 -72408 -29962 -4891 24299 8648 -69415 -62580 95513 -13691 -92575 -10376 40008 2041 -24616 -6934 -42025 68949 -87961 -91709 -46669 -36624 -75601 -83110 43195 86628 53287 -14813 -7263 -20579 -51021 37654 -13428\r\n", "output": "884\r\n+6\r\n+9\r\n-1\r\n+5\r\n+25\r\n-26\r\n-21\r\n-11\r\n+16\r\n-23\r\n+24\r\n+30\r\n-7\r\n-10\r\n-28\r\n+24\r\n-18\r\n+23\r\n+14\r\n+24\r\n+1\r\n-14\r\n+8\r\n+8\r\n-11\r\n-2\r\n+25\r\n+15\r\n+15\r\n+1\r\n-18\r\n-18\r\n+23\r\n+6\r\n-27\r\n+17\r\n-13\r\n-12\r\n+10\r\n-20\r\n+12\r\n-10\r\n+2\r\n+7\r\n-17\r\n-16\r\n-19\r\n+12\r\n+12\r\n+6\r\n+14\r\n+30\r\n+8\r\n-1\r\n+6\r\n-22\r\n+23\r\n-15\r\n+16\r\n+27\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_path = sys.argv[3]
# Read input
with open(input_path) as f:
lines = f.read().splitlines()
n, s = map(int, lines[0].split())
a = list(map(int, lines[1].split()))
# Read reference output
with open(output_path) as f:
ref_lines = f.read().splitlines()
ref_t = int(ref_lines[0])
# Read submission output
with open(submission_path) as f:
sub_lines = f.read().splitlines()
if not sub_lines:
print(0)
return
try:
sub_t = int(sub_lines[0])
except:
print(0)
return
if sub_t != ref_t:
print(0)
return
sub_steps = sub_lines[1:]
if len(sub_steps) != n:
print(0)
return
# Check each step format and collect x sum
sum_x = 0
steps = []
for line in sub_steps:
line = line.strip()
if not (line.startswith('+') or line.startswith('-')):
print(0)
return
try:
sign = line[0]
x = int(line[1:])
except:
print(0)
return
if x < 0 or x >= n:
print(0)
return
steps.append((sign, x))
sum_x += x
if sum_x != sub_t:
print(0)
return
# Simulate the steps
current_pos = s
visited = set()
prev_num = None
for sign, x in steps:
pos = current_pos - 1
if sign == '+':
pos = (pos + x) % n
else:
pos = (pos - x) % n
new_pos = pos + 1
if new_pos < 1 or new_pos > n:
print(0)
return
if new_pos in visited:
print(0)
return
visited.add(new_pos)
current_num = a[new_pos - 1]
if prev_num is not None and current_num < prev_num:
print(0)
return
prev_num = current_num
current_pos = new_pos
# Check all cells are visited
if len(visited) != n or any((i + 1) not in visited for i in range(n)):
print(0)
return
print(1)
if __name__ == "__main__":
main()
| true
|
489/A
|
489
|
A
|
PyPy 3-64
|
TESTS
| 0
| 31
| 0
|
201605435
|
n=int(input())
x = list(map(lambda q:int(q), input().split(" ")))
y=x.copy()
qq=x.copy()
qq.sort()
y.sort()
y.reverse()
t=0
a=[]
for i in range(n):
for j in range(n):
if y[i]==x[j]:
t+=1
m=x[n-1-i]
p=x[j]
x[n-i-1]=p
f=(j,(n-1-i))
a.append(f)
if x==qq:
break
print(t)
for i in range(t):
print(' '.join(map(str,a[i])))
| 22
| 140
| 5,632,000
|
197497947
|
n=int(input())
lst = list(map(int, input().strip().split(' ')))
print(n)
for i in range(n):
j=lst[i:].index(min(lst[i:]))
lst[i],lst[j+i]=lst[j+i],lst[i]
print(i,j+i,end=" ")
print()
|
Codeforces Round 277.5 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
SwapSort
|
In this problem your goal is to sort an array consisting of n integers in at most n swaps. For the given array find the sequence of swaps that makes the array sorted in the non-descending order. Swaps are performed consecutively, one after another.
Note that in this problem you do not have to minimize the number of swaps — your task is to find any sequence that is no longer than n.
|
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of array elements. The second line contains elements of array: a0, a1, ..., an - 1 ( - 109 ≤ ai ≤ 109), where ai is the i-th element of the array. The elements are numerated from 0 to n - 1 from left to right. Some integers may appear in the array more than once.
|
In the first line print k (0 ≤ k ≤ n) — the number of swaps. Next k lines must contain the descriptions of the k swaps, one per line. Each swap should be printed as a pair of integers i, j (0 ≤ i, j ≤ n - 1), representing the swap of elements ai and aj. You can print indices in the pairs in any order. The swaps are performed in the order they appear in the output, from the first to the last. It is allowed to print i = j and swap the same pair of elements multiple times.
If there are multiple answers, print any of them. It is guaranteed that at least one answer exists.
| null | null |
[{"input": "5\n5 2 5 1 4", "output": "2\n0 3\n4 2"}, {"input": "6\n10 20 20 40 60 60", "output": "0"}, {"input": "2\n101 100", "output": "1\n0 1"}]
| 1,200
|
["greedy", "implementation", "sortings"]
| 22
|
[{"input": "5\r\n5 2 5 1 4\r\n", "output": "2\r\n0 3\r\n4 2\r\n"}, {"input": "6\r\n10 20 20 40 60 60\r\n", "output": "0\r\n"}, {"input": "2\r\n101 100\r\n", "output": "1\r\n0 1\r\n"}, {"input": "1\r\n1000\r\n", "output": "0\r\n"}, {"input": "2\r\n1000000000 -1000000000\r\n", "output": "1\r\n0 1\r\n"}, {"input": "8\r\n5 2 6 8 3 1 6 8\r\n", "output": "4\r\n0 5\r\n4 2\r\n5 3\r\n6 5\r\n"}, {"input": "2\r\n200000000 199999999\r\n", "output": "1\r\n0 1\r\n"}, {"input": "3\r\n100000000 100000002 100000001\r\n", "output": "1\r\n1 2\r\n"}, {"input": "5\r\n1000000000 -10000000 0 8888888 7777777\r\n", "output": "3\r\n0 1\r\n2 1\r\n4 2\r\n"}, {"input": "5\r\n10 30 20 50 40\r\n", "output": "2\r\n1 2\r\n4 3\r\n"}]
| false
|
stdio
|
import sys
def main():
input_path = sys.argv[1]
output_path = sys.argv[2]
sub_path = sys.argv[3]
with open(input_path) as f:
n = int(f.readline().strip())
arr = list(map(int, f.readline().split()))
with open(sub_path) as f:
lines = f.readlines()
if not lines:
print(0)
return
try:
k = int(lines[0].strip())
swaps = []
for line in lines[1:1 + k]:
i, j = map(int, line.strip().split())
swaps.append((i, j))
except (ValueError, IndexError):
print(0)
return
if not (0 <= k <= n):
print(0)
return
for i, j in swaps:
if not (0 <= i < n and 0 <= j < n):
print(0)
return
current = arr.copy()
for i, j in swaps:
current[i], current[j] = current[j], current[i]
sorted_arr = sorted(arr)
if current == sorted_arr:
print(1)
else:
print(0)
if __name__ == "__main__":
main()
| true
|
358/D
|
358
|
D
|
Python 3
|
TESTS
| 3
| 31
| 0
|
5353788
|
n, u, v = int(input()), 0, 0
a, b, c = (list(map(int, input().split())) for i in range(3))
for i in range(n): u, v = max(v + a[i], u + b[i]), max(v + b[i], u + c[i])
print(u)
| 29
| 62
| 921,600
|
4892437
|
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
fed_left = {0 : a[0]}
not_fed_left = {0 : b[0]}
for i in range(1, n):
fed_left[i] = max(fed_left[i-1] + b[i], not_fed_left[i-1] + a[i]) # max(fed left, fed right)
not_fed_left[i] = max(fed_left[i-1] + c[i], not_fed_left[i-1] + b[i]) # max(fed left and right, fed right)
print(fed_left[n-1])
|
Codeforces Round 208 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Dima and Hares
|
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
|
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≤ ai, bi, ci ≤ 105.
Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number bi in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
|
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
| null | null |
[{"input": "4\n1 2 3 4\n4 3 2 1\n0 1 1 0", "output": "13"}, {"input": "7\n8 5 7 6 1 8 9\n2 7 9 5 4 3 1\n2 3 3 4 1 1 3", "output": "44"}, {"input": "3\n1 1 1\n1 2 1\n1 1 1", "output": "4"}]
| 1,800
|
["dp", "greedy"]
| 29
|
[{"input": "4\r\n1 2 3 4\r\n4 3 2 1\r\n0 1 1 0\r\n", "output": "13\r\n"}, {"input": "7\r\n8 5 7 6 1 8 9\r\n2 7 9 5 4 3 1\r\n2 3 3 4 1 1 3\r\n", "output": "44\r\n"}, {"input": "3\r\n1 1 1\r\n1 2 1\r\n1 1 1\r\n", "output": "4\r\n"}, {"input": "7\r\n1 3 8 9 3 4 4\r\n6 0 6 6 1 8 4\r\n9 6 3 7 8 8 2\r\n", "output": "42\r\n"}, {"input": "2\r\n3 5\r\n9 8\r\n4 0\r\n", "output": "14\r\n"}, {"input": "7\r\n3 6 1 5 4 2 0\r\n9 7 3 7 2 6 0\r\n1 6 5 7 5 4 1\r\n", "output": "37\r\n"}, {"input": "1\r\n0\r\n1\r\n4\r\n", "output": "0\r\n"}, {"input": "1\r\n7\r\n1\r\n7\r\n", "output": "7\r\n"}, {"input": "8\r\n7 3 3 5 9 9 8 1\r\n8 2 6 6 0 3 8 0\r\n1 2 5 0 9 4 7 8\r\n", "output": "49\r\n"}, {"input": "6\r\n1 2 0 1 6 4\r\n0 6 1 8 9 8\r\n4 1 4 3 9 8\r\n", "output": "33\r\n"}, {"input": "1\r\n0\r\n0\r\n0\r\n", "output": "0\r\n"}, {"input": "1\r\n100000\r\n100000\r\n100000\r\n", "output": "100000\r\n"}]
| false
|
stdio
| null | true
|
651/B
|
651
|
B
|
Python 3
|
TESTS
| 5
| 62
| 0
|
109991907
|
n=int(input())
arr=sorted(list(map(int,input().split())))
t=0
i=0
j=n-1
while i<j:
if arr[i]<arr[j]:
t+=1
i+=1
else:
j-=1
print(t)
| 31
| 46
| 0
|
229111689
|
n = int(input())
beauty_values = list(map(int, input().split()))
def frequency_dict(arr):
freq_dict = {}
for element in arr:
if element in freq_dict:
freq_dict[element] += 1
else:
freq_dict[element] = 1
return freq_dict
beauty_values_dict = frequency_dict(beauty_values)
max_happy_times = 0
def count_non_zero_keys(freq_dict):
count = 0
while any(value != 0 for value in freq_dict.values()):
for key in list(freq_dict.keys()):
if freq_dict[key] > 0:
freq_dict[key] -= 1
count += 1
else:
del freq_dict[key]
count -= 1
return count
max_happy_times = count_non_zero_keys(beauty_values_dict)
# Output the result
print(max_happy_times)
|
Codeforces Round 345 (Div. 2)
|
CF
| 2,016
| 1
| 256
|
Beautiful Paintings
|
There are n pictures delivered for the new exhibition. The i-th painting has beauty ai. We know that a visitor becomes happy every time he passes from a painting to a more beautiful one.
We are allowed to arranged pictures in any order. What is the maximum possible number of times the visitor may become happy while passing all pictures from first to last? In other words, we are allowed to rearrange elements of a in any order. What is the maximum possible number of indices i (1 ≤ i ≤ n - 1), such that ai + 1 > ai.
|
The first line of the input contains integer n (1 ≤ n ≤ 1000) — the number of painting.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where ai means the beauty of the i-th painting.
|
Print one integer — the maximum possible number of neighbouring pairs, such that ai + 1 > ai, after the optimal rearrangement.
| null |
In the first sample, the optimal order is: 10, 20, 30, 40, 50.
In the second sample, the optimal order is: 100, 200, 100, 200.
|
[{"input": "5\n20 30 10 50 40", "output": "4"}, {"input": "4\n200 100 100 200", "output": "2"}]
| 1,200
|
["greedy", "sortings"]
| 31
|
[{"input": "5\r\n20 30 10 50 40\r\n", "output": "4\r\n"}, {"input": "4\r\n200 100 100 200\r\n", "output": "2\r\n"}, {"input": "10\r\n2 2 2 2 2 2 2 2 2 2\r\n", "output": "0\r\n"}, {"input": "1\r\n1000\r\n", "output": "0\r\n"}, {"input": "2\r\n444 333\r\n", "output": "1\r\n"}, {"input": "100\r\n9 9 72 55 14 8 55 58 35 67 3 18 73 92 41 49 15 60 18 66 9 26 97 47 43 88 71 97 19 34 48 96 79 53 8 24 69 49 12 23 77 12 21 88 66 9 29 13 61 69 54 77 41 13 4 68 37 74 7 6 29 76 55 72 89 4 78 27 29 82 18 83 12 4 32 69 89 85 66 13 92 54 38 5 26 56 17 55 29 4 17 39 29 94 3 67 85 98 21 14\r\n", "output": "95\r\n"}, {"input": "1\r\n995\r\n", "output": "0\r\n"}, {"input": "10\r\n103 101 103 103 101 102 100 100 101 104\r\n", "output": "7\r\n"}, {"input": "20\r\n102 100 102 104 102 101 104 103 100 103 105 105 100 105 100 100 101 105 105 102\r\n", "output": "15\r\n"}, {"input": "20\r\n990 994 996 999 997 994 990 992 990 993 992 990 999 999 992 994 997 990 993 998\r\n", "output": "15\r\n"}, {"input": "100\r\n1 8 3 8 10 8 5 3 10 3 5 8 4 5 5 5 10 3 6 6 6 6 6 7 2 7 2 4 7 8 3 8 7 2 5 6 1 5 5 7 9 7 6 9 1 8 1 3 6 5 1 3 6 9 5 6 8 4 8 6 10 9 2 9 3 8 7 5 2 10 2 10 3 6 5 5 3 5 10 2 3 7 10 8 8 4 3 4 9 6 10 7 6 6 6 4 9 9 8 9\r\n", "output": "84\r\n"}]
| false
|
stdio
| null | true
|
388/C
|
388
|
C
|
Python 3
|
TESTS
| 0
| 93
| 307,200
|
69243958
|
def main(args):
nPiles = int(input())
piles = []
for i in range (nPiles):
line = list(map(int, input().split(' ')))
piles.append(line[1:])
ciel = 0
jiro = 0
turn = 1
while piles:
if turn%2 != 0:
ciel += piles[0][-1]
del piles[0][-1]
turn += 1
if not piles[0]:
del piles[0]
elif turn%2 == 0:
jiro += piles[0][0]
del piles[0][0]
turn += 1
if not piles[0]:
del piles[0]
print(ciel, jiro)
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))
| 43
| 109
| 307,200
|
54056488
|
n = int(input())
c = [list(map(int, input().split())) for _ in range(n)]
a, b = 0, 0
d = []
for i in range(n):
if len(c[i]) % 2:
a += sum(c[i][1:c[i][0]//2+1])
b += sum(c[i][c[i][0]//2+1:])
else:
a += sum(c[i][1:c[i][0]//2+1])
b += sum(c[i][c[i][0]//2+2:])
d.append(c[i][c[i][0]//2+1])
d.sort(reverse=True)
print(a+sum(d[0::2]), b+sum(d[1::2]))
|
Codeforces Round 228 (Div. 1)
|
CF
| 2,014
| 1
| 256
|
Fox and Card Game
|
Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card.
The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.
Suppose Ciel and Jiro play optimally, what is the score of the game?
|
The first line contain an integer n (1 ≤ n ≤ 100). Each of the next n lines contains a description of the pile: the first integer in the line is si (1 ≤ si ≤ 100) — the number of cards in the i-th pile; then follow si positive integers c1, c2, ..., ck, ..., csi (1 ≤ ck ≤ 1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile.
|
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
| null |
In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.
In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3.
|
[{"input": "2\n1 100\n2 1 10", "output": "101 10"}, {"input": "1\n9 2 8 6 5 9 4 7 1 3", "output": "30 15"}, {"input": "3\n3 1 3 2\n3 5 4 6\n2 8 7", "output": "18 18"}, {"input": "3\n3 1000 1000 1000\n6 1000 1000 1000 1000 1000 1000\n5 1000 1000 1000 1000 1000", "output": "7000 7000"}]
| 2,000
|
["games", "greedy", "sortings"]
| 43
|
[{"input": "2\r\n1 100\r\n2 1 10\r\n", "output": "101 10\r\n"}, {"input": "1\r\n9 2 8 6 5 9 4 7 1 3\r\n", "output": "30 15\r\n"}, {"input": "3\r\n3 1 3 2\r\n3 5 4 6\r\n2 8 7\r\n", "output": "18 18\r\n"}, {"input": "3\r\n3 1000 1000 1000\r\n6 1000 1000 1000 1000 1000 1000\r\n5 1000 1000 1000 1000 1000\r\n", "output": "7000 7000\r\n"}, {"input": "1\r\n1 1\r\n", "output": "1 0\r\n"}, {"input": "5\r\n1 3\r\n1 2\r\n1 8\r\n1 1\r\n1 4\r\n", "output": "12 6\r\n"}, {"input": "3\r\n5 1 2 3 4 5\r\n4 1 2 3 4\r\n8 1 2 3 4 5 6 7 8\r\n", "output": "19 42\r\n"}, {"input": "5\r\n5 1 1 1 1 1\r\n4 1 1 1 1\r\n3 1 1 1\r\n2 1 1\r\n1 1\r\n", "output": "8 7\r\n"}, {"input": "6\r\n2 1 1\r\n2 2 2\r\n2 3 3\r\n2 4 4\r\n2 5 5\r\n2 6 6\r\n", "output": "21 21\r\n"}, {"input": "2\r\n2 200 1\r\n3 1 100 2\r\n", "output": "301 3\r\n"}, {"input": "2\r\n3 1 1000 2\r\n3 2 1 1\r\n", "output": "1003 4\r\n"}, {"input": "4\r\n3 1 5 100\r\n3 1 5 100\r\n3 100 1 1\r\n3 100 1 1\r\n", "output": "208 208\r\n"}]
| false
|
stdio
| null | true
|
613/B
|
613
|
B
|
PyPy 3
|
TESTS
| 4
| 78
| 0
|
117529605
|
from bisect import bisect_left,bisect_right
n,max_lim,cf,cm,cost=map(int,input().split())
arr=list(map(int,input().split()))
r_arr=arr[::]
arr.sort()
arr.append(max_lim)
pre_arr=[0]
count=1
for i in range(1,n):
pre_arr.append((arr[i]-arr[i-1]) * count + pre_arr[-1])
count+=1
pt=n
dec_cost=0
maxi=0
r_cost,col_mini,col_pt=0,0,0
while pt >= 0:
i=0
j=max_lim
dec_cost=max_lim-arr[pt]
cost -= dec_cost
if cost <= 0:
break
while i < j:
m=(i+j)//2
r_cost=cost
collect=bisect_right(arr,m)-1
collect=min(collect,pt-1)
if collect > 0:
r_cost-=(pre_arr[collect]+(m-arr[collect])*(collect+1))
if r_cost >0:
i=m+1
else:
j=m-1
if (pre_arr[bisect_right(arr,min(n,i))-1]+(i-arr[bisect_right(arr,min(n,i))-1])*(bisect_right(arr,min(n,i))-1+1)) > cost:
i-=1
if maxi < i* cm + (n - pt) * cf:
maxi=i* cm + (n - pt) * cf
col_mini=i
col_pt=n-pt
pt-=1
d={}
for i in range(n-col_pt,n):
if arr[i] in d:
d[arr[i]]+=1
else:
d[arr[i]]=1
print(maxi)
for i in range(n):
if r_arr[i] <= col_mini:
print(col_mini,end=' ')
else:
if r_arr[i] in d and d[r_arr[i]] >0:
print(max_lim,end=' ')
d[r_arr[i]]-=1
print()
| 35
| 920
| 20,684,800
|
197615001
|
f = lambda: map(int, input().split())
g = lambda: m - l * p[l - 1] + s[l]
n, A, x, y, m = f()
t = sorted((q, i) for i, q in enumerate(f()))
p = [q for q, i in t]
s = [0] * (n + 1)
for j in range(n): s[j + 1] = p[j] + s[j]
l = r = n
F = L = R = B = -1
while 1:
if p:
while l > r or g() < 0: l -= 1
b = min(p[l - 1] + g() // l, A)
else: b, l = A, 0
f = x * (n - r) + y * b
if F < f: F, L, R, B = f, l, r, b
if not p: break
m += p.pop() - A
r -= 1
if m < 0: break
print(F)
p = [(i, B if j < L else q if j < R else A) for j, (q, i) in enumerate(t)]
for i, q in sorted(p): print(q)
|
Codeforces Round 339 (Div. 1)
|
CF
| 2,016
| 2
| 256
|
Skills
|
Lesha plays the recently published new version of the legendary game hacknet. In this version character skill mechanism was introduced. Now, each player character has exactly n skills. Each skill is represented by a non-negative integer ai — the current skill level. All skills have the same maximum level A.
Along with the skills, global ranking of all players was added. Players are ranked according to the so-called Force. The Force of a player is the sum of the following values:
- The number of skills that a character has perfected (i.e., such that ai = A), multiplied by coefficient cf.
- The minimum skill level among all skills (min ai), multiplied by coefficient cm.
Now Lesha has m hacknetian currency units, which he is willing to spend. Each currency unit can increase the current level of any skill by 1 (if it's not equal to A yet). Help him spend his money in order to achieve the maximum possible value of the Force.
|
The first line of the input contains five space-separated integers n, A, cf, cm and m (1 ≤ n ≤ 100 000, 1 ≤ A ≤ 109, 0 ≤ cf, cm ≤ 1000, 0 ≤ m ≤ 1015).
The second line contains exactly n integers ai (0 ≤ ai ≤ A), separated by spaces, — the current levels of skills.
|
On the first line print the maximum value of the Force that the character can achieve using no more than m currency units.
On the second line print n integers a'i (ai ≤ a'i ≤ A), skill levels which one must achieve in order to reach the specified value of the Force, while using no more than m currency units. Numbers should be separated by spaces.
| null |
In the first test the optimal strategy is to increase the second skill to its maximum, and increase the two others by 1.
In the second test one should increase all skills to maximum.
|
[{"input": "3 5 10 1 5\n1 3 1", "output": "12\n2 5 2"}, {"input": "3 5 10 1 339\n1 3 1", "output": "35\n5 5 5"}]
| 1,900
|
["binary search", "brute force", "dp", "greedy", "sortings", "two pointers"]
| 35
|
[{"input": "3 5 10 1 5\r\n1 3 1\r\n", "output": "12\r\n2 5 2 \r\n"}, {"input": "3 5 10 1 339\r\n1 3 1\r\n", "output": "35\r\n5 5 5 \r\n"}, {"input": "2 6 0 1 4\r\n5 1\r\n", "output": "5\r\n5 5 \r\n"}, {"input": "1 1000000000 1000 1000 1000000000000000\r\n0\r\n", "output": "1000000001000\r\n1000000000 \r\n"}, {"input": "1 100 1 2 30\r\n1\r\n", "output": "62\r\n31 \r\n"}, {"input": "1 100 1 2 30\r\n71\r\n", "output": "201\r\n100 \r\n"}, {"input": "1 1000000000 1000 1000 1000000000000000\r\n1000000000\r\n", "output": "1000000001000\r\n1000000000 \r\n"}, {"input": "5 5 10 20 50\r\n0 0 0 0 0\r\n", "output": "150\r\n5 5 5 5 5 \r\n"}, {"input": "5 5 10 20 50\r\n3 3 3 3 3\r\n", "output": "150\r\n5 5 5 5 5 \r\n"}, {"input": "4 5 3 7 15\r\n4 3 3 1\r\n", "output": "47\r\n5 5 5 5 \r\n"}, {"input": "3 6 4 6 8\r\n6 4 5\r\n", "output": "48\r\n6 6 6 \r\n"}]
| false
|
stdio
| null | true
|
484/D
|
484
|
D
|
PyPy 3-64
|
TESTS
| 6
| 46
| 0
|
170278982
|
import sys
input = sys.stdin.buffer.readline
def process(A):
n = len(A)
A.sort()
answer = 0
i1 = 0
i2 = n-1
while i1 < i2:
answer+=(A[i2]-A[i1])
i1+=1
i2-=1
print(answer)
n = int(input())
A = [int(x) for x in input().split()]
process(A)
| 71
| 1,840
| 133,529,600
|
231875215
|
n=int(input())
a=input().split(' ')
a+=[0]
m1=int(a[0])
m2=-int(a[0])
f=[0]*n
for i in range(0,n):
f[i]=max(m1-int(a[i]),m2+int(a[i]))
m1=max(m1,f[i]+int(a[i+1]))
m2=max(m2,f[i]-int(a[i+1]))
print(f[n-1])
|
Codeforces Round 276 (Div. 1)
|
CF
| 2,014
| 2
| 256
|
Kindergarten
|
In a kindergarten, the children are being divided into groups. The teacher put the children in a line and associated each child with his or her integer charisma value. Each child should go to exactly one group. Each group should be a nonempty segment of consecutive children of a line. A group's sociability is the maximum difference of charisma of two children in the group (in particular, if the group consists of one child, its sociability equals a zero).
The teacher wants to divide the children into some number of groups in such way that the total sociability of the groups is maximum. Help him find this value.
|
The first line contains integer n — the number of children in the line (1 ≤ n ≤ 106).
The second line contains n integers ai — the charisma of the i-th child ( - 109 ≤ ai ≤ 109).
|
Print the maximum possible total sociability of all groups.
| null |
In the first test sample one of the possible variants of an division is following: the first three children form a group with sociability 2, and the two remaining children form a group with sociability 1.
In the second test sample any division leads to the same result, the sociability will be equal to 0 in each group.
|
[{"input": "5\n1 2 3 1 2", "output": "3"}, {"input": "3\n3 3 3", "output": "0"}]
| 2,400
|
["data structures", "dp", "greedy"]
| 71
|
[{"input": "5\r\n1 2 3 1 2\r\n", "output": "3\r\n"}, {"input": "3\r\n3 3 3\r\n", "output": "0\r\n"}, {"input": "1\r\n0\r\n", "output": "0\r\n"}, {"input": "2\r\n-1000000000 1000000000\r\n", "output": "2000000000\r\n"}, {"input": "4\r\n1 4 2 3\r\n", "output": "4\r\n"}, {"input": "4\r\n23 5 7 1\r\n", "output": "24\r\n"}, {"input": "4\r\n23 7 5 1\r\n", "output": "22\r\n"}, {"input": "8\r\n23 2 7 5 15 8 4 10\r\n", "output": "37\r\n"}, {"input": "8\r\n4 5 3 6 2 7 1 8\r\n", "output": "16\r\n"}]
| false
|
stdio
| null | true
|
30/C
|
30
|
C
|
Python 3
|
TESTS
| 7
| 186
| 0
|
51459232
|
I = lambda : map(float , input().split())
n = int(input())
li = sorted ((list(I()) for i in range(n) ) ,key = lambda x : x[2] )
#print(li)
d = {}
for i in range (n) :
x1 = li[i][0] ; y1 = li[i][1] ; t1 = li[i][2] ; p1 = li[i][3] ;
#print(p1)
d[i] = p1
for j in range (i) :
x = li[j][0] ; y = li[j][1] ; t = li[j][2] ; p = li[j][3] ;
if ((y1-y)**2 + (x1-x)**2 )**(0.5) <= t1-t :
d[i] = max(d[i],d[j]+p1)
else :
d[i] = max(d[i],d[j])
print(d[n-1])
| 50
| 218
| 2,252,800
|
162584254
|
import sys
input = sys.stdin.readline
from operator import itemgetter
n=int(input())
T=[tuple(map(float,input().split())) for i in range(n)]
T.sort(key=itemgetter(2))
DP=[0]*n
for i in range(n):
x,y,t,p=T[i]
DP[i]=p
for j in range(i):
x2,y2,t2,p2=T[j]
if (x2-x)**2+(y2-y)**2<=(t2-t)**2:
DP[i]=max(DP[i],DP[j]+p)
print(max(DP))
|
Codeforces Beta Round 30 (Codeforces format)
|
CF
| 2,010
| 2
| 256
|
Shooting Gallery
|
One warm and sunny day king Copa decided to visit the shooting gallery, located at the Central Park, and try to win the main prize — big pink plush panda. The king is not good at shooting, so he invited you to help him.
The shooting gallery is an infinite vertical plane with Cartesian coordinate system on it. The targets are points on this plane. Each target is described by it's coordinates xi, and yi, by the time of it's appearance ti and by the number pi, which gives the probability that Copa hits this target if he aims at it.
A target appears and disappears instantly, so Copa can hit the target only if at the moment ti his gun sight aimed at (xi, yi). Speed of movement of the gun sight on the plane is equal to 1. Copa knows all the information about the targets beforehand (remember, he is a king!). He wants to play in the optimal way, which maximizes the expected value of the amount of hit targets. He can aim at any target at the moment 0.
|
The first line contains integer n (1 ≤ n ≤ 1000) — amount of targets in the shooting gallery. Then n lines follow, each describing one target. Each description consists of four numbers xi, yi, ti, pi (where xi, yi, ti — integers, - 1000 ≤ xi, yi ≤ 1000, 0 ≤ ti ≤ 109, real number pi is given with no more than 6 digits after the decimal point, 0 ≤ pi ≤ 1). No two targets may be at the same point.
|
Output the maximum expected value of the amount of targets that was shot by the king. Your answer will be accepted if it differs from the correct answer by not more than 10 - 6.
| null | null |
[{"input": "1\n0 0 0 0.5", "output": "0.5000000000"}, {"input": "2\n0 0 0 0.6\n5 0 5 0.7", "output": "1.3000000000"}]
| 1,800
|
["dp", "probabilities"]
| 50
|
[{"input": "1\r\n0 0 0 0.5\r\n", "output": "0.5000000000\r\n"}, {"input": "2\r\n0 0 0 0.6\r\n5 0 5 0.7\r\n", "output": "1.3000000000\r\n"}, {"input": "1\r\n-5 2 3 0.886986\r\n", "output": "0.8869860000\r\n"}, {"input": "4\r\n10 -7 14 0.926305\r\n-7 -8 12 0.121809\r\n-7 7 14 0.413446\r\n3 -8 6 0.859061\r\n", "output": "1.7853660000\r\n"}, {"input": "5\r\n-2 -2 34 0.127276\r\n5 -5 4 0.459998\r\n10 3 15 0.293766\r\n1 -3 7 0.089869\r\n-4 -7 11 0.772515\r\n", "output": "0.8997910000\r\n"}, {"input": "5\r\n2 5 1 0.955925\r\n9 -9 14 0.299977\r\n0 1 97 0.114582\r\n-4 -2 66 0.561033\r\n0 -10 75 0.135937\r\n", "output": "1.7674770000\r\n"}, {"input": "10\r\n-4 7 39 0.921693\r\n3 -1 50 0.111185\r\n-2 -8 27 0.976475\r\n-9 -2 25 0.541029\r\n6 -4 21 0.526054\r\n-7 2 19 0.488637\r\n-6 -5 50 0.819011\r\n-7 3 39 0.987596\r\n-3 -8 16 0.685997\r\n4 10 1 0.246686\r\n", "output": "3.0829590000\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(output_path, 'r') as f:
correct_line = f.readline().strip()
try:
correct = float(correct_line)
except:
print(0)
return
with open(submission_path, 'r') as f:
sub_line = f.readline().strip()
try:
sub = float(sub_line)
except:
print(0)
return
if abs(correct - sub) <= 1e-6 + 1e-12: # Adding small epsilon for floating point errors
print(1)
else:
print(0)
if __name__ == "__main__":
main()
| true
|
746/E
|
746
|
E
|
PyPy 3-64
|
TESTS
| 8
| 187
| 17,408,000
|
162953459
|
import sys
from array import array
def solve():
global ans
mem[a[i]] -= 1
par = parity[1] < parity[0]
while be[par] <= m and be[par] in mem:
be[par] += 2
if be[par] > m:
exit(print(-1))
a[i] = be[par]
ans += 1
mem[a[i]] = 1
parity[par] += 1
input = lambda: sys.stdin.buffer.readline().decode().strip()
n, m = map(int, input().split())
a = array('i', [int(x) for x in input().split()])
mem, parity = dict(), [0, 0]
be, ans = [2, 1], 0
for i in a:
if i not in mem:
mem[i] = 0
parity[i & 1] += 1
mem[i] += 1
for i in range(n):
if mem[a[i]] > 1:
solve()
for i in range(n):
if a[i] & 1 == parity[1] > parity[0] or a[i] & 1 == parity[1] >= parity[0]:
solve()
parity[parity[1] > parity[0]] -= 1
if parity[0] != parity[1]:
exit(print(-1))
print(f"{ans}\n{' '.join(map(str, a))}")
| 19
| 374
| 34,406,400
|
191738808
|
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n, m = map(int, input().split())
a = list(map(int, input().split()))
s = set(a)
x = [[] for _ in range(2)]
for i in range(1, min(m, 6 * n) + 1):
if not i in s:
x[i % 2].append(i)
n2 = n // 2
c = [n2] * 2
s = set()
for i in range(n):
ai = a[i]
if ai in s or not c[ai % 2]:
a[i] = 0
else:
c[ai % 2] -= 1
s.add(ai)
if len(x[0]) < c[0] or len(x[1]) < c[1]:
ans = -1
print(ans)
exit()
ans = c[0] + c[1]
for i in range(n):
if a[i]:
continue
for j in range(2):
if c[j]:
a[i] = x[j].pop()
c[j] -= 1
break
print(ans)
sys.stdout.write(" ".join(map(str, a)))
|
Codeforces Round 386 (Div. 2)
|
CF
| 2,016
| 1
| 256
|
Numbers Exchange
|
Eugeny has n cards, each of them has exactly one integer written on it. Eugeny wants to exchange some cards with Nikolay so that the number of even integers on his cards would equal the number of odd integers, and that all these numbers would be distinct.
Nikolay has m cards, distinct numbers from 1 to m are written on them, one per card. It means that Nikolay has exactly one card with number 1, exactly one card with number 2 and so on.
A single exchange is a process in which Eugeny gives one card to Nikolay and takes another one from those Nikolay has. Your task is to find the minimum number of card exchanges and determine which cards Eugeny should exchange.
|
The first line contains two integers n and m (2 ≤ n ≤ 2·105, 1 ≤ m ≤ 109) — the number of cards Eugeny has and the number of cards Nikolay has. It is guaranteed that n is even.
The second line contains a sequence of n positive integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the numbers on Eugeny's cards.
|
If there is no answer, print -1.
Otherwise, in the first line print the minimum number of exchanges. In the second line print n integers — Eugeny's cards after all the exchanges with Nikolay. The order of cards should coincide with the card's order in the input data. If the i-th card wasn't exchanged then the i-th number should coincide with the number from the input data. Otherwise, it is considered that this card was exchanged, and the i-th number should be equal to the number on the card it was exchanged to.
If there are multiple answers, it is allowed to print any of them.
| null | null |
[{"input": "6 2\n5 6 7 9 4 5", "output": "1\n5 6 7 9 4 2"}, {"input": "8 6\n7 7 7 7 8 8 8 8", "output": "6\n7 2 4 6 8 1 3 5"}, {"input": "4 1\n4 2 1 10", "output": "-1"}]
| 1,900
|
["greedy", "implementation", "math"]
| 19
|
[{"input": "6 2\r\n5 6 7 9 4 5\r\n", "output": "1\r\n5 6 7 9 4 2 \r\n"}, {"input": "8 6\r\n7 7 7 7 8 8 8 8\r\n", "output": "6\r\n7 2 4 6 8 1 3 5 \r\n"}, {"input": "4 1\r\n4 2 1 10\r\n", "output": "-1\r\n"}, {"input": "10 10\r\n12 13 10 20 13 10 19 15 21 11\r\n", "output": "2\r\n12 13 10 20 2 4 19 15 21 11 \r\n"}, {"input": "20 16\r\n23 27 17 29 23 21 24 23 19 25 16 24 20 17 18 17 16 17 28 17\r\n", "output": "8\r\n23 27 17 29 2 21 24 4 19 25 16 6 20 8 18 10 1 3 28 5 \r\n"}, {"input": "30 40\r\n26 22 10 20 29 18 38 11 41 8 33 37 37 3 14 4 3 9 21 38 27 27 7 7 33 12 39 37 17 5\r\n", "output": "7\r\n26 22 10 20 29 18 38 11 41 8 33 37 2 3 14 4 6 9 21 16 27 24 7 28 1 12 39 13 17 5 \r\n"}, {"input": "100 20\r\n28 42 37 40 26 40 46 46 25 28 36 36 35 38 45 40 21 38 36 22 30 24 40 38 27 50 47 40 30 45 39 20 18 32 34 24 34 26 27 37 18 40 42 41 26 50 22 27 37 21 30 30 49 36 16 48 46 26 33 22 47 32 38 50 29 46 31 42 26 24 50 26 40 42 26 34 18 32 44 24 16 32 50 30 20 48 26 41 32 30 32 41 30 24 18 32 49 23 20 44\r\n", "output": "-1\r\n"}, {"input": "2 1\r\n2 4\r\n", "output": "1\r\n2 1 \r\n"}, {"input": "2 1000000000\r\n1 1\r\n", "output": "1\r\n1 2 \r\n"}]
| false
|
stdio
|
import sys
def read_file(path):
with open(path, 'r') as f:
lines = [line.strip() for line in f.readlines() if line.strip()]
return lines
def main(input_path, correct_output_path, submission_output_path):
input_lines = read_file(input_path)
correct_lines = read_file(correct_output_path)
submission_lines = read_file(submission_output_path)
# Parse input
n, m = map(int, input_lines[0].split())
a = list(map(int, input_lines[1].split()))
# Parse correct output
correct_k = -1
correct_list = None
if correct_lines[0] == '-1':
correct_k = -1
else:
correct_k = int(correct_lines[0])
correct_list = list(map(int, correct_lines[1].split()))
# Parse submission output
if not submission_lines:
print(0)
return
sub_k_line = submission_lines[0].strip()
if sub_k_line == '-1':
sub_k = -1
sub_list = None
else:
if len(submission_lines) < 2:
print(0)
return
try:
sub_k = int(sub_k_line)
sub_list = list(map(int, submission_lines[1].split()))
except:
print(0)
return
if len(sub_list) != n:
print(0)
return
# Check if correct is -1
if correct_k == -1:
if sub_k == -1 and len(submission_lines) == 1:
print(1)
else:
print(0)
return
# Check submission has correct k
if sub_k != correct_k or len(submission_lines) != 2:
print(0)
return
# Check sub_list validity
if len(sub_list) != n:
print(0)
return
# All numbers must be unique
if len(set(sub_list)) != n:
print(0)
return
# Even and odd counts must be equal
evens = sum(1 for x in sub_list if x % 2 == 0)
if evens != n // 2:
print(0)
return
# Exchanged count must match k
exchanged = sum(1 for i in range(n) if a[i] != sub_list[i])
if exchanged != sub_k:
print(0)
return
# Check exchanged numbers are from Nikolay and unique among themselves
nikolay_nums = []
for i in range(n):
if a[i] == sub_list[i]:
continue
num = sub_list[i]
if num < 1 or num > m:
print(0)
return
nikolay_nums.append(num)
if len(set(nikolay_nums)) != len(nikolay_nums):
print(0)
return
print(1)
if __name__ == '__main__':
input_path = sys.argv[1]
correct_output_path = sys.argv[2]
submission_output_path = sys.argv[3]
main(input_path, correct_output_path, submission_output_path)
| true
|
298/A
|
298
|
A
|
PyPy 3
|
TESTS
| 4
| 280
| 0
|
77177368
|
n=int(input())
x=input()
a=list(x)
s=0
t=0
l=[]
r=[]
for i in range(n):
if a[i]=="R":
r.append(i+1)
if a[i]=="L":
l.append(i+1)
if len(l)==0:
s=max(r)
t=s+1
elif len(r)==0:
s=min(l)
t=s-1
else:
if a.count("R")==a.count("L"):
s=a.index("R")+1
t=s+1
elif a.count("L")>a.count("R"):
s=a.index("L")+abs(a.count("L")-a.count("R"))+1
t=s-abs(a.count("L")-a.count("R"))
else:
s=a.index("L")-1-abs(a.count("L")-a.count("R"))
t=s+abs(a.count("L")-a.count("R"))
print(s,t)
| 23
| 92
| 0
|
4724497
|
n = int(input())
t = input()
r, l = t.find('R'), t.find('L')
if r > 0:
if l > 0: print(r + 1, l)
else: print(r + 1, r + t[r:].find('.') + 1)
else: print(l + t[l:].find('.'), l)
|
Codeforces Round 180 (Div. 2)
|
CF
| 2,013
| 1
| 256
|
Snow Footprints
|
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.
At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
|
The first line of the input contains integer n (3 ≤ n ≤ 1000).
The second line contains the description of the road — the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
|
Print two space-separated integers — the values of s and t. If there are several possible solutions you can print any of them.
| null |
The first test sample is the one in the picture.
|
[{"input": "9\n..RRLL...", "output": "3 4"}, {"input": "11\n.RRRLLLLL..", "output": "7 5"}]
| 1,300
|
["greedy", "implementation"]
| 23
|
[{"input": "9\r\n..RRLL...\r\n", "output": "3 4\r\n"}, {"input": "11\r\n.RRRLLLLL..\r\n", "output": "7 5\r\n"}, {"input": "17\r\n.......RRRRR.....\r\n", "output": "12 13\r\n"}, {"input": "13\r\n....LLLLLL...\r\n", "output": "10 4\r\n"}, {"input": "4\r\n.RL.\r\n", "output": "3 2\r\n"}, {"input": "3\r\n.L.\r\n", "output": "2 1\r\n"}, {"input": "3\r\n.R.\r\n", "output": "2 3\r\n"}]
| false
|
stdio
|
import sys
from collections import deque
def main(input_path, output_path, submission_path):
with open(input_path) as f:
n = int(f.readline().strip())
road = f.readline().strip()
with open(submission_path) as f:
line = f.readline().strip()
try:
s, t = map(int, line.split())
except:
print(0)
return
if s < 1 or s > n or t < 1 or t > n:
print(0)
return
adj = [[] for _ in range(n+1)]
for i in range(1, n+1):
c = road[i-1]
if c == 'R':
if i+1 <= n:
adj[i].append(i+1)
elif c == 'L':
if i-1 >= 1:
adj[i].append(i-1)
visited = [False] * (n+1)
q = deque([s])
visited[s] = True
while q:
u = q.popleft()
if u == t:
print(1)
return
for v in adj[u]:
if not visited[v]:
visited[v] = True
q.append(v)
print(0)
if __name__ == "__main__":
input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3]
main(input_path, output_path, submission_path)
| true
|
298/A
|
298
|
A
|
Python 3
|
TESTS
| 3
| 62
| 0
|
232566637
|
n,rl=int(input()),input()
if "L" not in rl:print(rl.find("R")+1,rl.find(".",rl.find("R"))+1)
elif "R" not in rl:print(rl.find(".",rl.find("L")),rl.find("L")+1)
else:print(rl.find("R")+1,rl.find("L"))
| 23
| 92
| 0
|
142945428
|
n=int(input())
s=input()
l=n-s[::-1].find('L')
r=s.find('R')+1
if(r):
print(r,r+s.count('R'))
else:
print(l,l-s.count('L'))
|
Codeforces Round 180 (Div. 2)
|
CF
| 2,013
| 1
| 256
|
Snow Footprints
|
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.
At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
|
The first line of the input contains integer n (3 ≤ n ≤ 1000).
The second line contains the description of the road — the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
|
Print two space-separated integers — the values of s and t. If there are several possible solutions you can print any of them.
| null |
The first test sample is the one in the picture.
|
[{"input": "9\n..RRLL...", "output": "3 4"}, {"input": "11\n.RRRLLLLL..", "output": "7 5"}]
| 1,300
|
["greedy", "implementation"]
| 23
|
[{"input": "9\r\n..RRLL...\r\n", "output": "3 4\r\n"}, {"input": "11\r\n.RRRLLLLL..\r\n", "output": "7 5\r\n"}, {"input": "17\r\n.......RRRRR.....\r\n", "output": "12 13\r\n"}, {"input": "13\r\n....LLLLLL...\r\n", "output": "10 4\r\n"}, {"input": "4\r\n.RL.\r\n", "output": "3 2\r\n"}, {"input": "3\r\n.L.\r\n", "output": "2 1\r\n"}, {"input": "3\r\n.R.\r\n", "output": "2 3\r\n"}]
| false
|
stdio
|
import sys
from collections import deque
def main(input_path, output_path, submission_path):
with open(input_path) as f:
n = int(f.readline().strip())
road = f.readline().strip()
with open(submission_path) as f:
line = f.readline().strip()
try:
s, t = map(int, line.split())
except:
print(0)
return
if s < 1 or s > n or t < 1 or t > n:
print(0)
return
adj = [[] for _ in range(n+1)]
for i in range(1, n+1):
c = road[i-1]
if c == 'R':
if i+1 <= n:
adj[i].append(i+1)
elif c == 'L':
if i-1 >= 1:
adj[i].append(i-1)
visited = [False] * (n+1)
q = deque([s])
visited[s] = True
while q:
u = q.popleft()
if u == t:
print(1)
return
for v in adj[u]:
if not visited[v]:
visited[v] = True
q.append(v)
print(0)
if __name__ == "__main__":
input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3]
main(input_path, output_path, submission_path)
| true
|
384/B
|
384
|
B
|
Python 3
|
TESTS
| 1
| 31
| 0
|
162931539
|
nmk = [int(i) for i in input().split()]
n = nmk[0]
m = nmk[1]
k = nmk[2]
g = []
ans = []
anslen = 0
for z in range(n):
g.append([y for y in input().split()])
ans.append([])
if k == 0:
for i in range(m):
for j in range(i+1, m):
if g[z][i] > g[z][j]:
g[z][i], g[z][j] = g[z][j], g[z][i]
ans[z].append([])
try:
ans[z][len(ans[z])-1].index([i + 1, j + 1])
except:
ans[z][len(ans[z])-1].append([i + 1, j + 1])
anslen += 1
else:
for i in range(m):
for j in range(i+1, m):
if g[z][i] < g[z][j]:
g[z][i], g[z][j] = g[z][j], g[z][i]
ans[z].append([])
try:
ans[z][len(ans[z])-1].index([i + 1, j + 1])
except:
ans[z][len(ans[z])-1].append([i + 1, j + 1])
print(anslen)
for z in range(n):
for i in range(len(ans[z])):
for j in range(len(ans[z][i])):
print(*ans[z][i][j])
# Tue Jul 05 2022 19:21:31 GMT+0000 (Coordinated Universal Time)
| 31
| 78
| 409,600
|
5775338
|
n, m, k = map(int, input().split())
print(str(m * (m - 1) // 2))
for i in range(1, m):
for j in range(i + 1, m + 1):
if k == 0:
print (str(i) + " " + str(j))
else:
print(str(j) + " " + str(i))
|
Codeforces Round 225 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Multitasking
|
Iahub wants to enhance his multitasking abilities. In order to do this, he wants to sort n arrays simultaneously, each array consisting of m integers.
Iahub can choose a pair of distinct indices i and j (1 ≤ i, j ≤ m, i ≠ j). Then in each array the values at positions i and j are swapped only if the value at position i is strictly greater than the value at position j.
Iahub wants to find an array of pairs of distinct indices that, chosen in order, sort all of the n arrays in ascending or descending order (the particular order is given in input). The size of the array can be at most $$\frac{m(m-1)}{2}$$ (at most $$\frac{m(m-1)}{2}$$ pairs). Help Iahub, find any suitable array.
|
The first line contains three integers n (1 ≤ n ≤ 1000), m (1 ≤ m ≤ 100) and k. Integer k is 0 if the arrays must be sorted in ascending order, and 1 if the arrays must be sorted in descending order. Each line i of the next n lines contains m integers separated by a space, representing the i-th array. For each element x of the array i, 1 ≤ x ≤ 106 holds.
|
On the first line of the output print an integer p, the size of the array (p can be at most $$\frac{m(m-1)}{2}$$). Each of the next p lines must contain two distinct integers i and j (1 ≤ i, j ≤ m, i ≠ j), representing the chosen indices.
If there are multiple correct answers, you can print any.
| null |
Consider the first sample. After the first operation, the arrays become [1, 3, 2, 5, 4] and [1, 2, 3, 4, 5]. After the second operation, the arrays become [1, 2, 3, 5, 4] and [1, 2, 3, 4, 5]. After the third operation they become [1, 2, 3, 4, 5] and [1, 2, 3, 4, 5].
|
[{"input": "2 5 0\n1 3 2 5 4\n1 4 3 2 5", "output": "3\n2 4\n2 3\n4 5"}, {"input": "3 2 1\n1 2\n2 3\n3 4", "output": "1\n2 1"}]
| 1,500
|
["greedy", "implementation", "sortings", "two pointers"]
| 31
|
[{"input": "2 5 0\r\n1 3 2 5 4\r\n1 4 3 2 5\r\n", "output": "3\r\n2 4\r\n2 3\r\n4 5\r\n"}, {"input": "3 2 1\r\n1 2\r\n2 3\r\n3 4\r\n", "output": "1\r\n2 1\r\n"}, {"input": "2 5 0\r\n836096 600367 472071 200387 79763\r\n714679 505282 233544 157810 152591\r\n", "output": "10\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n2 3\r\n2 4\r\n2 5\r\n3 4\r\n3 5\r\n4 5\r\n"}, {"input": "2 5 1\r\n331081 525217 574775 753333 840639\r\n225591 347017 538639 620341 994088\r\n", "output": "10\r\n2 1\r\n3 1\r\n4 1\r\n5 1\r\n3 2\r\n4 2\r\n5 2\r\n4 3\r\n5 3\r\n5 4\r\n"}, {"input": "1 1 0\r\n1\r\n", "output": "0\r\n"}, {"input": "1 1 1\r\n1\r\n", "output": "0\r\n"}, {"input": "2 1 0\r\n1\r\n2\r\n", "output": "0\r\n"}, {"input": "1 2 1\r\n2 1\r\n", "output": "1\r\n2 1\r\n"}, {"input": "2 2 0\r\n2 1\r\n3 1\r\n", "output": "1\r\n1 2\r\n"}, {"input": "2 2 0\r\n2 1\r\n1 3\r\n", "output": "1\r\n1 2\r\n"}, {"input": "2 2 1\r\n2 1\r\n3 1\r\n", "output": "1\r\n2 1\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:
lines = f.read().splitlines()
n, m, k = map(int, lines[0].split())
arrays = [list(map(int, line.split())) for line in lines[1:n+1]]
try:
with open(submission_path) as f:
sub_lines = f.read().splitlines()
except:
print(0)
return
if not sub_lines:
print(0)
return
try:
p = int(sub_lines[0].strip())
if p < 0 or p > m * (m-1) // 2:
print(0)
return
except:
print(0)
return
swaps = []
for line in sub_lines[1:p+1]:
parts = line.strip().split()
if len(parts) != 2:
print(0)
return
try:
i = int(parts[0])
j = int(parts[1])
except:
print(0)
return
if i < 1 or i > m or j < 1 or j > m or i == j:
print(0)
return
swaps.append( (i-1, j-1) )
for arr in arrays:
current = arr.copy()
for i, j in swaps:
if current[i] > current[j]:
current[i], current[j] = current[j], current[i]
valid = True
if k == 0:
for x in range(m-1):
if current[x] > current[x+1]:
valid = False
break
else:
for x in range(m-1):
if current[x] < current[x+1]:
valid = False
break
if not valid:
print(0)
return
print(1)
if __name__ == "__main__":
main()
| true
|
613/B
|
613
|
B
|
Python 3
|
TESTS
| 2
| 61
| 0
|
15396093
|
__author__ = 'abdujabbor'
import operator
def calculate_strength(aa, a, cf, cm):
c = 0
_min = aa[0]
for k, v in aa.items():
if v == a:
c += 1
if v < _min:
_min = v
return c * cf + _min * cm
n, a, cf, cm, m = [int(x) for x in input().split()]
aa = [int(x) for x in input().split()]
_dict = dict()
for i in range(len(aa)):
_dict[i] = aa[i]
sorted_dict = sorted(_dict.items(), key=operator.itemgetter(1), reverse=True)
while m >= 0:
updated = False
for i in range(len(sorted_dict)):
if _dict[sorted_dict[i][0]] < a and m > 0:
_dict[sorted_dict[i][0]] += 1
m -= 1
updated = True
if updated is False:
break
m -= 1
print(calculate_strength(_dict, a, cf, cm))
for k, v in _dict.items():
print(v, end=' ')
| 35
| 1,045
| 16,486,400
|
15377652
|
import itertools
import bisect
n, A, cf, cm, m = [int(x) for x in input().split()]
skills = [int(x) for x in input().split()]
sorted_skills = list(sorted((k, i) for i, k in enumerate(skills)))
bottom_lift = [0 for i in range(n)]
for i in range(1, n):
bottom_lift[i] = bottom_lift[i-1] + i * (sorted_skills[i][0] - sorted_skills[i-1][0])
root_lift = [0 for i in range(n+1)]
for i in range(1, n+1):
root_lift[i] = root_lift[i-1] + A - sorted_skills[n-i][0]
max_level = -1
for i in range(n+1):
money_left = m - root_lift[i]
if money_left < 0: break
k = min(bisect.bisect(bottom_lift, money_left), n-i)
money_left -= bottom_lift[k-1]
min_level = min(A, sorted_skills[k-1][0] + money_left//k) if k > 0 else A
level = cf*i + cm*min_level
if max_level < level:
max_level = level
argmax = i
argmax_min_level = min_level
argmax_k = k
ans = [0 for i in range(n)]
for i, skill in enumerate(sorted_skills):
if i < argmax_k:
ans[skill[1]] = argmax_min_level
elif i >= n - argmax:
ans[skill[1]] = A
else:
ans[skill[1]] = skill[0]
print(max_level)
for a in ans:
print(a, end = ' ')
|
Codeforces Round 339 (Div. 1)
|
CF
| 2,016
| 2
| 256
|
Skills
|
Lesha plays the recently published new version of the legendary game hacknet. In this version character skill mechanism was introduced. Now, each player character has exactly n skills. Each skill is represented by a non-negative integer ai — the current skill level. All skills have the same maximum level A.
Along with the skills, global ranking of all players was added. Players are ranked according to the so-called Force. The Force of a player is the sum of the following values:
- The number of skills that a character has perfected (i.e., such that ai = A), multiplied by coefficient cf.
- The minimum skill level among all skills (min ai), multiplied by coefficient cm.
Now Lesha has m hacknetian currency units, which he is willing to spend. Each currency unit can increase the current level of any skill by 1 (if it's not equal to A yet). Help him spend his money in order to achieve the maximum possible value of the Force.
|
The first line of the input contains five space-separated integers n, A, cf, cm and m (1 ≤ n ≤ 100 000, 1 ≤ A ≤ 109, 0 ≤ cf, cm ≤ 1000, 0 ≤ m ≤ 1015).
The second line contains exactly n integers ai (0 ≤ ai ≤ A), separated by spaces, — the current levels of skills.
|
On the first line print the maximum value of the Force that the character can achieve using no more than m currency units.
On the second line print n integers a'i (ai ≤ a'i ≤ A), skill levels which one must achieve in order to reach the specified value of the Force, while using no more than m currency units. Numbers should be separated by spaces.
| null |
In the first test the optimal strategy is to increase the second skill to its maximum, and increase the two others by 1.
In the second test one should increase all skills to maximum.
|
[{"input": "3 5 10 1 5\n1 3 1", "output": "12\n2 5 2"}, {"input": "3 5 10 1 339\n1 3 1", "output": "35\n5 5 5"}]
| 1,900
|
["binary search", "brute force", "dp", "greedy", "sortings", "two pointers"]
| 35
|
[{"input": "3 5 10 1 5\r\n1 3 1\r\n", "output": "12\r\n2 5 2 \r\n"}, {"input": "3 5 10 1 339\r\n1 3 1\r\n", "output": "35\r\n5 5 5 \r\n"}, {"input": "2 6 0 1 4\r\n5 1\r\n", "output": "5\r\n5 5 \r\n"}, {"input": "1 1000000000 1000 1000 1000000000000000\r\n0\r\n", "output": "1000000001000\r\n1000000000 \r\n"}, {"input": "1 100 1 2 30\r\n1\r\n", "output": "62\r\n31 \r\n"}, {"input": "1 100 1 2 30\r\n71\r\n", "output": "201\r\n100 \r\n"}, {"input": "1 1000000000 1000 1000 1000000000000000\r\n1000000000\r\n", "output": "1000000001000\r\n1000000000 \r\n"}, {"input": "5 5 10 20 50\r\n0 0 0 0 0\r\n", "output": "150\r\n5 5 5 5 5 \r\n"}, {"input": "5 5 10 20 50\r\n3 3 3 3 3\r\n", "output": "150\r\n5 5 5 5 5 \r\n"}, {"input": "4 5 3 7 15\r\n4 3 3 1\r\n", "output": "47\r\n5 5 5 5 \r\n"}, {"input": "3 6 4 6 8\r\n6 4 5\r\n", "output": "48\r\n6 6 6 \r\n"}]
| false
|
stdio
| null | true
|
1004/D
|
1004
|
D
|
PyPy 3
|
TESTS
| 3
| 951
| 64,614,400
|
110583728
|
def get(n,m,a,b,t):
freq=[0]*(t+1)
for i in range(n):
for j in range(m):
val=abs(i-a)+abs(j-b)
freq[val]+=1
return freq
t=int(input())
a=list(map(int,input().split()))
mx=max(a)
f=[0]*(t+1)
for i in a:
f[i]+=1
b=-1
for i in range(1,mx+1):
if f[i]==4*i:
b=i
break
n=1
a=-1
x=0
y=0
mila=False
while n*n<=t:
if t%n==0:
m=t//n
a=n+m-mx-b-2
x,y=n,m
if a>=0 and a<n and b>=0 and b<m and f==get(n,m,a,b,t):
mila=True
break
if a>=0 and a<m and b>=0 and b<n and f==get(n,m,b,a,t):
mila=True
a,b=b,a
break
n+=1
if not mila:
print(-1)
else:
print(x,y)
print(a+1,b+1)
| 47
| 514
| 107,110,400
|
223052621
|
import collections
dd = lambda: collections.defaultdict(int)
t = int(input())
cnt = [0] * t
for i in map(int, input().split()):
cnt[i] += 1
ds = [c - 2 * b + a for a, b, c in zip([0, 0] + cnt, [0] + cnt + [0], cnt + [0, 0])]
ds[0] -= 1; ds[1] -= 2; ds[2] -= 1;
def bop(D):
while D:
i = min(D)
if D[i]:
D[i] += 1
D[i+1] += 1
return i
else:
del D[i]
return 420
def f(D, x = [], y = []):
if len(D) > 12 or max(len(x), len(y)) > 2: return
if len(x) == len(y) == 2: yield x + y
a = bop(D)
E = D.copy()
for i in y: E[a+i] -= 1
yield from f(E, x + [a], y)
E = D.copy()
for i in x: E[i+a] -= 1
yield from f(E, x, y + [a])
def gen(s):
D = dd()
for i in s:
D[i] -= 1
D[i+1] -= 1
for i in s[:2]:
for j in s[2:]:
D[i+j] += 1
return {i: v for i, v in D.items() if v}
D = dd() | {i: v for i, v in enumerate(ds) if v}
for s in f(D.copy()):
if gen(s) == D:
a, b, c, d = s
print(a + b - 1, c + d - 1)
print(a, c)
break
else:
print(-1)
|
Codeforces Round 495 (Div. 2)
|
CF
| 2,018
| 2
| 256
|
Sonya and Matrix
|
Since Sonya has just learned the basics of matrices, she decided to play with them a little bit.
Sonya imagined a new type of matrices that she called rhombic matrices. These matrices have exactly one zero, while all other cells have the Manhattan distance to the cell containing the zero. The cells with equal numbers have the form of a rhombus, that is why Sonya called this type so.
The Manhattan distance between two cells ($$$x_1$$$, $$$y_1$$$) and ($$$x_2$$$, $$$y_2$$$) is defined as $$$|x_1 - x_2| + |y_1 - y_2|$$$. For example, the Manhattan distance between the cells $$$(5, 2)$$$ and $$$(7, 1)$$$ equals to $$$|5-7|+|2-1|=3$$$.
Example of a rhombic matrix.
Note that rhombic matrices are uniquely defined by $$$n$$$, $$$m$$$, and the coordinates of the cell containing the zero.
She drew a $$$n\times m$$$ rhombic matrix. She believes that you can not recreate the matrix if she gives you only the elements of this matrix in some arbitrary order (i.e., the sequence of $$$n\cdot m$$$ numbers). Note that Sonya will not give you $$$n$$$ and $$$m$$$, so only the sequence of numbers in this matrix will be at your disposal.
Write a program that finds such an $$$n\times m$$$ rhombic matrix whose elements are the same as the elements in the sequence in some order.
|
The first line contains a single integer $$$t$$$ ($$$1\leq t\leq 10^6$$$) — the number of cells in the matrix.
The second line contains $$$t$$$ integers $$$a_1, a_2, \ldots, a_t$$$ ($$$0\leq a_i< t$$$) — the values in the cells in arbitrary order.
|
In the first line, print two positive integers $$$n$$$ and $$$m$$$ ($$$n \times m = t$$$) — the size of the matrix.
In the second line, print two integers $$$x$$$ and $$$y$$$ ($$$1\leq x\leq n$$$, $$$1\leq y\leq m$$$) — the row number and the column number where the cell with $$$0$$$ is located.
If there are multiple possible answers, print any of them. If there is no solution, print the single integer $$$-1$$$.
| null |
You can see the solution to the first example in the legend. You also can choose the cell $$$(2, 2)$$$ for the cell where $$$0$$$ is located. You also can choose a $$$5\times 4$$$ matrix with zero at $$$(4, 2)$$$.
In the second example, there is a $$$3\times 6$$$ matrix, where the zero is located at $$$(2, 3)$$$ there.
In the third example, a solution does not exist.
|
[{"input": "20\n1 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4", "output": "4 5\n2 2"}, {"input": "18\n2 2 3 2 4 3 3 3 0 2 4 2 1 3 2 1 1 1", "output": "3 6\n2 3"}, {"input": "6\n2 1 0 2 1 2", "output": "-1"}]
| 2,300
|
["brute force", "constructive algorithms", "implementation"]
| 47
|
[{"input": "20\r\n1 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4\r\n", "output": "4 5\r\n2 2\r\n"}, {"input": "18\r\n2 2 3 2 4 3 3 3 0 2 4 2 1 3 2 1 1 1\r\n", "output": "3 6\r\n2 3\r\n"}, {"input": "6\r\n2 1 0 2 1 2\r\n", "output": "-1\r\n"}, {"input": "1\r\n0\r\n", "output": "1 1\r\n1 1\r\n"}, {"input": "7\r\n0 1 2 3 4 2 6\r\n", "output": "-1\r\n"}, {"input": "6\r\n0 0 0 0 0 0\r\n", "output": "-1\r\n"}, {"input": "4\r\n0 0 0 0\r\n", "output": "-1\r\n"}]
| false
|
stdio
|
import sys
from collections import defaultdict
def read_ints(file):
return list(map(int, file.read().split()))
def main(input_path, output_path, submission_output_path):
with open(input_path) as f:
input_lines = f.read().splitlines()
t = int(input_lines[0])
a = list(map(int, input_lines[1].split()))
a_counts = defaultdict(int)
for num in a:
a_counts[num] += 1
max_d = max(a) if a else 0
zero_count = a_counts.get(0, 0)
with open(submission_output_path) as f:
submission_lines = [line.strip() for line in f.readlines() if line.strip()]
if not submission_lines:
print(0)
return
if submission_lines[0] == '-1':
if zero_count != 1:
print(100)
return
factors = []
for i in range(1, int(t**0.5) + 1):
if t % i == 0:
factors.append((i, t // i))
if i != t // i:
factors.append((t // i, i))
seen = set()
unique_factors = []
for n, m in factors:
if (n, m) not in seen:
seen.add((n, m))
unique_factors.append((n, m))
solution_found = False
for n, m in unique_factors:
if (n + m - 2) < max_d:
continue
a_val = n + m - 2 - max_d
if a_val < 0:
continue
x_plus_y = a_val + 2
x_min = max(1, x_plus_y - m)
x_max = min(n, x_plus_y - 1)
if x_min > x_max:
continue
for x in range(x_min, x_max + 1):
y = x_plus_y - x
if y < 1 or y > m:
continue
expected_counts = defaultdict(int)
expected_counts[0] = 1
for i in range(1, n + 1):
for j in range(1, m + 1):
if i == x and j == y:
continue
d = abs(i - x) + abs(j - y)
expected_counts[d] += 1
valid = True
if expected_counts[0] != zero_count:
valid = False
for d, cnt in expected_counts.items():
if a_counts.get(d, 0) != cnt:
valid = False
break
for d in a_counts:
if d != 0 and a_counts[d] != expected_counts.get(d, 0):
valid = False
break
if valid:
solution_found = True
break
if solution_found:
break
if solution_found:
break
print(0 if solution_found else 100)
return
else:
if len(submission_lines) < 2:
print(0)
return
try:
n, m = map(int, submission_lines[0].split())
x, y = map(int, submission_lines[1].split())
except:
print(0)
return
if n * m != t or x < 1 or x > n or y < 1 or y > m:
print(0)
return
expected_counts = defaultdict(int)
expected_counts[0] = 1
for i in range(1, n + 1):
for j in range(1, m + 1):
if i == x and j == y:
continue
d = abs(i - x) + abs(j - y)
expected_counts[d] += 1
valid = True
if expected_counts[0] != zero_count:
valid = False
for d, cnt in expected_counts.items():
if a_counts.get(d, 0) != cnt:
valid = False
break
for d in a_counts:
if d != 0 and a_counts[d] != expected_counts.get(d, 0):
valid = False
break
print(100 if valid else 0)
return
if __name__ == "__main__":
input_path, output_path, submission_output_path = sys.argv[1:4]
main(input_path, output_path, submission_output_path)
| true
|
298/A
|
298
|
A
|
Python 3
|
TESTS
| 7
| 218
| 307,200
|
92173839
|
#
# Author: eloyhz
# Date: Sep/07/2020
#
if __name__ == '__main__':
n = int(input())
road = input()
s = t = None
for i in range(n - 1):
if road[i] == 'R' and road[i + 1] == 'L':
s = i
t = i + 1
break
if s == t == None:
right = True
if road.count('R') > 0:
s = road.find('R') + 1
else:
right = False
s = road.find('L') + 1
for t in range(s, n):
if road[t] == '.':
break
if not right:
s, t = t, s
t -= 1
else:
t += 1
print(s, t)
| 23
| 92
| 0
|
144176599
|
n = input()
p = input()
s = []
t = []
for i in range(int(n)):
if p[i] == "R":
s.append(i+1)
elif p[i] == "L":
t.append(i+1)
if len(s) != 0:
if len(t) != 0:
print(str(s[0]) + " " + str(s[-1]))
else:
print(str(s[0]) + " " + str(s[-1]+1))
else:
print(str(t[-1]) + " " + str(t[0] - 1))
|
Codeforces Round 180 (Div. 2)
|
CF
| 2,013
| 1
| 256
|
Snow Footprints
|
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.
At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in the t-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
|
The first line of the input contains integer n (3 ≤ n ≤ 1000).
The second line contains the description of the road — the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
|
Print two space-separated integers — the values of s and t. If there are several possible solutions you can print any of them.
| null |
The first test sample is the one in the picture.
|
[{"input": "9\n..RRLL...", "output": "3 4"}, {"input": "11\n.RRRLLLLL..", "output": "7 5"}]
| 1,300
|
["greedy", "implementation"]
| 23
|
[{"input": "9\r\n..RRLL...\r\n", "output": "3 4\r\n"}, {"input": "11\r\n.RRRLLLLL..\r\n", "output": "7 5\r\n"}, {"input": "17\r\n.......RRRRR.....\r\n", "output": "12 13\r\n"}, {"input": "13\r\n....LLLLLL...\r\n", "output": "10 4\r\n"}, {"input": "4\r\n.RL.\r\n", "output": "3 2\r\n"}, {"input": "3\r\n.L.\r\n", "output": "2 1\r\n"}, {"input": "3\r\n.R.\r\n", "output": "2 3\r\n"}]
| false
|
stdio
|
import sys
from collections import deque
def main(input_path, output_path, submission_path):
with open(input_path) as f:
n = int(f.readline().strip())
road = f.readline().strip()
with open(submission_path) as f:
line = f.readline().strip()
try:
s, t = map(int, line.split())
except:
print(0)
return
if s < 1 or s > n or t < 1 or t > n:
print(0)
return
adj = [[] for _ in range(n+1)]
for i in range(1, n+1):
c = road[i-1]
if c == 'R':
if i+1 <= n:
adj[i].append(i+1)
elif c == 'L':
if i-1 >= 1:
adj[i].append(i-1)
visited = [False] * (n+1)
q = deque([s])
visited[s] = True
while q:
u = q.popleft()
if u == t:
print(1)
return
for v in adj[u]:
if not visited[v]:
visited[v] = True
q.append(v)
print(0)
if __name__ == "__main__":
input_path, output_path, submission_path = sys.argv[1], sys.argv[2], sys.argv[3]
main(input_path, output_path, submission_path)
| true
|
614/B
|
614
|
B
|
PyPy 3-64
|
TESTS
| 22
| 187
| 12,697,600
|
140157473
|
from math import log10
n = int(input())
arr = list(map(int, input().split()))
zeros = 0
num = 1
for i in range(n):
if arr[i] == 0:
num=0
zeros=0
break
if (log10(arr[i]))%1==0:
zeros += (int(log10(arr[i])))
else:
num = arr[i]
ans = str(num)+'0'*zeros
if ans.count('0') == len(ans):
ans = '0'
print(ans)
| 32
| 62
| 12,492,800
|
222637044
|
n = int(input())
numbers = list(map(str, input().split()))
# Initialize the product to 1
count = 0
n = '1'
# Count the number of '1's in each beautiful number
for num in numbers:
if num == '0':
break
# Convert the number to a string and count '1's
if "1"+"0"*(len(num)-1) == num:
count += len(num)-1
else:
n = num
if num == '0':
print("0")
else:
res= str(n)
count = int(count)
print(res + '0'*count)
|
Codeforces Round 339 (Div. 2)
|
CF
| 2,016
| 0.5
| 256
|
Gena's Code
|
It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, find their product. If it is turns to be too large, then the servers might have not enough time to assign tanks into teams and the whole game will collapse!
There are exactly n distinct countries in the world and the i-th country added ai tanks to the game. As the developers of the game are perfectionists, the number of tanks from each country is beautiful. A beautiful number, according to the developers, is such number that its decimal representation consists only of digits '1' and '0', moreover it contains at most one digit '1'. However, due to complaints from players, some number of tanks of one country was removed from the game, hence the number of tanks of this country may not remain beautiful.
Your task is to write the program that solves exactly the same problem in order to verify Gena's code correctness. Just in case.
|
The first line of the input contains the number of countries n (1 ≤ n ≤ 100 000). The second line contains n non-negative integers ai without leading zeroes — the number of tanks of the i-th country.
It is guaranteed that the second line contains at least n - 1 beautiful numbers and the total length of all these number's representations doesn't exceed 100 000.
|
Print a single number without leading zeroes — the product of the number of tanks presented by each country.
| null |
In sample 1 numbers 10 and 1 are beautiful, number 5 is not not.
In sample 2 number 11 is not beautiful (contains two '1's), all others are beautiful.
In sample 3 number 3 is not beautiful, all others are beautiful.
|
[{"input": "3\n5 10 1", "output": "50"}, {"input": "4\n1 1 10 11", "output": "110"}, {"input": "5\n0 3 1 100 1", "output": "0"}]
| 1,400
|
["implementation", "math"]
| 32
|
[{"input": "3\r\n5 10 1\r\n", "output": "50"}, {"input": "4\r\n1 1 10 11\r\n", "output": "110"}, {"input": "5\r\n0 3 1 100 1\r\n", "output": "0"}, {"input": "40\r\n10 100 10 1 10 10 100 10 10 100 10 100 100 10 1824868942 100 100 1 10 100 100 10 100 100 10 100 10 1 10 100 100 100 10 1 10 1 10 10 100 100\r\n", "output": "1824868942000000000000000000000000000000000000000000000000000"}, {"input": "6\r\n1000000000000000000000000000000000000 6643573784 1000000000000000000000000000000000000 1000000000000000000000000000000000000 1000000000000000000000000000000000000 1000000000000000000000000000000000000\r\n", "output": "6643573784000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"}, {"input": "1\r\n0\r\n", "output": "0"}, {"input": "1\r\n1\r\n", "output": "1"}, {"input": "1\r\n9\r\n", "output": "9"}, {"input": "2\r\n10 50\r\n", "output": "500"}, {"input": "3\r\n500 1 10\r\n", "output": "5000"}]
| false
|
stdio
| null | true
|
659/D
|
659
|
D
|
Python 3
|
TESTS
| 1
| 93
| 307,200
|
98591160
|
n=int(input())
dan = 0
for i in range(n+1):
if i==0:
xa,ya = map(int,input().split())
elif i==1:
xaa,yaa = map(int,input().split())
else:
x,y=map(int,input().split())
if (xaa>xa and yaa==ya) and (y>ya and x==xa):
dan+=1
if (xaa==xa and yaa<ya) and (y==ya and x<xa):
dan+=1
if (xaa>xa and yaa==ya) and (y<ya and x==xa):
dan+=1
if (xaa==xa and yaa<ya) and (y==ya and x>xa):
dan+=1
xaa,yaa=xa,ya
xa,ya = x,y
print(dan)
| 22
| 46
| 4,915,200
|
17055097
|
n = int(input())
points = []
ans = 0
for i in range(n):
x1,y1 = (int(i) for i in input().split())
points+=[[x1,y1]]
for i in range(n):
x1 = points[i][0]
y1 = points[i][1]
x2 = points[(i+1)%n][0]
y2 = points[(i+1)%n][1]
x3 = points[(i+2)%n][0]
y3 = points[(i+2)%n][1]
if y1 == y2:
if x2 > x1:
if y3 > y2 :
ans+=1
else:
if y3 < y2:
ans+=1
else:
if y2 > y1:
if x3 < x2:
ans+=1
else:
if x2 < x3:
ans+=1
print(ans)
|
Codeforces Round 346 (Div. 2)
|
CF
| 2,016
| 1
| 256
|
Bicycle Race
|
Maria participates in a bicycle race.
The speedway takes place on the shores of Lake Lucerne, just repeating its contour. As you know, the lake shore consists only of straight sections, directed to the north, south, east or west.
Let's introduce a system of coordinates, directing the Ox axis from west to east, and the Oy axis from south to north. As a starting position of the race the southernmost point of the track is selected (and if there are several such points, the most western among them). The participants start the race, moving to the north. At all straight sections of the track, the participants travel in one of the four directions (north, south, east or west) and change the direction of movement only in bends between the straight sections. The participants, of course, never turn back, that is, they do not change the direction of movement from north to south or from east to west (or vice versa).
Maria is still young, so she does not feel confident at some turns. Namely, Maria feels insecure if at a failed or untimely turn, she gets into the water. In other words, Maria considers the turn dangerous if she immediately gets into the water if it is ignored.
Help Maria get ready for the competition — determine the number of dangerous turns on the track.
|
The first line of the input contains an integer n (4 ≤ n ≤ 1000) — the number of straight sections of the track.
The following (n + 1)-th line contains pairs of integers (xi, yi) ( - 10 000 ≤ xi, yi ≤ 10 000). The first of these points is the starting position. The i-th straight section of the track begins at the point (xi, yi) and ends at the point (xi + 1, yi + 1).
It is guaranteed that:
- the first straight section is directed to the north;
- the southernmost (and if there are several, then the most western of among them) point of the track is the first point;
- the last point coincides with the first one (i.e., the start position);
- any pair of straight sections of the track has no shared points (except for the neighboring ones, they share exactly one point);
- no pair of points (except for the first and last one) is the same;
- no two adjacent straight sections are directed in the same direction or in opposite directions.
|
Print a single integer — the number of dangerous turns on the track.
| null |
The first sample corresponds to the picture:
The picture shows that you can get in the water under unfortunate circumstances only at turn at the point (1, 1). Thus, the answer is 1.
|
[{"input": "6\n0 0\n0 1\n1 1\n1 2\n2 2\n2 0\n0 0", "output": "1"}, {"input": "16\n1 1\n1 5\n3 5\n3 7\n2 7\n2 9\n6 9\n6 7\n5 7\n5 3\n4 3\n4 4\n3 4\n3 2\n5 2\n5 1\n1 1", "output": "6"}]
| 1,500
|
["geometry", "implementation", "math"]
| 22
|
[{"input": "6\r\n0 0\r\n0 1\r\n1 1\r\n1 2\r\n2 2\r\n2 0\r\n0 0\r\n", "output": "1\r\n"}, {"input": "16\r\n1 1\r\n1 5\r\n3 5\r\n3 7\r\n2 7\r\n2 9\r\n6 9\r\n6 7\r\n5 7\r\n5 3\r\n4 3\r\n4 4\r\n3 4\r\n3 2\r\n5 2\r\n5 1\r\n1 1\r\n", "output": "6\r\n"}, {"input": "4\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 -10000\r\n-10000 -10000\r\n", "output": "0\r\n"}, {"input": "4\r\n6 8\r\n6 9\r\n7 9\r\n7 8\r\n6 8\r\n", "output": "0\r\n"}, {"input": "8\r\n-10000 -10000\r\n-10000 5000\r\n0 5000\r\n0 10000\r\n10000 10000\r\n10000 0\r\n0 0\r\n0 -10000\r\n-10000 -10000\r\n", "output": "2\r\n"}, {"input": "20\r\n-4286 -10000\r\n-4286 -7778\r\n-7143 -7778\r\n-7143 -3334\r\n-10000 -3334\r\n-10000 1110\r\n-4286 1110\r\n-4286 -3334\r\n4285 -3334\r\n4285 -1112\r\n7142 -1112\r\n7142 3332\r\n4285 3332\r\n4285 9998\r\n9999 9998\r\n9999 -3334\r\n7142 -3334\r\n7142 -5556\r\n-1429 -5556\r\n-1429 -10000\r\n-4286 -10000\r\n", "output": "8\r\n"}, {"input": "24\r\n-10000 -10000\r\n-10000 9998\r\n9998 9998\r\n9998 -10000\r\n-6364 -10000\r\n-6364 6362\r\n6362 6362\r\n6362 -6364\r\n-2728 -6364\r\n-2728 2726\r\n2726 2726\r\n2726 -910\r\n908 -910\r\n908 908\r\n-910 908\r\n-910 -4546\r\n4544 -4546\r\n4544 4544\r\n-4546 4544\r\n-4546 -8182\r\n8180 -8182\r\n8180 8180\r\n-8182 8180\r\n-8182 -10000\r\n-10000 -10000\r\n", "output": "10\r\n"}, {"input": "12\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 6000\r\n-6000 6000\r\n-6000 2000\r\n10000 2000\r\n10000 -2000\r\n-6000 -2000\r\n-6000 -6000\r\n10000 -6000\r\n10000 -10000\r\n-10000 -10000\r\n", "output": "4\r\n"}, {"input": "12\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 6000\r\n-9800 6000\r\n-9800 2000\r\n10000 2000\r\n10000 -2000\r\n-9800 -2000\r\n-9800 -6000\r\n10000 -6000\r\n10000 -10000\r\n-10000 -10000\r\n", "output": "4\r\n"}, {"input": "4\r\n0 0\r\n0 10000\r\n10000 10000\r\n10000 0\r\n0 0\r\n", "output": "0\r\n"}, {"input": "4\r\n-10000 -10000\r\n-10000 10000\r\n10000 10000\r\n10000 -10000\r\n-10000 -10000\r\n", "output": "0\r\n"}]
| false
|
stdio
| null | true
|
651/B
|
651
|
B
|
PyPy 3-64
|
TESTS
| 5
| 62
| 2,048,000
|
161510050
|
n=int(input())
a=[int(x) for x in input().split()]
a.sort()
l,r=0,1
for i in range(n-1):
if a[i]==a[i+1]:
for j in range(i+2,n):
if a[j]>a[i+1]:
a[i+1],a[j]=a[j],a[i+1]
ans = 0
for i in range(1,n):
if a[i]>a[i-1]:
ans+=1
print(ans)
| 31
| 46
| 0
|
230732367
|
def maximum_number_of_indices(nums):
n = len(nums)
count_map = {}
for x in nums:
if x not in count_map:
count_map[x] = 0
count_map[x] += 1
max_count = 0
for k in count_map:
max_count = max(max_count, count_map[k])
return n - max_count
#t = int(input())
t = 1
for _ in range(t):
n = int(input())
A = list(map(int,input().split()))
print(maximum_number_of_indices(A))
|
Codeforces Round 345 (Div. 2)
|
CF
| 2,016
| 1
| 256
|
Beautiful Paintings
|
There are n pictures delivered for the new exhibition. The i-th painting has beauty ai. We know that a visitor becomes happy every time he passes from a painting to a more beautiful one.
We are allowed to arranged pictures in any order. What is the maximum possible number of times the visitor may become happy while passing all pictures from first to last? In other words, we are allowed to rearrange elements of a in any order. What is the maximum possible number of indices i (1 ≤ i ≤ n - 1), such that ai + 1 > ai.
|
The first line of the input contains integer n (1 ≤ n ≤ 1000) — the number of painting.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where ai means the beauty of the i-th painting.
|
Print one integer — the maximum possible number of neighbouring pairs, such that ai + 1 > ai, after the optimal rearrangement.
| null |
In the first sample, the optimal order is: 10, 20, 30, 40, 50.
In the second sample, the optimal order is: 100, 200, 100, 200.
|
[{"input": "5\n20 30 10 50 40", "output": "4"}, {"input": "4\n200 100 100 200", "output": "2"}]
| 1,200
|
["greedy", "sortings"]
| 31
|
[{"input": "5\r\n20 30 10 50 40\r\n", "output": "4\r\n"}, {"input": "4\r\n200 100 100 200\r\n", "output": "2\r\n"}, {"input": "10\r\n2 2 2 2 2 2 2 2 2 2\r\n", "output": "0\r\n"}, {"input": "1\r\n1000\r\n", "output": "0\r\n"}, {"input": "2\r\n444 333\r\n", "output": "1\r\n"}, {"input": "100\r\n9 9 72 55 14 8 55 58 35 67 3 18 73 92 41 49 15 60 18 66 9 26 97 47 43 88 71 97 19 34 48 96 79 53 8 24 69 49 12 23 77 12 21 88 66 9 29 13 61 69 54 77 41 13 4 68 37 74 7 6 29 76 55 72 89 4 78 27 29 82 18 83 12 4 32 69 89 85 66 13 92 54 38 5 26 56 17 55 29 4 17 39 29 94 3 67 85 98 21 14\r\n", "output": "95\r\n"}, {"input": "1\r\n995\r\n", "output": "0\r\n"}, {"input": "10\r\n103 101 103 103 101 102 100 100 101 104\r\n", "output": "7\r\n"}, {"input": "20\r\n102 100 102 104 102 101 104 103 100 103 105 105 100 105 100 100 101 105 105 102\r\n", "output": "15\r\n"}, {"input": "20\r\n990 994 996 999 997 994 990 992 990 993 992 990 999 999 992 994 997 990 993 998\r\n", "output": "15\r\n"}, {"input": "100\r\n1 8 3 8 10 8 5 3 10 3 5 8 4 5 5 5 10 3 6 6 6 6 6 7 2 7 2 4 7 8 3 8 7 2 5 6 1 5 5 7 9 7 6 9 1 8 1 3 6 5 1 3 6 9 5 6 8 4 8 6 10 9 2 9 3 8 7 5 2 10 2 10 3 6 5 5 3 5 10 2 3 7 10 8 8 4 3 4 9 6 10 7 6 6 6 4 9 9 8 9\r\n", "output": "84\r\n"}]
| false
|
stdio
| null | true
|
358/D
|
358
|
D
|
Python 3
|
TESTS
| 3
| 46
| 204,800
|
5056478
|
'''
Created on Nov 11, 2013
@author: Ismael
'''
import sys
def solve():
bbCum = lBB[0]
baCum = lBA[0]
abCum = lAB[0]
aaCum = lAA[0]
for i in range(1,len(lBB)):
m1 = max(baCum,aaCum)
m2 = max(bbCum,abCum)
bbCum = lBB[i] + m1
baCum = lBA[i] + m1
abCum = lAB[i] + m2
aaCum = lAA[i] + m2
res = max(bbCum,baCum,abCum,aaCum)
return res
def main():
global lBB
global lBA
global lAB
global lAA
f = sys.stdin
#f = open("input3.txt")
n = int(f.readline())
lBB = list(map(int,f.readline().split()))
lBA = list(map(int,f.readline().split()))
lAB = lBA
lAA = list(map(int,f.readline().split()))
#resExpected = int(f.readline())
res = solve()
print(res)
main()
| 29
| 62
| 7,577,600
|
131618904
|
n=int(input())
a=[int(v) for v in input().split()]
b=[int(v) for v in input().split()]
c=[int(v) for v in input().split()]
f=[[-100000000, -100000000] for v in range(3010)]
f[0][1]=0
for i in range(1,n+1):
f[i][1]=max(f[i-1][1] + b[i-1], f[i-1][0] + c[i-1])
f[i][0]=max(f[i-1][1] + a[i-1], f[i-1][0] + b[i-1])
print(f[n][0])
|
Codeforces Round 208 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Dima and Hares
|
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
|
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≤ ai, bi, ci ≤ 105.
Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number bi in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
|
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
| null | null |
[{"input": "4\n1 2 3 4\n4 3 2 1\n0 1 1 0", "output": "13"}, {"input": "7\n8 5 7 6 1 8 9\n2 7 9 5 4 3 1\n2 3 3 4 1 1 3", "output": "44"}, {"input": "3\n1 1 1\n1 2 1\n1 1 1", "output": "4"}]
| 1,800
|
["dp", "greedy"]
| 29
|
[{"input": "4\r\n1 2 3 4\r\n4 3 2 1\r\n0 1 1 0\r\n", "output": "13\r\n"}, {"input": "7\r\n8 5 7 6 1 8 9\r\n2 7 9 5 4 3 1\r\n2 3 3 4 1 1 3\r\n", "output": "44\r\n"}, {"input": "3\r\n1 1 1\r\n1 2 1\r\n1 1 1\r\n", "output": "4\r\n"}, {"input": "7\r\n1 3 8 9 3 4 4\r\n6 0 6 6 1 8 4\r\n9 6 3 7 8 8 2\r\n", "output": "42\r\n"}, {"input": "2\r\n3 5\r\n9 8\r\n4 0\r\n", "output": "14\r\n"}, {"input": "7\r\n3 6 1 5 4 2 0\r\n9 7 3 7 2 6 0\r\n1 6 5 7 5 4 1\r\n", "output": "37\r\n"}, {"input": "1\r\n0\r\n1\r\n4\r\n", "output": "0\r\n"}, {"input": "1\r\n7\r\n1\r\n7\r\n", "output": "7\r\n"}, {"input": "8\r\n7 3 3 5 9 9 8 1\r\n8 2 6 6 0 3 8 0\r\n1 2 5 0 9 4 7 8\r\n", "output": "49\r\n"}, {"input": "6\r\n1 2 0 1 6 4\r\n0 6 1 8 9 8\r\n4 1 4 3 9 8\r\n", "output": "33\r\n"}, {"input": "1\r\n0\r\n0\r\n0\r\n", "output": "0\r\n"}, {"input": "1\r\n100000\r\n100000\r\n100000\r\n", "output": "100000\r\n"}]
| false
|
stdio
| null | true
|
652/F
|
652
|
F
|
PyPy 3-64
|
TESTS
| 3
| 61
| 0
|
176110805
|
from sys import stdin
input=lambda :stdin.readline()[:-1]
n,l,t=map(int,input().split())
ants=[]
last=[]
xwi=[]
for i in range(n):
x,w=input().split()
x=int(x)
if w=='L':
w=2
else:
w=1
xwi.append((x,w,i))
xwi.sort(key=lambda x:x[0])
for i in range(n):
x,w=xwi[i][:2]
ants.append((x,w))
if w==1:
last.append(((x+t)%l,1,i))
else:
last.append(((x-t)%l,0,i))
ans=[]
if ants[0][1]==1:
last.sort()
for i in range(n):
if last[i][2]==0:
idx0=i
x0=ants[0][0]
cross=0
for x,w in ants[1:]:
if w==2:
cross+=(l-(x-x0)+2*t)//l
cross%=n
for i in range(n):
ans.append(last[(idx0-cross+i)%n][0])
else:
last.sort()
for i in range(n):
if last[i][2]==0:
idx0=i
x0=ants[0][0]
cross=0
for x,w in ants[1:]:
if w==1:
cross+=(x-x0+2*t)//l
cross%=n
for i in range(n):
ans.append(last[(idx0+cross+i)%n][0])
ANS=[]
for i in range(n):
res=ans[xwi[i][2]]
if res==0:
res=l
ANS.append(res)
print(*ANS)
| 30
| 888
| 55,091,200
|
193624502
|
import os
import sys
from io import BytesIO, IOBase
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")
n, l, t = map(int, input().split())
ind = 0
pos = []
P = []
for i in range(n):
x, w = input().split()
x = int(x)
x -= 1
P.append((x, i))
if w == 'R':
p = x + t
else:
p = x - t
ind -= p // l
ind %= n
pos.append(p % l)
P.sort(key=lambda x:x[0])
ans = [-1] * n
pos.sort()
for p in pos:
ans[ind] = p + 1
ind = (ind + 1) % n
ans2 = [-1] * n
for i, (_, j) in enumerate(P):
ans2[j] = ans[i]
print(*ans2)
|
Educational Codeforces Round 10
|
ICPC
| 2,016
| 2
| 256
|
Ants on a Circle
|
n ants are on a circle of length m. An ant travels one unit of distance per one unit of time. Initially, the ant number i is located at the position si and is facing in the direction di (which is either L or R). Positions are numbered in counterclockwise order starting from some point. Positions of the all ants are distinct.
All the ants move simultaneously, and whenever two ants touch, they will both switch their directions. Note that it is possible for an ant to move in some direction for a half of a unit of time and in opposite direction for another half of a unit of time.
Print the positions of the ants after t time units.
|
The first line contains three integers n, m and t (2 ≤ n ≤ 3·105, 2 ≤ m ≤ 109, 0 ≤ t ≤ 1018) — the number of ants, the length of the circle and the number of time units.
Each of the next n lines contains integer si and symbol di (1 ≤ si ≤ m and di is either L or R) — the position and the direction of the i-th ant at the start. The directions L and R corresponds to the clockwise and counterclockwise directions, respectively.
It is guaranteed that all positions si are distinct.
|
Print n integers xj — the position of the j-th ant after t units of time. The ants are numbered from 1 to n in order of their appearing in input.
| null | null |
[{"input": "2 4 8\n1 R\n3 L", "output": "1 3"}, {"input": "4 8 6\n6 R\n5 L\n1 R\n8 L", "output": "7 4 2 7"}, {"input": "4 8 2\n1 R\n5 L\n6 L\n8 R", "output": "3 3 4 2"}]
| 2,800
|
["constructive algorithms", "math"]
| 30
|
[{"input": "2 4 8\r\n1 R\r\n3 L\r\n", "output": "1 3\r\n"}, {"input": "4 8 6\r\n6 R\r\n5 L\r\n1 R\r\n8 L\r\n", "output": "7 4 2 7\r\n"}, {"input": "4 8 2\r\n1 R\r\n5 L\r\n6 L\r\n8 R\r\n", "output": "3 3 4 2\r\n"}, {"input": "10 10 90\r\n2 R\r\n1 R\r\n3 L\r\n4 R\r\n7 L\r\n8 L\r\n6 R\r\n9 R\r\n5 R\r\n10 L\r\n", "output": "10 9 1 2 5 6 4 7 3 8\r\n"}, {"input": "10 20 85\r\n6 L\r\n12 R\r\n2 L\r\n20 R\r\n18 L\r\n8 R\r\n16 R\r\n14 L\r\n10 L\r\n4 R\r\n", "output": "5 13 1 1 17 9 17 13 9 5\r\n"}, {"input": "10 20 59\r\n1 R\r\n15 L\r\n7 L\r\n13 R\r\n5 R\r\n17 R\r\n3 L\r\n9 R\r\n11 L\r\n19 L\r\n", "output": "20 16 8 12 4 16 4 8 12 20\r\n"}, {"input": "2 2 0\r\n2 L\r\n1 R\r\n", "output": "2 1\r\n"}, {"input": "2 2 0\r\n2 L\r\n1 R\r\n", "output": "2 1\r\n"}, {"input": "4 8 6\r\n6 R\r\n5 L\r\n1 R\r\n8 R\r\n", "output": "7 7 6 4\r\n"}]
| false
|
stdio
| null | true
|
651/B
|
651
|
B
|
PyPy 3-64
|
TESTS
| 5
| 61
| 0
|
137589890
|
def main_function():
n = int(input())
a = sorted([int(i) for i in input().split(" ")])
hash_a = {}
for i in a:
if i in hash_a:
hash_a[i] += 1
else:
hash_a[i] = 1
counter = 0
is_there_non_zero = True
while is_there_non_zero:
internal_counter = -1
for i in hash_a:
if hash_a[i] > 0:
internal_counter += 1
hash_a[i] -= 1
counter += internal_counter
#print(hash_a)
for i in hash_a:
if hash_a[i] == 0:
is_there_non_zero = False
break
print(counter)
main_function()
| 31
| 46
| 102,400
|
132050929
|
from collections import Counter
no=int(input())
List=list(map(int,input().split()))
dic=Counter(List)
ans = 0
while dic:
ans += len(dic)-1
for i in list(dic):
dic[i] -= 1
if not dic[i]:
del dic[i]
print(ans)
|
Codeforces Round 345 (Div. 2)
|
CF
| 2,016
| 1
| 256
|
Beautiful Paintings
|
There are n pictures delivered for the new exhibition. The i-th painting has beauty ai. We know that a visitor becomes happy every time he passes from a painting to a more beautiful one.
We are allowed to arranged pictures in any order. What is the maximum possible number of times the visitor may become happy while passing all pictures from first to last? In other words, we are allowed to rearrange elements of a in any order. What is the maximum possible number of indices i (1 ≤ i ≤ n - 1), such that ai + 1 > ai.
|
The first line of the input contains integer n (1 ≤ n ≤ 1000) — the number of painting.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where ai means the beauty of the i-th painting.
|
Print one integer — the maximum possible number of neighbouring pairs, such that ai + 1 > ai, after the optimal rearrangement.
| null |
In the first sample, the optimal order is: 10, 20, 30, 40, 50.
In the second sample, the optimal order is: 100, 200, 100, 200.
|
[{"input": "5\n20 30 10 50 40", "output": "4"}, {"input": "4\n200 100 100 200", "output": "2"}]
| 1,200
|
["greedy", "sortings"]
| 31
|
[{"input": "5\r\n20 30 10 50 40\r\n", "output": "4\r\n"}, {"input": "4\r\n200 100 100 200\r\n", "output": "2\r\n"}, {"input": "10\r\n2 2 2 2 2 2 2 2 2 2\r\n", "output": "0\r\n"}, {"input": "1\r\n1000\r\n", "output": "0\r\n"}, {"input": "2\r\n444 333\r\n", "output": "1\r\n"}, {"input": "100\r\n9 9 72 55 14 8 55 58 35 67 3 18 73 92 41 49 15 60 18 66 9 26 97 47 43 88 71 97 19 34 48 96 79 53 8 24 69 49 12 23 77 12 21 88 66 9 29 13 61 69 54 77 41 13 4 68 37 74 7 6 29 76 55 72 89 4 78 27 29 82 18 83 12 4 32 69 89 85 66 13 92 54 38 5 26 56 17 55 29 4 17 39 29 94 3 67 85 98 21 14\r\n", "output": "95\r\n"}, {"input": "1\r\n995\r\n", "output": "0\r\n"}, {"input": "10\r\n103 101 103 103 101 102 100 100 101 104\r\n", "output": "7\r\n"}, {"input": "20\r\n102 100 102 104 102 101 104 103 100 103 105 105 100 105 100 100 101 105 105 102\r\n", "output": "15\r\n"}, {"input": "20\r\n990 994 996 999 997 994 990 992 990 993 992 990 999 999 992 994 997 990 993 998\r\n", "output": "15\r\n"}, {"input": "100\r\n1 8 3 8 10 8 5 3 10 3 5 8 4 5 5 5 10 3 6 6 6 6 6 7 2 7 2 4 7 8 3 8 7 2 5 6 1 5 5 7 9 7 6 9 1 8 1 3 6 5 1 3 6 9 5 6 8 4 8 6 10 9 2 9 3 8 7 5 2 10 2 10 3 6 5 5 3 5 10 2 3 7 10 8 8 4 3 4 9 6 10 7 6 6 6 4 9 9 8 9\r\n", "output": "84\r\n"}]
| false
|
stdio
| null | true
|
651/B
|
651
|
B
|
PyPy 3-64
|
TESTS
| 5
| 61
| 0
|
160774827
|
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
a.sort()
l = []
c = 1
for i in range(1, n):
if(a[i] == a[i - 1]):
c += 1
else:
l.append(c)
c = 1
l.append(c)
c = 0
for i in range(1, len(l)):
c += min(l[i], l[i - 1])
print(c)
| 31
| 46
| 102,400
|
178405122
|
from heapq import heappush,heappop
from collections import deque
#t = int(input())
t = 1
for tc in range(1,t+1):
n = int(input())
#n,l = map(int,input().split())
a = list(map(int,input().split()))
#b = list(map(int,input().split()))
#s = input()
#p = input()
a.sort()
maxlowers = 0
ans = 0
i=0
while i<n:
curcnt = 1
while i<n-1 and a[i]==a[i+1]:
curcnt+=1
i+=1
ans+= min(maxlowers,curcnt)
maxlowers=max(maxlowers,curcnt)
i+=1
print(ans)
|
Codeforces Round 345 (Div. 2)
|
CF
| 2,016
| 1
| 256
|
Beautiful Paintings
|
There are n pictures delivered for the new exhibition. The i-th painting has beauty ai. We know that a visitor becomes happy every time he passes from a painting to a more beautiful one.
We are allowed to arranged pictures in any order. What is the maximum possible number of times the visitor may become happy while passing all pictures from first to last? In other words, we are allowed to rearrange elements of a in any order. What is the maximum possible number of indices i (1 ≤ i ≤ n - 1), such that ai + 1 > ai.
|
The first line of the input contains integer n (1 ≤ n ≤ 1000) — the number of painting.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where ai means the beauty of the i-th painting.
|
Print one integer — the maximum possible number of neighbouring pairs, such that ai + 1 > ai, after the optimal rearrangement.
| null |
In the first sample, the optimal order is: 10, 20, 30, 40, 50.
In the second sample, the optimal order is: 100, 200, 100, 200.
|
[{"input": "5\n20 30 10 50 40", "output": "4"}, {"input": "4\n200 100 100 200", "output": "2"}]
| 1,200
|
["greedy", "sortings"]
| 31
|
[{"input": "5\r\n20 30 10 50 40\r\n", "output": "4\r\n"}, {"input": "4\r\n200 100 100 200\r\n", "output": "2\r\n"}, {"input": "10\r\n2 2 2 2 2 2 2 2 2 2\r\n", "output": "0\r\n"}, {"input": "1\r\n1000\r\n", "output": "0\r\n"}, {"input": "2\r\n444 333\r\n", "output": "1\r\n"}, {"input": "100\r\n9 9 72 55 14 8 55 58 35 67 3 18 73 92 41 49 15 60 18 66 9 26 97 47 43 88 71 97 19 34 48 96 79 53 8 24 69 49 12 23 77 12 21 88 66 9 29 13 61 69 54 77 41 13 4 68 37 74 7 6 29 76 55 72 89 4 78 27 29 82 18 83 12 4 32 69 89 85 66 13 92 54 38 5 26 56 17 55 29 4 17 39 29 94 3 67 85 98 21 14\r\n", "output": "95\r\n"}, {"input": "1\r\n995\r\n", "output": "0\r\n"}, {"input": "10\r\n103 101 103 103 101 102 100 100 101 104\r\n", "output": "7\r\n"}, {"input": "20\r\n102 100 102 104 102 101 104 103 100 103 105 105 100 105 100 100 101 105 105 102\r\n", "output": "15\r\n"}, {"input": "20\r\n990 994 996 999 997 994 990 992 990 993 992 990 999 999 992 994 997 990 993 998\r\n", "output": "15\r\n"}, {"input": "100\r\n1 8 3 8 10 8 5 3 10 3 5 8 4 5 5 5 10 3 6 6 6 6 6 7 2 7 2 4 7 8 3 8 7 2 5 6 1 5 5 7 9 7 6 9 1 8 1 3 6 5 1 3 6 9 5 6 8 4 8 6 10 9 2 9 3 8 7 5 2 10 2 10 3 6 5 5 3 5 10 2 3 7 10 8 8 4 3 4 9 6 10 7 6 6 6 4 9 9 8 9\r\n", "output": "84\r\n"}]
| false
|
stdio
| null | true
|
24/A
|
24
|
A
|
Python 3
|
TESTS
| 4
| 92
| 102,400
|
203464182
|
from collections import defaultdict
n = int(input())
roads = defaultdict(list)
for _ in range(n):
start,end,weight = map(int,input().split())
roads[start].append((end,weight))
roads[end].append((start,-weight))
ans = [0,0]
# print(roads,"roads")
visited = set()
def dfs(start):
visited.add(start)
for end,weight in roads[start]:
if end not in visited:
if weight > 0:
ans[0]+= weight
else:
ans[1] -= weight
dfs(end)
elif len(visited) == n and end == 1:
# print(start,end,weight)
if weight > 0:
ans[0] += weight
else:
ans[1] -= weight
# break
dfs(1)
print(min(ans))
| 21
| 92
| 0
|
146940370
|
def findNextPath(matrix, path, index):
for i in range(len(matrix)):
if matrix[index][i] != 0 and i not in path or \
matrix[i][index] != 0 and i not in path:
return i
return -1
n = int(input())
matrix = [[0] * n for i in range(n)]
for i in range(n):
city1, city2, price = [int(item) for item in input().split(' ')]
matrix[city1 - 1][city2 - 1] = price
path = [0]
index = findNextPath(matrix, path, 0)
while index != -1:
path.append(index)
index = findNextPath(matrix, path, index)
cw = matrix[path[n - 1]][0]
acw = matrix[0][path[n - 1]]
i, j = 0, n - 1
while i < n - 1:
cw += matrix[path[i]][path[i + 1]]
acw += matrix[path[j]][path[j - 1]]
i += 1
j -= 1
print(cw if cw < acw else acw)
|
Codeforces Beta Round 24
|
ICPC
| 2,010
| 2
| 256
|
Ring road
|
Nowadays the one-way traffic is introduced all over the world in order to improve driving safety and reduce traffic jams. The government of Berland decided to keep up with new trends. Formerly all n cities of Berland were connected by n two-way roads in the ring, i. e. each city was connected directly to exactly two other cities, and from each city it was possible to get to any other city. Government of Berland introduced one-way traffic on all n roads, but it soon became clear that it's impossible to get from some of the cities to some others. Now for each road is known in which direction the traffic is directed at it, and the cost of redirecting the traffic. What is the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other?
|
The first line contains integer n (3 ≤ n ≤ 100) — amount of cities (and roads) in Berland. Next n lines contain description of roads. Each road is described by three integers ai, bi, ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 100) — road is directed from city ai to city bi, redirecting the traffic costs ci.
|
Output single integer — the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other.
| null | null |
[{"input": "3\n1 3 1\n1 2 1\n3 2 1", "output": "1"}, {"input": "3\n1 3 1\n1 2 5\n3 2 1", "output": "2"}, {"input": "6\n1 5 4\n5 3 8\n2 4 15\n1 6 16\n2 3 23\n4 6 42", "output": "39"}, {"input": "4\n1 2 9\n2 3 8\n3 4 7\n4 1 5", "output": "0"}]
| 1,400
|
["graphs"]
| 21
|
[{"input": "3\r\n1 3 1\r\n1 2 1\r\n3 2 1\r\n", "output": "1\r\n"}, {"input": "3\r\n1 3 1\r\n1 2 5\r\n3 2 1\r\n", "output": "2\r\n"}, {"input": "6\r\n1 5 4\r\n5 3 8\r\n2 4 15\r\n1 6 16\r\n2 3 23\r\n4 6 42\r\n", "output": "39\r\n"}, {"input": "4\r\n1 2 9\r\n2 3 8\r\n3 4 7\r\n4 1 5\r\n", "output": "0\r\n"}, {"input": "5\r\n5 3 89\r\n2 3 43\r\n4 2 50\r\n1 4 69\r\n1 5 54\r\n", "output": "143\r\n"}, {"input": "10\r\n1 8 16\r\n6 1 80\r\n6 5 27\r\n5 7 86\r\n7 9 72\r\n4 9 20\r\n4 3 54\r\n3 2 57\r\n10 2 61\r\n8 10 90\r\n", "output": "267\r\n"}, {"input": "17\r\n8 12 43\r\n13 12 70\r\n7 13 68\r\n11 7 19\r\n5 11 24\r\n5 1 100\r\n4 1 10\r\n3 4 68\r\n2 3 46\r\n15 2 58\r\n15 6 38\r\n6 9 91\r\n9 10 72\r\n14 10 32\r\n14 17 97\r\n17 16 67\r\n8 16 40\r\n", "output": "435\r\n"}, {"input": "22\r\n18 22 46\r\n18 21 87\r\n5 21 17\r\n5 10 82\r\n10 12 81\r\n17 12 98\r\n16 17 17\r\n16 13 93\r\n4 13 64\r\n4 11 65\r\n15 11 18\r\n6 15 35\r\n6 7 61\r\n7 19 12\r\n19 1 65\r\n8 1 32\r\n8 2 46\r\n9 2 19\r\n9 3 58\r\n3 14 65\r\n20 14 67\r\n20 22 2\r\n", "output": "413\r\n"}, {"input": "3\r\n3 1 1\r\n2 1 1\r\n2 3 1\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
24/A
|
24
|
A
|
Python 3
|
TESTS
| 5
| 92
| 307,200
|
109813897
|
l, a, b, p, s, n = [], {0}, {0}, [], 0, int(input())
for i in range(n):
q, w, e = map(int, input().split())
l += [[q, w]]
a.add(q)
b.add(w)
p += [e]
t = a & b
m =set(a)
k = []
a -= t
b -= t
v = []
x=set()
def cc(z):
q = l[z][1]
b=p[z]
while q in m:
for i in range(n):
if l[i][0]==q:
if i not in x:
x.add(i)
q=l[i][1]
b+=p[i]
break
return [[l[z][0], q]],b
for i in a:
for j in range(n):
if l[j][0] == i:
f,g=cc(j)
k+=f
v+=[g]
vv=0
for i in a:
for j in b:
q = [i, j]
if q in k:
s += v[k.index(q)]
if q in l:
vv+=p[l.index(q)]
print(max(min(s, sum(v) - s),min(vv,sum(p)-vv)))
| 21
| 92
| 0
|
158971756
|
def main():
n = int(input())
graph = {}
for _ in range(n):
inp = input().split()
u = int(inp[0])
v = int(inp[1])
w = int(inp[2])
if u not in graph:
graph[u] = []
if v not in graph:
graph[v] = []
graph[u].append([v,0])
graph[v].append([u,w])
# print(graph)
visited1 = set()
visited2 = set()
visited1.add(1)
visited2.add(1)
current1 = graph[1][0][0]
current2 = graph[1][1][0]
cost1 = graph[1][0][1]
cost2 = graph[1][1][1]
while(current1 not in visited1):
# print(current1)
visited1.add(current1)
if graph[current1][0][0] not in visited1:
cost1 += graph[current1][0][1]
# print(cost1)
# print(cost1)
current1 = graph[current1][0][0]
elif graph[current1][1][0] not in visited1:
cost1 += graph[current1][1][1]
# print(cost1)
current1 = graph[current1][1][0]
if(graph[current1][0][0] == 1):
cost1 += graph[current1][0][1]
else:
cost1 += graph[current1][1][1]
while(current2 not in visited2):
# print(current2)
visited2.add(current2)
if graph[current2][0][0] not in visited2:
cost2 += graph[current2][0][1]
# print(cost2)
# print(cost2)
current2 = graph[current2][0][0]
elif graph[current2][1][0] not in visited2:
cost2 += graph[current2][1][1]
# print(cost2)
current2 = graph[current2][1][0]
if(graph[current2][0][0] == 1):
cost2 += graph[current2][0][1]
else:
cost2 += graph[current2][1][1]
print(min(cost1, cost2))
# print(cost1)
return 0
if __name__=="__main__":
main()
|
Codeforces Beta Round 24
|
ICPC
| 2,010
| 2
| 256
|
Ring road
|
Nowadays the one-way traffic is introduced all over the world in order to improve driving safety and reduce traffic jams. The government of Berland decided to keep up with new trends. Formerly all n cities of Berland were connected by n two-way roads in the ring, i. e. each city was connected directly to exactly two other cities, and from each city it was possible to get to any other city. Government of Berland introduced one-way traffic on all n roads, but it soon became clear that it's impossible to get from some of the cities to some others. Now for each road is known in which direction the traffic is directed at it, and the cost of redirecting the traffic. What is the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other?
|
The first line contains integer n (3 ≤ n ≤ 100) — amount of cities (and roads) in Berland. Next n lines contain description of roads. Each road is described by three integers ai, bi, ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 100) — road is directed from city ai to city bi, redirecting the traffic costs ci.
|
Output single integer — the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other.
| null | null |
[{"input": "3\n1 3 1\n1 2 1\n3 2 1", "output": "1"}, {"input": "3\n1 3 1\n1 2 5\n3 2 1", "output": "2"}, {"input": "6\n1 5 4\n5 3 8\n2 4 15\n1 6 16\n2 3 23\n4 6 42", "output": "39"}, {"input": "4\n1 2 9\n2 3 8\n3 4 7\n4 1 5", "output": "0"}]
| 1,400
|
["graphs"]
| 21
|
[{"input": "3\r\n1 3 1\r\n1 2 1\r\n3 2 1\r\n", "output": "1\r\n"}, {"input": "3\r\n1 3 1\r\n1 2 5\r\n3 2 1\r\n", "output": "2\r\n"}, {"input": "6\r\n1 5 4\r\n5 3 8\r\n2 4 15\r\n1 6 16\r\n2 3 23\r\n4 6 42\r\n", "output": "39\r\n"}, {"input": "4\r\n1 2 9\r\n2 3 8\r\n3 4 7\r\n4 1 5\r\n", "output": "0\r\n"}, {"input": "5\r\n5 3 89\r\n2 3 43\r\n4 2 50\r\n1 4 69\r\n1 5 54\r\n", "output": "143\r\n"}, {"input": "10\r\n1 8 16\r\n6 1 80\r\n6 5 27\r\n5 7 86\r\n7 9 72\r\n4 9 20\r\n4 3 54\r\n3 2 57\r\n10 2 61\r\n8 10 90\r\n", "output": "267\r\n"}, {"input": "17\r\n8 12 43\r\n13 12 70\r\n7 13 68\r\n11 7 19\r\n5 11 24\r\n5 1 100\r\n4 1 10\r\n3 4 68\r\n2 3 46\r\n15 2 58\r\n15 6 38\r\n6 9 91\r\n9 10 72\r\n14 10 32\r\n14 17 97\r\n17 16 67\r\n8 16 40\r\n", "output": "435\r\n"}, {"input": "22\r\n18 22 46\r\n18 21 87\r\n5 21 17\r\n5 10 82\r\n10 12 81\r\n17 12 98\r\n16 17 17\r\n16 13 93\r\n4 13 64\r\n4 11 65\r\n15 11 18\r\n6 15 35\r\n6 7 61\r\n7 19 12\r\n19 1 65\r\n8 1 32\r\n8 2 46\r\n9 2 19\r\n9 3 58\r\n3 14 65\r\n20 14 67\r\n20 22 2\r\n", "output": "413\r\n"}, {"input": "3\r\n3 1 1\r\n2 1 1\r\n2 3 1\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
24/A
|
24
|
A
|
PyPy 3
|
TESTS
| 6
| 248
| 0
|
56998659
|
n = int(input())
e = [ [] for i in range(n)]
e2 = [ [] for i in range(n)]
C = 0
for i in range(n):
a,b,c = map(int,input().split())
a-=1
b-=1
e[a].append((b,c))
e[b].append((a,c))
e2[a].append(b)
C += c
col = [ -1 for i in range(n)]
col[0] = 0
od = 0
def dfs(n):
global od
for v in e[n]:
if col[v[0]] == -1:
if e2[n]:
if v[0] in e2[n] :
od += v[1]
col[v[0]] = col[n] + v[1]
dfs(v[0])
dfs(0)
if len(e2[0]) == 1:
for v in e[0]:
if e2[0][0] != v[0]:
od += v[1]
print(min( abs(C - od) , od))
| 21
| 92
| 0
|
165506594
|
def findNextPath(cont, path, ind):
for i in range(len(cont)):
if cont[ind][i] != 0 and i not in path or \
cont[i][ind] != 0 and i not in path:
return i
return -1
N = int(input())
cont = [[0] * N for i in range(N)]
for i in range(N):
city1, city2, price = [int(item) for item in input().split(' ')]
cont[city1 - 1][city2 - 1] = price
path = [0]
ind = findNextPath(cont, path, 0)
while ind != -1:
path.append(ind)
ind = findNextPath(cont, path, ind)
cw = cont[path[N - 1]][0]
acw = cont[0][path[N - 1]]
i, j = 0, N - 1
while i < N - 1:
cw += cont[path[i]][path[i + 1]]
acw += cont[path[j]][path[j - 1]]
i += 1
j -= 1
print(min(cw, acw))
|
Codeforces Beta Round 24
|
ICPC
| 2,010
| 2
| 256
|
Ring road
|
Nowadays the one-way traffic is introduced all over the world in order to improve driving safety and reduce traffic jams. The government of Berland decided to keep up with new trends. Formerly all n cities of Berland were connected by n two-way roads in the ring, i. e. each city was connected directly to exactly two other cities, and from each city it was possible to get to any other city. Government of Berland introduced one-way traffic on all n roads, but it soon became clear that it's impossible to get from some of the cities to some others. Now for each road is known in which direction the traffic is directed at it, and the cost of redirecting the traffic. What is the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other?
|
The first line contains integer n (3 ≤ n ≤ 100) — amount of cities (and roads) in Berland. Next n lines contain description of roads. Each road is described by three integers ai, bi, ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 100) — road is directed from city ai to city bi, redirecting the traffic costs ci.
|
Output single integer — the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other.
| null | null |
[{"input": "3\n1 3 1\n1 2 1\n3 2 1", "output": "1"}, {"input": "3\n1 3 1\n1 2 5\n3 2 1", "output": "2"}, {"input": "6\n1 5 4\n5 3 8\n2 4 15\n1 6 16\n2 3 23\n4 6 42", "output": "39"}, {"input": "4\n1 2 9\n2 3 8\n3 4 7\n4 1 5", "output": "0"}]
| 1,400
|
["graphs"]
| 21
|
[{"input": "3\r\n1 3 1\r\n1 2 1\r\n3 2 1\r\n", "output": "1\r\n"}, {"input": "3\r\n1 3 1\r\n1 2 5\r\n3 2 1\r\n", "output": "2\r\n"}, {"input": "6\r\n1 5 4\r\n5 3 8\r\n2 4 15\r\n1 6 16\r\n2 3 23\r\n4 6 42\r\n", "output": "39\r\n"}, {"input": "4\r\n1 2 9\r\n2 3 8\r\n3 4 7\r\n4 1 5\r\n", "output": "0\r\n"}, {"input": "5\r\n5 3 89\r\n2 3 43\r\n4 2 50\r\n1 4 69\r\n1 5 54\r\n", "output": "143\r\n"}, {"input": "10\r\n1 8 16\r\n6 1 80\r\n6 5 27\r\n5 7 86\r\n7 9 72\r\n4 9 20\r\n4 3 54\r\n3 2 57\r\n10 2 61\r\n8 10 90\r\n", "output": "267\r\n"}, {"input": "17\r\n8 12 43\r\n13 12 70\r\n7 13 68\r\n11 7 19\r\n5 11 24\r\n5 1 100\r\n4 1 10\r\n3 4 68\r\n2 3 46\r\n15 2 58\r\n15 6 38\r\n6 9 91\r\n9 10 72\r\n14 10 32\r\n14 17 97\r\n17 16 67\r\n8 16 40\r\n", "output": "435\r\n"}, {"input": "22\r\n18 22 46\r\n18 21 87\r\n5 21 17\r\n5 10 82\r\n10 12 81\r\n17 12 98\r\n16 17 17\r\n16 13 93\r\n4 13 64\r\n4 11 65\r\n15 11 18\r\n6 15 35\r\n6 7 61\r\n7 19 12\r\n19 1 65\r\n8 1 32\r\n8 2 46\r\n9 2 19\r\n9 3 58\r\n3 14 65\r\n20 14 67\r\n20 22 2\r\n", "output": "413\r\n"}, {"input": "3\r\n3 1 1\r\n2 1 1\r\n2 3 1\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
24/A
|
24
|
A
|
Python 3
|
TESTS
| 5
| 216
| 409,600
|
73869176
|
from collections import defaultdict
class Graph:
def __init__(self):
self.ActualConfig = defaultdict(int)
self.undirGraph = defaultdict(list)
self.Visited = defaultdict(bool)
self.Clock = 0
self.Total = 0
self.flag = False
def addEdge(self,fr,to,w):
self.undirGraph[fr].append(to)
self.undirGraph[to].append(fr)
self.ActualConfig[(fr,to)] = w
self.Total += w
def DFSUtil(self,root):
for i in self.undirGraph[root]:
if(self.Visited[i]==False):
if(self.flag == False):
self.Visited[i] = False
self.flag = True
else:
self.Visited[i] = True
# print(i,"->",end = " ")
if(self.ActualConfig[(root,i)]==0):
self.Clock+=self.ActualConfig[(i,root)]
# print("i,root: ",self.ActualConfig[(i,root)])
self.DFSUtil(i)
def DFSHelper(self):
self.DFSUtil(1)
self.Visited[1] = True
def solve(self):
return min(self.Clock,self.Total-self.Clock)
n = int(input())
G = Graph()
for _ in range(n):
fr,to,w = map(int, input().split())
G.addEdge(fr,to,w)
G.DFSHelper()
print(G.solve())
| 21
| 92
| 0
|
187196677
|
s, e = [0] * 101, [0] * 101
sum, val = (0, 0)
for _ in range(int(input())):
a, b, c = map(int,input().split())
if s[a] or e[b]: val += c; s[b] = e[a] = 1
else: s[a] = e[b] = 1
sum += c
print(min(val, sum - val))
|
Codeforces Beta Round 24
|
ICPC
| 2,010
| 2
| 256
|
Ring road
|
Nowadays the one-way traffic is introduced all over the world in order to improve driving safety and reduce traffic jams. The government of Berland decided to keep up with new trends. Formerly all n cities of Berland were connected by n two-way roads in the ring, i. e. each city was connected directly to exactly two other cities, and from each city it was possible to get to any other city. Government of Berland introduced one-way traffic on all n roads, but it soon became clear that it's impossible to get from some of the cities to some others. Now for each road is known in which direction the traffic is directed at it, and the cost of redirecting the traffic. What is the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other?
|
The first line contains integer n (3 ≤ n ≤ 100) — amount of cities (and roads) in Berland. Next n lines contain description of roads. Each road is described by three integers ai, bi, ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 100) — road is directed from city ai to city bi, redirecting the traffic costs ci.
|
Output single integer — the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other.
| null | null |
[{"input": "3\n1 3 1\n1 2 1\n3 2 1", "output": "1"}, {"input": "3\n1 3 1\n1 2 5\n3 2 1", "output": "2"}, {"input": "6\n1 5 4\n5 3 8\n2 4 15\n1 6 16\n2 3 23\n4 6 42", "output": "39"}, {"input": "4\n1 2 9\n2 3 8\n3 4 7\n4 1 5", "output": "0"}]
| 1,400
|
["graphs"]
| 21
|
[{"input": "3\r\n1 3 1\r\n1 2 1\r\n3 2 1\r\n", "output": "1\r\n"}, {"input": "3\r\n1 3 1\r\n1 2 5\r\n3 2 1\r\n", "output": "2\r\n"}, {"input": "6\r\n1 5 4\r\n5 3 8\r\n2 4 15\r\n1 6 16\r\n2 3 23\r\n4 6 42\r\n", "output": "39\r\n"}, {"input": "4\r\n1 2 9\r\n2 3 8\r\n3 4 7\r\n4 1 5\r\n", "output": "0\r\n"}, {"input": "5\r\n5 3 89\r\n2 3 43\r\n4 2 50\r\n1 4 69\r\n1 5 54\r\n", "output": "143\r\n"}, {"input": "10\r\n1 8 16\r\n6 1 80\r\n6 5 27\r\n5 7 86\r\n7 9 72\r\n4 9 20\r\n4 3 54\r\n3 2 57\r\n10 2 61\r\n8 10 90\r\n", "output": "267\r\n"}, {"input": "17\r\n8 12 43\r\n13 12 70\r\n7 13 68\r\n11 7 19\r\n5 11 24\r\n5 1 100\r\n4 1 10\r\n3 4 68\r\n2 3 46\r\n15 2 58\r\n15 6 38\r\n6 9 91\r\n9 10 72\r\n14 10 32\r\n14 17 97\r\n17 16 67\r\n8 16 40\r\n", "output": "435\r\n"}, {"input": "22\r\n18 22 46\r\n18 21 87\r\n5 21 17\r\n5 10 82\r\n10 12 81\r\n17 12 98\r\n16 17 17\r\n16 13 93\r\n4 13 64\r\n4 11 65\r\n15 11 18\r\n6 15 35\r\n6 7 61\r\n7 19 12\r\n19 1 65\r\n8 1 32\r\n8 2 46\r\n9 2 19\r\n9 3 58\r\n3 14 65\r\n20 14 67\r\n20 22 2\r\n", "output": "413\r\n"}, {"input": "3\r\n3 1 1\r\n2 1 1\r\n2 3 1\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
231/A
|
231
|
A
|
Python 3
|
TESTS
| 4
| 92
| 0
|
226442888
|
n=int(input())
count=0
sure=0
for i in range(0,n):
view=input()
for a in view:
if a=="1":
sure=sure+1
if sure>2:
count=count+1
print(count)
| 21
| 62
| 0
|
226183655
|
n=int(input())
t=0
while (n>0):
a1, a2 ,a3=input().split()
if int(a1)+int(a2)+int(a3)>=2:
t=t+1
n=n-1
print(t)
|
Codeforces Round 143 (Div. 2)
|
CF
| 2,012
| 2
| 256
|
Team
|
One day three best friends Petya, Vasya and Tonya decided to form a team and take part in programming contests. Participants are usually offered several problems during programming contests. Long before the start the friends decided that they will implement a problem if at least two of them are sure about the solution. Otherwise, the friends won't write the problem's solution.
This contest offers n problems to the participants. For each problem we know, which friend is sure about the solution. Help the friends find the number of problems for which they will write a solution.
|
The first input line contains a single integer n (1 ≤ n ≤ 1000) — the number of problems in the contest. Then n lines contain three integers each, each integer is either 0 or 1. If the first number in the line equals 1, then Petya is sure about the problem's solution, otherwise he isn't sure. The second number shows Vasya's view on the solution, the third number shows Tonya's view. The numbers on the lines are separated by spaces.
|
Print a single integer — the number of problems the friends will implement on the contest.
| null |
In the first sample Petya and Vasya are sure that they know how to solve the first problem and all three of them know how to solve the second problem. That means that they will write solutions for these problems. Only Petya is sure about the solution for the third problem, but that isn't enough, so the friends won't take it.
In the second sample the friends will only implement the second problem, as Vasya and Tonya are sure about the solution.
|
[{"input": "3\n1 1 0\n1 1 1\n1 0 0", "output": "2"}, {"input": "2\n1 0 0\n0 1 1", "output": "1"}]
| 800
|
["brute force", "greedy"]
| 21
|
[{"input": "3\r\n1 1 0\r\n1 1 1\r\n1 0 0\r\n", "output": "2\r\n"}, {"input": "2\r\n1 0 0\r\n0 1 1\r\n", "output": "1\r\n"}, {"input": "1\r\n1 0 0\r\n", "output": "0\r\n"}, {"input": "2\r\n1 0 0\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "5\r\n1 0 0\r\n0 1 0\r\n1 1 1\r\n0 0 1\r\n0 0 0\r\n", "output": "1\r\n"}, {"input": "10\r\n0 1 0\r\n0 1 0\r\n1 1 0\r\n1 0 0\r\n0 0 1\r\n0 1 1\r\n1 1 1\r\n1 1 0\r\n0 0 0\r\n0 0 0\r\n", "output": "4\r\n"}, {"input": "15\r\n0 1 0\r\n1 0 0\r\n1 1 0\r\n1 1 1\r\n0 1 0\r\n0 0 1\r\n1 0 1\r\n1 0 1\r\n1 0 1\r\n0 0 0\r\n1 1 1\r\n1 1 0\r\n0 1 1\r\n1 1 0\r\n1 1 1\r\n", "output": "10\r\n"}, {"input": "50\r\n0 0 0\r\n0 1 1\r\n1 1 1\r\n0 1 0\r\n1 0 1\r\n1 1 1\r\n0 0 1\r\n1 0 0\r\n1 1 0\r\n1 0 1\r\n0 1 0\r\n0 0 1\r\n1 1 0\r\n0 1 0\r\n1 1 0\r\n0 0 0\r\n1 1 1\r\n1 0 1\r\n0 0 1\r\n1 1 0\r\n1 1 1\r\n0 1 1\r\n1 1 0\r\n0 0 0\r\n0 0 0\r\n1 1 1\r\n0 0 0\r\n1 1 1\r\n0 1 1\r\n0 0 1\r\n0 0 0\r\n0 0 0\r\n1 1 0\r\n1 1 0\r\n1 0 1\r\n1 0 0\r\n1 0 1\r\n1 0 1\r\n0 1 1\r\n1 1 0\r\n1 1 0\r\n0 1 0\r\n1 0 1\r\n0 0 0\r\n0 0 0\r\n0 0 0\r\n0 0 1\r\n1 1 1\r\n0 1 1\r\n1 0 1\r\n", "output": "29\r\n"}, {"input": "1\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "8\r\n0 0 0\r\n0 0 1\r\n0 0 0\r\n0 1 1\r\n1 0 0\r\n1 0 1\r\n1 1 0\r\n1 1 1\r\n", "output": "4\r\n"}, {"input": "16\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n", "output": "16\r\n"}]
| false
|
stdio
| null | true
|
237/B
|
237
|
B
|
Python 3
|
TESTS
| 0
| 62
| 0
|
142703507
|
import math
import sys
def solve():
n = int(input())
rows = list(map(int, input().split()))
r = []
cords = {}
for i in range(n):
r.append(list(map(int, input().split())))
for j in range(rows[i]):
cords[r[i][j]] = [i, j]
it = 1
ans = []
for i in range(n):
for j in range(rows[i]):
if r[i][j] != it:
r[i][j], r[cords[it][0]][cords[it][1]] = r[cords[it][0]][cords[it][1]], r[i][j]
ans.append([i, j, cords[it][0], cords[it][1]])
it += 1
print(len(ans))
for i in ans:
for j in i:
print(j + 1, end=" ")
print()
if __name__ == '__main__':
solve()
| 55
| 124
| 0
|
184964582
|
n=int(input())
p=[[0,0]]
l=[0]
v=[]
for i, c in enumerate(map(int, input().split())):
p.extend([[i + 1, j + 1] for j in range(c)])
l.extend(list(map(int, input().split())))
for i in range(1, len(l)):
if l[i] != i:
j = l.index(i)
v.append(p[i] + p[j])
l[i], l[j] = l[j], l[i]
print(len(v))
for x in v:
print(' '.join(map(str, x)))
|
Codeforces Round 147 (Div. 2)
|
CF
| 2,012
| 2
| 256
|
Young Table
|
You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci cells, at that for all i (1 < i ≤ n) holds ci ≤ ci - 1.
Let's denote s as the total number of cells of table a, that is, $$s = \sum_{i=1}^{n} c_i$$. We know that each cell of the table contains a single integer from 1 to s, at that all written integers are distinct.
Let's assume that the cells of the i-th row of table a are numbered from 1 to ci, then let's denote the number written in the j-th cell of the i-th row as ai, j. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions:
1. for all i, j (1 < i ≤ n; 1 ≤ j ≤ ci) holds ai, j > ai - 1, j;
2. for all i, j (1 ≤ i ≤ n; 1 < j ≤ ci) holds ai, j > ai, j - 1.
In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap.
Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than s. You do not have to minimize the number of operations.
|
The first line contains a single integer n (1 ≤ n ≤ 50) that shows the number of rows in the table. The second line contains n space-separated integers ci (1 ≤ ci ≤ 50; ci ≤ ci - 1) — the numbers of cells on the corresponding rows.
Next n lines contain table а. The i-th of them contains ci space-separated integers: the j-th integer in this line represents ai, j.
It is guaranteed that all the given numbers ai, j are positive and do not exceed s. It is guaranteed that all ai, j are distinct.
|
In the first line print a single integer m (0 ≤ m ≤ s), representing the number of performed swaps.
In the next m lines print the description of these swap operations. In the i-th line print four space-separated integers xi, yi, pi, qi (1 ≤ xi, pi ≤ n; 1 ≤ yi ≤ cxi; 1 ≤ qi ≤ cpi). The printed numbers denote swapping the contents of cells axi, yi and api, qi. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed.
| null | null |
[{"input": "3\n3 2 1\n4 3 5\n6 1\n2", "output": "2\n1 1 2 2\n2 1 3 1"}, {"input": "1\n4\n4 3 2 1", "output": "2\n1 1 1 4\n1 2 1 3"}]
| 1,500
|
["implementation", "sortings"]
| 55
|
[{"input": "3\r\n3 2 1\r\n4 3 5\r\n6 1\r\n2\r\n", "output": "2\r\n1 1 2 2\r\n2 1 3 1\r\n"}, {"input": "1\r\n4\r\n4 3 2 1\r\n", "output": "2\r\n1 1 1 4\r\n1 2 1 3\r\n"}, {"input": "5\r\n4 4 3 3 1\r\n14 13 4 15\r\n11 1 2 5\r\n7 6 10\r\n8 9 3\r\n12\r\n", "output": "13\r\n1 1 2 2\r\n1 2 2 3\r\n1 3 4 3\r\n1 4 4 3\r\n2 1 2 4\r\n2 2 3 2\r\n2 3 3 1\r\n2 4 4 1\r\n3 1 4 2\r\n3 2 3 3\r\n3 3 4 1\r\n4 1 5 1\r\n4 3 5 1\r\n"}, {"input": "2\r\n8 6\r\n1 2 3 13 10 4 11 7\r\n9 12 8 5 14 6\r\n", "output": "7\r\n1 4 1 6\r\n1 5 2 4\r\n1 6 2 6\r\n1 7 1 8\r\n1 8 2 3\r\n2 2 2 4\r\n2 5 2 6\r\n"}, {"input": "6\r\n10 9 7 6 4 3\r\n18 20 29 19 5 28 31 30 32 15\r\n38 33 11 8 39 2 6 9 3\r\n13 37 27 24 26 1 17\r\n36 10 35 21 7 16\r\n22 23 4 12\r\n34 25 14\r\n", "output": "33\n1 1 3 6\n1 2 2 6\n1 3 2 9\n1 4 5 3\n1 6 2 7\n1 7 4 5\n1 8 2 4\n1 9 2 8\n1 10 4 2\n2 1 2 3\n2 2 5 4\n2 3 3 1\n2 4 6 3\n2 5 4 2\n2 6 4 6\n2 7 3 7\n2 8 3 6\n2 9 5 3\n3 1 4 6\n3 2 4 4\n3 3 5 1\n3 4 5 2\n3 5 5 2\n3 6 6 2\n3 7 5 2\n4 1 5 1\n4 2 5 2\n4 3 5 3\n4 4 6 3\n4 6 6 2\n5 1 5 4\n5 2 6 1\n6 1 6 3\n"}, {"input": "8\r\n2 2 2 2 1 1 1 1\r\n10 9\r\n11 5\r\n7 3\r\n2 6\r\n12\r\n1\r\n8\r\n4\r\n", "output": "9\r\n1 1 6 1\r\n1 2 4 1\r\n2 1 3 2\r\n2 2 8 1\r\n3 1 8 1\r\n3 2 4 2\r\n4 1 8 1\r\n4 2 7 1\r\n5 1 8 1\r\n"}, {"input": "4\r\n3 3 3 2\r\n6 3 11\r\n10 7 1\r\n9 4 5\r\n2 8\r\n", "output": "8\r\n1 1 2 3\r\n1 2 4 1\r\n1 3 4 1\r\n2 1 3 2\r\n2 2 3 3\r\n3 1 3 3\r\n3 2 4 2\r\n4 1 4 2\r\n"}, {"input": "1\r\n1\r\n1\r\n", "output": "0\r\n"}, {"input": "2\r\n35 7\r\n6 8 35 9 28 25 10 41 33 39 19 24 5 12 30 40 18 2 4 11 32 13 31 21 14 27 3 34 37 16 17 29 1 42 36\r\n20 23 38 15 26 7 22\r\n", "output": "39\n1 1 1 33\n1 2 1 18\n1 3 1 27\n1 4 1 19\n1 5 1 13\n1 6 1 33\n1 7 2 6\n1 8 1 18\n1 9 1 19\n1 10 2 6\n1 11 1 20\n1 12 1 14\n1 13 1 22\n1 14 1 25\n1 15 2 4\n1 16 1 30\n1 17 1 31\n1 18 1 31\n1 19 1 20\n1 20 2 1\n1 21 1 24\n1 22 2 7\n1 23 2 2\n1 24 1 25\n1 25 1 33\n1 26 2 5\n1 27 2 5\n1 28 2 7\n1 29 1 32\n1 30 2 4\n1 31 2 2\n1 32 1 33\n1 33 2 1\n1 34 2 7\n1 35 2 5\n2 1 2 5\n2 2 2 5\n2 4 2 6\n2 5 2 6\n"}, {"input": "3\r\n36 28 14\r\n46 15 35 60 41 65 73 33 18 20 68 22 28 23 67 44 2 24 21 51 37 3 48 69 12 50 32 72 45 53 17 47 56 52 29 57\r\n8 62 10 19 26 64 7 49 6 25 34 63 74 31 14 43 30 4 11 76 16 55 36 5 70 61 77 27\r\n38 40 1 78 58 42 66 71 75 59 54 9 39 13\r\n", "output": "73\n1 1 3 3\n1 2 1 17\n1 3 1 22\n1 4 2 18\n1 5 2 24\n1 6 2 9\n1 7 2 7\n1 8 2 1\n1 9 3 12\n1 10 2 3\n1 11 2 19\n1 12 1 25\n1 13 3 14\n1 14 2 15\n1 15 1 17\n1 16 2 21\n1 17 1 31\n1 18 3 12\n1 19 2 4\n1 20 2 3\n1 21 2 4\n1 22 1 25\n1 23 2 15\n1 24 3 12\n1 25 2 10\n1 26 2 5\n1 27 2 28\n1 28 3 14\n1 29 1 35\n1 30 2 17\n1 31 2 14\n1 32 2 28\n1 33 2 1\n1 34 2 11\n1 35 2 10\n1 36 2 23\n2 1 2 4\n2 2 3 1\n2 3 3 13\n2 4 3 2\n2 5 2 24\n2 6 3 6\n2 7 2 16\n2 8 2 21\n2 9 2 10\n2 10 3 3\n2 11 2 28\n2 12 2 15\n2 13 2 21\n2 14 2 24\n2 15 3 13\n2 16 2 28\n2 18 3 11\n2 19 2 22\n2 20 3 2\n2 21 2 23\n2 22 3 5\n2 23 3 10\n2 24 3 11\n2 25 2 26\n2 26 3 1\n2 27 3 13\n2 28 3 6\n3 1 3 3\n3 2 3 7\n3 3 3 11\n3 4 3 5\n3 5 3 12\n3 6 3 11\n3 7 3 8\n3 8 3 14\n3 9 3 11\n3 12 3 14\n"}, {"input": "5\r\n5 2 2 2 1\r\n1 3 4 5 12\r\n2 6\r\n8 9\r\n7 10\r\n11\r\n", "output": "8\r\n1 2 2 1\r\n1 3 2 1\r\n1 4 2 1\r\n1 5 2 1\r\n2 1 2 2\r\n2 2 4 1\r\n4 1 4 2\r\n4 2 5 1\r\n"}, {"input": "5\r\n5 4 3 2 1\r\n1 2 3 4 5\r\n6 7 8 9\r\n10 11 12\r\n13 14\r\n15\r\n", "output": "0\r\n"}, {"input": "1\r\n1\r\n1\r\n", "output": "0\r\n"}, {"input": "4\r\n4 4 2 2\r\n1 2 3 4\r\n5 6 7 8\r\n9 10\r\n11 12\r\n", "output": "0\r\n"}, {"input": "1\r\n50\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50\r\n", "output": "0\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:
lines = f.read().splitlines()
ptr = 0
n = int(lines[ptr])
ptr += 1
c = list(map(int, lines[ptr].split()))
ptr += 1
s = sum(c)
table = []
for i in range(n):
row = list(map(int, lines[ptr].split()))
table.append(row)
ptr += 1
original_numbers = set()
for row in table:
for num in row:
original_numbers.add(num)
with open(submission_path) as f:
sub_lines = f.read().splitlines()
if not sub_lines:
print(0)
return
try:
m = int(sub_lines[0].strip())
except:
print(0)
return
if m < 0 or m > s:
print(0)
return
if len(sub_lines) < m + 1:
print(0)
return
swaps = []
for line in sub_lines[1:1 + m]:
parts = line.strip().split()
if len(parts) != 4:
print(0)
return
try:
xi, yi, pi, qi = map(int, parts)
xi -= 1
yi -= 1
pi -= 1
qi -= 1
except:
print(0)
return
swaps.append((xi, yi, pi, qi))
current_table = [row.copy() for row in table]
for swap in swaps:
xi, yi, pi, qi = swap
if xi < 0 or xi >= n or pi < 0 or pi >= n:
print(0)
return
if yi < 0 or yi >= c[xi] or qi < 0 or qi >= c[pi]:
print(0)
return
if (xi, yi) == (pi, qi):
print(0)
return
current_table[xi][yi], current_table[pi][qi] = current_table[pi][qi], current_table[xi][yi]
for row in current_table:
for j in range(1, len(row)):
if row[j] <= row[j - 1]:
print(0)
return
for i in range(1, n):
for j in range(len(current_table[i])):
if current_table[i][j] <= current_table[i - 1][j]:
print(0)
return
current_numbers = set()
for row in current_table:
for num in row:
current_numbers.add(num)
if current_numbers != original_numbers:
print(0)
return
print(1)
if __name__ == "__main__":
main()
| true
|
583/A
|
583
|
A
|
Python 3
|
TESTS
| 5
| 108
| 0
|
58902651
|
R = lambda:list(map(int,input().split()))
n = int(input())
c,v,l,k = 0,0,[],[]
for i in range(n*n):
n,m = R()
if i==0:c=n;v=m
if n==m:l.append(i+1);
else:k.append(i+1)
if c==v : print(*l)
else:print(*k)
| 39
| 46
| 0
|
165528867
|
num_inp=lambda: int(input())
arr_inp=lambda: list(map(int,input().split()))
sp_inp=lambda: map(int,input().split())
n=int(input())
r=[0]*(n+1)
c=[0]*(n+1)
for i in range(n*n):
a,b=map(int,input().split())
if r[a]==0 and c[b]==0:
print(i+1)
r[a]=c[b]=1
|
Codeforces Round 323 (Div. 2)
|
CF
| 2,015
| 1
| 256
|
Asphalting Roads
|
City X consists of n vertical and n horizontal infinite roads, forming n × n intersections. Roads (both vertical and horizontal) are numbered from 1 to n, and the intersections are indicated by the numbers of the roads that form them.
Sand roads have long been recognized out of date, so the decision was made to asphalt them. To do this, a team of workers was hired and a schedule of work was made, according to which the intersections should be asphalted.
Road repairs are planned for n2 days. On the i-th day of the team arrives at the i-th intersection in the list and if none of the two roads that form the intersection were already asphalted they asphalt both roads. Otherwise, the team leaves the intersection, without doing anything with the roads.
According to the schedule of road works tell in which days at least one road will be asphalted.
|
The first line contains integer n (1 ≤ n ≤ 50) — the number of vertical and horizontal roads in the city.
Next n2 lines contain the order of intersections in the schedule. The i-th of them contains two numbers hi, vi (1 ≤ hi, vi ≤ n), separated by a space, and meaning that the intersection that goes i-th in the timetable is at the intersection of the hi-th horizontal and vi-th vertical roads. It is guaranteed that all the intersections in the timetable are distinct.
|
In the single line print the numbers of the days when road works will be in progress in ascending order. The days are numbered starting from 1.
| null |
In the sample the brigade acts like that:
1. On the first day the brigade comes to the intersection of the 1-st horizontal and the 1-st vertical road. As none of them has been asphalted, the workers asphalt the 1-st vertical and the 1-st horizontal road;
2. On the second day the brigade of the workers comes to the intersection of the 1-st horizontal and the 2-nd vertical road. The 2-nd vertical road hasn't been asphalted, but as the 1-st horizontal road has been asphalted on the first day, the workers leave and do not asphalt anything;
3. On the third day the brigade of the workers come to the intersection of the 2-nd horizontal and the 1-st vertical road. The 2-nd horizontal road hasn't been asphalted but as the 1-st vertical road has been asphalted on the first day, the workers leave and do not asphalt anything;
4. On the fourth day the brigade come to the intersection formed by the intersection of the 2-nd horizontal and 2-nd vertical road. As none of them has been asphalted, the workers asphalt the 2-nd vertical and the 2-nd horizontal road.
|
[{"input": "2\n1 1\n1 2\n2 1\n2 2", "output": "1 4"}, {"input": "1\n1 1", "output": "1"}]
| 1,000
|
["implementation"]
| 39
|
[{"input": "2\r\n1 1\r\n1 2\r\n2 1\r\n2 2\r\n", "output": "1 4 \r\n"}, {"input": "1\r\n1 1\r\n", "output": "1 \r\n"}, {"input": "2\r\n1 1\r\n2 2\r\n1 2\r\n2 1\r\n", "output": "1 2 \r\n"}, {"input": "2\r\n1 2\r\n2 2\r\n2 1\r\n1 1\r\n", "output": "1 3 \r\n"}, {"input": "3\r\n2 2\r\n1 2\r\n3 2\r\n3 3\r\n1 1\r\n2 3\r\n1 3\r\n3 1\r\n2 1\r\n", "output": "1 4 5 \r\n"}, {"input": "3\r\n1 3\r\n3 1\r\n2 1\r\n1 1\r\n1 2\r\n2 2\r\n3 2\r\n3 3\r\n2 3\r\n", "output": "1 2 6 \r\n"}, {"input": "4\r\n1 3\r\n2 3\r\n2 4\r\n4 4\r\n3 1\r\n1 1\r\n3 4\r\n2 1\r\n1 4\r\n4 3\r\n4 1\r\n3 2\r\n1 2\r\n4 2\r\n2 2\r\n3 3\r\n", "output": "1 3 5 14 \r\n"}, {"input": "4\r\n3 3\r\n4 2\r\n2 3\r\n3 4\r\n4 4\r\n1 2\r\n3 2\r\n2 2\r\n1 4\r\n3 1\r\n4 1\r\n2 1\r\n1 3\r\n1 1\r\n4 3\r\n2 4\r\n", "output": "1 2 9 12 \r\n"}, {"input": "9\r\n4 5\r\n2 3\r\n8 3\r\n5 6\r\n9 3\r\n4 4\r\n5 4\r\n4 7\r\n1 7\r\n8 4\r\n1 4\r\n1 5\r\n5 7\r\n7 8\r\n7 1\r\n9 9\r\n8 7\r\n7 5\r\n3 7\r\n6 6\r\n7 3\r\n5 2\r\n3 6\r\n7 4\r\n9 6\r\n5 8\r\n9 7\r\n6 3\r\n7 9\r\n1 2\r\n1 1\r\n6 2\r\n5 3\r\n7 2\r\n1 6\r\n4 1\r\n6 1\r\n8 9\r\n2 2\r\n3 9\r\n2 9\r\n7 7\r\n2 8\r\n9 4\r\n2 5\r\n8 6\r\n3 4\r\n2 1\r\n2 7\r\n6 5\r\n9 1\r\n3 3\r\n3 8\r\n5 5\r\n4 3\r\n3 1\r\n1 9\r\n6 4\r\n3 2\r\n6 8\r\n2 6\r\n5 9\r\n8 5\r\n8 8\r\n9 5\r\n6 9\r\n9 2\r\n3 5\r\n4 9\r\n4 8\r\n2 4\r\n5 1\r\n4 6\r\n7 6\r\n9 8\r\n1 3\r\n4 2\r\n8 1\r\n8 2\r\n6 7\r\n1 8\r\n", "output": "1 2 4 9 10 14 16 32 56 \r\n"}, {"input": "8\r\n1 1\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n8 6\r\n1 7\r\n1 8\r\n2 1\r\n8 5\r\n2 3\r\n2 4\r\n2 5\r\n2 6\r\n4 3\r\n2 2\r\n3 1\r\n3 2\r\n3 3\r\n3 4\r\n3 5\r\n3 6\r\n5 6\r\n3 8\r\n4 1\r\n4 2\r\n2 7\r\n4 4\r\n8 8\r\n4 6\r\n4 7\r\n4 8\r\n5 1\r\n5 2\r\n5 3\r\n6 5\r\n5 5\r\n3 7\r\n5 7\r\n5 8\r\n6 1\r\n6 2\r\n6 3\r\n6 4\r\n5 4\r\n6 6\r\n6 7\r\n6 8\r\n7 1\r\n7 2\r\n7 3\r\n7 4\r\n7 5\r\n7 6\r\n7 7\r\n7 8\r\n8 1\r\n8 2\r\n8 3\r\n8 4\r\n2 8\r\n1 6\r\n8 7\r\n4 5\r\n", "output": "1 6 11 18 28 36 39 56 \r\n"}, {"input": "9\r\n9 9\r\n5 5\r\n8 8\r\n3 3\r\n2 2\r\n6 6\r\n4 4\r\n1 1\r\n7 7\r\n8 4\r\n1 4\r\n1 5\r\n5 7\r\n7 8\r\n7 1\r\n1 7\r\n8 7\r\n7 5\r\n3 7\r\n5 6\r\n7 3\r\n5 2\r\n3 6\r\n7 4\r\n9 6\r\n5 8\r\n9 7\r\n6 3\r\n7 9\r\n1 2\r\n4 5\r\n6 2\r\n5 3\r\n7 2\r\n1 6\r\n4 1\r\n6 1\r\n8 9\r\n2 3\r\n3 9\r\n2 9\r\n5 4\r\n2 8\r\n9 4\r\n2 5\r\n8 6\r\n3 4\r\n2 1\r\n2 7\r\n6 5\r\n9 1\r\n8 3\r\n3 8\r\n9 3\r\n4 3\r\n3 1\r\n1 9\r\n6 4\r\n3 2\r\n6 8\r\n2 6\r\n5 9\r\n8 5\r\n4 7\r\n9 5\r\n6 9\r\n9 2\r\n3 5\r\n4 9\r\n4 8\r\n2 4\r\n5 1\r\n4 6\r\n7 6\r\n9 8\r\n1 3\r\n4 2\r\n8 1\r\n8 2\r\n6 7\r\n1 8\r\n", "output": "1 2 3 4 5 6 7 8 9 \r\n"}]
| false
|
stdio
| null | true
|
231/A
|
231
|
A
|
Python 3
|
TESTS
| 4
| 92
| 0
|
232220033
|
count=0
for _ in range(int(input())):
a,b,c=map(int,input().split())
if a==1 and b==1 or c!=0:
count=count+1
elif a!=1 or b==1 and c==1:
count=count+1
elif b!=0 or a==1 and c==1:
count=count+1
print(count)
| 21
| 62
| 0
|
226202143
|
def solve(word):
if len(word) <= 10 : return word
return f"{word[0]}{len(word)-2}{word[-1]}"
n = int(input())
p = 0
for _ in range(n):
s = sum(map(int, input().split()))
if s >= 2:
p += 1
print(p)
|
Codeforces Round 143 (Div. 2)
|
CF
| 2,012
| 2
| 256
|
Team
|
One day three best friends Petya, Vasya and Tonya decided to form a team and take part in programming contests. Participants are usually offered several problems during programming contests. Long before the start the friends decided that they will implement a problem if at least two of them are sure about the solution. Otherwise, the friends won't write the problem's solution.
This contest offers n problems to the participants. For each problem we know, which friend is sure about the solution. Help the friends find the number of problems for which they will write a solution.
|
The first input line contains a single integer n (1 ≤ n ≤ 1000) — the number of problems in the contest. Then n lines contain three integers each, each integer is either 0 or 1. If the first number in the line equals 1, then Petya is sure about the problem's solution, otherwise he isn't sure. The second number shows Vasya's view on the solution, the third number shows Tonya's view. The numbers on the lines are separated by spaces.
|
Print a single integer — the number of problems the friends will implement on the contest.
| null |
In the first sample Petya and Vasya are sure that they know how to solve the first problem and all three of them know how to solve the second problem. That means that they will write solutions for these problems. Only Petya is sure about the solution for the third problem, but that isn't enough, so the friends won't take it.
In the second sample the friends will only implement the second problem, as Vasya and Tonya are sure about the solution.
|
[{"input": "3\n1 1 0\n1 1 1\n1 0 0", "output": "2"}, {"input": "2\n1 0 0\n0 1 1", "output": "1"}]
| 800
|
["brute force", "greedy"]
| 21
|
[{"input": "3\r\n1 1 0\r\n1 1 1\r\n1 0 0\r\n", "output": "2\r\n"}, {"input": "2\r\n1 0 0\r\n0 1 1\r\n", "output": "1\r\n"}, {"input": "1\r\n1 0 0\r\n", "output": "0\r\n"}, {"input": "2\r\n1 0 0\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "5\r\n1 0 0\r\n0 1 0\r\n1 1 1\r\n0 0 1\r\n0 0 0\r\n", "output": "1\r\n"}, {"input": "10\r\n0 1 0\r\n0 1 0\r\n1 1 0\r\n1 0 0\r\n0 0 1\r\n0 1 1\r\n1 1 1\r\n1 1 0\r\n0 0 0\r\n0 0 0\r\n", "output": "4\r\n"}, {"input": "15\r\n0 1 0\r\n1 0 0\r\n1 1 0\r\n1 1 1\r\n0 1 0\r\n0 0 1\r\n1 0 1\r\n1 0 1\r\n1 0 1\r\n0 0 0\r\n1 1 1\r\n1 1 0\r\n0 1 1\r\n1 1 0\r\n1 1 1\r\n", "output": "10\r\n"}, {"input": "50\r\n0 0 0\r\n0 1 1\r\n1 1 1\r\n0 1 0\r\n1 0 1\r\n1 1 1\r\n0 0 1\r\n1 0 0\r\n1 1 0\r\n1 0 1\r\n0 1 0\r\n0 0 1\r\n1 1 0\r\n0 1 0\r\n1 1 0\r\n0 0 0\r\n1 1 1\r\n1 0 1\r\n0 0 1\r\n1 1 0\r\n1 1 1\r\n0 1 1\r\n1 1 0\r\n0 0 0\r\n0 0 0\r\n1 1 1\r\n0 0 0\r\n1 1 1\r\n0 1 1\r\n0 0 1\r\n0 0 0\r\n0 0 0\r\n1 1 0\r\n1 1 0\r\n1 0 1\r\n1 0 0\r\n1 0 1\r\n1 0 1\r\n0 1 1\r\n1 1 0\r\n1 1 0\r\n0 1 0\r\n1 0 1\r\n0 0 0\r\n0 0 0\r\n0 0 0\r\n0 0 1\r\n1 1 1\r\n0 1 1\r\n1 0 1\r\n", "output": "29\r\n"}, {"input": "1\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "8\r\n0 0 0\r\n0 0 1\r\n0 0 0\r\n0 1 1\r\n1 0 0\r\n1 0 1\r\n1 1 0\r\n1 1 1\r\n", "output": "4\r\n"}, {"input": "16\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n", "output": "16\r\n"}]
| false
|
stdio
| null | true
|
231/A
|
231
|
A
|
PyPy 3-64
|
TESTS
| 2
| 124
| 0
|
231059587
|
a = []
l = 0
for d in range(int(input())):
a.append([int(i) for i in input('').split(' ')])
for x in a:
l += 1; p = 0
for b in x:
p = p + b
if p==3:
print(l)
break
elif x[0]==1 and x[1]==0 and x[2]==0:
print(l)
break
| 21
| 62
| 0
|
226206112
|
a=input()
b=[]
for i in range(int(a)):
c=input()
b.append(c)
d=0
for i in range(int(a)):
k=b[i]
n=k.count("1")
if(n>=2):
d+=1
print(d)
|
Codeforces Round 143 (Div. 2)
|
CF
| 2,012
| 2
| 256
|
Team
|
One day three best friends Petya, Vasya and Tonya decided to form a team and take part in programming contests. Participants are usually offered several problems during programming contests. Long before the start the friends decided that they will implement a problem if at least two of them are sure about the solution. Otherwise, the friends won't write the problem's solution.
This contest offers n problems to the participants. For each problem we know, which friend is sure about the solution. Help the friends find the number of problems for which they will write a solution.
|
The first input line contains a single integer n (1 ≤ n ≤ 1000) — the number of problems in the contest. Then n lines contain three integers each, each integer is either 0 or 1. If the first number in the line equals 1, then Petya is sure about the problem's solution, otherwise he isn't sure. The second number shows Vasya's view on the solution, the third number shows Tonya's view. The numbers on the lines are separated by spaces.
|
Print a single integer — the number of problems the friends will implement on the contest.
| null |
In the first sample Petya and Vasya are sure that they know how to solve the first problem and all three of them know how to solve the second problem. That means that they will write solutions for these problems. Only Petya is sure about the solution for the third problem, but that isn't enough, so the friends won't take it.
In the second sample the friends will only implement the second problem, as Vasya and Tonya are sure about the solution.
|
[{"input": "3\n1 1 0\n1 1 1\n1 0 0", "output": "2"}, {"input": "2\n1 0 0\n0 1 1", "output": "1"}]
| 800
|
["brute force", "greedy"]
| 21
|
[{"input": "3\r\n1 1 0\r\n1 1 1\r\n1 0 0\r\n", "output": "2\r\n"}, {"input": "2\r\n1 0 0\r\n0 1 1\r\n", "output": "1\r\n"}, {"input": "1\r\n1 0 0\r\n", "output": "0\r\n"}, {"input": "2\r\n1 0 0\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "5\r\n1 0 0\r\n0 1 0\r\n1 1 1\r\n0 0 1\r\n0 0 0\r\n", "output": "1\r\n"}, {"input": "10\r\n0 1 0\r\n0 1 0\r\n1 1 0\r\n1 0 0\r\n0 0 1\r\n0 1 1\r\n1 1 1\r\n1 1 0\r\n0 0 0\r\n0 0 0\r\n", "output": "4\r\n"}, {"input": "15\r\n0 1 0\r\n1 0 0\r\n1 1 0\r\n1 1 1\r\n0 1 0\r\n0 0 1\r\n1 0 1\r\n1 0 1\r\n1 0 1\r\n0 0 0\r\n1 1 1\r\n1 1 0\r\n0 1 1\r\n1 1 0\r\n1 1 1\r\n", "output": "10\r\n"}, {"input": "50\r\n0 0 0\r\n0 1 1\r\n1 1 1\r\n0 1 0\r\n1 0 1\r\n1 1 1\r\n0 0 1\r\n1 0 0\r\n1 1 0\r\n1 0 1\r\n0 1 0\r\n0 0 1\r\n1 1 0\r\n0 1 0\r\n1 1 0\r\n0 0 0\r\n1 1 1\r\n1 0 1\r\n0 0 1\r\n1 1 0\r\n1 1 1\r\n0 1 1\r\n1 1 0\r\n0 0 0\r\n0 0 0\r\n1 1 1\r\n0 0 0\r\n1 1 1\r\n0 1 1\r\n0 0 1\r\n0 0 0\r\n0 0 0\r\n1 1 0\r\n1 1 0\r\n1 0 1\r\n1 0 0\r\n1 0 1\r\n1 0 1\r\n0 1 1\r\n1 1 0\r\n1 1 0\r\n0 1 0\r\n1 0 1\r\n0 0 0\r\n0 0 0\r\n0 0 0\r\n0 0 1\r\n1 1 1\r\n0 1 1\r\n1 0 1\r\n", "output": "29\r\n"}, {"input": "1\r\n1 1 1\r\n", "output": "1\r\n"}, {"input": "8\r\n0 0 0\r\n0 0 1\r\n0 0 0\r\n0 1 1\r\n1 0 0\r\n1 0 1\r\n1 1 0\r\n1 1 1\r\n", "output": "4\r\n"}, {"input": "16\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n1 1 1\r\n", "output": "16\r\n"}]
| false
|
stdio
| null | true
|
583/A
|
583
|
A
|
Python 3
|
TESTS
| 5
| 61
| 0
|
13402525
|
n=int(input())
hor=[False]*n
ver=[False]*n
for i in range(n*n):
k=list(map(int,input().split()))
if (hor[k[0]-1]==True and ver[k[1]-1]==True):
break
if not(hor[k[0]-1]==True or ver[k[1]-1]==True):
ver[k[1]-1]=True
hor[k[0]-1]=True
print(i+1,end=' ')
| 39
| 46
| 0
|
173958781
|
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
n = int(input())
hzt = list(0 for x in range(n))
lzt = list(0 for x in range(n))
hgj = list(0 for x in range(n*n))
lgj = list(0 for x in range(n*n))
out = ""
for i in range(n*n):
inp = input().split()
hgj[i] = int(inp[0])-1
lgj[i] = int(inp[1])-1
for i in range(n*n):
if hzt[hgj[i]] == 0 and lzt[lgj[i]] == 0:
hzt[hgj[i]] = lzt[lgj[i]] = 1
out += f"{i+1} "
print(out)
|
Codeforces Round 323 (Div. 2)
|
CF
| 2,015
| 1
| 256
|
Asphalting Roads
|
City X consists of n vertical and n horizontal infinite roads, forming n × n intersections. Roads (both vertical and horizontal) are numbered from 1 to n, and the intersections are indicated by the numbers of the roads that form them.
Sand roads have long been recognized out of date, so the decision was made to asphalt them. To do this, a team of workers was hired and a schedule of work was made, according to which the intersections should be asphalted.
Road repairs are planned for n2 days. On the i-th day of the team arrives at the i-th intersection in the list and if none of the two roads that form the intersection were already asphalted they asphalt both roads. Otherwise, the team leaves the intersection, without doing anything with the roads.
According to the schedule of road works tell in which days at least one road will be asphalted.
|
The first line contains integer n (1 ≤ n ≤ 50) — the number of vertical and horizontal roads in the city.
Next n2 lines contain the order of intersections in the schedule. The i-th of them contains two numbers hi, vi (1 ≤ hi, vi ≤ n), separated by a space, and meaning that the intersection that goes i-th in the timetable is at the intersection of the hi-th horizontal and vi-th vertical roads. It is guaranteed that all the intersections in the timetable are distinct.
|
In the single line print the numbers of the days when road works will be in progress in ascending order. The days are numbered starting from 1.
| null |
In the sample the brigade acts like that:
1. On the first day the brigade comes to the intersection of the 1-st horizontal and the 1-st vertical road. As none of them has been asphalted, the workers asphalt the 1-st vertical and the 1-st horizontal road;
2. On the second day the brigade of the workers comes to the intersection of the 1-st horizontal and the 2-nd vertical road. The 2-nd vertical road hasn't been asphalted, but as the 1-st horizontal road has been asphalted on the first day, the workers leave and do not asphalt anything;
3. On the third day the brigade of the workers come to the intersection of the 2-nd horizontal and the 1-st vertical road. The 2-nd horizontal road hasn't been asphalted but as the 1-st vertical road has been asphalted on the first day, the workers leave and do not asphalt anything;
4. On the fourth day the brigade come to the intersection formed by the intersection of the 2-nd horizontal and 2-nd vertical road. As none of them has been asphalted, the workers asphalt the 2-nd vertical and the 2-nd horizontal road.
|
[{"input": "2\n1 1\n1 2\n2 1\n2 2", "output": "1 4"}, {"input": "1\n1 1", "output": "1"}]
| 1,000
|
["implementation"]
| 39
|
[{"input": "2\r\n1 1\r\n1 2\r\n2 1\r\n2 2\r\n", "output": "1 4 \r\n"}, {"input": "1\r\n1 1\r\n", "output": "1 \r\n"}, {"input": "2\r\n1 1\r\n2 2\r\n1 2\r\n2 1\r\n", "output": "1 2 \r\n"}, {"input": "2\r\n1 2\r\n2 2\r\n2 1\r\n1 1\r\n", "output": "1 3 \r\n"}, {"input": "3\r\n2 2\r\n1 2\r\n3 2\r\n3 3\r\n1 1\r\n2 3\r\n1 3\r\n3 1\r\n2 1\r\n", "output": "1 4 5 \r\n"}, {"input": "3\r\n1 3\r\n3 1\r\n2 1\r\n1 1\r\n1 2\r\n2 2\r\n3 2\r\n3 3\r\n2 3\r\n", "output": "1 2 6 \r\n"}, {"input": "4\r\n1 3\r\n2 3\r\n2 4\r\n4 4\r\n3 1\r\n1 1\r\n3 4\r\n2 1\r\n1 4\r\n4 3\r\n4 1\r\n3 2\r\n1 2\r\n4 2\r\n2 2\r\n3 3\r\n", "output": "1 3 5 14 \r\n"}, {"input": "4\r\n3 3\r\n4 2\r\n2 3\r\n3 4\r\n4 4\r\n1 2\r\n3 2\r\n2 2\r\n1 4\r\n3 1\r\n4 1\r\n2 1\r\n1 3\r\n1 1\r\n4 3\r\n2 4\r\n", "output": "1 2 9 12 \r\n"}, {"input": "9\r\n4 5\r\n2 3\r\n8 3\r\n5 6\r\n9 3\r\n4 4\r\n5 4\r\n4 7\r\n1 7\r\n8 4\r\n1 4\r\n1 5\r\n5 7\r\n7 8\r\n7 1\r\n9 9\r\n8 7\r\n7 5\r\n3 7\r\n6 6\r\n7 3\r\n5 2\r\n3 6\r\n7 4\r\n9 6\r\n5 8\r\n9 7\r\n6 3\r\n7 9\r\n1 2\r\n1 1\r\n6 2\r\n5 3\r\n7 2\r\n1 6\r\n4 1\r\n6 1\r\n8 9\r\n2 2\r\n3 9\r\n2 9\r\n7 7\r\n2 8\r\n9 4\r\n2 5\r\n8 6\r\n3 4\r\n2 1\r\n2 7\r\n6 5\r\n9 1\r\n3 3\r\n3 8\r\n5 5\r\n4 3\r\n3 1\r\n1 9\r\n6 4\r\n3 2\r\n6 8\r\n2 6\r\n5 9\r\n8 5\r\n8 8\r\n9 5\r\n6 9\r\n9 2\r\n3 5\r\n4 9\r\n4 8\r\n2 4\r\n5 1\r\n4 6\r\n7 6\r\n9 8\r\n1 3\r\n4 2\r\n8 1\r\n8 2\r\n6 7\r\n1 8\r\n", "output": "1 2 4 9 10 14 16 32 56 \r\n"}, {"input": "8\r\n1 1\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n8 6\r\n1 7\r\n1 8\r\n2 1\r\n8 5\r\n2 3\r\n2 4\r\n2 5\r\n2 6\r\n4 3\r\n2 2\r\n3 1\r\n3 2\r\n3 3\r\n3 4\r\n3 5\r\n3 6\r\n5 6\r\n3 8\r\n4 1\r\n4 2\r\n2 7\r\n4 4\r\n8 8\r\n4 6\r\n4 7\r\n4 8\r\n5 1\r\n5 2\r\n5 3\r\n6 5\r\n5 5\r\n3 7\r\n5 7\r\n5 8\r\n6 1\r\n6 2\r\n6 3\r\n6 4\r\n5 4\r\n6 6\r\n6 7\r\n6 8\r\n7 1\r\n7 2\r\n7 3\r\n7 4\r\n7 5\r\n7 6\r\n7 7\r\n7 8\r\n8 1\r\n8 2\r\n8 3\r\n8 4\r\n2 8\r\n1 6\r\n8 7\r\n4 5\r\n", "output": "1 6 11 18 28 36 39 56 \r\n"}, {"input": "9\r\n9 9\r\n5 5\r\n8 8\r\n3 3\r\n2 2\r\n6 6\r\n4 4\r\n1 1\r\n7 7\r\n8 4\r\n1 4\r\n1 5\r\n5 7\r\n7 8\r\n7 1\r\n1 7\r\n8 7\r\n7 5\r\n3 7\r\n5 6\r\n7 3\r\n5 2\r\n3 6\r\n7 4\r\n9 6\r\n5 8\r\n9 7\r\n6 3\r\n7 9\r\n1 2\r\n4 5\r\n6 2\r\n5 3\r\n7 2\r\n1 6\r\n4 1\r\n6 1\r\n8 9\r\n2 3\r\n3 9\r\n2 9\r\n5 4\r\n2 8\r\n9 4\r\n2 5\r\n8 6\r\n3 4\r\n2 1\r\n2 7\r\n6 5\r\n9 1\r\n8 3\r\n3 8\r\n9 3\r\n4 3\r\n3 1\r\n1 9\r\n6 4\r\n3 2\r\n6 8\r\n2 6\r\n5 9\r\n8 5\r\n4 7\r\n9 5\r\n6 9\r\n9 2\r\n3 5\r\n4 9\r\n4 8\r\n2 4\r\n5 1\r\n4 6\r\n7 6\r\n9 8\r\n1 3\r\n4 2\r\n8 1\r\n8 2\r\n6 7\r\n1 8\r\n", "output": "1 2 3 4 5 6 7 8 9 \r\n"}]
| false
|
stdio
| null | true
|
364/D
|
364
|
D
|
PyPy 3
|
TESTS
| 2
| 3,899
| 120,832,000
|
71106127
|
from typing import List, Set
from random import randrange
from sys import stdin
from functools import reduce, lru_cache
def read_integers():
return list(map(int, stdin.readline().strip().split()))
@lru_cache(None)
def decompose(num: int) -> List[int]:
if num <= 1:
return []
i = 2
while i ** 2 < num:
if num % i == 0:
left = decompose(i)
right = decompose(num // i)
return left + right
i += 1
return [num]
def ghd(arr: List[int]) -> int:
n_trials = 30
rd_lst = [randrange(0, len(arr)) for _ in range(n_trials)] # type: List[int]
factors = set(reduce(list.__add__, (decompose(arr[rd]) for rd in rd_lst))) # type: Set[int]
cnts = {f: sum(num % f == 0 for num in arr) for f in factors}
cnts = {k: v for k, v in cnts.items() if v * 2 >= len(arr)}
return max(cnts.keys()) if cnts else 1
if True:
_, = read_integers()
input_arr = read_integers()
print(ghd(input_arr))
| 115
| 3,900
| 133,734,400
|
179459001
|
import math
from random import randint
from bisect import bisect_left
test = False
if test:
n = randint(100000,100000)
a = [randint(1,100000000) for _ in range(n)]
#print('n =', n)
#print('a =', a)
else:
n = int(input())
a=[*map(int,input().split())]
dr = 1
for x in range(9):
r = randint(0, n-1)
ar = a[r]
m = int(math.sqrt(ar))
dells = []
dells1 = []
for i in range(1, m+1):
if (ar%i == 0):
if ar//i > dr:
dells1.insert(0, ar//i)
if i > dr:
dells.append(i)
dells.extend(dells1)
num_dells = len(dells)
s = [0]*num_dells
for i in range(n):
k = math.gcd(ar, a[i])
lo = bisect_left(dells, k)
if lo < num_dells and dells[lo] == k:
s[lo] += 1
for i in range(num_dells):
for j in range(i+1, num_dells):
if dells[j] % dells[i]==0:
s[i] += s[j]
if (s[i]*2>=n and dr < dells[i]):
dr = dells[i]
print(dr)
|
Codeforces Round 213 (Div. 1)
|
CF
| 2,013
| 4
| 256
|
Ghd
|
John Doe offered his sister Jane Doe find the gcd of some set of numbers a.
Gcd is a positive integer g, such that all number from the set are evenly divisible by g and there isn't such g' (g' > g), that all numbers of the set are evenly divisible by g'.
Unfortunately Jane couldn't cope with the task and John offered her to find the ghd of the same subset of numbers.
Ghd is a positive integer g, such that at least half of numbers from the set are evenly divisible by g and there isn't such g' (g' > g) that at least half of the numbers from the set are evenly divisible by g'.
Jane coped with the task for two hours. Please try it, too.
|
The first line contains an integer n (1 ≤ n ≤ 106) showing how many numbers are in set a. The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 1012). Please note, that given set can contain equal numbers.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the %I64d specifier.
|
Print a single integer g — the Ghd of set a.
| null | null |
[{"input": "6\n6 2 3 4 5 6", "output": "3"}, {"input": "5\n5 5 6 10 15", "output": "5"}]
| 2,900
|
["brute force", "math", "probabilities"]
| 115
|
[{"input": "6\r\n6 2 3 4 5 6\r\n", "output": "3\r\n"}, {"input": "5\r\n5 5 6 10 15\r\n", "output": "5\r\n"}, {"input": "100\r\n32 40 7 3 7560 21 7560 7560 10 12 3 7560 7560 7560 7560 5 7560 7560 6 7560 7560 7560 35 7560 18 7560 7560 7560 7560 7560 48 2 7 25 7560 2 2 49 7560 7560 15 16 7560 7560 2 7560 27 7560 7560 7560 7560 3 5 7560 8 7560 42 45 5 7560 5 7560 4 7 3 7560 7 3 7560 7 2 7560 7560 5 3 7560 7560 28 7560 7560 14 7560 5 7560 20 7560 24 7560 2 9 36 7 7560 7560 7560 7560 7560 30 7560 50\r\n", "output": "7560\r\n"}, {"input": "1\r\n3\r\n", "output": "3\r\n"}, {"input": "1\r\n7\r\n", "output": "7\r\n"}, {"input": "2\r\n1 7\r\n", "output": "7\r\n"}, {"input": "1\r\n1\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
355/B
|
355
|
B
|
Python 3
|
TESTS
| 0
| 92
| 0
|
48968117
|
s=input().split();c1,c2,c3,c4=int(s[0]),int(s[1]),int(s[2]),int(s[3])
s=input().split();n,m=int(s[0]),int(s[1]);a,b,c,d=0,0,0,0
x=input().split();y=input().split();xx=[];yy=[]
a=c1*(n+m);b=c2*(n+m);c=c3*2;d=c4
print(min(a,b,c,d))
| 27
| 46
| 307,200
|
4771087
|
#PublicTransport:
costs = input().split(" ")
costs = [int(x) for x in costs]
data = input().split(" ")
totalBuses = int(data[0])
totalTrolleys = int(data[1])
buses = input().split(" ")
trolleys = input().split(" ")
buses = [int(x) for x in buses]
trolleys = [int(x) for x in trolleys]
possibleCosts = [0,0,0,0]
totalBusTravels = sum(buses)
totalTrolleysTravels = sum(trolleys)
cost = [0,0]
#Buses
for busCost in buses:
if busCost*costs[0] > costs[1]:
cost[0] += costs[1]
else:
cost[0] += busCost*costs[0]
if cost[0] > costs[2]:
cost[0] = costs[2]
# Trolleys
for trolleyCost in trolleys:
if trolleyCost*costs[0] > costs[1]:
cost[1] += costs[1]
else:
cost[1] += trolleyCost*costs[0]
if cost[1] > costs[2]:
cost[1] = costs[2]
if cost[0]+cost[1] > costs[3]:
print(costs[3])
else:
print(cost[0]+cost[1])
|
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
|
358/D
|
358
|
D
|
Python 3
|
TESTS
| 3
| 46
| 0
|
5348404
|
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
def f(i, j):
if j - i == 1: return max(a[i], b[i])
k = (i + j) // 2
u, v = a[k], a[k - 1]
a[k], b[k] = b[k], c[k]
x = f(i, k) + f(k, j)
a[k], b[k], c[k] = u, a[k], b[k]
a[k - 1], b[k - 1] = b[k - 1], c[k - 1]
y = f(i, k) + f(k, j)
a[k - 1], b[k - 1], c[k - 1] = v, a[k - 1], b[k - 1]
return max(x, y)
print(f(0, n))
| 29
| 78
| 3,072,000
|
154457435
|
I = lambda:list(map(int, input().split()))
n,= I()
a = I()
b = I()
c = I()
before = a[0] # choosing this before the next element
after = b[0] # choosing this after the next element
for i in range(1,n):
before, after = max(after + a[i], before + b[i]), max(after + b[i], before + c[i])
print(before)
|
Codeforces Round 208 (Div. 2)
|
CF
| 2,013
| 2
| 256
|
Dima and Hares
|
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.
Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?
Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.
Help Inna maximize the total joy the hares radiate. :)
|
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≤ ai, bi, ci ≤ 105.
Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number bi in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
|
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
| null | null |
[{"input": "4\n1 2 3 4\n4 3 2 1\n0 1 1 0", "output": "13"}, {"input": "7\n8 5 7 6 1 8 9\n2 7 9 5 4 3 1\n2 3 3 4 1 1 3", "output": "44"}, {"input": "3\n1 1 1\n1 2 1\n1 1 1", "output": "4"}]
| 1,800
|
["dp", "greedy"]
| 29
|
[{"input": "4\r\n1 2 3 4\r\n4 3 2 1\r\n0 1 1 0\r\n", "output": "13\r\n"}, {"input": "7\r\n8 5 7 6 1 8 9\r\n2 7 9 5 4 3 1\r\n2 3 3 4 1 1 3\r\n", "output": "44\r\n"}, {"input": "3\r\n1 1 1\r\n1 2 1\r\n1 1 1\r\n", "output": "4\r\n"}, {"input": "7\r\n1 3 8 9 3 4 4\r\n6 0 6 6 1 8 4\r\n9 6 3 7 8 8 2\r\n", "output": "42\r\n"}, {"input": "2\r\n3 5\r\n9 8\r\n4 0\r\n", "output": "14\r\n"}, {"input": "7\r\n3 6 1 5 4 2 0\r\n9 7 3 7 2 6 0\r\n1 6 5 7 5 4 1\r\n", "output": "37\r\n"}, {"input": "1\r\n0\r\n1\r\n4\r\n", "output": "0\r\n"}, {"input": "1\r\n7\r\n1\r\n7\r\n", "output": "7\r\n"}, {"input": "8\r\n7 3 3 5 9 9 8 1\r\n8 2 6 6 0 3 8 0\r\n1 2 5 0 9 4 7 8\r\n", "output": "49\r\n"}, {"input": "6\r\n1 2 0 1 6 4\r\n0 6 1 8 9 8\r\n4 1 4 3 9 8\r\n", "output": "33\r\n"}, {"input": "1\r\n0\r\n0\r\n0\r\n", "output": "0\r\n"}, {"input": "1\r\n100000\r\n100000\r\n100000\r\n", "output": "100000\r\n"}]
| false
|
stdio
| null | true
|
245/A
|
245
|
A
|
Python 3
|
TESTS
| 6
| 218
| 0
|
57454249
|
m=n=0
n=int(input())
for i in range(n):
count1, x, y = map(int, input().split())
if count1==1:
m+=(x-y)
else:
n+=(x-y)
if m>=0:
print('LIVE')
else:
print('DEAD')
if n>=0:
print('LIVE')
else:
print('DEAD')
| 13
| 62
| 0
|
146715578
|
n = int(input())
x = 0
xa = 0
y = 0
ya = 0
for i in range(n):
s = input()
s = s.split()
if s[0] == "1":
x = x + int(s[1])
xa = xa + int(s[1]) + int(s[2])
elif s[0] == "2":
y = y + int(s[1])
ya = ya + int(s[1]) + int(s[2])
if x >= xa//2:
print("LIVE")
else:
print("DEAD")
if y >= ya//2:
print("LIVE")
else:
print("DEAD")
|
CROC-MBTU 2012, Elimination Round (ACM-ICPC)
|
ICPC
| 2,012
| 2
| 256
|
System Administrator
|
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.
Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.
Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
|
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost.
It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
|
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes).
In the second line print the state of server b in the similar format.
| null |
Consider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.
Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
|
[{"input": "2\n1 5 5\n2 6 4", "output": "LIVE\nLIVE"}, {"input": "3\n1 0 10\n2 0 10\n1 10 0", "output": "LIVE\nDEAD"}]
| 800
|
["implementation"]
| 23
|
[{"input": "2\r\n1 5 5\r\n2 6 4\r\n", "output": "LIVE\r\nLIVE\r\n"}, {"input": "3\r\n1 0 10\r\n2 0 10\r\n1 10 0\r\n", "output": "LIVE\r\nDEAD\r\n"}, {"input": "10\r\n1 3 7\r\n2 4 6\r\n1 2 8\r\n2 5 5\r\n2 10 0\r\n2 10 0\r\n1 8 2\r\n2 2 8\r\n2 10 0\r\n1 1 9\r\n", "output": "DEAD\r\nLIVE\r\n"}, {"input": "11\r\n1 8 2\r\n1 6 4\r\n1 9 1\r\n1 7 3\r\n2 0 10\r\n2 0 10\r\n1 8 2\r\n2 2 8\r\n2 6 4\r\n2 7 3\r\n2 9 1\r\n", "output": "LIVE\r\nDEAD\r\n"}, {"input": "12\r\n1 5 5\r\n1 0 10\r\n1 4 6\r\n1 2 8\r\n1 2 8\r\n1 5 5\r\n1 9 1\r\n2 9 1\r\n1 5 5\r\n1 1 9\r\n2 9 1\r\n2 7 3\r\n", "output": "DEAD\r\nLIVE\r\n"}, {"input": "13\r\n1 8 2\r\n1 4 6\r\n1 5 5\r\n1 5 5\r\n2 10 0\r\n2 9 1\r\n1 3 7\r\n2 6 4\r\n2 6 4\r\n2 5 5\r\n1 7 3\r\n2 3 7\r\n2 9 1\r\n", "output": "LIVE\r\nLIVE\r\n"}, {"input": "14\r\n1 7 3\r\n1 0 10\r\n1 7 3\r\n1 1 9\r\n2 2 8\r\n2 0 10\r\n1 1 9\r\n2 8 2\r\n2 6 4\r\n1 3 7\r\n1 3 7\r\n2 6 4\r\n2 1 9\r\n2 7 3\r\n", "output": "DEAD\r\nDEAD\r\n"}]
| false
|
stdio
| null | true
|
245/A
|
245
|
A
|
PyPy 3-64
|
TESTS
| 6
| 124
| 0
|
193245929
|
n = int(input())
ping_a = 0
ping_b = 0
for _ in range(n):
t, x, y = map(int, input().split())
if t == 1:
if x >= y:
ping_a += x
else:
ping_a -= y
else:
if x >= y:
ping_b += x
else:
ping_b -= y
if ping_a >= 0:
print("LIVE")
else:
print("DEAD")
if ping_b >= 0:
print("LIVE")
else:
print("DEAD")
| 13
| 62
| 0
|
149083102
|
a=b=0;
for _ in [0]*int(input()):
l,x,y=map(int,input().split())
if l==1:a+=x-y
else:b+=x-y
print('DLEIAVDE'[a>=0::2])
print('DLEIAVDE'[b>=0::2])
|
CROC-MBTU 2012, Elimination Round (ACM-ICPC)
|
ICPC
| 2,012
| 2
| 256
|
System Administrator
|
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.
Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.
Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
|
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost.
It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
|
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes).
In the second line print the state of server b in the similar format.
| null |
Consider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.
Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
|
[{"input": "2\n1 5 5\n2 6 4", "output": "LIVE\nLIVE"}, {"input": "3\n1 0 10\n2 0 10\n1 10 0", "output": "LIVE\nDEAD"}]
| 800
|
["implementation"]
| 23
|
[{"input": "2\r\n1 5 5\r\n2 6 4\r\n", "output": "LIVE\r\nLIVE\r\n"}, {"input": "3\r\n1 0 10\r\n2 0 10\r\n1 10 0\r\n", "output": "LIVE\r\nDEAD\r\n"}, {"input": "10\r\n1 3 7\r\n2 4 6\r\n1 2 8\r\n2 5 5\r\n2 10 0\r\n2 10 0\r\n1 8 2\r\n2 2 8\r\n2 10 0\r\n1 1 9\r\n", "output": "DEAD\r\nLIVE\r\n"}, {"input": "11\r\n1 8 2\r\n1 6 4\r\n1 9 1\r\n1 7 3\r\n2 0 10\r\n2 0 10\r\n1 8 2\r\n2 2 8\r\n2 6 4\r\n2 7 3\r\n2 9 1\r\n", "output": "LIVE\r\nDEAD\r\n"}, {"input": "12\r\n1 5 5\r\n1 0 10\r\n1 4 6\r\n1 2 8\r\n1 2 8\r\n1 5 5\r\n1 9 1\r\n2 9 1\r\n1 5 5\r\n1 1 9\r\n2 9 1\r\n2 7 3\r\n", "output": "DEAD\r\nLIVE\r\n"}, {"input": "13\r\n1 8 2\r\n1 4 6\r\n1 5 5\r\n1 5 5\r\n2 10 0\r\n2 9 1\r\n1 3 7\r\n2 6 4\r\n2 6 4\r\n2 5 5\r\n1 7 3\r\n2 3 7\r\n2 9 1\r\n", "output": "LIVE\r\nLIVE\r\n"}, {"input": "14\r\n1 7 3\r\n1 0 10\r\n1 7 3\r\n1 1 9\r\n2 2 8\r\n2 0 10\r\n1 1 9\r\n2 8 2\r\n2 6 4\r\n1 3 7\r\n1 3 7\r\n2 6 4\r\n2 1 9\r\n2 7 3\r\n", "output": "DEAD\r\nDEAD\r\n"}]
| false
|
stdio
| null | true
|
67/A
|
67
|
A
|
PyPy 3
|
TESTS
| 6
| 312
| 66,457,600
|
78491182
|
import sys
from math import log2,floor,ceil,sqrt,gcd
import bisect
# from collections import deque
sys.setrecursionlimit(10**5)
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 1000000007
# for _ in range(int(ri())):
n = ri()
st = ri()
arr = []
cnt = 1
arr.append(cnt)
for i in st:
if i == 'L':
arr.append(cnt-1)
cnt-=1
elif i == 'R':
arr.append(cnt+1)
cnt+=1
else:
arr.append(cnt)
minn = min(arr)
if minn <= 0:
arr = [ arr[i]+abs(minn)+1 for i in range(len(arr))]
print(*arr)
| 43
| 186
| 7,065,600
|
36777991
|
n=int(input())
s=input()
w=[1]*n
for i in range(n-1):
if s[i]=='=':
w[i+1]=w[i]
elif s[i]=='R':
if w[i+1]<=w[i]:
w[i+1]=w[i]+1
else:
if w[i]<=w[i+1]:
w[i]=w[i+1]+1
for i in range(n-2,-1,-1):
if s[i]=='=':
w[i]=w[i+1]
elif s[i]=='R':
if w[i+1]==w[i]:
w[i+1]=w[i]+1
else:
if w[i]<=w[i+1]:
w[i]=w[i+1]+1
for i in range(n):
print(w[i],end=' ')
|
Manthan 2011
|
CF
| 2,011
| 1
| 256
|
Partial Teacher
|
A teacher decides to give toffees to his students. He asks n students to stand in a queue. Since the teacher is very partial, he follows the following rule to distribute toffees.
He looks at the first two students and gives more toffees to the student having higher marks than the other one. If they have the same marks they get the same number of toffees. The same procedure is followed for each pair of adjacent students starting from the first one to the last one.
It is given that each student receives at least one toffee. You have to find the number of toffees given to each student by the teacher such that the total number of toffees is minimum.
|
The first line of input contains the number of students n (2 ≤ n ≤ 1000). The second line gives (n - 1) characters consisting of "L", "R" and "=". For each pair of adjacent students "L" means that the left student has higher marks, "R" means that the right student has higher marks and "=" means that both have equal marks.
|
Output consists of n integers separated by a space representing the number of toffees each student receives in the queue starting from the first one to the last one.
| null | null |
[{"input": "5\nLRLR", "output": "2 1 2 1 2"}, {"input": "5\n=RRR", "output": "1 1 2 3 4"}]
| 1,800
|
["dp", "graphs", "greedy", "implementation"]
| 43
|
[{"input": "5\r\nLRLR\r\n", "output": "2 1 2 1 2\r\n"}, {"input": "5\r\n=RRR\r\n", "output": "1 1 2 3 4\r\n"}, {"input": "6\r\nRLRL=\r\n", "output": "1 2 1 2 1 1\r\n"}, {"input": "3\r\nR=\r\n", "output": "1 2 2\r\n"}, {"input": "7\r\nRR==RR\r\n", "output": "1 2 3 3 3 4 5\r\n"}, {"input": "166\r\nR===RL=LRRR=RRRL=LRR=R=RR==L=R=R=RRR=RR=RLLRRL=LLRL==L=R==RLR==RL=RR=LR==R=R=LLRLRLR=RR=RLLRLR=RRLL==L=LR=RR=RRRL=RLLLR==L=RRLRLLLLLLLRL===LRLRLRLRRLL=LRLL===LRLRR==\r\n", "output": "1 2 2 2 2 3 2 2 1 2 3 4 4 5 6 7 2 2 1 2 3 3 4 4 5 6 6 6 1 1 2 2 3 3 4 5 6 6 7 8 8 9 2 1 2 4 3 3 2 1 3 2 2 2 1 1 2 2 2 3 1 2 2 2 3 1 1 2 3 3 1 2 2 2 3 3 4 4 2 1 2 1 2 1 2 2 3 4 4 5 2 1 2 1 2 2 3 5 4 3 3 3 2 2 1 2 2 3 4 4 5 6 7 1 1 4 3 2 1 2 2 2 1 1 2 3 1 8 7 6 5 4 3 2 1 3 2 2 2 2 1 2 1 2 1 2 1 2 4 3 2 2 1 4 3 2 2 2 2 1 2 1 2 3 3 3\n"}, {"input": "24\r\nR=R==RL=RL=RLL=LLL=LLRL\r\n", "output": "1 2 2 3 3 3 4 1 1 2 1 1 8 7 6 6 5 4 3 3 2 1 2 1\r\n"}, {"input": "100\r\n=L=L=L=R=LR=RRRLRL=LRL=RRLLLLRL=R==R=LLLRR===RR=LR==LRLR===RRLRLLRLLR=LRLRR=L=LRRLLLRR==LLRLLLL==RL\r\n", "output": "4 4 3 3 2 2 1 1 2 2 1 2 2 3 4 5 1 3 2 2 1 2 1 1 2 5 4 3 2 1 2 1 1 2 2 2 4 4 3 2 1 2 3 3 3 3 4 5 5 1 2 2 2 1 2 1 2 2 2 2 3 4 1 3 2 1 3 2 1 2 2 1 2 1 2 3 3 2 2 1 2 4 3 2 1 2 3 3 3 2 1 5 4 3 2 1 1 1 2 1\r\n"}, {"input": "198\r\nLLRRR=RRRRLRRLRR=R===R=RL==R=RLLLR=R=L=LR=R====RRL=RRR=LL=R=RR=RRRLRRLRRR==L=LRLLL====LR=RL==L===LRR=L=L==R==R==L=LLL===R=LLL=R=L=LLLLRLL=RL=LRRLR=RL==RR=R==RLR==R=R==RLRL=LL=RRR=R===LLLRRRRL=RLRLL\r\n", "output": "3 2 1 2 3 4 4 5 6 7 8 1 2 3 1 2 3 3 4 4 4 4 5 5 6 1 1 1 2 2 4 3 2 1 2 2 3 3 2 2 1 2 2 3 3 3 3 3 4 5 1 1 2 3 4 4 2 1 1 2 2 3 4 4 5 6 7 1 2 3 1 2 3 4 4 4 2 2 1 5 4 3 2 2 2 2 2 1 2 2 4 3 3 3 2 2 2 2 1 2 3 3 2 2 1 1 1 2 2 2 5 5 5 4 4 3 2 1 1 1 1 4 4 3 2 1 1 6 6 5 5 4 3 2 1 3 2 1 1 3 2 2 1 2 3 1 2 2 3 1 1 1 2 3 3 4 4 4 5 1 2 2 2 3 3 4 4 4 5 1 4 3 3 2 1 1 2 3 4 4 5 5 5 5 3 2 1 2 3 4 5 1 1 2 1 3 2 1\n"}, {"input": "10\r\nRL=R=RLR=\r\n", "output": "1 2 1 1 2 2 3 1 2 2\r\n"}, {"input": "2\r\nL\r\n", "output": "2 1\r\n"}, {"input": "100\r\nR=R=RRR=R=RR=RRLL=RLRLLLLLR==L=======L=LLR==RL=R=LRLLLR==LLLL=RRRL=LRL=LR=====L=LLLRRL=LLR===RLR=RR\r\n", "output": "1 2 2 3 3 4 5 6 6 7 7 8 9 9 10 11 2 1 1 2 1 6 5 4 3 2 1 5 5 5 4 4 4 4 4 4 4 4 3 3 2 1 2 2 2 3 1 1 2 2 1 4 3 2 1 5 5 5 4 3 2 1 1 2 3 4 2 2 1 3 2 2 1 5 5 5 5 5 5 4 4 3 2 1 2 4 3 3 2 1 2 2 2 2 3 1 2 2 3 4\r\n"}, {"input": "23\r\nL=LLLLRL=RR=RLLLL=RR==\r\n", "output": "6 5 5 4 3 2 1 2 1 1 2 3 3 5 4 3 2 1 1 2 3 3 3\r\n"}, {"input": "4\r\nRRL\r\n", "output": "1 2 3 1\r\n"}, {"input": "17\r\n=RRR=L==LLLLRRRL\r\n", "output": "1 1 2 3 6 6 5 5 5 4 3 2 1 2 3 4 1\r\n"}, {"input": "20\r\nRRLLLLLRRRRRRRRLRLR\r\n", "output": "1 2 6 5 4 3 2 1 2 3 4 5 6 7 8 9 1 2 1 2\r\n"}, {"input": "9\r\nR===RRLL\r\n", "output": "1 2 2 2 2 3 4 2 1\r\n"}, {"input": "15\r\n=RRR=LLLLLRRRL\r\n", "output": "1 1 2 3 6 6 5 4 3 2 1 2 3 4 1\r\n"}]
| false
|
stdio
| null | true
|
43/C
|
43
|
C
|
PyPy 3
|
TESTS
| 3
| 154
| 102,400
|
116967416
|
from collections import defaultdict, deque
from heapq import heappush, heappop
from math import inf
ri = lambda : map(int, input().split())
def sm(x):
ans = 0
while x:
ans += (x % 10)
x //= 10
return ans
def solve():
n = int(input())
A = list(ri())
ans = 0
cnt = defaultdict(int)
for x in A:
d = sm(x)
if d % 3 == 0:
if cnt[0] > 1:
cnt[0] -= 1
ans += 1
else:
cnt[0] += 1
elif cnt[3-(d%3)] > 1:
cnt[3-(d%3)] -= 1
ans += 1
else:
cnt[d%3] += 1
print(ans)
t = 1
#t = int(input())
while t:
t -= 1
solve()
| 21
| 92
| 409,600
|
138404090
|
def r(a):#SHIT Luogu's robot can't catch my AC
return int(a)%3
a=input();a=list(map(r,input().split()));print(a.count(0)//2+min(a.count(1),a.count(2)))
|
Codeforces Beta Round 42 (Div. 2)
|
CF
| 2,010
| 2
| 256
|
Lucky Tickets
|
Vasya thinks that lucky tickets are the tickets whose numbers are divisible by 3. He gathered quite a large collection of such tickets but one day his younger brother Leonid was having a sulk and decided to destroy the collection. First he tore every ticket exactly in two, but he didn’t think it was enough and Leonid also threw part of the pieces away. Having seen this, Vasya got terrified but still tried to restore the collection. He chose several piece pairs and glued each pair together so that each pair formed a lucky ticket. The rest of the pieces Vasya threw away reluctantly. Thus, after the gluing of the 2t pieces he ended up with t tickets, each of which was lucky.
When Leonid tore the tickets in two pieces, one piece contained the first several letters of his number and the second piece contained the rest.
Vasya can glue every pair of pieces in any way he likes, but it is important that he gets a lucky ticket in the end. For example, pieces 123 and 99 can be glued in two ways: 12399 and 99123.
What maximum number of tickets could Vasya get after that?
|
The first line contains integer n (1 ≤ n ≤ 104) — the number of pieces. The second line contains n space-separated numbers ai (1 ≤ ai ≤ 108) — the numbers on the pieces. Vasya can only glue the pieces in pairs. Even if the number of a piece is already lucky, Vasya should glue the piece with some other one for it to count as lucky. Vasya does not have to use all the pieces. The numbers on the pieces an on the resulting tickets may coincide.
|
Print the single number — the maximum number of lucky tickets that will be able to be restored. Don't forget that every lucky ticket is made of exactly two pieces glued together.
| null | null |
[{"input": "3\n123 123 99", "output": "1"}, {"input": "6\n1 1 1 23 10 3", "output": "1"}]
| 1,300
|
["greedy"]
| 21
|
[{"input": "3\r\n123 123 99\r\n", "output": "1\r\n"}, {"input": "6\r\n1 1 1 23 10 3\r\n", "output": "1\r\n"}, {"input": "3\r\n43440907 58238452 82582355\r\n", "output": "1\r\n"}, {"input": "4\r\n31450303 81222872 67526764 17516401\r\n", "output": "1\r\n"}, {"input": "5\r\n83280 20492640 21552119 7655071 47966344\r\n", "output": "2\r\n"}, {"input": "6\r\n94861402 89285133 30745405 41537407 90189008 83594323\r\n", "output": "1\r\n"}, {"input": "7\r\n95136773 99982752 97528336 79027944 96847471 96928960 89423004\r\n", "output": "2\r\n"}, {"input": "1\r\n19938466\r\n", "output": "0\r\n"}, {"input": "2\r\n55431511 35254032\r\n", "output": "0\r\n"}, {"input": "2\r\n28732939 23941418\r\n", "output": "1\r\n"}, {"input": "10\r\n77241684 71795210 50866429 35232438 22664883 56785812 91050433 75677099 84393937 43832346\r\n", "output": "4\r\n"}]
| false
|
stdio
| null | true
|
67/A
|
67
|
A
|
PyPy 3
|
TESTS
| 6
| 310
| 204,800
|
41874586
|
n = int(input())
s = input()
ans = [1]
now = 1
d = 1
i = 0
for el in s:
if el == 'L':
now -= 1
elif el == '=':
pass
else:
now += 1
ans.append(now)
d = min(now, d)
i += 1
for el in ans:
print(el - d + 1)
| 43
| 216
| 2,048,000
|
105218138
|
n = int(input())
s = input()
ans = [0 for i in range(n)]
for i in range(n):
min_val = 1
if i > 0:
if s[i - 1] == '=':
ans[i] = ans[i - 1]
continue
if s[i - 1] == 'R':
min_val = ans[i - 1] + 1
cnt_low = 0
for j in range(i, n - 1):
if s[j] == '=':
continue
if s[j] == 'L':
cnt_low += 1
if s[j] == 'R':
break
min_val = max(min_val, cnt_low + 1)
ans[i] = min_val
print(*ans)
|
Manthan 2011
|
CF
| 2,011
| 1
| 256
|
Partial Teacher
|
A teacher decides to give toffees to his students. He asks n students to stand in a queue. Since the teacher is very partial, he follows the following rule to distribute toffees.
He looks at the first two students and gives more toffees to the student having higher marks than the other one. If they have the same marks they get the same number of toffees. The same procedure is followed for each pair of adjacent students starting from the first one to the last one.
It is given that each student receives at least one toffee. You have to find the number of toffees given to each student by the teacher such that the total number of toffees is minimum.
|
The first line of input contains the number of students n (2 ≤ n ≤ 1000). The second line gives (n - 1) characters consisting of "L", "R" and "=". For each pair of adjacent students "L" means that the left student has higher marks, "R" means that the right student has higher marks and "=" means that both have equal marks.
|
Output consists of n integers separated by a space representing the number of toffees each student receives in the queue starting from the first one to the last one.
| null | null |
[{"input": "5\nLRLR", "output": "2 1 2 1 2"}, {"input": "5\n=RRR", "output": "1 1 2 3 4"}]
| 1,800
|
["dp", "graphs", "greedy", "implementation"]
| 43
|
[{"input": "5\r\nLRLR\r\n", "output": "2 1 2 1 2\r\n"}, {"input": "5\r\n=RRR\r\n", "output": "1 1 2 3 4\r\n"}, {"input": "6\r\nRLRL=\r\n", "output": "1 2 1 2 1 1\r\n"}, {"input": "3\r\nR=\r\n", "output": "1 2 2\r\n"}, {"input": "7\r\nRR==RR\r\n", "output": "1 2 3 3 3 4 5\r\n"}, {"input": "166\r\nR===RL=LRRR=RRRL=LRR=R=RR==L=R=R=RRR=RR=RLLRRL=LLRL==L=R==RLR==RL=RR=LR==R=R=LLRLRLR=RR=RLLRLR=RRLL==L=LR=RR=RRRL=RLLLR==L=RRLRLLLLLLLRL===LRLRLRLRRLL=LRLL===LRLRR==\r\n", "output": "1 2 2 2 2 3 2 2 1 2 3 4 4 5 6 7 2 2 1 2 3 3 4 4 5 6 6 6 1 1 2 2 3 3 4 5 6 6 7 8 8 9 2 1 2 4 3 3 2 1 3 2 2 2 1 1 2 2 2 3 1 2 2 2 3 1 1 2 3 3 1 2 2 2 3 3 4 4 2 1 2 1 2 1 2 2 3 4 4 5 2 1 2 1 2 2 3 5 4 3 3 3 2 2 1 2 2 3 4 4 5 6 7 1 1 4 3 2 1 2 2 2 1 1 2 3 1 8 7 6 5 4 3 2 1 3 2 2 2 2 1 2 1 2 1 2 1 2 4 3 2 2 1 4 3 2 2 2 2 1 2 1 2 3 3 3\n"}, {"input": "24\r\nR=R==RL=RL=RLL=LLL=LLRL\r\n", "output": "1 2 2 3 3 3 4 1 1 2 1 1 8 7 6 6 5 4 3 3 2 1 2 1\r\n"}, {"input": "100\r\n=L=L=L=R=LR=RRRLRL=LRL=RRLLLLRL=R==R=LLLRR===RR=LR==LRLR===RRLRLLRLLR=LRLRR=L=LRRLLLRR==LLRLLLL==RL\r\n", "output": "4 4 3 3 2 2 1 1 2 2 1 2 2 3 4 5 1 3 2 2 1 2 1 1 2 5 4 3 2 1 2 1 1 2 2 2 4 4 3 2 1 2 3 3 3 3 4 5 5 1 2 2 2 1 2 1 2 2 2 2 3 4 1 3 2 1 3 2 1 2 2 1 2 1 2 3 3 2 2 1 2 4 3 2 1 2 3 3 3 2 1 5 4 3 2 1 1 1 2 1\r\n"}, {"input": "198\r\nLLRRR=RRRRLRRLRR=R===R=RL==R=RLLLR=R=L=LR=R====RRL=RRR=LL=R=RR=RRRLRRLRRR==L=LRLLL====LR=RL==L===LRR=L=L==R==R==L=LLL===R=LLL=R=L=LLLLRLL=RL=LRRLR=RL==RR=R==RLR==R=R==RLRL=LL=RRR=R===LLLRRRRL=RLRLL\r\n", "output": "3 2 1 2 3 4 4 5 6 7 8 1 2 3 1 2 3 3 4 4 4 4 5 5 6 1 1 1 2 2 4 3 2 1 2 2 3 3 2 2 1 2 2 3 3 3 3 3 4 5 1 1 2 3 4 4 2 1 1 2 2 3 4 4 5 6 7 1 2 3 1 2 3 4 4 4 2 2 1 5 4 3 2 2 2 2 2 1 2 2 4 3 3 3 2 2 2 2 1 2 3 3 2 2 1 1 1 2 2 2 5 5 5 4 4 3 2 1 1 1 1 4 4 3 2 1 1 6 6 5 5 4 3 2 1 3 2 1 1 3 2 2 1 2 3 1 2 2 3 1 1 1 2 3 3 4 4 4 5 1 2 2 2 3 3 4 4 4 5 1 4 3 3 2 1 1 2 3 4 4 5 5 5 5 3 2 1 2 3 4 5 1 1 2 1 3 2 1\n"}, {"input": "10\r\nRL=R=RLR=\r\n", "output": "1 2 1 1 2 2 3 1 2 2\r\n"}, {"input": "2\r\nL\r\n", "output": "2 1\r\n"}, {"input": "100\r\nR=R=RRR=R=RR=RRLL=RLRLLLLLR==L=======L=LLR==RL=R=LRLLLR==LLLL=RRRL=LRL=LR=====L=LLLRRL=LLR===RLR=RR\r\n", "output": "1 2 2 3 3 4 5 6 6 7 7 8 9 9 10 11 2 1 1 2 1 6 5 4 3 2 1 5 5 5 4 4 4 4 4 4 4 4 3 3 2 1 2 2 2 3 1 1 2 2 1 4 3 2 1 5 5 5 4 3 2 1 1 2 3 4 2 2 1 3 2 2 1 5 5 5 5 5 5 4 4 3 2 1 2 4 3 3 2 1 2 2 2 2 3 1 2 2 3 4\r\n"}, {"input": "23\r\nL=LLLLRL=RR=RLLLL=RR==\r\n", "output": "6 5 5 4 3 2 1 2 1 1 2 3 3 5 4 3 2 1 1 2 3 3 3\r\n"}, {"input": "4\r\nRRL\r\n", "output": "1 2 3 1\r\n"}, {"input": "17\r\n=RRR=L==LLLLRRRL\r\n", "output": "1 1 2 3 6 6 5 5 5 4 3 2 1 2 3 4 1\r\n"}, {"input": "20\r\nRRLLLLLRRRRRRRRLRLR\r\n", "output": "1 2 6 5 4 3 2 1 2 3 4 5 6 7 8 9 1 2 1 2\r\n"}, {"input": "9\r\nR===RRLL\r\n", "output": "1 2 2 2 2 3 4 2 1\r\n"}, {"input": "15\r\n=RRR=LLLLLRRRL\r\n", "output": "1 1 2 3 6 6 5 4 3 2 1 2 3 4 1\r\n"}]
| false
|
stdio
| null | true
|
67/A
|
67
|
A
|
PyPy 3
|
TESTS
| 6
| 218
| 2,150,400
|
105213737
|
n = int(input())
s = input()
ans = []
best_val = 10 ** 9
cur = [0 for i in range(n)]
for fs in range(1, n + 8):
cur[0] = fs
bad = False
for i, c in enumerate(s):
if c == '=':
cur[i + 1] = cur[i]
elif c == 'L':
cur[i + 1] = cur[i] - 1
else:
cur[i + 1] = cur[i] + 1
if cur[i + 1] <= 0:
bad = True
break
if bad:
continue
cur_sum = sum(cur)
if cur_sum < best_val:
best_val = cur_sum
ans = cur[:]
print(*ans)
| 43
| 218
| 0
|
41982823
|
n = int(input())
p = 'R' + input() + 'R'
t = [0] * n
i = t[0] = l = r = 1
L = R = 0
while i < n + 1:
if p[i] == 'L':
if r > 1:
t[R] = r
L, r = i, 1
l += 1
elif p[i] == 'R':
if l > 1:
if t[R] < l: t[R] = l
else: t[L] = l - 1
l = 1
R = i
r += 1
i += 1
for i in range(1, n):
if t[i] == 0:
if p[i] == 'L': t[i] = t[i - 1] - 1
elif p[i] == 'R': t[i] = t[i - 1] + 1
else: t[i] = t[i - 1]
print(' '.join(map(str, t)))
|
Manthan 2011
|
CF
| 2,011
| 1
| 256
|
Partial Teacher
|
A teacher decides to give toffees to his students. He asks n students to stand in a queue. Since the teacher is very partial, he follows the following rule to distribute toffees.
He looks at the first two students and gives more toffees to the student having higher marks than the other one. If they have the same marks they get the same number of toffees. The same procedure is followed for each pair of adjacent students starting from the first one to the last one.
It is given that each student receives at least one toffee. You have to find the number of toffees given to each student by the teacher such that the total number of toffees is minimum.
|
The first line of input contains the number of students n (2 ≤ n ≤ 1000). The second line gives (n - 1) characters consisting of "L", "R" and "=". For each pair of adjacent students "L" means that the left student has higher marks, "R" means that the right student has higher marks and "=" means that both have equal marks.
|
Output consists of n integers separated by a space representing the number of toffees each student receives in the queue starting from the first one to the last one.
| null | null |
[{"input": "5\nLRLR", "output": "2 1 2 1 2"}, {"input": "5\n=RRR", "output": "1 1 2 3 4"}]
| 1,800
|
["dp", "graphs", "greedy", "implementation"]
| 43
|
[{"input": "5\r\nLRLR\r\n", "output": "2 1 2 1 2\r\n"}, {"input": "5\r\n=RRR\r\n", "output": "1 1 2 3 4\r\n"}, {"input": "6\r\nRLRL=\r\n", "output": "1 2 1 2 1 1\r\n"}, {"input": "3\r\nR=\r\n", "output": "1 2 2\r\n"}, {"input": "7\r\nRR==RR\r\n", "output": "1 2 3 3 3 4 5\r\n"}, {"input": "166\r\nR===RL=LRRR=RRRL=LRR=R=RR==L=R=R=RRR=RR=RLLRRL=LLRL==L=R==RLR==RL=RR=LR==R=R=LLRLRLR=RR=RLLRLR=RRLL==L=LR=RR=RRRL=RLLLR==L=RRLRLLLLLLLRL===LRLRLRLRRLL=LRLL===LRLRR==\r\n", "output": "1 2 2 2 2 3 2 2 1 2 3 4 4 5 6 7 2 2 1 2 3 3 4 4 5 6 6 6 1 1 2 2 3 3 4 5 6 6 7 8 8 9 2 1 2 4 3 3 2 1 3 2 2 2 1 1 2 2 2 3 1 2 2 2 3 1 1 2 3 3 1 2 2 2 3 3 4 4 2 1 2 1 2 1 2 2 3 4 4 5 2 1 2 1 2 2 3 5 4 3 3 3 2 2 1 2 2 3 4 4 5 6 7 1 1 4 3 2 1 2 2 2 1 1 2 3 1 8 7 6 5 4 3 2 1 3 2 2 2 2 1 2 1 2 1 2 1 2 4 3 2 2 1 4 3 2 2 2 2 1 2 1 2 3 3 3\n"}, {"input": "24\r\nR=R==RL=RL=RLL=LLL=LLRL\r\n", "output": "1 2 2 3 3 3 4 1 1 2 1 1 8 7 6 6 5 4 3 3 2 1 2 1\r\n"}, {"input": "100\r\n=L=L=L=R=LR=RRRLRL=LRL=RRLLLLRL=R==R=LLLRR===RR=LR==LRLR===RRLRLLRLLR=LRLRR=L=LRRLLLRR==LLRLLLL==RL\r\n", "output": "4 4 3 3 2 2 1 1 2 2 1 2 2 3 4 5 1 3 2 2 1 2 1 1 2 5 4 3 2 1 2 1 1 2 2 2 4 4 3 2 1 2 3 3 3 3 4 5 5 1 2 2 2 1 2 1 2 2 2 2 3 4 1 3 2 1 3 2 1 2 2 1 2 1 2 3 3 2 2 1 2 4 3 2 1 2 3 3 3 2 1 5 4 3 2 1 1 1 2 1\r\n"}, {"input": "198\r\nLLRRR=RRRRLRRLRR=R===R=RL==R=RLLLR=R=L=LR=R====RRL=RRR=LL=R=RR=RRRLRRLRRR==L=LRLLL====LR=RL==L===LRR=L=L==R==R==L=LLL===R=LLL=R=L=LLLLRLL=RL=LRRLR=RL==RR=R==RLR==R=R==RLRL=LL=RRR=R===LLLRRRRL=RLRLL\r\n", "output": "3 2 1 2 3 4 4 5 6 7 8 1 2 3 1 2 3 3 4 4 4 4 5 5 6 1 1 1 2 2 4 3 2 1 2 2 3 3 2 2 1 2 2 3 3 3 3 3 4 5 1 1 2 3 4 4 2 1 1 2 2 3 4 4 5 6 7 1 2 3 1 2 3 4 4 4 2 2 1 5 4 3 2 2 2 2 2 1 2 2 4 3 3 3 2 2 2 2 1 2 3 3 2 2 1 1 1 2 2 2 5 5 5 4 4 3 2 1 1 1 1 4 4 3 2 1 1 6 6 5 5 4 3 2 1 3 2 1 1 3 2 2 1 2 3 1 2 2 3 1 1 1 2 3 3 4 4 4 5 1 2 2 2 3 3 4 4 4 5 1 4 3 3 2 1 1 2 3 4 4 5 5 5 5 3 2 1 2 3 4 5 1 1 2 1 3 2 1\n"}, {"input": "10\r\nRL=R=RLR=\r\n", "output": "1 2 1 1 2 2 3 1 2 2\r\n"}, {"input": "2\r\nL\r\n", "output": "2 1\r\n"}, {"input": "100\r\nR=R=RRR=R=RR=RRLL=RLRLLLLLR==L=======L=LLR==RL=R=LRLLLR==LLLL=RRRL=LRL=LR=====L=LLLRRL=LLR===RLR=RR\r\n", "output": "1 2 2 3 3 4 5 6 6 7 7 8 9 9 10 11 2 1 1 2 1 6 5 4 3 2 1 5 5 5 4 4 4 4 4 4 4 4 3 3 2 1 2 2 2 3 1 1 2 2 1 4 3 2 1 5 5 5 4 3 2 1 1 2 3 4 2 2 1 3 2 2 1 5 5 5 5 5 5 4 4 3 2 1 2 4 3 3 2 1 2 2 2 2 3 1 2 2 3 4\r\n"}, {"input": "23\r\nL=LLLLRL=RR=RLLLL=RR==\r\n", "output": "6 5 5 4 3 2 1 2 1 1 2 3 3 5 4 3 2 1 1 2 3 3 3\r\n"}, {"input": "4\r\nRRL\r\n", "output": "1 2 3 1\r\n"}, {"input": "17\r\n=RRR=L==LLLLRRRL\r\n", "output": "1 1 2 3 6 6 5 5 5 4 3 2 1 2 3 4 1\r\n"}, {"input": "20\r\nRRLLLLLRRRRRRRRLRLR\r\n", "output": "1 2 6 5 4 3 2 1 2 3 4 5 6 7 8 9 1 2 1 2\r\n"}, {"input": "9\r\nR===RRLL\r\n", "output": "1 2 2 2 2 3 4 2 1\r\n"}, {"input": "15\r\n=RRR=LLLLLRRRL\r\n", "output": "1 1 2 3 6 6 5 4 3 2 1 2 3 4 1\r\n"}]
| false
|
stdio
| null | true
|
840/B
|
840
|
B
|
PyPy 3
|
TESTS
| 6
| 2,932
| 75,673,600
|
75710122
|
# https://codeforces.com/problemset/problem/840/B
n, m = map(int, input().split())
d = list(map(int, input().split()))
g={}
def push(g, u, v, i):
if u not in g:
g[u]=[]
if v not in g:
g[v]=[]
g[u].append([v, i+1])
g[v].append([u, i+1])
for i in range(m):
u, v = map(int, input().split())
push(g, u-1, v-1, i)
def solve(d, g):
pos = []
S = 0
for i, x in enumerate(d):
if x==-1:
pos.append(i)
else:
S+=x
if S%2==1:
if len(pos)==0:
return False, None
d[pos[0]]=1
for x in pos[1:]:
d[x]=0
root=0
S = [root]
edge = [-1]
used = [0]*n
used[root]=1
p=[None]*n
i=0
while i<len(S):
u=S[i]
if u in g:
for v, edge_ in g[u]:
if v==p[u] or used[v]==1:
continue
p[v]=u
used[v]=1
S.append(v)
edge.append(edge_)
i+=1
ans=[]
for v, edge_ in zip(S[1:][::-1], edge[1:][::-1]):
if d[v]==1:
u = p[v]
d[u]=1-d[u]
ans.append(str(edge_))
return True, ans
flg, ans =solve(d, g)
if flg==False:
print(-1)
else:
print(len(ans))
print('\n'.join(ans))
| 73
| 2,979
| 87,756,800
|
167362143
|
import bisect
import copy
import decimal
import fractions
import heapq
import itertools
import math
import random
import sys
import time
from collections import Counter,deque,defaultdict
from functools import lru_cache,reduce
from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max
def _heappush_max(heap,item):
heap.append(item)
heapq._siftdown_max(heap, 0, len(heap)-1)
def _heappushpop_max(heap, item):
if heap and item < heap[0]:
item, heap[0] = heap[0], item
heapq._siftup_max(heap, 0)
return item
from math import gcd as GCD
read=sys.stdin.read
readline=sys.stdin.readline
readlines=sys.stdin.readlines
class Graph:
def __init__(self,V,edges=False,graph=False,directed=False,weighted=False,inf=float("inf")):
self.V=V
self.directed=directed
self.weighted=weighted
self.inf=inf
if graph:
self.graph=graph
self.edges=[]
for i in range(self.V):
if self.weighted:
for j,d in self.graph[i]:
if self.directed or not self.directed and i<=j:
self.edges.append((i,j,d))
else:
for j in self.graph[i]:
if self.directed or not self.directed and i<=j:
self.edges.append((i,j))
else:
self.edges=edges
self.graph=[[] for i in range(self.V)]
if weighted:
for i,j,d in self.edges:
self.graph[i].append((j,d))
if not self.directed:
self.graph[j].append((i,d))
else:
for i,j in self.edges:
self.graph[i].append(j)
if not self.directed:
self.graph[j].append(i)
def SIV_DFS(self,s,bipartite_graph=False,cycle_detection=False,directed_acyclic=False,euler_tour=False,linked_components=False,lowlink=False,parents=False,postorder=False,preorder=False,subtree_size=False,topological_sort=False,unweighted_dist=False,weighted_dist=False):
seen=[False]*self.V
finished=[False]*self.V
if directed_acyclic or cycle_detection or topological_sort:
dag=True
if euler_tour:
et=[]
if linked_components:
lc=[]
if lowlink:
order=[None]*self.V
ll=[None]*self.V
idx=0
if parents or cycle_detection or lowlink or subtree_size:
ps=[None]*self.V
if postorder or topological_sort:
post=[]
if preorder:
pre=[]
if subtree_size:
ss=[1]*self.V
if unweighted_dist or bipartite_graph:
uwd=[self.inf]*self.V
uwd[s]=0
if weighted_dist:
wd=[self.inf]*self.V
wd[s]=0
stack=[(s,0)] if self.weighted else [s]
while stack:
if self.weighted:
x,d=stack.pop()
else:
x=stack.pop()
if not seen[x]:
seen[x]=True
stack.append((x,d) if self.weighted else x)
if euler_tour:
et.append(x)
if linked_components:
lc.append(x)
if lowlink:
order[x]=idx
ll[x]=idx
idx+=1
if preorder:
pre.append(x)
for y in self.graph[x]:
if self.weighted:
y,d=y
if not seen[y]:
stack.append((y,d) if self.weighted else y)
if parents or cycle_detection or lowlink or subtree_size:
ps[y]=x
if unweighted_dist or bipartite_graph:
uwd[y]=uwd[x]+1
if weighted_dist:
wd[y]=wd[x]+d
elif not finished[y]:
if (directed_acyclic or cycle_detection or topological_sort) and dag:
dag=False
if cycle_detection:
cd=(y,x)
elif not finished[x]:
finished[x]=True
if euler_tour:
et.append(~x)
if lowlink:
bl=True
for y in self.graph[x]:
if self.weighted:
y,d=y
if ps[x]==y and bl:
bl=False
continue
ll[x]=min(ll[x],order[y])
if x!=s:
ll[ps[x]]=min(ll[ps[x]],ll[x])
if postorder or topological_sort:
post.append(x)
if subtree_size:
for y in self.graph[x]:
if self.weighted:
y,d=y
if y==ps[x]:
continue
ss[x]+=ss[y]
if bipartite_graph:
bg=[[],[]]
for tpl in self.edges:
x,y=tpl[:2] if self.weighted else tpl
if uwd[x]==self.inf or uwd[y]==self.inf:
continue
if not uwd[x]%2^uwd[y]%2:
bg=False
break
else:
for x in range(self.V):
if uwd[x]==self.inf:
continue
bg[uwd[x]%2].append(x)
retu=()
if bipartite_graph:
retu+=(bg,)
if cycle_detection:
if dag:
cd=[]
else:
y,x=cd
cd=self.Route_Restoration(y,x,ps)
retu+=(cd,)
if directed_acyclic:
retu+=(dag,)
if euler_tour:
retu+=(et,)
if linked_components:
retu+=(lc,)
if lowlink:
retu=(ll,)
if parents:
retu+=(ps,)
if postorder:
retu+=(post,)
if preorder:
retu+=(pre,)
if subtree_size:
retu+=(ss,)
if topological_sort:
if dag:
tp_sort=post[::-1]
else:
tp_sort=[]
retu+=(tp_sort,)
if unweighted_dist:
retu+=(uwd,)
if weighted_dist:
retu+=(wd,)
if len(retu)==1:
retu=retu[0]
return retu
N,M=map(int,readline().split())
D=list(map(int,readline().split()))
edges=[]
idx={}
for i in range(M):
u,v=map(int,readline().split())
u-=1;v-=1
edges.append((u,v))
idx[(u,v)]=i
idx[(v,u)]=i
G=Graph(N,edges=edges)
parents,tour=G.SIV_DFS(0,parents=True,postorder=True)
if D.count(1)%2:
for i in range(N):
if D[i]==-1:
D[i]=1
break
for i in range(N):
if D[i]==-1:
D[i]=0
if D.count(1)%2:
print(-1)
exit()
ans_lst=[]
for x in tour[:-1]:
if D[x]:
ans_lst.append(idx[(x,parents[x])]+1)
D[x]^=1
D[parents[x]]^=1
print(len(ans_lst))
if ans_lst:
print(*ans_lst,sep="\n")
|
Codeforces Round 429 (Div. 1)
|
CF
| 2,017
| 3
| 256
|
Leha and another game about graph
|
Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or - 1. To pass the level, he needs to find a «good» subset of edges of the graph or say, that it doesn't exist. Subset is called «good», if by by leaving only edges from this subset in the original graph, we obtain the following: for every vertex i, di = - 1 or it's degree modulo 2 is equal to di. Leha wants to pass the game as soon as possible and ask you to help him. In case of multiple correct answers, print any of them.
|
The first line contains two integers n, m (1 ≤ n ≤ 3·105, n - 1 ≤ m ≤ 3·105) — number of vertices and edges.
The second line contains n integers d1, d2, ..., dn ( - 1 ≤ di ≤ 1) — numbers on the vertices.
Each of the next m lines contains two integers u and v (1 ≤ u, v ≤ n) — edges. It's guaranteed, that graph in the input is connected.
|
Print - 1 in a single line, if solution doesn't exist. Otherwise in the first line k — number of edges in a subset. In the next k lines indexes of edges. Edges are numerated in order as they are given in the input, starting from 1.
| null |
In the first sample we have single vertex without edges. It's degree is 0 and we can not get 1.
|
[{"input": "1 0\n1", "output": "-1"}, {"input": "4 5\n0 0 0 -1\n1 2\n2 3\n3 4\n1 4\n2 4", "output": "0"}, {"input": "2 1\n1 1\n1 2", "output": "1\n1"}, {"input": "3 3\n0 -1 1\n1 2\n2 3\n1 3", "output": "1\n2"}]
| 2,100
|
["constructive algorithms", "data structures", "dfs and similar", "dp", "graphs"]
| 73
|
[{"input": "1 0\r\n1\r\n", "output": "-1\r\n"}, {"input": "4 5\r\n0 0 0 -1\r\n1 2\r\n2 3\r\n3 4\r\n1 4\r\n2 4\r\n", "output": "0\r\n"}, {"input": "2 1\r\n1 1\r\n1 2\r\n", "output": "1\r\n1\r\n"}, {"input": "3 3\r\n0 -1 1\r\n1 2\r\n2 3\r\n1 3\r\n", "output": "1\r\n2\r\n"}, {"input": "10 10\r\n-1 -1 -1 -1 -1 -1 -1 -1 -1 -1\r\n6 7\r\n8 3\r\n6 4\r\n4 2\r\n9 2\r\n5 10\r\n9 8\r\n10 7\r\n5 1\r\n6 2\r\n", "output": "0\r\n"}, {"input": "3 2\r\n1 0 1\r\n1 2\r\n2 3\r\n", "output": "2\r\n2\r\n1\r\n"}]
| false
|
stdio
|
import sys
def main(input_path, output_path, submission_output_path):
# Read input
with open(input_path, 'r') as f:
n, m = map(int, f.readline().split())
d = list(map(int, f.readline().split()))
edges = [tuple(map(int, f.readline().split())) for _ in range(m)]
# Read submission output
with open(submission_output_path, 'r') as f:
lines = f.read().strip().split('\n')
if not lines:
print(0)
return
first_line = lines[0].strip()
if first_line == '-1':
sum_d = sum(x for x in d if x != -1)
has_neg1 = any(x == -1 for x in d)
exists = (sum_d % 2 == 0) or (has_neg1 and (sum_d % 2 == 1))
print(0 if exists else 1)
return
else:
try:
k = int(first_line)
if len(lines) < k + 1:
print(0)
return
selected = list(map(int, lines[1:1 + k]))
except:
print(0)
return
# Check edge indices
if any(e < 1 or e > m for e in selected):
print(0)
return
if len(set(selected)) != len(selected):
print(0)
return
# Compute degrees
degree = [0] * (n + 1) # 1-based
for e in selected:
u, v = edges[e - 1]
degree[u] += 1
degree[v] += 1
# Check each vertex
valid = True
for i in range(n):
di = d[i]
if di == -1:
continue
if degree[i + 1] % 2 != di:
valid = False
break
print(1 if valid else 0)
if __name__ == '__main__':
input_path = sys.argv[1]
output_path = sys.argv[2]
submission_output_path = sys.argv[3]
main(input_path, output_path, submission_output_path)
| true
|
367/A
|
367
|
A
|
PyPy 3
|
TESTS
| 2
| 93
| 21,401,600
|
37013331
|
s = input()
px, py, pz = ([0], [0], [0])
for i in range(len(s)):
if s[i] == 'x':
px.append(px[-1] + 1)
py.append(py[-1])
pz.append(pz[-1])
elif s[i] == 'y':
px.append(px[-1])
py.append(py[-1] + 1)
pz.append(pz[-1])
else:
px.append(px[-1])
py.append(py[-1])
pz.append(pz[-1] + 1)
m = int(input())
for i in range(m):
l, r = map(int, input().split())
x = px[r] - px[l - 1]
y = py[r] - py[l - 1]
z = pz[r] - pz[l - 1]
mn = min(x, y, z)
mx = max(x, y, z)
if r-l+1>3 and mx - mn > 1:
print('NO')
else:
print('YES')
| 38
| 436
| 10,752,000
|
103951315
|
import sys
# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')
input = sys.stdin.readline
s = input().strip()
x_c, y_c, z_c = [0], [0], [0]
for el in s:
x_c.append(x_c[-1]+int(el=="x"))
y_c.append(y_c[-1]+int(el=="y"))
z_c.append(z_c[-1]+int(el=="z"))
for _ in range(int(input())):
l, r = map(int, input().split())
if r - l < 2:
print("YES")
continue
x, y, z = x_c[r]-x_c[l-1], y_c[r]-y_c[l-1], z_c[r]-z_c[l-1]
if max(x, y, z) - min(x, y, z) > 1:
print("NO")
else:
print("YES")
|
Codeforces Round 215 (Div. 1)
|
CF
| 2,013
| 1
| 256
|
Sereja and Algorithm
|
Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
2. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.
|
The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.
The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
|
For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.
| null |
In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.
|
[{"input": "zyxxxxxxyyz\n5\n5 5\n1 3\n1 11\n1 4\n3 6", "output": "YES\nYES\nNO\nYES\nNO"}]
| 1,500
|
["data structures", "implementation"]
| 38
|
[{"input": "zyxxxxxxyyz\r\n5\r\n5 5\r\n1 3\r\n1 11\r\n1 4\r\n3 6\r\n", "output": "YES\r\nYES\r\nNO\r\nYES\r\nNO\r\n"}, {"input": "yxzyzxzzxyyzzxxxzyyzzyzxxzxyzyyzxyzxyxxyzxyxzyzxyzxyyxzzzyzxyyxyzxxy\r\n10\r\n17 67\r\n6 35\r\n12 45\r\n56 56\r\n14 30\r\n25 54\r\n1 1\r\n46 54\r\n3 33\r\n19 40\r\n", "output": "NO\r\nNO\r\nNO\r\nYES\r\nYES\r\nNO\r\nYES\r\nNO\r\nNO\r\nYES\r\n"}, {"input": "xxxxyyxyyzzyxyxzxyzyxzyyyzyzzxxxxzyyzzzzyxxxxzzyzzyzx\r\n5\r\n4 4\r\n3 3\r\n1 24\r\n3 28\r\n18 39\r\n", "output": "YES\r\nYES\r\nNO\r\nNO\r\nNO\r\n"}, {"input": "yzxyzxyzxzxzyzxyzyzzzyxzyz\r\n9\r\n4 6\r\n2 7\r\n3 5\r\n14 24\r\n3 13\r\n2 24\r\n2 5\r\n2 14\r\n3 15\r\n", "output": "YES\r\nYES\r\nYES\r\nNO\r\nYES\r\nNO\r\nYES\r\nNO\r\nNO\r\n"}, {"input": "zxyzxyzyyzxzzxyzxyzx\r\n15\r\n7 10\r\n17 17\r\n6 7\r\n8 14\r\n4 7\r\n11 18\r\n12 13\r\n1 1\r\n3 8\r\n1 1\r\n9 17\r\n4 4\r\n5 11\r\n3 15\r\n1 1\r\n", "output": "NO\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nYES\r\nNO\r\nYES\r\nNO\r\nYES\r\nYES\r\nNO\r\nYES\r\n"}, {"input": "x\r\n1\r\n1 1\r\n", "output": "YES\r\n"}]
| false
|
stdio
| null | true
|
641/B
|
641
|
B
|
PyPy 3-64
|
TESTS
| 5
| 61
| 3,379,200
|
167458638
|
import sys
input = sys.stdin.readline
n, m, q = map(int, input().split())
d1 = [0]*n
d2 = [0]*m
d = [[0]*m for i in range(m)]
for _ in range(q):
w = list(map(int, input().split()))
if w[0] == 1:
d1[w[1]-1] += 1
elif w[0] == 2:
d2[w[1]-1] += 1
else:
a, b, c = w[1]-1, w[2]-1, w[3]
a = (d2[a] + a) % n
b = (d1[b] + b) % m
d[a][b] = c
for i in d:
print(' '.join(map(str, i)))
| 26
| 202
| 1,843,200
|
22010274
|
import sys
import time
from pprint import pprint
from sys import stderr
from itertools import combinations
INF = 10 ** 18 + 3
EPS = 1e-10
MAX_CACHE = 10 ** 9
# Decorators
def time_it(function, output=stderr):
def wrapped(*args, **kwargs):
start = time.time()
res = function(*args, **kwargs)
elapsed_time = time.time() - start
print('"%s" took %f ms' % (function.__name__, elapsed_time * 1000),
file=output)
return res
return wrapped
@time_it
def main():
n, m, q = map(int, input().split())
matrix = [[(x, y) for x in range(m)]
for y in range(n)]
init_matrix = [[0] * m for _ in range(n)]
for _ in range(q):
args = list(map(int, input().split()))
t = args[1] - 1
if args[0] == 1:
matrix[t] = matrix[t][1:] + matrix[t][:1]
elif args[0] == 2:
first = matrix[0][t]
for i in range(n - 1):
matrix[i][t] = matrix[i + 1][t]
matrix[n - 1][t] = first
else:
r = args[2] - 1
x = args[3]
init_matrix[matrix[t][r][1]][matrix[t][r][0]] = x
for row in init_matrix:
print(*row)
def set_input(file):
global input
input = lambda: file.readline().strip()
def set_output(file):
global print
local_print = print
def print(*args, **kwargs):
kwargs["file"] = kwargs.get("file", file)
return local_print(*args, **kwargs)
if __name__ == '__main__':
set_input(open("input.txt", "r") if "MINE" in sys.argv else sys.stdin)
set_output(sys.stdout)
main()
|
VK Cup 2016 - Round 2
|
CF
| 2,016
| 2
| 256
|
Little Artem and Matrix
|
Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.
That element can store information about the matrix of integers size n × m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to m from left to right.
Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.
Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists.
|
The first line of the input contains three integers n, m and q (1 ≤ n, m ≤ 100, 1 ≤ q ≤ 10 000) — dimensions of the matrix and the number of turns in the experiment, respectively.
Next q lines contain turns descriptions, one per line. Each description starts with an integer ti (1 ≤ ti ≤ 3) that defines the type of the operation. For the operation of first and second type integer ri (1 ≤ ri ≤ n) or ci (1 ≤ ci ≤ m) follows, while for the operations of the third type three integers ri, ci and xi (1 ≤ ri ≤ n, 1 ≤ ci ≤ m, - 109 ≤ xi ≤ 109) are given.
Operation of the first type (ti = 1) means that signal comes to the input corresponding to row ri, that is it will shift cyclically. Operation of the second type (ti = 2) means that column ci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row ri and column ci stores value xi.
|
Print the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 109 by their absolute value.
If there are multiple valid solutions, output any of them.
| null | null |
[{"input": "2 2 6\n2 1\n2 2\n3 1 1 1\n3 2 2 2\n3 1 2 8\n3 2 1 8", "output": "8 2\n1 8"}, {"input": "3 3 2\n1 2\n3 2 2 5", "output": "0 0 0\n0 0 5\n0 0 0"}]
| 1,400
|
["implementation"]
| 26
|
[{"input": "2 2 6\r\n2 1\r\n2 2\r\n3 1 1 1\r\n3 2 2 2\r\n3 1 2 8\r\n3 2 1 8\r\n", "output": "8 2 \r\n1 8 \r\n"}, {"input": "3 3 2\r\n1 2\r\n3 2 2 5\r\n", "output": "0 0 0 \r\n0 0 5 \r\n0 0 0 \r\n"}, {"input": "5 5 1\r\n1 5\r\n", "output": "0 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"}, {"input": "1 1 3\r\n1 1\r\n2 1\r\n3 1 1 1000000000\r\n", "output": "1000000000 \r\n"}, {"input": "1 1 3\r\n1 1\r\n2 1\r\n3 1 1 -1000000000\r\n", "output": "-1000000000 \r\n"}, {"input": "2 2 6\r\n2 1\r\n2 2\r\n3 1 1 -1\r\n3 2 2 -1\r\n3 1 2 -1\r\n3 2 1 -1\r\n", "output": "-1 -1 \r\n-1 -1 \r\n"}, {"input": "1 4 5\r\n1 1\r\n3 1 1 1\r\n3 1 2 2\r\n3 1 3 3\r\n3 1 4 4\r\n", "output": "4 1 2 3 \r\n"}, {"input": "4 2 5\r\n2 1\r\n3 1 1 5\r\n3 2 1 6\r\n3 3 1 7\r\n3 4 1 9\r\n", "output": "9 0 \r\n5 0 \r\n6 0 \r\n7 0 \r\n"}, {"input": "3 10 2\r\n1 2\r\n3 2 7 5\r\n", "output": "0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 5 0 0 \r\n0 0 0 0 0 0 0 0 0 0 \r\n"}, {"input": "1 2 2\r\n1 1\r\n3 1 2 15\r\n", "output": "15 0 \r\n"}]
| false
|
stdio
| null | true
|
415/B
|
415
|
B
|
Python 3
|
TESTS
| 3
| 109
| 7,577,600
|
206730855
|
n, a, b = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
ans = []
for i in range(n):
ans.append((c[i] * a) % b)
print(*ans)
| 47
| 77
| 11,571,200
|
227718511
|
i=input;d=int;n,a,b=map(d,i().split());print("".join(f'{(x*a%b)//a} 'for x in map(d,i().split())))
|
Codeforces Round 240 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Mashmokh and Tokens
|
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $${ \left[ { \frac { w \cdot a } { b } } \right] }$$ dollars.
Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x1, x2, ..., xn. Number xi is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save.
|
The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The second line of input contains n space-separated integers x1, x2, ..., xn (1 ≤ xi ≤ 109).
|
Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day.
| null | null |
[{"input": "5 1 4\n12 6 11 9 1", "output": "0 2 3 1 1"}, {"input": "3 1 2\n1 2 3", "output": "1 0 1"}, {"input": "1 1 1\n1", "output": "0"}]
| 1,500
|
["binary search", "greedy", "implementation", "math"]
| 47
|
[{"input": "5 1 4\r\n12 6 11 9 1\r\n", "output": "0 2 3 1 1 "}, {"input": "3 1 2\r\n1 2 3\r\n", "output": "1 0 1 "}, {"input": "1 1 1\r\n1\r\n", "output": "0 "}, {"input": "1 1 1000000000\r\n1000000000\r\n", "output": "0 "}, {"input": "1 1 1000000000\r\n999999999\r\n", "output": "999999999 "}, {"input": "10 1 100000000\r\n999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999\r\n", "output": "99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 "}]
| false
|
stdio
| null | true
|
415/B
|
415
|
B
|
Python 3
|
TESTS
| 3
| 109
| 10,035,200
|
24410593
|
n, a, b = map(int, input().split())
x = list(map(int, input().split()))
print(' '.join( str( ( xi * a ) % b ) for xi in x ) )
| 47
| 93
| 18,227,200
|
172591635
|
n, a, b = map(int, input().split())
tokens = list(map(int, input().split()))
ans = []
for t in tokens:
ans.append(((t * a) % b) // a)
print(' '.join(map(str, ans)))
|
Codeforces Round 240 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Mashmokh and Tokens
|
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $${ \left[ { \frac { w \cdot a } { b } } \right] }$$ dollars.
Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x1, x2, ..., xn. Number xi is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save.
|
The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The second line of input contains n space-separated integers x1, x2, ..., xn (1 ≤ xi ≤ 109).
|
Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day.
| null | null |
[{"input": "5 1 4\n12 6 11 9 1", "output": "0 2 3 1 1"}, {"input": "3 1 2\n1 2 3", "output": "1 0 1"}, {"input": "1 1 1\n1", "output": "0"}]
| 1,500
|
["binary search", "greedy", "implementation", "math"]
| 47
|
[{"input": "5 1 4\r\n12 6 11 9 1\r\n", "output": "0 2 3 1 1 "}, {"input": "3 1 2\r\n1 2 3\r\n", "output": "1 0 1 "}, {"input": "1 1 1\r\n1\r\n", "output": "0 "}, {"input": "1 1 1000000000\r\n1000000000\r\n", "output": "0 "}, {"input": "1 1 1000000000\r\n999999999\r\n", "output": "999999999 "}, {"input": "10 1 100000000\r\n999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999\r\n", "output": "99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 "}]
| false
|
stdio
| null | true
|
415/B
|
415
|
B
|
Python 3
|
PRETESTS
| 3
| 109
| 6,246,400
|
6278757
|
def main():
N, A, B = map(int, input().split())
numbers = list(map(int, input().split()))
result = []
for i in range(N):
result += [(numbers[i] * A) % B]
print(' '.join(map(str, result)))
main()
| 47
| 124
| 17,510,400
|
168750648
|
n, a, b = map(int, input().split(' '))
d = b/a
arr = list(map(int, input().split(' ')))
res = []
for x in arr:
q = x//d
r = x - q*d
res.append(int(r))
print(' '.join([str(x) for x in res]))
|
Codeforces Round 240 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Mashmokh and Tokens
|
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $${ \left[ { \frac { w \cdot a } { b } } \right] }$$ dollars.
Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x1, x2, ..., xn. Number xi is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save.
|
The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The second line of input contains n space-separated integers x1, x2, ..., xn (1 ≤ xi ≤ 109).
|
Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day.
| null | null |
[{"input": "5 1 4\n12 6 11 9 1", "output": "0 2 3 1 1"}, {"input": "3 1 2\n1 2 3", "output": "1 0 1"}, {"input": "1 1 1\n1", "output": "0"}]
| 1,500
|
["binary search", "greedy", "implementation", "math"]
| 47
|
[{"input": "5 1 4\r\n12 6 11 9 1\r\n", "output": "0 2 3 1 1 "}, {"input": "3 1 2\r\n1 2 3\r\n", "output": "1 0 1 "}, {"input": "1 1 1\r\n1\r\n", "output": "0 "}, {"input": "1 1 1000000000\r\n1000000000\r\n", "output": "0 "}, {"input": "1 1 1000000000\r\n999999999\r\n", "output": "999999999 "}, {"input": "10 1 100000000\r\n999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999\r\n", "output": "99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 "}]
| false
|
stdio
| null | true
|
415/B
|
415
|
B
|
Python 3
|
PRETESTS
| 3
| 109
| 6,860,800
|
6280784
|
import math
def f2(x, a, b):
t = math.floor(x * a / b)
r = x % t if t > 0 else x
#print(x, a, b, r)
return r
def f(x, a, b):
r = (x * a) % b
#print(x, a, b, r)
return r
n, a, b = map(int, input().split(" "))
x = list(map(int, input().split(" ")))
#r = [f(x[i], a, b) for i in range(len(x))]
r = [(x[i] * a) % b for i in range(len(x))]
print(" ".join(map(str, r)))
| 47
| 139
| 13,619,200
|
177516683
|
import sys
input = sys.stdin.readline
n, a, b = map(int, input().split())
w = list(map(int, input().split()))
for i in w:
print((i*a) % b // a, end=' ')
|
Codeforces Round 240 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Mashmokh and Tokens
|
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $${ \left[ { \frac { w \cdot a } { b } } \right] }$$ dollars.
Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x1, x2, ..., xn. Number xi is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save.
|
The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The second line of input contains n space-separated integers x1, x2, ..., xn (1 ≤ xi ≤ 109).
|
Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day.
| null | null |
[{"input": "5 1 4\n12 6 11 9 1", "output": "0 2 3 1 1"}, {"input": "3 1 2\n1 2 3", "output": "1 0 1"}, {"input": "1 1 1\n1", "output": "0"}]
| 1,500
|
["binary search", "greedy", "implementation", "math"]
| 47
|
[{"input": "5 1 4\r\n12 6 11 9 1\r\n", "output": "0 2 3 1 1 "}, {"input": "3 1 2\r\n1 2 3\r\n", "output": "1 0 1 "}, {"input": "1 1 1\r\n1\r\n", "output": "0 "}, {"input": "1 1 1000000000\r\n1000000000\r\n", "output": "0 "}, {"input": "1 1 1000000000\r\n999999999\r\n", "output": "999999999 "}, {"input": "10 1 100000000\r\n999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999\r\n", "output": "99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 "}]
| false
|
stdio
| null | true
|
67/A
|
67
|
A
|
PyPy 3
|
TESTS
| 6
| 186
| 307,200
|
14437589
|
n = int(input())
result = n * [ None ]
result[0] = 1
min_value = 1
for i, ch in enumerate(input().strip()):
if ch == 'L':
result[i + 1] = result[i] - 1
min_value = min(min_value, result[i + 1])
elif ch == 'R':
result[i + 1] = result[i] + 1
else:
result[i + 1] = result[i]
delta = 1 - min_value
if delta != 0:
for i in range(n):
result[i] += delta
print(' '.join(map(str, result)))
| 43
| 248
| 921,600
|
52528999
|
from collections import defaultdict
class PartialTeacher():
def __init__(self, n, prefs):
self.prefs = prefs
def create_graph(self):
num_nodes = len(list(filter(lambda x: x != '=',list(self.prefs))))+1
node_cntr = 0
node_cnts = defaultdict(lambda: 1)
edge_map = defaultdict(list)
rev_map = defaultdict(list)
outgoing_edges = defaultdict(int)
for ch in self.prefs:
if ch == 'R':
edge_map[node_cntr].append(node_cntr+1)
rev_map[node_cntr+1].append(node_cntr)
outgoing_edges[node_cntr] += 1
node_cntr += 1
elif ch == 'L':
edge_map[node_cntr+1].append(node_cntr)
rev_map[node_cntr].append(node_cntr+1)
outgoing_edges[node_cntr+1] += 1
node_cntr += 1
else:
node_cnts[node_cntr] += 1
s = set()
for i in range(node_cntr+1):
if outgoing_edges[i] == 0:
s.add(i)
order = []
while len(s) > 0:
v = s.pop()
order.append(v)
for u in rev_map[v]:
outgoing_edges[u] -= 1
if outgoing_edges[u] == 0:
s.add(u)
order.reverse()
values = [-1]*len(order)
for v in order:
val_list = list(map(lambda x: values[x], rev_map[v]))
val = 1 if len(val_list) == 0 else max(val_list)+1
values[v] = val
complete_values = []
for i in range(len(values)):
complete_values += [values[i]]*node_cnts[i]
print(*complete_values)
n = int(input())
prefs = input().strip(' ')
pt = PartialTeacher(n, prefs)
pt.create_graph()
|
Manthan 2011
|
CF
| 2,011
| 1
| 256
|
Partial Teacher
|
A teacher decides to give toffees to his students. He asks n students to stand in a queue. Since the teacher is very partial, he follows the following rule to distribute toffees.
He looks at the first two students and gives more toffees to the student having higher marks than the other one. If they have the same marks they get the same number of toffees. The same procedure is followed for each pair of adjacent students starting from the first one to the last one.
It is given that each student receives at least one toffee. You have to find the number of toffees given to each student by the teacher such that the total number of toffees is minimum.
|
The first line of input contains the number of students n (2 ≤ n ≤ 1000). The second line gives (n - 1) characters consisting of "L", "R" and "=". For each pair of adjacent students "L" means that the left student has higher marks, "R" means that the right student has higher marks and "=" means that both have equal marks.
|
Output consists of n integers separated by a space representing the number of toffees each student receives in the queue starting from the first one to the last one.
| null | null |
[{"input": "5\nLRLR", "output": "2 1 2 1 2"}, {"input": "5\n=RRR", "output": "1 1 2 3 4"}]
| 1,800
|
["dp", "graphs", "greedy", "implementation"]
| 43
|
[{"input": "5\r\nLRLR\r\n", "output": "2 1 2 1 2\r\n"}, {"input": "5\r\n=RRR\r\n", "output": "1 1 2 3 4\r\n"}, {"input": "6\r\nRLRL=\r\n", "output": "1 2 1 2 1 1\r\n"}, {"input": "3\r\nR=\r\n", "output": "1 2 2\r\n"}, {"input": "7\r\nRR==RR\r\n", "output": "1 2 3 3 3 4 5\r\n"}, {"input": "166\r\nR===RL=LRRR=RRRL=LRR=R=RR==L=R=R=RRR=RR=RLLRRL=LLRL==L=R==RLR==RL=RR=LR==R=R=LLRLRLR=RR=RLLRLR=RRLL==L=LR=RR=RRRL=RLLLR==L=RRLRLLLLLLLRL===LRLRLRLRRLL=LRLL===LRLRR==\r\n", "output": "1 2 2 2 2 3 2 2 1 2 3 4 4 5 6 7 2 2 1 2 3 3 4 4 5 6 6 6 1 1 2 2 3 3 4 5 6 6 7 8 8 9 2 1 2 4 3 3 2 1 3 2 2 2 1 1 2 2 2 3 1 2 2 2 3 1 1 2 3 3 1 2 2 2 3 3 4 4 2 1 2 1 2 1 2 2 3 4 4 5 2 1 2 1 2 2 3 5 4 3 3 3 2 2 1 2 2 3 4 4 5 6 7 1 1 4 3 2 1 2 2 2 1 1 2 3 1 8 7 6 5 4 3 2 1 3 2 2 2 2 1 2 1 2 1 2 1 2 4 3 2 2 1 4 3 2 2 2 2 1 2 1 2 3 3 3\n"}, {"input": "24\r\nR=R==RL=RL=RLL=LLL=LLRL\r\n", "output": "1 2 2 3 3 3 4 1 1 2 1 1 8 7 6 6 5 4 3 3 2 1 2 1\r\n"}, {"input": "100\r\n=L=L=L=R=LR=RRRLRL=LRL=RRLLLLRL=R==R=LLLRR===RR=LR==LRLR===RRLRLLRLLR=LRLRR=L=LRRLLLRR==LLRLLLL==RL\r\n", "output": "4 4 3 3 2 2 1 1 2 2 1 2 2 3 4 5 1 3 2 2 1 2 1 1 2 5 4 3 2 1 2 1 1 2 2 2 4 4 3 2 1 2 3 3 3 3 4 5 5 1 2 2 2 1 2 1 2 2 2 2 3 4 1 3 2 1 3 2 1 2 2 1 2 1 2 3 3 2 2 1 2 4 3 2 1 2 3 3 3 2 1 5 4 3 2 1 1 1 2 1\r\n"}, {"input": "198\r\nLLRRR=RRRRLRRLRR=R===R=RL==R=RLLLR=R=L=LR=R====RRL=RRR=LL=R=RR=RRRLRRLRRR==L=LRLLL====LR=RL==L===LRR=L=L==R==R==L=LLL===R=LLL=R=L=LLLLRLL=RL=LRRLR=RL==RR=R==RLR==R=R==RLRL=LL=RRR=R===LLLRRRRL=RLRLL\r\n", "output": "3 2 1 2 3 4 4 5 6 7 8 1 2 3 1 2 3 3 4 4 4 4 5 5 6 1 1 1 2 2 4 3 2 1 2 2 3 3 2 2 1 2 2 3 3 3 3 3 4 5 1 1 2 3 4 4 2 1 1 2 2 3 4 4 5 6 7 1 2 3 1 2 3 4 4 4 2 2 1 5 4 3 2 2 2 2 2 1 2 2 4 3 3 3 2 2 2 2 1 2 3 3 2 2 1 1 1 2 2 2 5 5 5 4 4 3 2 1 1 1 1 4 4 3 2 1 1 6 6 5 5 4 3 2 1 3 2 1 1 3 2 2 1 2 3 1 2 2 3 1 1 1 2 3 3 4 4 4 5 1 2 2 2 3 3 4 4 4 5 1 4 3 3 2 1 1 2 3 4 4 5 5 5 5 3 2 1 2 3 4 5 1 1 2 1 3 2 1\n"}, {"input": "10\r\nRL=R=RLR=\r\n", "output": "1 2 1 1 2 2 3 1 2 2\r\n"}, {"input": "2\r\nL\r\n", "output": "2 1\r\n"}, {"input": "100\r\nR=R=RRR=R=RR=RRLL=RLRLLLLLR==L=======L=LLR==RL=R=LRLLLR==LLLL=RRRL=LRL=LR=====L=LLLRRL=LLR===RLR=RR\r\n", "output": "1 2 2 3 3 4 5 6 6 7 7 8 9 9 10 11 2 1 1 2 1 6 5 4 3 2 1 5 5 5 4 4 4 4 4 4 4 4 3 3 2 1 2 2 2 3 1 1 2 2 1 4 3 2 1 5 5 5 4 3 2 1 1 2 3 4 2 2 1 3 2 2 1 5 5 5 5 5 5 4 4 3 2 1 2 4 3 3 2 1 2 2 2 2 3 1 2 2 3 4\r\n"}, {"input": "23\r\nL=LLLLRL=RR=RLLLL=RR==\r\n", "output": "6 5 5 4 3 2 1 2 1 1 2 3 3 5 4 3 2 1 1 2 3 3 3\r\n"}, {"input": "4\r\nRRL\r\n", "output": "1 2 3 1\r\n"}, {"input": "17\r\n=RRR=L==LLLLRRRL\r\n", "output": "1 1 2 3 6 6 5 5 5 4 3 2 1 2 3 4 1\r\n"}, {"input": "20\r\nRRLLLLLRRRRRRRRLRLR\r\n", "output": "1 2 6 5 4 3 2 1 2 3 4 5 6 7 8 9 1 2 1 2\r\n"}, {"input": "9\r\nR===RRLL\r\n", "output": "1 2 2 2 2 3 4 2 1\r\n"}, {"input": "15\r\n=RRR=LLLLLRRRL\r\n", "output": "1 1 2 3 6 6 5 4 3 2 1 2 3 4 1\r\n"}]
| false
|
stdio
| null | true
|
67/A
|
67
|
A
|
PyPy 3
|
TESTS
| 6
| 154
| 1,433,600
|
140588190
|
n = int(input())
s = str(input())
res = [2000 for i in range(n)]
for i in range(n - 1):
if s[i] == "R":
res[i + 1] = 1 + res[i]
elif s[i] == "L":
res[i + 1] = res[i] - 1
else:
res[i + 1] = res[i]
d = min(res) - 1
for i in range(n):
print(res[i] - d, end=" ")
| 43
| 248
| 7,065,600
|
87914373
|
from collections import deque
n = int(input())
s = input()
ans = [1]*n
for i in range(1,n):
if s[i-1]=='R':
ans[i]=ans[i-1]+1
elif s[i-1]=='=':
ans[i]=ans[i-1]
for i in range(n-2, -1, -1):
if s[i]=='L':
ans[i]=max(ans[i+1]+1, ans[i])
elif s[i]=='=':
ans[i]=max(ans[i], ans[i+1])
print(*ans)
|
Manthan 2011
|
CF
| 2,011
| 1
| 256
|
Partial Teacher
|
A teacher decides to give toffees to his students. He asks n students to stand in a queue. Since the teacher is very partial, he follows the following rule to distribute toffees.
He looks at the first two students and gives more toffees to the student having higher marks than the other one. If they have the same marks they get the same number of toffees. The same procedure is followed for each pair of adjacent students starting from the first one to the last one.
It is given that each student receives at least one toffee. You have to find the number of toffees given to each student by the teacher such that the total number of toffees is minimum.
|
The first line of input contains the number of students n (2 ≤ n ≤ 1000). The second line gives (n - 1) characters consisting of "L", "R" and "=". For each pair of adjacent students "L" means that the left student has higher marks, "R" means that the right student has higher marks and "=" means that both have equal marks.
|
Output consists of n integers separated by a space representing the number of toffees each student receives in the queue starting from the first one to the last one.
| null | null |
[{"input": "5\nLRLR", "output": "2 1 2 1 2"}, {"input": "5\n=RRR", "output": "1 1 2 3 4"}]
| 1,800
|
["dp", "graphs", "greedy", "implementation"]
| 43
|
[{"input": "5\r\nLRLR\r\n", "output": "2 1 2 1 2\r\n"}, {"input": "5\r\n=RRR\r\n", "output": "1 1 2 3 4\r\n"}, {"input": "6\r\nRLRL=\r\n", "output": "1 2 1 2 1 1\r\n"}, {"input": "3\r\nR=\r\n", "output": "1 2 2\r\n"}, {"input": "7\r\nRR==RR\r\n", "output": "1 2 3 3 3 4 5\r\n"}, {"input": "166\r\nR===RL=LRRR=RRRL=LRR=R=RR==L=R=R=RRR=RR=RLLRRL=LLRL==L=R==RLR==RL=RR=LR==R=R=LLRLRLR=RR=RLLRLR=RRLL==L=LR=RR=RRRL=RLLLR==L=RRLRLLLLLLLRL===LRLRLRLRRLL=LRLL===LRLRR==\r\n", "output": "1 2 2 2 2 3 2 2 1 2 3 4 4 5 6 7 2 2 1 2 3 3 4 4 5 6 6 6 1 1 2 2 3 3 4 5 6 6 7 8 8 9 2 1 2 4 3 3 2 1 3 2 2 2 1 1 2 2 2 3 1 2 2 2 3 1 1 2 3 3 1 2 2 2 3 3 4 4 2 1 2 1 2 1 2 2 3 4 4 5 2 1 2 1 2 2 3 5 4 3 3 3 2 2 1 2 2 3 4 4 5 6 7 1 1 4 3 2 1 2 2 2 1 1 2 3 1 8 7 6 5 4 3 2 1 3 2 2 2 2 1 2 1 2 1 2 1 2 4 3 2 2 1 4 3 2 2 2 2 1 2 1 2 3 3 3\n"}, {"input": "24\r\nR=R==RL=RL=RLL=LLL=LLRL\r\n", "output": "1 2 2 3 3 3 4 1 1 2 1 1 8 7 6 6 5 4 3 3 2 1 2 1\r\n"}, {"input": "100\r\n=L=L=L=R=LR=RRRLRL=LRL=RRLLLLRL=R==R=LLLRR===RR=LR==LRLR===RRLRLLRLLR=LRLRR=L=LRRLLLRR==LLRLLLL==RL\r\n", "output": "4 4 3 3 2 2 1 1 2 2 1 2 2 3 4 5 1 3 2 2 1 2 1 1 2 5 4 3 2 1 2 1 1 2 2 2 4 4 3 2 1 2 3 3 3 3 4 5 5 1 2 2 2 1 2 1 2 2 2 2 3 4 1 3 2 1 3 2 1 2 2 1 2 1 2 3 3 2 2 1 2 4 3 2 1 2 3 3 3 2 1 5 4 3 2 1 1 1 2 1\r\n"}, {"input": "198\r\nLLRRR=RRRRLRRLRR=R===R=RL==R=RLLLR=R=L=LR=R====RRL=RRR=LL=R=RR=RRRLRRLRRR==L=LRLLL====LR=RL==L===LRR=L=L==R==R==L=LLL===R=LLL=R=L=LLLLRLL=RL=LRRLR=RL==RR=R==RLR==R=R==RLRL=LL=RRR=R===LLLRRRRL=RLRLL\r\n", "output": "3 2 1 2 3 4 4 5 6 7 8 1 2 3 1 2 3 3 4 4 4 4 5 5 6 1 1 1 2 2 4 3 2 1 2 2 3 3 2 2 1 2 2 3 3 3 3 3 4 5 1 1 2 3 4 4 2 1 1 2 2 3 4 4 5 6 7 1 2 3 1 2 3 4 4 4 2 2 1 5 4 3 2 2 2 2 2 1 2 2 4 3 3 3 2 2 2 2 1 2 3 3 2 2 1 1 1 2 2 2 5 5 5 4 4 3 2 1 1 1 1 4 4 3 2 1 1 6 6 5 5 4 3 2 1 3 2 1 1 3 2 2 1 2 3 1 2 2 3 1 1 1 2 3 3 4 4 4 5 1 2 2 2 3 3 4 4 4 5 1 4 3 3 2 1 1 2 3 4 4 5 5 5 5 3 2 1 2 3 4 5 1 1 2 1 3 2 1\n"}, {"input": "10\r\nRL=R=RLR=\r\n", "output": "1 2 1 1 2 2 3 1 2 2\r\n"}, {"input": "2\r\nL\r\n", "output": "2 1\r\n"}, {"input": "100\r\nR=R=RRR=R=RR=RRLL=RLRLLLLLR==L=======L=LLR==RL=R=LRLLLR==LLLL=RRRL=LRL=LR=====L=LLLRRL=LLR===RLR=RR\r\n", "output": "1 2 2 3 3 4 5 6 6 7 7 8 9 9 10 11 2 1 1 2 1 6 5 4 3 2 1 5 5 5 4 4 4 4 4 4 4 4 3 3 2 1 2 2 2 3 1 1 2 2 1 4 3 2 1 5 5 5 4 3 2 1 1 2 3 4 2 2 1 3 2 2 1 5 5 5 5 5 5 4 4 3 2 1 2 4 3 3 2 1 2 2 2 2 3 1 2 2 3 4\r\n"}, {"input": "23\r\nL=LLLLRL=RR=RLLLL=RR==\r\n", "output": "6 5 5 4 3 2 1 2 1 1 2 3 3 5 4 3 2 1 1 2 3 3 3\r\n"}, {"input": "4\r\nRRL\r\n", "output": "1 2 3 1\r\n"}, {"input": "17\r\n=RRR=L==LLLLRRRL\r\n", "output": "1 1 2 3 6 6 5 5 5 4 3 2 1 2 3 4 1\r\n"}, {"input": "20\r\nRRLLLLLRRRRRRRRLRLR\r\n", "output": "1 2 6 5 4 3 2 1 2 3 4 5 6 7 8 9 1 2 1 2\r\n"}, {"input": "9\r\nR===RRLL\r\n", "output": "1 2 2 2 2 3 4 2 1\r\n"}, {"input": "15\r\n=RRR=LLLLLRRRL\r\n", "output": "1 1 2 3 6 6 5 4 3 2 1 2 3 4 1\r\n"}]
| false
|
stdio
| null | true
|
415/B
|
415
|
B
|
PyPy 3-64
|
TESTS
| 3
| 93
| 11,059,200
|
175167120
|
n,a,b = map(int,input().split())
arr = list(map(int,input().split()))
for x in arr :
print(x- (x//b) * b,end = ' ')
| 47
| 140
| 14,233,600
|
177031617
|
'''
# Submitted By M7moud Ala3rj
Don't Copy This Code, CopyRight . [email protected] © 2022-2023 :)
'''
# Problem Name = "Mashmokh and Tokens"
# Class: B
import sys
#sys.setrecursionlimit(2147483647)
input = sys.stdin.readline
def print(*args, end='\n', sep=' ') -> None:
sys.stdout.write(sep.join(map(str, args)) + end)
def Solve():
n, a, b = list(map(int, input().split()))
for i in list(map(int, input().split())):
l = 0 ; r = i ; ans = 0 ; z = i*a//b
while l<=r:
mid = (l + r) // 2
if (i-mid)*a >= z*b:
ans = max(ans, mid)
l = mid + 1
else:
r = mid - 1
print(ans, end = " ")
if __name__ == "__main__":
# for t in range(int(input())):
Solve()
|
Codeforces Round 240 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Mashmokh and Tokens
|
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $${ \left[ { \frac { w \cdot a } { b } } \right] }$$ dollars.
Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x1, x2, ..., xn. Number xi is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save.
|
The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The second line of input contains n space-separated integers x1, x2, ..., xn (1 ≤ xi ≤ 109).
|
Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day.
| null | null |
[{"input": "5 1 4\n12 6 11 9 1", "output": "0 2 3 1 1"}, {"input": "3 1 2\n1 2 3", "output": "1 0 1"}, {"input": "1 1 1\n1", "output": "0"}]
| 1,500
|
["binary search", "greedy", "implementation", "math"]
| 47
|
[{"input": "5 1 4\r\n12 6 11 9 1\r\n", "output": "0 2 3 1 1 "}, {"input": "3 1 2\r\n1 2 3\r\n", "output": "1 0 1 "}, {"input": "1 1 1\r\n1\r\n", "output": "0 "}, {"input": "1 1 1000000000\r\n1000000000\r\n", "output": "0 "}, {"input": "1 1 1000000000\r\n999999999\r\n", "output": "999999999 "}, {"input": "10 1 100000000\r\n999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999\r\n", "output": "99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 "}]
| false
|
stdio
| null | true
|
415/B
|
415
|
B
|
PyPy 3-64
|
TESTS
| 3
| 46
| 8,908,800
|
227717754
|
n,a,b=map(int,input().split());print("".join(f'{(i*a)%b} 'for i in map(int,input().split())))
| 47
| 155
| 9,011,200
|
113085923
|
n, a, b = map(int, input().split())
x = list(map(int, input().split()))
print(' '.join( str( ( ( xi * a ) % b ) // a ) for xi in x ) )
|
Codeforces Round 240 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Mashmokh and Tokens
|
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $${ \left[ { \frac { w \cdot a } { b } } \right] }$$ dollars.
Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x1, x2, ..., xn. Number xi is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save.
|
The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The second line of input contains n space-separated integers x1, x2, ..., xn (1 ≤ xi ≤ 109).
|
Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day.
| null | null |
[{"input": "5 1 4\n12 6 11 9 1", "output": "0 2 3 1 1"}, {"input": "3 1 2\n1 2 3", "output": "1 0 1"}, {"input": "1 1 1\n1", "output": "0"}]
| 1,500
|
["binary search", "greedy", "implementation", "math"]
| 47
|
[{"input": "5 1 4\r\n12 6 11 9 1\r\n", "output": "0 2 3 1 1 "}, {"input": "3 1 2\r\n1 2 3\r\n", "output": "1 0 1 "}, {"input": "1 1 1\r\n1\r\n", "output": "0 "}, {"input": "1 1 1000000000\r\n1000000000\r\n", "output": "0 "}, {"input": "1 1 1000000000\r\n999999999\r\n", "output": "999999999 "}, {"input": "10 1 100000000\r\n999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999\r\n", "output": "99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 "}]
| false
|
stdio
| null | true
|
415/B
|
415
|
B
|
PyPy 3-64
|
TESTS
| 3
| 108
| 11,571,200
|
194727465
|
a,b,c=list(map(int,input().split()))
aa=list(map(int,input().split()))
from math import gcd,floor
for i in aa:
dd=(i*b/c)-floor(i*b/c)
x=(dd*c)/b
if x-floor(x)>=0.99:
print(round(x),end=" ")
else:
print(floor(x),end=" ")
print()
| 47
| 155
| 9,113,600
|
15143719
|
def main():
n, a, b = list(map(int, input().split()))
x_list = list(map(int, input().split()))
res = list()
for x in x_list:
res.append(str(x * a % b // a))
print(' '.join(res))
if __name__ == '__main__':
main()
|
Codeforces Round 240 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Mashmokh and Tokens
|
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $${ \left[ { \frac { w \cdot a } { b } } \right] }$$ dollars.
Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x1, x2, ..., xn. Number xi is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save.
|
The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The second line of input contains n space-separated integers x1, x2, ..., xn (1 ≤ xi ≤ 109).
|
Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day.
| null | null |
[{"input": "5 1 4\n12 6 11 9 1", "output": "0 2 3 1 1"}, {"input": "3 1 2\n1 2 3", "output": "1 0 1"}, {"input": "1 1 1\n1", "output": "0"}]
| 1,500
|
["binary search", "greedy", "implementation", "math"]
| 47
|
[{"input": "5 1 4\r\n12 6 11 9 1\r\n", "output": "0 2 3 1 1 "}, {"input": "3 1 2\r\n1 2 3\r\n", "output": "1 0 1 "}, {"input": "1 1 1\r\n1\r\n", "output": "0 "}, {"input": "1 1 1000000000\r\n1000000000\r\n", "output": "0 "}, {"input": "1 1 1000000000\r\n999999999\r\n", "output": "999999999 "}, {"input": "10 1 100000000\r\n999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999\r\n", "output": "99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 "}]
| false
|
stdio
| null | true
|
415/B
|
415
|
B
|
Python 3
|
TESTS
| 3
| 93
| 5,529,600
|
158774961
|
n, a, b = map(int, input().strip().split(' '));
x = map(int, input().strip().split(' '))
ans = []
for i in x:
choco = i * a // b;
i -= choco * b // a;
ans.append(str(i))
print(' '.join(ans))
| 47
| 155
| 13,926,400
|
216898932
|
n,a,b = map(int,input().split())
x = list(map(int,input().split()))
for i in range(n):
Max_money = (x[i] * a)//b
Mn = (Max_money*b + (a- 1))//a
print(x[i] - Mn, end = " ")# 1691004971.4493392
|
Codeforces Round 240 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Mashmokh and Tokens
|
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $${ \left[ { \frac { w \cdot a } { b } } \right] }$$ dollars.
Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x1, x2, ..., xn. Number xi is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save.
|
The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The second line of input contains n space-separated integers x1, x2, ..., xn (1 ≤ xi ≤ 109).
|
Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day.
| null | null |
[{"input": "5 1 4\n12 6 11 9 1", "output": "0 2 3 1 1"}, {"input": "3 1 2\n1 2 3", "output": "1 0 1"}, {"input": "1 1 1\n1", "output": "0"}]
| 1,500
|
["binary search", "greedy", "implementation", "math"]
| 47
|
[{"input": "5 1 4\r\n12 6 11 9 1\r\n", "output": "0 2 3 1 1 "}, {"input": "3 1 2\r\n1 2 3\r\n", "output": "1 0 1 "}, {"input": "1 1 1\r\n1\r\n", "output": "0 "}, {"input": "1 1 1000000000\r\n1000000000\r\n", "output": "0 "}, {"input": "1 1 1000000000\r\n999999999\r\n", "output": "999999999 "}, {"input": "10 1 100000000\r\n999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999\r\n", "output": "99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 "}]
| false
|
stdio
| null | true
|
641/B
|
641
|
B
|
PyPy 3-64
|
TESTS
| 5
| 280
| 4,505,600
|
162482009
|
#I = lambda: [int(i) for i in input().split()]
#import io, os, sys
#input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
# n = int(input())
# l1 = list(map(int,input().split()))
# n,x = map(int,input().split())
# s = input()
mod = 1000000007
# print("Case #"+str(_+1)+":",)
from collections import defaultdict
import bisect
import sys,math
n,m,q = map(int,input().split())
d = defaultdict(lambda:0)
row = defaultdict(lambda:0)
col = defaultdict(lambda:0)
for i in range(q):
l = list(map(int,input().split()))
if l[0]==1:
col[l[1]]+=1
elif l[0]==2:
row[l[1]]+=1
elif l[0]==3:
r = row[l[1]]%n
c = col[l[2]]%m
if n>=r+l[1]:
r= l[1]+r
else:
r = r-(n-l[1])
if m>=c+l[2]:
c = l[2]+c
else:
c = c-(m-l[2])
d[str(r)+str(c)]=l[3]
for i in range(1,n+1):
for j in range(1,m+1):
print(d[str(i)+str(j)],end=' ')
print()
| 26
| 217
| 6,656,000
|
34251994
|
f = lambda: map(int, input().split())
n, m, q = f()
p = [[0] * m for j in range(n)]
for t in [list(f()) for k in range(q)][::-1]:
j = t[1] - 1
if t[0] == 1: p[j].insert(0, p[j].pop())
elif t[0] == 2:
s = p[-1][j]
for i in range(n - 1, 0, -1): p[i][j] = p[i - 1][j]
p[0][j] = s
else: p[j][t[2] - 1] = t[3]
for d in p: print(*d)
|
VK Cup 2016 - Round 2
|
CF
| 2,016
| 2
| 256
|
Little Artem and Matrix
|
Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.
That element can store information about the matrix of integers size n × m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to m from left to right.
Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.
Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists.
|
The first line of the input contains three integers n, m and q (1 ≤ n, m ≤ 100, 1 ≤ q ≤ 10 000) — dimensions of the matrix and the number of turns in the experiment, respectively.
Next q lines contain turns descriptions, one per line. Each description starts with an integer ti (1 ≤ ti ≤ 3) that defines the type of the operation. For the operation of first and second type integer ri (1 ≤ ri ≤ n) or ci (1 ≤ ci ≤ m) follows, while for the operations of the third type three integers ri, ci and xi (1 ≤ ri ≤ n, 1 ≤ ci ≤ m, - 109 ≤ xi ≤ 109) are given.
Operation of the first type (ti = 1) means that signal comes to the input corresponding to row ri, that is it will shift cyclically. Operation of the second type (ti = 2) means that column ci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row ri and column ci stores value xi.
|
Print the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 109 by their absolute value.
If there are multiple valid solutions, output any of them.
| null | null |
[{"input": "2 2 6\n2 1\n2 2\n3 1 1 1\n3 2 2 2\n3 1 2 8\n3 2 1 8", "output": "8 2\n1 8"}, {"input": "3 3 2\n1 2\n3 2 2 5", "output": "0 0 0\n0 0 5\n0 0 0"}]
| 1,400
|
["implementation"]
| 26
|
[{"input": "2 2 6\r\n2 1\r\n2 2\r\n3 1 1 1\r\n3 2 2 2\r\n3 1 2 8\r\n3 2 1 8\r\n", "output": "8 2 \r\n1 8 \r\n"}, {"input": "3 3 2\r\n1 2\r\n3 2 2 5\r\n", "output": "0 0 0 \r\n0 0 5 \r\n0 0 0 \r\n"}, {"input": "5 5 1\r\n1 5\r\n", "output": "0 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"}, {"input": "1 1 3\r\n1 1\r\n2 1\r\n3 1 1 1000000000\r\n", "output": "1000000000 \r\n"}, {"input": "1 1 3\r\n1 1\r\n2 1\r\n3 1 1 -1000000000\r\n", "output": "-1000000000 \r\n"}, {"input": "2 2 6\r\n2 1\r\n2 2\r\n3 1 1 -1\r\n3 2 2 -1\r\n3 1 2 -1\r\n3 2 1 -1\r\n", "output": "-1 -1 \r\n-1 -1 \r\n"}, {"input": "1 4 5\r\n1 1\r\n3 1 1 1\r\n3 1 2 2\r\n3 1 3 3\r\n3 1 4 4\r\n", "output": "4 1 2 3 \r\n"}, {"input": "4 2 5\r\n2 1\r\n3 1 1 5\r\n3 2 1 6\r\n3 3 1 7\r\n3 4 1 9\r\n", "output": "9 0 \r\n5 0 \r\n6 0 \r\n7 0 \r\n"}, {"input": "3 10 2\r\n1 2\r\n3 2 7 5\r\n", "output": "0 0 0 0 0 0 0 0 0 0 \r\n0 0 0 0 0 0 0 5 0 0 \r\n0 0 0 0 0 0 0 0 0 0 \r\n"}, {"input": "1 2 2\r\n1 1\r\n3 1 2 15\r\n", "output": "15 0 \r\n"}]
| false
|
stdio
| null | true
|
415/B
|
415
|
B
|
PyPy 3-64
|
TESTS
| 3
| 108
| 11,059,200
|
222464498
|
n, a, b = map(int, input().split())
tokens = list(map(int, input().split()))
for i in range(n):
# Calculate the number of tokens Mashmokh can save on the i-th day
saved_tokens = (tokens[i] * a) % b #// a
print(saved_tokens, end=" ")
| 47
| 155
| 15,052,800
|
115461157
|
def tokens(n, a, b, arr):
arrs = []
for t in arr:
c = (t * a) % b
arrs.append(int(c / a))
arr_aux = " ".join(map(str, arrs))
return arr_aux
n, a, b = map(int, input().split())
arr = map(int, input().split())
print(tokens(n, a, b, arr))
|
Codeforces Round 240 (Div. 2)
|
CF
| 2,014
| 1
| 256
|
Mashmokh and Tokens
|
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $${ \left[ { \frac { w \cdot a } { b } } \right] }$$ dollars.
Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x1, x2, ..., xn. Number xi is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save.
|
The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The second line of input contains n space-separated integers x1, x2, ..., xn (1 ≤ xi ≤ 109).
|
Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day.
| null | null |
[{"input": "5 1 4\n12 6 11 9 1", "output": "0 2 3 1 1"}, {"input": "3 1 2\n1 2 3", "output": "1 0 1"}, {"input": "1 1 1\n1", "output": "0"}]
| 1,500
|
["binary search", "greedy", "implementation", "math"]
| 47
|
[{"input": "5 1 4\r\n12 6 11 9 1\r\n", "output": "0 2 3 1 1 "}, {"input": "3 1 2\r\n1 2 3\r\n", "output": "1 0 1 "}, {"input": "1 1 1\r\n1\r\n", "output": "0 "}, {"input": "1 1 1000000000\r\n1000000000\r\n", "output": "0 "}, {"input": "1 1 1000000000\r\n999999999\r\n", "output": "999999999 "}, {"input": "10 1 100000000\r\n999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999\r\n", "output": "99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 99999999 "}]
| false
|
stdio
| null | true
|
67/D
|
67
|
D
|
Python 3
|
TESTS
| 2
| 122
| 0
|
27763140
|
t=int(input())
a=str(input())
b=str(input())
if a[0]==b[2*t-2]:
print(a[0])
elif b[0]==a[2*t-2]:
print(b[0])
| 59
| 4,210
| 90,214,400
|
89108504
|
from bisect import bisect_left as bl
I=10000000
n=int(input())+1
c=[0]*n
for i,x in enumerate(map(int,input().split())): c[x]=i
d = [n-c[int(x)] for x in input().split()]
c=[I]*n
for i in d: c[bl(c,i)]=i
print( c.index(I))
|
Manthan 2011
|
CF
| 2,011
| 5
| 256
|
Optical Experiment
|
Professor Phunsuk Wangdu has performed some experiments on rays. The setup for n rays is as follows.
There is a rectangular box having exactly n holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line.
Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".
Professor Wangdu now is in trouble and knowing your intellect he asks you to help him.
|
The first line contains n (1 ≤ n ≤ 106), the number of rays. The second line contains n distinct integers. The i-th integer xi (1 ≤ xi ≤ n) shows that the xi-th ray enters from the i-th hole. Similarly, third line contains n distinct integers. The i-th integer yi (1 ≤ yi ≤ n) shows that the yi-th ray exits from the i-th hole. All rays are numbered from 1 to n.
|
Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other.
| null |
For the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint.
|
[{"input": "5\n1 4 5 2 3\n3 4 2 1 5", "output": "3"}, {"input": "3\n3 1 2\n2 3 1", "output": "2"}]
| 1,900
|
["binary search", "data structures", "dp"]
| 59
|
[{"input": "5\r\n1 4 5 2 3\r\n3 4 2 1 5\r\n", "output": "3\r\n"}, {"input": "3\r\n3 1 2\r\n2 3 1\r\n", "output": "2\r\n"}, {"input": "5\r\n1 2 4 5 3\r\n1 5 4 2 3\r\n", "output": "3\r\n"}, {"input": "3\r\n3 1 2\r\n1 3 2\r\n", "output": "2\r\n"}, {"input": "7\r\n1 5 2 7 4 3 6\r\n6 3 1 2 5 4 7\r\n", "output": "4\r\n"}, {"input": "4\r\n1 4 2 3\r\n2 3 1 4\r\n", "output": "2\r\n"}, {"input": "4\r\n2 4 1 3\r\n2 3 1 4\r\n", "output": "3\r\n"}, {"input": "10\r\n4 7 8 1 2 3 5 9 6 10\r\n6 3 8 7 10 2 1 4 5 9\r\n", "output": "5\r\n"}, {"input": "7\r\n1 5 7 2 4 3 6\r\n3 2 5 7 6 1 4\r\n", "output": "4\r\n"}, {"input": "9\r\n1 7 4 9 3 8 2 5 6\r\n8 4 7 1 3 2 9 6 5\r\n", "output": "4\r\n"}, {"input": "5\r\n1 4 5 2 3\r\n3 4 2 1 5\r\n", "output": "3\r\n"}, {"input": "3\r\n1 2 3\r\n2 3 1\r\n", "output": "2\r\n"}, {"input": "2\r\n1 2\r\n1 2\r\n", "output": "1\r\n"}, {"input": "2\r\n1 2\r\n2 1\r\n", "output": "2\r\n"}, {"input": "3\r\n1 2 3\r\n2 1 3\r\n", "output": "2\r\n"}, {"input": "3\r\n1 2 3\r\n1 3 2\r\n", "output": "2\r\n"}, {"input": "3\r\n1 2 3\r\n3 2 1\r\n", "output": "3\r\n"}, {"input": "3\r\n1 2 3\r\n1 2 3\r\n", "output": "1\r\n"}, {"input": "1\r\n1\r\n1\r\n", "output": "1\r\n"}, {"input": "5\r\n1 2 5 3 4\r\n3 5 4 2 1\r\n", "output": "4\r\n"}, {"input": "5\r\n5 3 2 4 1\r\n2 4 5 1 3\r\n", "output": "2\r\n"}, {"input": "5\r\n1 2 4 5 3\r\n1 2 5 4 3\r\n", "output": "2\r\n"}, {"input": "5\r\n1 2 3 4 5\r\n1 2 3 4 5\r\n", "output": "1\r\n"}, {"input": "5\r\n5 4 3 2 1\r\n1 2 3 4 5\r\n", "output": "5\r\n"}, {"input": "5\r\n1 3 5 4 2\r\n1 4 5 3 2\r\n", "output": "3\r\n"}, {"input": "5\r\n1 5 2 4 3\r\n4 3 2 5 1\r\n", "output": "4\r\n"}, {"input": "25\r\n21 19 25 9 24 23 20 18 16 22 17 7 4 15 13 11 2 3 10 12 14 6 8 5 1\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25\r\n", "output": "13\r\n"}, {"input": "30\r\n30 29 28 27 26 25 19 24 9 23 21 20 18 16 22 17 7 4 15 13 11 2 3 10 12 14 6 8 5 1\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30\r\n", "output": "19\r\n"}, {"input": "40\r\n40 27 29 39 30 34 28 26 25 38 19 32 24 9 37 23 21 20 18 33 36 16 22 35 17 7 4 15 31 13 11 2 3 10 12 14 6 8 5 1\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40\r\n", "output": "19\r\n"}, {"input": "1\r\n1\r\n1\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
67/A
|
67
|
A
|
Python 3
|
TESTS
| 6
| 124
| 4,710,400
|
10782101
|
from array import array
import io
import sys
def main():
f = sys.stdin
n = int(f.readline())
s = f.readline()
k = 0
m = 0
b = {'L': -1, '=': 0, 'R': 1}
a = [0 for _ in range(n)]
for i in range(1, n):
a[i] = a[i - 1] + b[s[i - 1]]
m = min(m, a[i])
m = 1 - m
[print(a[i] + m) for i in range(n)]
if __name__ == '__main__':
main()
| 43
| 278
| 0
|
212205722
|
def ans():
n=int(input())
k=n+1
s=input()
a=[]
for i in range(n):
a.append(0)
a[n-1]=1
for i in range(len(s)-1,-1,-1):
if(s[i]=='L'):
a[i]=a[i+1]+1
elif(s[i]=='R'):
if(a[i+1]==1):
a[i+1]=2
for j in range(i+1,n-1):
if(s[j]=='='):
a[j+1]=a[j]
elif(s[j]=='L'):
break
elif(a[j]==a[j+1]):
a[j+1]+=1
a[i]=1
else:
a[i]=a[i+1]
print(*a)
for i in range(1):
ans()
|
Manthan 2011
|
CF
| 2,011
| 1
| 256
|
Partial Teacher
|
A teacher decides to give toffees to his students. He asks n students to stand in a queue. Since the teacher is very partial, he follows the following rule to distribute toffees.
He looks at the first two students and gives more toffees to the student having higher marks than the other one. If they have the same marks they get the same number of toffees. The same procedure is followed for each pair of adjacent students starting from the first one to the last one.
It is given that each student receives at least one toffee. You have to find the number of toffees given to each student by the teacher such that the total number of toffees is minimum.
|
The first line of input contains the number of students n (2 ≤ n ≤ 1000). The second line gives (n - 1) characters consisting of "L", "R" and "=". For each pair of adjacent students "L" means that the left student has higher marks, "R" means that the right student has higher marks and "=" means that both have equal marks.
|
Output consists of n integers separated by a space representing the number of toffees each student receives in the queue starting from the first one to the last one.
| null | null |
[{"input": "5\nLRLR", "output": "2 1 2 1 2"}, {"input": "5\n=RRR", "output": "1 1 2 3 4"}]
| 1,800
|
["dp", "graphs", "greedy", "implementation"]
| 43
|
[{"input": "5\r\nLRLR\r\n", "output": "2 1 2 1 2\r\n"}, {"input": "5\r\n=RRR\r\n", "output": "1 1 2 3 4\r\n"}, {"input": "6\r\nRLRL=\r\n", "output": "1 2 1 2 1 1\r\n"}, {"input": "3\r\nR=\r\n", "output": "1 2 2\r\n"}, {"input": "7\r\nRR==RR\r\n", "output": "1 2 3 3 3 4 5\r\n"}, {"input": "166\r\nR===RL=LRRR=RRRL=LRR=R=RR==L=R=R=RRR=RR=RLLRRL=LLRL==L=R==RLR==RL=RR=LR==R=R=LLRLRLR=RR=RLLRLR=RRLL==L=LR=RR=RRRL=RLLLR==L=RRLRLLLLLLLRL===LRLRLRLRRLL=LRLL===LRLRR==\r\n", "output": "1 2 2 2 2 3 2 2 1 2 3 4 4 5 6 7 2 2 1 2 3 3 4 4 5 6 6 6 1 1 2 2 3 3 4 5 6 6 7 8 8 9 2 1 2 4 3 3 2 1 3 2 2 2 1 1 2 2 2 3 1 2 2 2 3 1 1 2 3 3 1 2 2 2 3 3 4 4 2 1 2 1 2 1 2 2 3 4 4 5 2 1 2 1 2 2 3 5 4 3 3 3 2 2 1 2 2 3 4 4 5 6 7 1 1 4 3 2 1 2 2 2 1 1 2 3 1 8 7 6 5 4 3 2 1 3 2 2 2 2 1 2 1 2 1 2 1 2 4 3 2 2 1 4 3 2 2 2 2 1 2 1 2 3 3 3\n"}, {"input": "24\r\nR=R==RL=RL=RLL=LLL=LLRL\r\n", "output": "1 2 2 3 3 3 4 1 1 2 1 1 8 7 6 6 5 4 3 3 2 1 2 1\r\n"}, {"input": "100\r\n=L=L=L=R=LR=RRRLRL=LRL=RRLLLLRL=R==R=LLLRR===RR=LR==LRLR===RRLRLLRLLR=LRLRR=L=LRRLLLRR==LLRLLLL==RL\r\n", "output": "4 4 3 3 2 2 1 1 2 2 1 2 2 3 4 5 1 3 2 2 1 2 1 1 2 5 4 3 2 1 2 1 1 2 2 2 4 4 3 2 1 2 3 3 3 3 4 5 5 1 2 2 2 1 2 1 2 2 2 2 3 4 1 3 2 1 3 2 1 2 2 1 2 1 2 3 3 2 2 1 2 4 3 2 1 2 3 3 3 2 1 5 4 3 2 1 1 1 2 1\r\n"}, {"input": "198\r\nLLRRR=RRRRLRRLRR=R===R=RL==R=RLLLR=R=L=LR=R====RRL=RRR=LL=R=RR=RRRLRRLRRR==L=LRLLL====LR=RL==L===LRR=L=L==R==R==L=LLL===R=LLL=R=L=LLLLRLL=RL=LRRLR=RL==RR=R==RLR==R=R==RLRL=LL=RRR=R===LLLRRRRL=RLRLL\r\n", "output": "3 2 1 2 3 4 4 5 6 7 8 1 2 3 1 2 3 3 4 4 4 4 5 5 6 1 1 1 2 2 4 3 2 1 2 2 3 3 2 2 1 2 2 3 3 3 3 3 4 5 1 1 2 3 4 4 2 1 1 2 2 3 4 4 5 6 7 1 2 3 1 2 3 4 4 4 2 2 1 5 4 3 2 2 2 2 2 1 2 2 4 3 3 3 2 2 2 2 1 2 3 3 2 2 1 1 1 2 2 2 5 5 5 4 4 3 2 1 1 1 1 4 4 3 2 1 1 6 6 5 5 4 3 2 1 3 2 1 1 3 2 2 1 2 3 1 2 2 3 1 1 1 2 3 3 4 4 4 5 1 2 2 2 3 3 4 4 4 5 1 4 3 3 2 1 1 2 3 4 4 5 5 5 5 3 2 1 2 3 4 5 1 1 2 1 3 2 1\n"}, {"input": "10\r\nRL=R=RLR=\r\n", "output": "1 2 1 1 2 2 3 1 2 2\r\n"}, {"input": "2\r\nL\r\n", "output": "2 1\r\n"}, {"input": "100\r\nR=R=RRR=R=RR=RRLL=RLRLLLLLR==L=======L=LLR==RL=R=LRLLLR==LLLL=RRRL=LRL=LR=====L=LLLRRL=LLR===RLR=RR\r\n", "output": "1 2 2 3 3 4 5 6 6 7 7 8 9 9 10 11 2 1 1 2 1 6 5 4 3 2 1 5 5 5 4 4 4 4 4 4 4 4 3 3 2 1 2 2 2 3 1 1 2 2 1 4 3 2 1 5 5 5 4 3 2 1 1 2 3 4 2 2 1 3 2 2 1 5 5 5 5 5 5 4 4 3 2 1 2 4 3 3 2 1 2 2 2 2 3 1 2 2 3 4\r\n"}, {"input": "23\r\nL=LLLLRL=RR=RLLLL=RR==\r\n", "output": "6 5 5 4 3 2 1 2 1 1 2 3 3 5 4 3 2 1 1 2 3 3 3\r\n"}, {"input": "4\r\nRRL\r\n", "output": "1 2 3 1\r\n"}, {"input": "17\r\n=RRR=L==LLLLRRRL\r\n", "output": "1 1 2 3 6 6 5 5 5 4 3 2 1 2 3 4 1\r\n"}, {"input": "20\r\nRRLLLLLRRRRRRRRLRLR\r\n", "output": "1 2 6 5 4 3 2 1 2 3 4 5 6 7 8 9 1 2 1 2\r\n"}, {"input": "9\r\nR===RRLL\r\n", "output": "1 2 2 2 2 3 4 2 1\r\n"}, {"input": "15\r\n=RRR=LLLLLRRRL\r\n", "output": "1 1 2 3 6 6 5 4 3 2 1 2 3 4 1\r\n"}]
| false
|
stdio
| null | true
|
67/A
|
67
|
A
|
Python 3
|
TESTS
| 6
| 124
| 5,529,600
|
26240203
|
n = int(input())
difference = input()
answer = [0]
flag = ''
for i in range(1, n):
flag = difference[i - 1]
if flag == "=":
answer.append(answer[i - 1])
if flag == 'L':
answer.append(answer[i - 1] - 1)
if flag == 'R':
answer.append(answer[i - 1] + 1)
minimal = min(answer)
for i in range(len(answer)):
answer[i] = str(answer[i] + 1 - minimal)
print(' '.join(answer))
| 43
| 310
| 1,945,600
|
103730144
|
n=int(input())
r=[1]*n
s=input()
y=1
while y:
y=0
for i in range(n-1):
if s[i]=='=' and r[i]!=r[i+1]:r[i]=r[i+1]=max(r[i:i+2]);y=1
if s[i]=='L' and r[i]<=r[i+1]:r[i]=r[i+1]+1;y=1
if s[i]=='R' and r[i]>=r[i+1]:r[i+1]=r[i]+1;y=1
print(*r)
|
Manthan 2011
|
CF
| 2,011
| 1
| 256
|
Partial Teacher
|
A teacher decides to give toffees to his students. He asks n students to stand in a queue. Since the teacher is very partial, he follows the following rule to distribute toffees.
He looks at the first two students and gives more toffees to the student having higher marks than the other one. If they have the same marks they get the same number of toffees. The same procedure is followed for each pair of adjacent students starting from the first one to the last one.
It is given that each student receives at least one toffee. You have to find the number of toffees given to each student by the teacher such that the total number of toffees is minimum.
|
The first line of input contains the number of students n (2 ≤ n ≤ 1000). The second line gives (n - 1) characters consisting of "L", "R" and "=". For each pair of adjacent students "L" means that the left student has higher marks, "R" means that the right student has higher marks and "=" means that both have equal marks.
|
Output consists of n integers separated by a space representing the number of toffees each student receives in the queue starting from the first one to the last one.
| null | null |
[{"input": "5\nLRLR", "output": "2 1 2 1 2"}, {"input": "5\n=RRR", "output": "1 1 2 3 4"}]
| 1,800
|
["dp", "graphs", "greedy", "implementation"]
| 43
|
[{"input": "5\r\nLRLR\r\n", "output": "2 1 2 1 2\r\n"}, {"input": "5\r\n=RRR\r\n", "output": "1 1 2 3 4\r\n"}, {"input": "6\r\nRLRL=\r\n", "output": "1 2 1 2 1 1\r\n"}, {"input": "3\r\nR=\r\n", "output": "1 2 2\r\n"}, {"input": "7\r\nRR==RR\r\n", "output": "1 2 3 3 3 4 5\r\n"}, {"input": "166\r\nR===RL=LRRR=RRRL=LRR=R=RR==L=R=R=RRR=RR=RLLRRL=LLRL==L=R==RLR==RL=RR=LR==R=R=LLRLRLR=RR=RLLRLR=RRLL==L=LR=RR=RRRL=RLLLR==L=RRLRLLLLLLLRL===LRLRLRLRRLL=LRLL===LRLRR==\r\n", "output": "1 2 2 2 2 3 2 2 1 2 3 4 4 5 6 7 2 2 1 2 3 3 4 4 5 6 6 6 1 1 2 2 3 3 4 5 6 6 7 8 8 9 2 1 2 4 3 3 2 1 3 2 2 2 1 1 2 2 2 3 1 2 2 2 3 1 1 2 3 3 1 2 2 2 3 3 4 4 2 1 2 1 2 1 2 2 3 4 4 5 2 1 2 1 2 2 3 5 4 3 3 3 2 2 1 2 2 3 4 4 5 6 7 1 1 4 3 2 1 2 2 2 1 1 2 3 1 8 7 6 5 4 3 2 1 3 2 2 2 2 1 2 1 2 1 2 1 2 4 3 2 2 1 4 3 2 2 2 2 1 2 1 2 3 3 3\n"}, {"input": "24\r\nR=R==RL=RL=RLL=LLL=LLRL\r\n", "output": "1 2 2 3 3 3 4 1 1 2 1 1 8 7 6 6 5 4 3 3 2 1 2 1\r\n"}, {"input": "100\r\n=L=L=L=R=LR=RRRLRL=LRL=RRLLLLRL=R==R=LLLRR===RR=LR==LRLR===RRLRLLRLLR=LRLRR=L=LRRLLLRR==LLRLLLL==RL\r\n", "output": "4 4 3 3 2 2 1 1 2 2 1 2 2 3 4 5 1 3 2 2 1 2 1 1 2 5 4 3 2 1 2 1 1 2 2 2 4 4 3 2 1 2 3 3 3 3 4 5 5 1 2 2 2 1 2 1 2 2 2 2 3 4 1 3 2 1 3 2 1 2 2 1 2 1 2 3 3 2 2 1 2 4 3 2 1 2 3 3 3 2 1 5 4 3 2 1 1 1 2 1\r\n"}, {"input": "198\r\nLLRRR=RRRRLRRLRR=R===R=RL==R=RLLLR=R=L=LR=R====RRL=RRR=LL=R=RR=RRRLRRLRRR==L=LRLLL====LR=RL==L===LRR=L=L==R==R==L=LLL===R=LLL=R=L=LLLLRLL=RL=LRRLR=RL==RR=R==RLR==R=R==RLRL=LL=RRR=R===LLLRRRRL=RLRLL\r\n", "output": "3 2 1 2 3 4 4 5 6 7 8 1 2 3 1 2 3 3 4 4 4 4 5 5 6 1 1 1 2 2 4 3 2 1 2 2 3 3 2 2 1 2 2 3 3 3 3 3 4 5 1 1 2 3 4 4 2 1 1 2 2 3 4 4 5 6 7 1 2 3 1 2 3 4 4 4 2 2 1 5 4 3 2 2 2 2 2 1 2 2 4 3 3 3 2 2 2 2 1 2 3 3 2 2 1 1 1 2 2 2 5 5 5 4 4 3 2 1 1 1 1 4 4 3 2 1 1 6 6 5 5 4 3 2 1 3 2 1 1 3 2 2 1 2 3 1 2 2 3 1 1 1 2 3 3 4 4 4 5 1 2 2 2 3 3 4 4 4 5 1 4 3 3 2 1 1 2 3 4 4 5 5 5 5 3 2 1 2 3 4 5 1 1 2 1 3 2 1\n"}, {"input": "10\r\nRL=R=RLR=\r\n", "output": "1 2 1 1 2 2 3 1 2 2\r\n"}, {"input": "2\r\nL\r\n", "output": "2 1\r\n"}, {"input": "100\r\nR=R=RRR=R=RR=RRLL=RLRLLLLLR==L=======L=LLR==RL=R=LRLLLR==LLLL=RRRL=LRL=LR=====L=LLLRRL=LLR===RLR=RR\r\n", "output": "1 2 2 3 3 4 5 6 6 7 7 8 9 9 10 11 2 1 1 2 1 6 5 4 3 2 1 5 5 5 4 4 4 4 4 4 4 4 3 3 2 1 2 2 2 3 1 1 2 2 1 4 3 2 1 5 5 5 4 3 2 1 1 2 3 4 2 2 1 3 2 2 1 5 5 5 5 5 5 4 4 3 2 1 2 4 3 3 2 1 2 2 2 2 3 1 2 2 3 4\r\n"}, {"input": "23\r\nL=LLLLRL=RR=RLLLL=RR==\r\n", "output": "6 5 5 4 3 2 1 2 1 1 2 3 3 5 4 3 2 1 1 2 3 3 3\r\n"}, {"input": "4\r\nRRL\r\n", "output": "1 2 3 1\r\n"}, {"input": "17\r\n=RRR=L==LLLLRRRL\r\n", "output": "1 1 2 3 6 6 5 5 5 4 3 2 1 2 3 4 1\r\n"}, {"input": "20\r\nRRLLLLLRRRRRRRRLRLR\r\n", "output": "1 2 6 5 4 3 2 1 2 3 4 5 6 7 8 9 1 2 1 2\r\n"}, {"input": "9\r\nR===RRLL\r\n", "output": "1 2 2 2 2 3 4 2 1\r\n"}, {"input": "15\r\n=RRR=LLLLLRRRL\r\n", "output": "1 1 2 3 6 6 5 4 3 2 1 2 3 4 1\r\n"}]
| false
|
stdio
| null | true
|
67/A
|
67
|
A
|
Python 3
|
TESTS
| 6
| 122
| 409,600
|
109165110
|
toffees = [1]
minimo = 1
numero_alumnos = int(input())
reglas = input()
for i in range(numero_alumnos-1):
if reglas[i] == 'L':
toffees.append(toffees[i] - 1)
if toffees[i+1] < minimo:
minimo = toffees[i+1]
elif reglas[i] == 'R':
toffees.append(toffees[i] + 1)
else:
toffees.append(toffees[i])
if minimo < 1:
for i in range(numero_alumnos):
toffees[i] = toffees[i] + abs(minimo) + 1
respuesta = str(toffees[0])
for i in range(numero_alumnos-1):
respuesta = respuesta + ' ' + str(toffees[i+1])
print(respuesta)
| 43
| 342
| 716,800
|
41874869
|
n = int(input())
s = input()
ans = [1] * n
t = True
while t:
t = False
for i in range(n - 1):
if s[i] == '=' and ans[i] != ans[i + 1]:
ans[i] = ans[i + 1] = max(ans[i], ans[i + 1])
t = True
elif s[i] == 'R' and ans[i] >= ans[i + 1]:
ans[i + 1] += 1
t = True
elif s[i] == 'L' and ans[i] <= ans[i + 1]:
ans[i] += 1
t = True
print(*ans)
|
Manthan 2011
|
CF
| 2,011
| 1
| 256
|
Partial Teacher
|
A teacher decides to give toffees to his students. He asks n students to stand in a queue. Since the teacher is very partial, he follows the following rule to distribute toffees.
He looks at the first two students and gives more toffees to the student having higher marks than the other one. If they have the same marks they get the same number of toffees. The same procedure is followed for each pair of adjacent students starting from the first one to the last one.
It is given that each student receives at least one toffee. You have to find the number of toffees given to each student by the teacher such that the total number of toffees is minimum.
|
The first line of input contains the number of students n (2 ≤ n ≤ 1000). The second line gives (n - 1) characters consisting of "L", "R" and "=". For each pair of adjacent students "L" means that the left student has higher marks, "R" means that the right student has higher marks and "=" means that both have equal marks.
|
Output consists of n integers separated by a space representing the number of toffees each student receives in the queue starting from the first one to the last one.
| null | null |
[{"input": "5\nLRLR", "output": "2 1 2 1 2"}, {"input": "5\n=RRR", "output": "1 1 2 3 4"}]
| 1,800
|
["dp", "graphs", "greedy", "implementation"]
| 43
|
[{"input": "5\r\nLRLR\r\n", "output": "2 1 2 1 2\r\n"}, {"input": "5\r\n=RRR\r\n", "output": "1 1 2 3 4\r\n"}, {"input": "6\r\nRLRL=\r\n", "output": "1 2 1 2 1 1\r\n"}, {"input": "3\r\nR=\r\n", "output": "1 2 2\r\n"}, {"input": "7\r\nRR==RR\r\n", "output": "1 2 3 3 3 4 5\r\n"}, {"input": "166\r\nR===RL=LRRR=RRRL=LRR=R=RR==L=R=R=RRR=RR=RLLRRL=LLRL==L=R==RLR==RL=RR=LR==R=R=LLRLRLR=RR=RLLRLR=RRLL==L=LR=RR=RRRL=RLLLR==L=RRLRLLLLLLLRL===LRLRLRLRRLL=LRLL===LRLRR==\r\n", "output": "1 2 2 2 2 3 2 2 1 2 3 4 4 5 6 7 2 2 1 2 3 3 4 4 5 6 6 6 1 1 2 2 3 3 4 5 6 6 7 8 8 9 2 1 2 4 3 3 2 1 3 2 2 2 1 1 2 2 2 3 1 2 2 2 3 1 1 2 3 3 1 2 2 2 3 3 4 4 2 1 2 1 2 1 2 2 3 4 4 5 2 1 2 1 2 2 3 5 4 3 3 3 2 2 1 2 2 3 4 4 5 6 7 1 1 4 3 2 1 2 2 2 1 1 2 3 1 8 7 6 5 4 3 2 1 3 2 2 2 2 1 2 1 2 1 2 1 2 4 3 2 2 1 4 3 2 2 2 2 1 2 1 2 3 3 3\n"}, {"input": "24\r\nR=R==RL=RL=RLL=LLL=LLRL\r\n", "output": "1 2 2 3 3 3 4 1 1 2 1 1 8 7 6 6 5 4 3 3 2 1 2 1\r\n"}, {"input": "100\r\n=L=L=L=R=LR=RRRLRL=LRL=RRLLLLRL=R==R=LLLRR===RR=LR==LRLR===RRLRLLRLLR=LRLRR=L=LRRLLLRR==LLRLLLL==RL\r\n", "output": "4 4 3 3 2 2 1 1 2 2 1 2 2 3 4 5 1 3 2 2 1 2 1 1 2 5 4 3 2 1 2 1 1 2 2 2 4 4 3 2 1 2 3 3 3 3 4 5 5 1 2 2 2 1 2 1 2 2 2 2 3 4 1 3 2 1 3 2 1 2 2 1 2 1 2 3 3 2 2 1 2 4 3 2 1 2 3 3 3 2 1 5 4 3 2 1 1 1 2 1\r\n"}, {"input": "198\r\nLLRRR=RRRRLRRLRR=R===R=RL==R=RLLLR=R=L=LR=R====RRL=RRR=LL=R=RR=RRRLRRLRRR==L=LRLLL====LR=RL==L===LRR=L=L==R==R==L=LLL===R=LLL=R=L=LLLLRLL=RL=LRRLR=RL==RR=R==RLR==R=R==RLRL=LL=RRR=R===LLLRRRRL=RLRLL\r\n", "output": "3 2 1 2 3 4 4 5 6 7 8 1 2 3 1 2 3 3 4 4 4 4 5 5 6 1 1 1 2 2 4 3 2 1 2 2 3 3 2 2 1 2 2 3 3 3 3 3 4 5 1 1 2 3 4 4 2 1 1 2 2 3 4 4 5 6 7 1 2 3 1 2 3 4 4 4 2 2 1 5 4 3 2 2 2 2 2 1 2 2 4 3 3 3 2 2 2 2 1 2 3 3 2 2 1 1 1 2 2 2 5 5 5 4 4 3 2 1 1 1 1 4 4 3 2 1 1 6 6 5 5 4 3 2 1 3 2 1 1 3 2 2 1 2 3 1 2 2 3 1 1 1 2 3 3 4 4 4 5 1 2 2 2 3 3 4 4 4 5 1 4 3 3 2 1 1 2 3 4 4 5 5 5 5 3 2 1 2 3 4 5 1 1 2 1 3 2 1\n"}, {"input": "10\r\nRL=R=RLR=\r\n", "output": "1 2 1 1 2 2 3 1 2 2\r\n"}, {"input": "2\r\nL\r\n", "output": "2 1\r\n"}, {"input": "100\r\nR=R=RRR=R=RR=RRLL=RLRLLLLLR==L=======L=LLR==RL=R=LRLLLR==LLLL=RRRL=LRL=LR=====L=LLLRRL=LLR===RLR=RR\r\n", "output": "1 2 2 3 3 4 5 6 6 7 7 8 9 9 10 11 2 1 1 2 1 6 5 4 3 2 1 5 5 5 4 4 4 4 4 4 4 4 3 3 2 1 2 2 2 3 1 1 2 2 1 4 3 2 1 5 5 5 4 3 2 1 1 2 3 4 2 2 1 3 2 2 1 5 5 5 5 5 5 4 4 3 2 1 2 4 3 3 2 1 2 2 2 2 3 1 2 2 3 4\r\n"}, {"input": "23\r\nL=LLLLRL=RR=RLLLL=RR==\r\n", "output": "6 5 5 4 3 2 1 2 1 1 2 3 3 5 4 3 2 1 1 2 3 3 3\r\n"}, {"input": "4\r\nRRL\r\n", "output": "1 2 3 1\r\n"}, {"input": "17\r\n=RRR=L==LLLLRRRL\r\n", "output": "1 1 2 3 6 6 5 5 5 4 3 2 1 2 3 4 1\r\n"}, {"input": "20\r\nRRLLLLLRRRRRRRRLRLR\r\n", "output": "1 2 6 5 4 3 2 1 2 3 4 5 6 7 8 9 1 2 1 2\r\n"}, {"input": "9\r\nR===RRLL\r\n", "output": "1 2 2 2 2 3 4 2 1\r\n"}, {"input": "15\r\n=RRR=LLLLLRRRL\r\n", "output": "1 1 2 3 6 6 5 4 3 2 1 2 3 4 1\r\n"}]
| false
|
stdio
| null | true
|
353/B
|
353
|
B
|
Python 3
|
TESTS
| 5
| 124
| 4,608,000
|
16872038
|
#!/usr/bin/env python3
n = int(input())
a = list(map(int,input().split()))
c = [0] * 100
for i in a:
c[i] += 1
x = [0] * 100
y = [0] * 100
xk = 0
yk = 0
j = 0
for i in range(100):
x[i] += c[i] // 2
y[i] += c[i] // 2
if c[i] % 2 == 1:
[x, y][j][i] += 1
j = 1 - j
if x[i]:
xk += 1
if y[i]:
yk += 1
print(xk * yk)
zs = [None] * (2 * n)
for i in range(2*n):
if x[a[i]] > 0:
x[a[i]] -= 1
zs[i] = 1
else:
assert y[a[i]] > 0
y[a[i]] -= 1
zs[i] = 2
print(*zs)
| 40
| 124
| 0
|
4734405
|
N = int(input())
Nums = list(map(int, input().split()))
Good = [1] * (N * 2)
Amounts = [0] * 100
Mono, Duo = 0, 0
for Num in Nums:
if Amounts[Num] == 0:
Mono += 1
elif Amounts[Num] == 1:
Duo += 1
Mono -= 1
Amounts[Num] += 1
Flag = Mono % 2
Duo_Flag = False
Counts = [0] * 100
for i in range(2 * N):
Num = Nums[i]
if Amounts[Num] == 1:
if Flag:
Good[i] = 1
else:
Good[i] = 2
Flag = not Flag
else:
if Counts[Num] == 0:
Good[i] = 1
elif Counts[Num] == 1:
Good[i] = 2
else:
if Duo_Flag:
Good[i] = 1
else:
Good[i] = 2
Duo_Flag = not Duo_Flag
Counts[Num] += 1
print((Duo + (Mono // 2)) * (Duo + ((Mono + 1) // 2)))
print(*Good)
|
Codeforces Round 205 (Div. 2)
|
CF
| 2,013
| 1
| 256
|
Two Heaps
|
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
|
The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.
|
In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
| null |
In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
|
[{"input": "1\n10 99", "output": "1\n2 1"}, {"input": "2\n13 24 13 45", "output": "4\n1 2 2 1"}]
| 1,900
|
["combinatorics", "constructive algorithms", "greedy", "implementation", "math", "sortings"]
| 40
|
[{"input": "1\r\n10 99\r\n", "output": "1\r\n2 1 \r\n"}, {"input": "2\r\n13 24 13 45\r\n", "output": "4\r\n1 2 2 1 \r\n"}, {"input": "5\r\n21 60 18 21 17 39 58 74 62 34\r\n", "output": "25\r\n1 1 1 2 2 1 2 1 2 2 \r\n"}, {"input": "10\r\n26 43 29 92 22 27 95 56 72 55 93 51 91 30 70 77 32 69 87 98\r\n", "output": "100\r\n1 2 1 2 2 2 2 1 2 2 1 1 1 2 1 1 1 2 2 1 \r\n"}, {"input": "20\r\n80 56 58 61 75 60 25 49 59 15 43 39 21 73 67 13 75 31 18 87 32 44 53 15 53 76 79 94 85 80 27 25 48 78 32 18 20 78 46 37\r\n", "output": "400\r\n1 2 1 2 1 1 1 1 2 1 1 2 2 2 1 2 2 2 1 2 1 2 1 2 2 1 2 1 1 2 1 2 2 1 2 2 1 2 1 1 \r\n"}, {"input": "50\r\n49 13 81 20 73 62 19 49 65 95 32 84 24 96 51 57 53 83 40 44 26 65 78 80 92 87 87 95 56 46 22 44 69 80 41 61 97 92 58 53 42 78 53 19 47 36 25 77 65 81 14 61 38 99 27 58 67 37 67 80 77 51 32 43 31 48 19 79 31 91 46 97 91 71 27 63 22 84 73 73 89 44 34 84 70 23 45 31 56 73 83 38 68 45 99 33 83 86 87 80\r\n", "output": "1936\r\n1 2 1 2 1 2 1 2 1 1 1 1 2 1 1 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 2 1 2 2 1 1 2 1 2 1 2 1 2 1 1 1 1 1 2 1 2 1 1 1 2 1 2 2 1 2 2 2 2 1 2 1 2 2 1 2 2 2 1 2 1 2 2 2 1 2 1 2 2 2 1 1 2 2 2 2 2 2 2 2 1 2 1 2 2 \r\n"}, {"input": "2\r\n10 10 10 11\r\n", "output": "2\r\n1 2 1 2 \r\n"}]
| false
|
stdio
|
import sys
def main(input_path, output_path, submission_path):
# Read input
with open(input_path) as f:
n = int(f.readline())
cubes = list(map(int, f.readline().split()))
# Read reference output (to get the expected maximum)
with open(output_path) as f:
expected_max = int(f.readline().strip())
# Read submission output
with open(submission_path) as f:
lines = f.readlines()
if len(lines) < 2:
print(0)
return
submission_max_line = lines[0].strip()
submission_partition_line = lines[1].strip()
try:
submission_max = int(submission_max_line)
submission_partition = list(map(int, submission_partition_line.split()))
except:
print(0)
return
# Check submission_max matches expected_max
if submission_max != expected_max:
print(0)
return
# Check partition is valid
if len(submission_partition) != 2 * n:
print(0)
return
count_1 = submission_partition.count(1)
count_2 = submission_partition.count(2)
if count_1 != n or count_2 != n:
print(0)
return
# Separate heaps
heap1 = [cube for cube, heap in zip(cubes, submission_partition) if heap == 1]
heap2 = [cube for cube, heap in zip(cubes, submission_partition) if heap == 2]
# Generate all possible four-digit numbers
numbers = set()
for x in heap1:
for y in heap2:
numbers.add(x * 100 + y)
actual_count = len(numbers)
if actual_count == submission_max:
print(1)
else:
print(0)
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
|
353/B
|
353
|
B
|
PyPy 3
|
TESTS
| 10
| 280
| 0
|
82437310
|
n=int(input())
arr=list(map(int,input().split()))
g=[[] for i in range(100)]
for i in range(2*n):
g[arr[i]].append(i)
x=0
y=0
curr=1
flip=1
for i in range(10,100):
if len(g[i])==1:
arr[g[i][0]]=curr
if curr==1:
x+=1
else:
y+=1
curr=3-curr
if len(g[i])>1:
arr[g[i][0]]=1
arr[g[i][1]]=2
x+=1
y+=1
for j in range(2,len(g[i])):
arr[g[i][j]]=flip
flip=3-flip
print(x*y)
print(*arr)
| 40
| 124
| 0
|
170345321
|
R,P=lambda:map(int,input().split()),print;n,=R();n*=2;a=[*R()]
c,C,z=[0]*100,[0,0],[0]*n
for x in a:c[x]+=1
for v in c:
if v:C[v==1]+=1
tar1=C[1]//2;P((C[0]+tar1)*(C[0]+C[1]-tar1))
s=sorted([(c[x],x,i)for i,x in enumerate(a)])
for i,v in enumerate(s):z[v[2]]=[1,2][i&1>0]
P(*z)
|
Codeforces Round 205 (Div. 2)
|
CF
| 2,013
| 1
| 256
|
Two Heaps
|
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
|
The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.
|
In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
| null |
In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
|
[{"input": "1\n10 99", "output": "1\n2 1"}, {"input": "2\n13 24 13 45", "output": "4\n1 2 2 1"}]
| 1,900
|
["combinatorics", "constructive algorithms", "greedy", "implementation", "math", "sortings"]
| 40
|
[{"input": "1\r\n10 99\r\n", "output": "1\r\n2 1 \r\n"}, {"input": "2\r\n13 24 13 45\r\n", "output": "4\r\n1 2 2 1 \r\n"}, {"input": "5\r\n21 60 18 21 17 39 58 74 62 34\r\n", "output": "25\r\n1 1 1 2 2 1 2 1 2 2 \r\n"}, {"input": "10\r\n26 43 29 92 22 27 95 56 72 55 93 51 91 30 70 77 32 69 87 98\r\n", "output": "100\r\n1 2 1 2 2 2 2 1 2 2 1 1 1 2 1 1 1 2 2 1 \r\n"}, {"input": "20\r\n80 56 58 61 75 60 25 49 59 15 43 39 21 73 67 13 75 31 18 87 32 44 53 15 53 76 79 94 85 80 27 25 48 78 32 18 20 78 46 37\r\n", "output": "400\r\n1 2 1 2 1 1 1 1 2 1 1 2 2 2 1 2 2 2 1 2 1 2 1 2 2 1 2 1 1 2 1 2 2 1 2 2 1 2 1 1 \r\n"}, {"input": "50\r\n49 13 81 20 73 62 19 49 65 95 32 84 24 96 51 57 53 83 40 44 26 65 78 80 92 87 87 95 56 46 22 44 69 80 41 61 97 92 58 53 42 78 53 19 47 36 25 77 65 81 14 61 38 99 27 58 67 37 67 80 77 51 32 43 31 48 19 79 31 91 46 97 91 71 27 63 22 84 73 73 89 44 34 84 70 23 45 31 56 73 83 38 68 45 99 33 83 86 87 80\r\n", "output": "1936\r\n1 2 1 2 1 2 1 2 1 1 1 1 2 1 1 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 2 1 2 2 1 1 2 1 2 1 2 1 2 1 1 1 1 1 2 1 2 1 1 1 2 1 2 2 1 2 2 2 2 1 2 1 2 2 1 2 2 2 1 2 1 2 2 2 1 2 1 2 2 2 1 1 2 2 2 2 2 2 2 2 1 2 1 2 2 \r\n"}, {"input": "2\r\n10 10 10 11\r\n", "output": "2\r\n1 2 1 2 \r\n"}]
| false
|
stdio
|
import sys
def main(input_path, output_path, submission_path):
# Read input
with open(input_path) as f:
n = int(f.readline())
cubes = list(map(int, f.readline().split()))
# Read reference output (to get the expected maximum)
with open(output_path) as f:
expected_max = int(f.readline().strip())
# Read submission output
with open(submission_path) as f:
lines = f.readlines()
if len(lines) < 2:
print(0)
return
submission_max_line = lines[0].strip()
submission_partition_line = lines[1].strip()
try:
submission_max = int(submission_max_line)
submission_partition = list(map(int, submission_partition_line.split()))
except:
print(0)
return
# Check submission_max matches expected_max
if submission_max != expected_max:
print(0)
return
# Check partition is valid
if len(submission_partition) != 2 * n:
print(0)
return
count_1 = submission_partition.count(1)
count_2 = submission_partition.count(2)
if count_1 != n or count_2 != n:
print(0)
return
# Separate heaps
heap1 = [cube for cube, heap in zip(cubes, submission_partition) if heap == 1]
heap2 = [cube for cube, heap in zip(cubes, submission_partition) if heap == 2]
# Generate all possible four-digit numbers
numbers = set()
for x in heap1:
for y in heap2:
numbers.add(x * 100 + y)
actual_count = len(numbers)
if actual_count == submission_max:
print(1)
else:
print(0)
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
|
353/B
|
353
|
B
|
Python 3
|
TESTS
| 5
| 124
| 5,632,000
|
28011152
|
from collections import defaultdict as defdict
n = int(input())
d = defdict(int)
a = list(map(int, input().split()))
for i in a:
d[i] += 1
b = [i for i in a if d[i] > 1]
d = defdict(int)
v = []
u = []
answ = []
for i, x in enumerate(a):
if x in b:
if d[x] == 0:
u.append(x)
answ.append('1')
elif d[x] == 1:
v.append(x)
answ.append('2')
else:
if len(v) > len(u):
u.append(x)
answ.append('1')
else:
v.append(x)
answ.append('2')
d[x] += 1
else:
if len(v) > len(u):
u.append(x)
answ.append('1')
else:
v.append(x)
answ.append('2')
print(len(set(u)) * len(set(v)))
print(' '.join(answ))
| 40
| 154
| 28,979,200
|
135058301
|
n=int(input())
arr=list(map(int,input().split()))
g=[[] for i in range(100)]
for i in range(2*n):
g[arr[i]].append(i)
x=0
y=0
curr=1
r=[]
for i in range(10,100):
if len(g[i])==1:
arr[g[i][0]]=curr
if curr==1:
x+=1
else:
y+=1
curr=3-curr
if len(g[i])>1:
arr[g[i][0]]=1
arr[g[i][1]]=2
x+=1
y+=1
for j in range(2,len(g[i])):
r.append(g[i][j])
for i in range(len(r)):
arr[r[i]]=2-(i%2)
print(x*y)
print(*arr)
|
Codeforces Round 205 (Div. 2)
|
CF
| 2,013
| 1
| 256
|
Two Heaps
|
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
|
The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.
|
In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
| null |
In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
|
[{"input": "1\n10 99", "output": "1\n2 1"}, {"input": "2\n13 24 13 45", "output": "4\n1 2 2 1"}]
| 1,900
|
["combinatorics", "constructive algorithms", "greedy", "implementation", "math", "sortings"]
| 40
|
[{"input": "1\r\n10 99\r\n", "output": "1\r\n2 1 \r\n"}, {"input": "2\r\n13 24 13 45\r\n", "output": "4\r\n1 2 2 1 \r\n"}, {"input": "5\r\n21 60 18 21 17 39 58 74 62 34\r\n", "output": "25\r\n1 1 1 2 2 1 2 1 2 2 \r\n"}, {"input": "10\r\n26 43 29 92 22 27 95 56 72 55 93 51 91 30 70 77 32 69 87 98\r\n", "output": "100\r\n1 2 1 2 2 2 2 1 2 2 1 1 1 2 1 1 1 2 2 1 \r\n"}, {"input": "20\r\n80 56 58 61 75 60 25 49 59 15 43 39 21 73 67 13 75 31 18 87 32 44 53 15 53 76 79 94 85 80 27 25 48 78 32 18 20 78 46 37\r\n", "output": "400\r\n1 2 1 2 1 1 1 1 2 1 1 2 2 2 1 2 2 2 1 2 1 2 1 2 2 1 2 1 1 2 1 2 2 1 2 2 1 2 1 1 \r\n"}, {"input": "50\r\n49 13 81 20 73 62 19 49 65 95 32 84 24 96 51 57 53 83 40 44 26 65 78 80 92 87 87 95 56 46 22 44 69 80 41 61 97 92 58 53 42 78 53 19 47 36 25 77 65 81 14 61 38 99 27 58 67 37 67 80 77 51 32 43 31 48 19 79 31 91 46 97 91 71 27 63 22 84 73 73 89 44 34 84 70 23 45 31 56 73 83 38 68 45 99 33 83 86 87 80\r\n", "output": "1936\r\n1 2 1 2 1 2 1 2 1 1 1 1 2 1 1 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 2 1 2 2 1 1 2 1 2 1 2 1 2 1 1 1 1 1 2 1 2 1 1 1 2 1 2 2 1 2 2 2 2 1 2 1 2 2 1 2 2 2 1 2 1 2 2 2 1 2 1 2 2 2 1 1 2 2 2 2 2 2 2 2 1 2 1 2 2 \r\n"}, {"input": "2\r\n10 10 10 11\r\n", "output": "2\r\n1 2 1 2 \r\n"}]
| false
|
stdio
|
import sys
def main(input_path, output_path, submission_path):
# Read input
with open(input_path) as f:
n = int(f.readline())
cubes = list(map(int, f.readline().split()))
# Read reference output (to get the expected maximum)
with open(output_path) as f:
expected_max = int(f.readline().strip())
# Read submission output
with open(submission_path) as f:
lines = f.readlines()
if len(lines) < 2:
print(0)
return
submission_max_line = lines[0].strip()
submission_partition_line = lines[1].strip()
try:
submission_max = int(submission_max_line)
submission_partition = list(map(int, submission_partition_line.split()))
except:
print(0)
return
# Check submission_max matches expected_max
if submission_max != expected_max:
print(0)
return
# Check partition is valid
if len(submission_partition) != 2 * n:
print(0)
return
count_1 = submission_partition.count(1)
count_2 = submission_partition.count(2)
if count_1 != n or count_2 != n:
print(0)
return
# Separate heaps
heap1 = [cube for cube, heap in zip(cubes, submission_partition) if heap == 1]
heap2 = [cube for cube, heap in zip(cubes, submission_partition) if heap == 2]
# Generate all possible four-digit numbers
numbers = set()
for x in heap1:
for y in heap2:
numbers.add(x * 100 + y)
actual_count = len(numbers)
if actual_count == submission_max:
print(1)
else:
print(0)
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
|
353/B
|
353
|
B
|
Python 3
|
PRETESTS
| 5
| 92
| 0
|
4735486
|
n = int(input())
set1 = set()
set2 = set()
res = [0] * (2 * n)
S = list(map(int, input().split()))
#A = [[S[i], i] for i in range(2 * n)]
c = 0
for i in sorted([[S[i], i] for i in range(2 * n)]):
if c == 0:
set1.add(i[0])
res[i[1]] = 1
c = 1
else:
set2.add(i[0])
res[i[1]] = 2
c = 0
print(len(set1) * len(set2))
print(' '.join(map(str, res)))
| 40
| 156
| 921,600
|
4735879
|
from math import*
from random import*
n = int(input()) * 2
A = list(map(int, input().split()))
amount = [0] * 101
B = []
for i in range(n):
if amount[A[i]] < 2:
amount[A[i]] += 1
B += [(A[i], i)]
B.sort()
x, y = [], []
for i in range(len(B)):
if(i % 2 == 0):
x.append(B[i][1])
else:
y.append(B[i][1])
lolka = 0
aaa = 0
# print(x)
# print(y)
print(len(x) * len(y))
for i in range(n):
if i in x:
lolka += 1
aaa += 1
print(1, end = ' ')
elif i in y:
print(2, end = ' ')
else:
if len(x) - lolka + aaa < n // 2:
print(1, end = ' ')
aaa += 1
else:
print(2, end = ' ')
print()
# B, C = [], []
# for i in range(n):
# S = list(set(A))
# where = [0] * 101
# am1, am2 = 0, 0
# for i in range(len(S)):
# if(i % 2 == 0):
# where[S[i]] = 1
# am1 += 1
# else:
# where[S[i]] = 2
# am2 += 1
# used = [0] * 201
# for i in range(n):
# if not used[A[i]]:
# print(where[A[i]])
# used[A[i]] = True
# else:
# print(3 - where[A[i]])
|
Codeforces Round 205 (Div. 2)
|
CF
| 2,013
| 1
| 256
|
Two Heaps
|
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
|
The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.
|
In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
| null |
In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
|
[{"input": "1\n10 99", "output": "1\n2 1"}, {"input": "2\n13 24 13 45", "output": "4\n1 2 2 1"}]
| 1,900
|
["combinatorics", "constructive algorithms", "greedy", "implementation", "math", "sortings"]
| 40
|
[{"input": "1\r\n10 99\r\n", "output": "1\r\n2 1 \r\n"}, {"input": "2\r\n13 24 13 45\r\n", "output": "4\r\n1 2 2 1 \r\n"}, {"input": "5\r\n21 60 18 21 17 39 58 74 62 34\r\n", "output": "25\r\n1 1 1 2 2 1 2 1 2 2 \r\n"}, {"input": "10\r\n26 43 29 92 22 27 95 56 72 55 93 51 91 30 70 77 32 69 87 98\r\n", "output": "100\r\n1 2 1 2 2 2 2 1 2 2 1 1 1 2 1 1 1 2 2 1 \r\n"}, {"input": "20\r\n80 56 58 61 75 60 25 49 59 15 43 39 21 73 67 13 75 31 18 87 32 44 53 15 53 76 79 94 85 80 27 25 48 78 32 18 20 78 46 37\r\n", "output": "400\r\n1 2 1 2 1 1 1 1 2 1 1 2 2 2 1 2 2 2 1 2 1 2 1 2 2 1 2 1 1 2 1 2 2 1 2 2 1 2 1 1 \r\n"}, {"input": "50\r\n49 13 81 20 73 62 19 49 65 95 32 84 24 96 51 57 53 83 40 44 26 65 78 80 92 87 87 95 56 46 22 44 69 80 41 61 97 92 58 53 42 78 53 19 47 36 25 77 65 81 14 61 38 99 27 58 67 37 67 80 77 51 32 43 31 48 19 79 31 91 46 97 91 71 27 63 22 84 73 73 89 44 34 84 70 23 45 31 56 73 83 38 68 45 99 33 83 86 87 80\r\n", "output": "1936\r\n1 2 1 2 1 2 1 2 1 1 1 1 2 1 1 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 2 1 2 2 1 1 2 1 2 1 2 1 2 1 1 1 1 1 2 1 2 1 1 1 2 1 2 2 1 2 2 2 2 1 2 1 2 2 1 2 2 2 1 2 1 2 2 2 1 2 1 2 2 2 1 1 2 2 2 2 2 2 2 2 1 2 1 2 2 \r\n"}, {"input": "2\r\n10 10 10 11\r\n", "output": "2\r\n1 2 1 2 \r\n"}]
| false
|
stdio
|
import sys
def main(input_path, output_path, submission_path):
# Read input
with open(input_path) as f:
n = int(f.readline())
cubes = list(map(int, f.readline().split()))
# Read reference output (to get the expected maximum)
with open(output_path) as f:
expected_max = int(f.readline().strip())
# Read submission output
with open(submission_path) as f:
lines = f.readlines()
if len(lines) < 2:
print(0)
return
submission_max_line = lines[0].strip()
submission_partition_line = lines[1].strip()
try:
submission_max = int(submission_max_line)
submission_partition = list(map(int, submission_partition_line.split()))
except:
print(0)
return
# Check submission_max matches expected_max
if submission_max != expected_max:
print(0)
return
# Check partition is valid
if len(submission_partition) != 2 * n:
print(0)
return
count_1 = submission_partition.count(1)
count_2 = submission_partition.count(2)
if count_1 != n or count_2 != n:
print(0)
return
# Separate heaps
heap1 = [cube for cube, heap in zip(cubes, submission_partition) if heap == 1]
heap2 = [cube for cube, heap in zip(cubes, submission_partition) if heap == 2]
# Generate all possible four-digit numbers
numbers = set()
for x in heap1:
for y in heap2:
numbers.add(x * 100 + y)
actual_count = len(numbers)
if actual_count == submission_max:
print(1)
else:
print(0)
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
|
353/B
|
353
|
B
|
Python 3
|
TESTS
| 5
| 218
| 307,200
|
63354989
|
n = int(input())
num = list(map(int, input().split(" ")))
initial = list(enumerate(num))
initial = [list(x) for x in initial]
sorted_init = sorted(initial, key = lambda x: x[1])
# sorted_init = list(enumerate(sorted_init))
left = sorted_init[::2]
left_places = [[i[0],1] for i in left]
right = sorted_init[1::2]
right_places = [[i[0],2] for i in right]
all_places = left_places+right_places
all_places = sorted(all_places, key = lambda x: x[0])
only_places = [i[1] for i in all_places]
answer_2 = " ".join(map(str, only_places))
left_unique = list(set([i[1] for i in left]))
right_unique = list(set([i[1] for i in right]))
answer_1 = len(left_unique)*len(right_unique)
print(answer_1)
print(answer_2)
| 40
| 184
| 7,372,800
|
36673593
|
n = int(input())
a = list(map(int, input().split()))
R = range(100)
c = [[] for _ in [0]*100]
for i in range(n*2):
c[a[i]].append(i)
d = [0]*200
heap = 1
z = [0, 0, 0]
for i in R:
if len(c[i]) == 1:
z[heap]+=1
d[c[i][0]] = heap
heap = 3 - heap;
for i in R:
if len(c[i]) > 1:
z[1]+=1
z[2]+=1
while len(c[i]):
d[c[i].pop()] = heap
heap = 3 - heap
print(z[1]*z[2])
print(' '.join(map(str, d[:n*2])))
|
Codeforces Round 205 (Div. 2)
|
CF
| 2,013
| 1
| 256
|
Two Heaps
|
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
|
The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.
|
In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
| null |
In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
|
[{"input": "1\n10 99", "output": "1\n2 1"}, {"input": "2\n13 24 13 45", "output": "4\n1 2 2 1"}]
| 1,900
|
["combinatorics", "constructive algorithms", "greedy", "implementation", "math", "sortings"]
| 40
|
[{"input": "1\r\n10 99\r\n", "output": "1\r\n2 1 \r\n"}, {"input": "2\r\n13 24 13 45\r\n", "output": "4\r\n1 2 2 1 \r\n"}, {"input": "5\r\n21 60 18 21 17 39 58 74 62 34\r\n", "output": "25\r\n1 1 1 2 2 1 2 1 2 2 \r\n"}, {"input": "10\r\n26 43 29 92 22 27 95 56 72 55 93 51 91 30 70 77 32 69 87 98\r\n", "output": "100\r\n1 2 1 2 2 2 2 1 2 2 1 1 1 2 1 1 1 2 2 1 \r\n"}, {"input": "20\r\n80 56 58 61 75 60 25 49 59 15 43 39 21 73 67 13 75 31 18 87 32 44 53 15 53 76 79 94 85 80 27 25 48 78 32 18 20 78 46 37\r\n", "output": "400\r\n1 2 1 2 1 1 1 1 2 1 1 2 2 2 1 2 2 2 1 2 1 2 1 2 2 1 2 1 1 2 1 2 2 1 2 2 1 2 1 1 \r\n"}, {"input": "50\r\n49 13 81 20 73 62 19 49 65 95 32 84 24 96 51 57 53 83 40 44 26 65 78 80 92 87 87 95 56 46 22 44 69 80 41 61 97 92 58 53 42 78 53 19 47 36 25 77 65 81 14 61 38 99 27 58 67 37 67 80 77 51 32 43 31 48 19 79 31 91 46 97 91 71 27 63 22 84 73 73 89 44 34 84 70 23 45 31 56 73 83 38 68 45 99 33 83 86 87 80\r\n", "output": "1936\r\n1 2 1 2 1 2 1 2 1 1 1 1 2 1 1 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 2 1 2 2 1 1 2 1 2 1 2 1 2 1 1 1 1 1 2 1 2 1 1 1 2 1 2 2 1 2 2 2 2 1 2 1 2 2 1 2 2 2 1 2 1 2 2 2 1 2 1 2 2 2 1 1 2 2 2 2 2 2 2 2 1 2 1 2 2 \r\n"}, {"input": "2\r\n10 10 10 11\r\n", "output": "2\r\n1 2 1 2 \r\n"}]
| false
|
stdio
|
import sys
def main(input_path, output_path, submission_path):
# Read input
with open(input_path) as f:
n = int(f.readline())
cubes = list(map(int, f.readline().split()))
# Read reference output (to get the expected maximum)
with open(output_path) as f:
expected_max = int(f.readline().strip())
# Read submission output
with open(submission_path) as f:
lines = f.readlines()
if len(lines) < 2:
print(0)
return
submission_max_line = lines[0].strip()
submission_partition_line = lines[1].strip()
try:
submission_max = int(submission_max_line)
submission_partition = list(map(int, submission_partition_line.split()))
except:
print(0)
return
# Check submission_max matches expected_max
if submission_max != expected_max:
print(0)
return
# Check partition is valid
if len(submission_partition) != 2 * n:
print(0)
return
count_1 = submission_partition.count(1)
count_2 = submission_partition.count(2)
if count_1 != n or count_2 != n:
print(0)
return
# Separate heaps
heap1 = [cube for cube, heap in zip(cubes, submission_partition) if heap == 1]
heap2 = [cube for cube, heap in zip(cubes, submission_partition) if heap == 2]
# Generate all possible four-digit numbers
numbers = set()
for x in heap1:
for y in heap2:
numbers.add(x * 100 + y)
actual_count = len(numbers)
if actual_count == submission_max:
print(1)
else:
print(0)
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
|
353/B
|
353
|
B
|
Python 3
|
PRETESTS
| 5
| 62
| 0
|
4731845
|
m = int(input())
n = 2 * m
C = list(map(int, input().split()))
A = [[C[i], i] for i in range(n)]
A.sort()
B = [0 for q in range(n)]
i = 0
count = 0
diff = 0
while i < n - 1:
if A[i][0] == A[i + 1][0]:
B[A[i][1]] = 1
B[A[i + 1][1]] = 2
count += 1
while i < n - 1 and A[i][0] == A[i + 1][0]:
i += 1
else:
i += 1
diff += 1
diff += 1
diff -= count
diff1 = diff // 2
diff2 = diff - diff1
print((diff1+ count) * (diff2 +count))
B[A[0][1]] = 1
if A[0][0] != A[1][0]:
diff1 -= 1
for i in range(1, n):
if B[A[i][1]] == 0 and A[i][0] != A[i - 1][0]:
if diff1 > 0:
B[A[i][1]] = 1
diff1 -= 1
else:
B[A[i][1]] = 2
count1 = m - count - diff1
for i in range(1, n):
if B[A[i][1]] == 0:
if count1 > 0:
B[A[i][1]] = 1
count1 -= 1
else:
B[A[i][1]] = 2
print(' '.join(map(str, B)))
| 40
| 248
| 0
|
42125110
|
n = int(input())
a = list(map(int, input().split()))
R = range(100)
c = [[] for _ in [0]*100]
for i in range(n*2):
c[a[i]].append(i)
d = [0]*200
heap = 1
z = [0, 0, 0]
for i in R:
if len(c[i]) == 1:
z[heap]+=1
d[c[i][0]] = heap
heap = 3 - heap;
for i in R:
if len(c[i]) > 1:
z[1]+=1
z[2]+=1
while len(c[i]):
d[c[i].pop()] = heap
heap = 3 - heap
print(z[1]*z[2])
print(' '.join(map(str, d[:n*2])))
# Made By Mostafa_Khaled
|
Codeforces Round 205 (Div. 2)
|
CF
| 2,013
| 1
| 256
|
Two Heaps
|
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
|
The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.
|
In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
| null |
In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
|
[{"input": "1\n10 99", "output": "1\n2 1"}, {"input": "2\n13 24 13 45", "output": "4\n1 2 2 1"}]
| 1,900
|
["combinatorics", "constructive algorithms", "greedy", "implementation", "math", "sortings"]
| 40
|
[{"input": "1\r\n10 99\r\n", "output": "1\r\n2 1 \r\n"}, {"input": "2\r\n13 24 13 45\r\n", "output": "4\r\n1 2 2 1 \r\n"}, {"input": "5\r\n21 60 18 21 17 39 58 74 62 34\r\n", "output": "25\r\n1 1 1 2 2 1 2 1 2 2 \r\n"}, {"input": "10\r\n26 43 29 92 22 27 95 56 72 55 93 51 91 30 70 77 32 69 87 98\r\n", "output": "100\r\n1 2 1 2 2 2 2 1 2 2 1 1 1 2 1 1 1 2 2 1 \r\n"}, {"input": "20\r\n80 56 58 61 75 60 25 49 59 15 43 39 21 73 67 13 75 31 18 87 32 44 53 15 53 76 79 94 85 80 27 25 48 78 32 18 20 78 46 37\r\n", "output": "400\r\n1 2 1 2 1 1 1 1 2 1 1 2 2 2 1 2 2 2 1 2 1 2 1 2 2 1 2 1 1 2 1 2 2 1 2 2 1 2 1 1 \r\n"}, {"input": "50\r\n49 13 81 20 73 62 19 49 65 95 32 84 24 96 51 57 53 83 40 44 26 65 78 80 92 87 87 95 56 46 22 44 69 80 41 61 97 92 58 53 42 78 53 19 47 36 25 77 65 81 14 61 38 99 27 58 67 37 67 80 77 51 32 43 31 48 19 79 31 91 46 97 91 71 27 63 22 84 73 73 89 44 34 84 70 23 45 31 56 73 83 38 68 45 99 33 83 86 87 80\r\n", "output": "1936\r\n1 2 1 2 1 2 1 2 1 1 1 1 2 1 1 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 2 1 2 2 1 1 2 1 2 1 2 1 2 1 1 1 1 1 2 1 2 1 1 1 2 1 2 2 1 2 2 2 2 1 2 1 2 2 1 2 2 2 1 2 1 2 2 2 1 2 1 2 2 2 1 1 2 2 2 2 2 2 2 2 1 2 1 2 2 \r\n"}, {"input": "2\r\n10 10 10 11\r\n", "output": "2\r\n1 2 1 2 \r\n"}]
| false
|
stdio
|
import sys
def main(input_path, output_path, submission_path):
# Read input
with open(input_path) as f:
n = int(f.readline())
cubes = list(map(int, f.readline().split()))
# Read reference output (to get the expected maximum)
with open(output_path) as f:
expected_max = int(f.readline().strip())
# Read submission output
with open(submission_path) as f:
lines = f.readlines()
if len(lines) < 2:
print(0)
return
submission_max_line = lines[0].strip()
submission_partition_line = lines[1].strip()
try:
submission_max = int(submission_max_line)
submission_partition = list(map(int, submission_partition_line.split()))
except:
print(0)
return
# Check submission_max matches expected_max
if submission_max != expected_max:
print(0)
return
# Check partition is valid
if len(submission_partition) != 2 * n:
print(0)
return
count_1 = submission_partition.count(1)
count_2 = submission_partition.count(2)
if count_1 != n or count_2 != n:
print(0)
return
# Separate heaps
heap1 = [cube for cube, heap in zip(cubes, submission_partition) if heap == 1]
heap2 = [cube for cube, heap in zip(cubes, submission_partition) if heap == 2]
# Generate all possible four-digit numbers
numbers = set()
for x in heap1:
for y in heap2:
numbers.add(x * 100 + y)
actual_count = len(numbers)
if actual_count == submission_max:
print(1)
else:
print(0)
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
|
904/C
|
906
|
A
|
Python 3
|
TESTS
| 7
| 77
| 5,939,200
|
33661546
|
n = int(input())
no = set()
yes = set()
ls = []
count = 0
for i in range(n):
x = input().split()
if x[0] != '.':
count += 1
ls.append(x)
count -= 1
act = 0
for i in range(n - 1):
x = ls[i]
if x[0] == '.':
no |= set(x[1])
elif x[0] == '!':
act += 1
if len(yes) == 0:
yes |= set(x[1])
yes &= set(x[1])
else:
act += 1
no |= set(x[1])
yes -= no
if len(yes) == 1:
break
print(count - act)
| 38
| 202
| 3,891,200
|
77132636
|
input=__import__('sys').stdin.readline
left,bad=set([chr(i+ord('a')) for i in range(26)]),0
q=int(input())
for i in range(q-1):
op,s = input().split()
bad += len(left)==1and op!='.'
if op=='!': left=left&set(s)
else: left=left-set(s)
print(bad)
|
Технокубок 2018 - Отборочный Раунд 4
|
CF
| 2,017
| 2
| 256
|
Shockers
|
Valentin participates in a show called "Shockers". The rules are quite easy: jury selects one letter which Valentin doesn't know. He should make a small speech, but every time he pronounces a word that contains the selected letter, he receives an electric shock. He can make guesses which letter is selected, but for each incorrect guess he receives an electric shock too. The show ends when Valentin guesses the selected letter correctly.
Valentin can't keep in mind everything, so he could guess the selected letter much later than it can be uniquely determined and get excessive electric shocks. Excessive electric shocks are those which Valentin got after the moment the selected letter can be uniquely determined. You should find out the number of excessive electric shocks.
|
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of actions Valentin did.
The next n lines contain descriptions of his actions, each line contains description of one action. Each action can be of one of three types:
1. Valentin pronounced some word and didn't get an electric shock. This action is described by the string ". w" (without quotes), in which "." is a dot (ASCII-code 46), and w is the word that Valentin said.
2. Valentin pronounced some word and got an electric shock. This action is described by the string "! w" (without quotes), in which "!" is an exclamation mark (ASCII-code 33), and w is the word that Valentin said.
3. Valentin made a guess about the selected letter. This action is described by the string "? s" (without quotes), in which "?" is a question mark (ASCII-code 63), and s is the guess — a lowercase English letter.
All words consist only of lowercase English letters. The total length of all words does not exceed 105.
It is guaranteed that last action is a guess about the selected letter. Also, it is guaranteed that Valentin didn't make correct guesses about the selected letter before the last action. Moreover, it's guaranteed that if Valentin got an electric shock after pronouncing some word, then it contains the selected letter; and also if Valentin didn't get an electric shock after pronouncing some word, then it does not contain the selected letter.
|
Output a single integer — the number of electric shocks that Valentin could have avoided if he had told the selected letter just after it became uniquely determined.
| null |
In the first test case after the first action it becomes clear that the selected letter is one of the following: a, b, c. After the second action we can note that the selected letter is not a. Valentin tells word "b" and doesn't get a shock. After that it is clear that the selected letter is c, but Valentin pronounces the word cd and gets an excessive electric shock.
In the second test case after the first two electric shocks we understand that the selected letter is e or o. Valentin tries some words consisting of these letters and after the second word it's clear that the selected letter is e, but Valentin makes 3 more actions before he makes a correct hypothesis.
In the third example the selected letter can be uniquely determined only when Valentin guesses it, so he didn't get excessive electric shocks.
|
[{"input": "5\n! abc\n. ad\n. b\n! cd\n? c", "output": "1"}, {"input": "8\n! hello\n! codeforces\n? c\n. o\n? d\n? h\n. l\n? e", "output": "2"}, {"input": "7\n! ababahalamaha\n? a\n? b\n? a\n? b\n? a\n? h", "output": "0"}]
| 1,600
|
["strings"]
| 38
|
[{"input": "5\r\n! abc\r\n. ad\r\n. b\r\n! cd\r\n? c\r\n", "output": "1\r\n"}, {"input": "8\r\n! hello\r\n! codeforces\r\n? c\r\n. o\r\n? d\r\n? h\r\n. l\r\n? e\r\n", "output": "2\r\n"}, {"input": "7\r\n! ababahalamaha\r\n? a\r\n? b\r\n? a\r\n? b\r\n? a\r\n? h\r\n", "output": "0\r\n"}, {"input": "4\r\n! abcd\r\n! cdef\r\n? d\r\n? c\r\n", "output": "0\r\n"}, {"input": "1\r\n? q\r\n", "output": "0\r\n"}, {"input": "15\r\n. r\r\n? e\r\n. s\r\n. rw\r\n? y\r\n. fj\r\n. zftyd\r\n? r\r\n! wq\r\n? w\r\n? p\r\n. ours\r\n. dto\r\n. lbyfru\r\n? q\r\n", "output": "2\r\n"}, {"input": "3\r\n. abcdefghijklmnopqrstuvwxy\r\n? a\r\n? z\r\n", "output": "1\r\n"}, {"input": "3\r\n. abcdefghijklmnopqrstuvwxy\r\n! z\r\n? z\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
246/B
|
246
|
B
|
Python 3
|
TESTS
| 6
| 186
| 3,276,800
|
96334672
|
n=int(input())
m=list(map(int,input().split()))
a=n-(m.count(max(m)))
print([(n),(n)-1][a%2!=0])
| 30
| 92
| 2,560,000
|
156293637
|
I=input
n=int(I())
if sum(map(int,I().split()))%n:n-=1
print(n)
|
Codeforces Round 151 (Div. 2)
|
CF
| 2,012
| 2
| 256
|
Increase and Decrease
|
Polycarpus has an array, consisting of n integers a1, a2, ..., an. Polycarpus likes it when numbers in an array match. That's why he wants the array to have as many equal numbers as possible. For that Polycarpus performs the following operation multiple times:
- he chooses two elements of the array ai, aj (i ≠ j);
- he simultaneously increases number ai by 1 and decreases number aj by 1, that is, executes ai = ai + 1 and aj = aj - 1.
The given operation changes exactly two distinct array elements. Polycarpus can apply the described operation an infinite number of times.
Now he wants to know what maximum number of equal array elements he can get if he performs an arbitrary number of such operation. Help Polycarpus.
|
The first line contains integer n (1 ≤ n ≤ 105) — the array size. The second line contains space-separated integers a1, a2, ..., an (|ai| ≤ 104) — the original array.
|
Print a single integer — the maximum number of equal array elements he can get if he performs an arbitrary number of the given operation.
| null | null |
[{"input": "2\n2 1", "output": "1"}, {"input": "3\n1 4 1", "output": "3"}]
| 1,300
|
["greedy", "math"]
| 30
|
[{"input": "2\r\n2 1\r\n", "output": "1\r\n"}, {"input": "3\r\n1 4 1\r\n", "output": "3\r\n"}, {"input": "4\r\n2 -7 -2 -6\r\n", "output": "3\r\n"}, {"input": "4\r\n2 0 -2 -1\r\n", "output": "3\r\n"}, {"input": "6\r\n-1 1 0 0 -1 -1\r\n", "output": "5\r\n"}, {"input": "5\r\n0 0 0 0 0\r\n", "output": "5\r\n"}]
| false
|
stdio
| null | true
|
246/B
|
246
|
B
|
PyPy 3
|
TESTS
| 12
| 280
| 25,292,800
|
122638865
|
n=int(input());l=list(map(int,input().split()));l.sort()
print(n if n%2 else n-1)
| 30
| 92
| 2,969,600
|
153579057
|
n=int(input())
l=list(map(int,input().split(" ")))
s=sum(l)
val=n if s%n==0 else n-1
print(val)
|
Codeforces Round 151 (Div. 2)
|
CF
| 2,012
| 2
| 256
|
Increase and Decrease
|
Polycarpus has an array, consisting of n integers a1, a2, ..., an. Polycarpus likes it when numbers in an array match. That's why he wants the array to have as many equal numbers as possible. For that Polycarpus performs the following operation multiple times:
- he chooses two elements of the array ai, aj (i ≠ j);
- he simultaneously increases number ai by 1 and decreases number aj by 1, that is, executes ai = ai + 1 and aj = aj - 1.
The given operation changes exactly two distinct array elements. Polycarpus can apply the described operation an infinite number of times.
Now he wants to know what maximum number of equal array elements he can get if he performs an arbitrary number of such operation. Help Polycarpus.
|
The first line contains integer n (1 ≤ n ≤ 105) — the array size. The second line contains space-separated integers a1, a2, ..., an (|ai| ≤ 104) — the original array.
|
Print a single integer — the maximum number of equal array elements he can get if he performs an arbitrary number of the given operation.
| null | null |
[{"input": "2\n2 1", "output": "1"}, {"input": "3\n1 4 1", "output": "3"}]
| 1,300
|
["greedy", "math"]
| 30
|
[{"input": "2\r\n2 1\r\n", "output": "1\r\n"}, {"input": "3\r\n1 4 1\r\n", "output": "3\r\n"}, {"input": "4\r\n2 -7 -2 -6\r\n", "output": "3\r\n"}, {"input": "4\r\n2 0 -2 -1\r\n", "output": "3\r\n"}, {"input": "6\r\n-1 1 0 0 -1 -1\r\n", "output": "5\r\n"}, {"input": "5\r\n0 0 0 0 0\r\n", "output": "5\r\n"}]
| false
|
stdio
| null | true
|
904/C
|
906
|
A
|
Python 3
|
TESTS
| 7
| 124
| 307,200
|
43946102
|
m = int(input())
goodSet = set()
badSet = set()
count = 0
for i in range(m-1):
mode,word = input().split(' ')
if(len(goodSet)==1):
count += int(mode!='.')
continue
word = set(word)
if(mode=='!'):
if(len(goodSet)==0):
goodSet |= word;
goodSet -= badSet;
else:
goodSet &= word;
elif(mode=='.' or mode=='?'):
badSet |= word;
goodSet -= badSet;
input()
print(count)
| 38
| 218
| 34,508,800
|
80300935
|
from sys import stdin, stdout
input = stdin.readline
n = int(input())
queries = list(input().split() for _ in range(n))
chars = [chr(ord('a')+i) for i in range(26)]
chars_set = set(list(chars))
res, cnt = '', 0
for c, f in queries:
if c == '.':
for q in f:
if q in chars_set:
chars_set.discard(q)
elif c == '!':
if res: cnt += 1
chars_set = set(list(f)) & chars_set
elif c == '?':
if res and f != res: cnt += 1
chars_set.discard(f)
if not res and len(chars_set) == 1:
res = list(chars_set)[0]
print(cnt)
|
Технокубок 2018 - Отборочный Раунд 4
|
CF
| 2,017
| 2
| 256
|
Shockers
|
Valentin participates in a show called "Shockers". The rules are quite easy: jury selects one letter which Valentin doesn't know. He should make a small speech, but every time he pronounces a word that contains the selected letter, he receives an electric shock. He can make guesses which letter is selected, but for each incorrect guess he receives an electric shock too. The show ends when Valentin guesses the selected letter correctly.
Valentin can't keep in mind everything, so he could guess the selected letter much later than it can be uniquely determined and get excessive electric shocks. Excessive electric shocks are those which Valentin got after the moment the selected letter can be uniquely determined. You should find out the number of excessive electric shocks.
|
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of actions Valentin did.
The next n lines contain descriptions of his actions, each line contains description of one action. Each action can be of one of three types:
1. Valentin pronounced some word and didn't get an electric shock. This action is described by the string ". w" (without quotes), in which "." is a dot (ASCII-code 46), and w is the word that Valentin said.
2. Valentin pronounced some word and got an electric shock. This action is described by the string "! w" (without quotes), in which "!" is an exclamation mark (ASCII-code 33), and w is the word that Valentin said.
3. Valentin made a guess about the selected letter. This action is described by the string "? s" (without quotes), in which "?" is a question mark (ASCII-code 63), and s is the guess — a lowercase English letter.
All words consist only of lowercase English letters. The total length of all words does not exceed 105.
It is guaranteed that last action is a guess about the selected letter. Also, it is guaranteed that Valentin didn't make correct guesses about the selected letter before the last action. Moreover, it's guaranteed that if Valentin got an electric shock after pronouncing some word, then it contains the selected letter; and also if Valentin didn't get an electric shock after pronouncing some word, then it does not contain the selected letter.
|
Output a single integer — the number of electric shocks that Valentin could have avoided if he had told the selected letter just after it became uniquely determined.
| null |
In the first test case after the first action it becomes clear that the selected letter is one of the following: a, b, c. After the second action we can note that the selected letter is not a. Valentin tells word "b" and doesn't get a shock. After that it is clear that the selected letter is c, but Valentin pronounces the word cd and gets an excessive electric shock.
In the second test case after the first two electric shocks we understand that the selected letter is e or o. Valentin tries some words consisting of these letters and after the second word it's clear that the selected letter is e, but Valentin makes 3 more actions before he makes a correct hypothesis.
In the third example the selected letter can be uniquely determined only when Valentin guesses it, so he didn't get excessive electric shocks.
|
[{"input": "5\n! abc\n. ad\n. b\n! cd\n? c", "output": "1"}, {"input": "8\n! hello\n! codeforces\n? c\n. o\n? d\n? h\n. l\n? e", "output": "2"}, {"input": "7\n! ababahalamaha\n? a\n? b\n? a\n? b\n? a\n? h", "output": "0"}]
| 1,600
|
["strings"]
| 38
|
[{"input": "5\r\n! abc\r\n. ad\r\n. b\r\n! cd\r\n? c\r\n", "output": "1\r\n"}, {"input": "8\r\n! hello\r\n! codeforces\r\n? c\r\n. o\r\n? d\r\n? h\r\n. l\r\n? e\r\n", "output": "2\r\n"}, {"input": "7\r\n! ababahalamaha\r\n? a\r\n? b\r\n? a\r\n? b\r\n? a\r\n? h\r\n", "output": "0\r\n"}, {"input": "4\r\n! abcd\r\n! cdef\r\n? d\r\n? c\r\n", "output": "0\r\n"}, {"input": "1\r\n? q\r\n", "output": "0\r\n"}, {"input": "15\r\n. r\r\n? e\r\n. s\r\n. rw\r\n? y\r\n. fj\r\n. zftyd\r\n? r\r\n! wq\r\n? w\r\n? p\r\n. ours\r\n. dto\r\n. lbyfru\r\n? q\r\n", "output": "2\r\n"}, {"input": "3\r\n. abcdefghijklmnopqrstuvwxy\r\n? a\r\n? z\r\n", "output": "1\r\n"}, {"input": "3\r\n. abcdefghijklmnopqrstuvwxy\r\n! z\r\n? z\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
353/B
|
353
|
B
|
PyPy 3-64
|
TESTS
| 5
| 154
| 0
|
170339955
|
R,P,G=lambda:map(int,input().split()),print,range;n,=R();n*=2;a=[*R()];z=[0]*n
from collections import Counter as CT;cnt=CT(a);c=[0,0];i1,i2,h1=0,{},0
for x in cnt:
if cnt[x]>1:c[1]+=1;i2[x]=0
else:c[0]+=1
for i,x in enumerate(a):
if cnt[x]>1 and i2[x]<2:
if i2[x]==0:z[i]=1;h1+=1
else:z[i]=2
i2[x]+=1
for i,x in enumerate(a):
if cnt[x]==1:
if i1<c[0]//2:z[i]=1;h1+=1;i1+=1
else:z[i]=2
for i,x in enumerate(a):
if cnt[x]>1 and z[i]==0:
if h1<n:z[i]=1;h1+=1
else:z[i]=2
P((c[1]+c[0]//2)*(c[1]+c[0]-c[0]//2));P(*z)
| 40
| 248
| 0
|
54372401
|
n = int(input())
arr = list(map(int, input().split()))
adj = [[] for i in range(100)]
for i in range(2*n):
adj[arr[i]].append(i)
res = [0] * (2 * n)
mul, curr = [], 1
x = [0, 0]
for i in range(10, 100):
if len(adj[i]) == 1 :
res[adj[i][0]] = curr
x[curr-1] += 1
curr = 3 - curr
if len(adj[i]) > 1:
res[adj[i][0]] = 1
res[adj[i][1]] = 2
x[0] += 1
x[1] += 1
for j in range(2, len(adj[i])):
mul.append(adj[i][j])
for i in range(len(mul)):
res[mul[i]] = curr
curr = 3 - curr
print(x[0] * x[1])
print(*res)
|
Codeforces Round 205 (Div. 2)
|
CF
| 2,013
| 1
| 256
|
Two Heaps
|
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
|
The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.
|
In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
| null |
In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
|
[{"input": "1\n10 99", "output": "1\n2 1"}, {"input": "2\n13 24 13 45", "output": "4\n1 2 2 1"}]
| 1,900
|
["combinatorics", "constructive algorithms", "greedy", "implementation", "math", "sortings"]
| 40
|
[{"input": "1\r\n10 99\r\n", "output": "1\r\n2 1 \r\n"}, {"input": "2\r\n13 24 13 45\r\n", "output": "4\r\n1 2 2 1 \r\n"}, {"input": "5\r\n21 60 18 21 17 39 58 74 62 34\r\n", "output": "25\r\n1 1 1 2 2 1 2 1 2 2 \r\n"}, {"input": "10\r\n26 43 29 92 22 27 95 56 72 55 93 51 91 30 70 77 32 69 87 98\r\n", "output": "100\r\n1 2 1 2 2 2 2 1 2 2 1 1 1 2 1 1 1 2 2 1 \r\n"}, {"input": "20\r\n80 56 58 61 75 60 25 49 59 15 43 39 21 73 67 13 75 31 18 87 32 44 53 15 53 76 79 94 85 80 27 25 48 78 32 18 20 78 46 37\r\n", "output": "400\r\n1 2 1 2 1 1 1 1 2 1 1 2 2 2 1 2 2 2 1 2 1 2 1 2 2 1 2 1 1 2 1 2 2 1 2 2 1 2 1 1 \r\n"}, {"input": "50\r\n49 13 81 20 73 62 19 49 65 95 32 84 24 96 51 57 53 83 40 44 26 65 78 80 92 87 87 95 56 46 22 44 69 80 41 61 97 92 58 53 42 78 53 19 47 36 25 77 65 81 14 61 38 99 27 58 67 37 67 80 77 51 32 43 31 48 19 79 31 91 46 97 91 71 27 63 22 84 73 73 89 44 34 84 70 23 45 31 56 73 83 38 68 45 99 33 83 86 87 80\r\n", "output": "1936\r\n1 2 1 2 1 2 1 2 1 1 1 1 2 1 1 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 2 1 2 2 1 1 2 1 2 1 2 1 2 1 1 1 1 1 2 1 2 1 1 1 2 1 2 2 1 2 2 2 2 1 2 1 2 2 1 2 2 2 1 2 1 2 2 2 1 2 1 2 2 2 1 1 2 2 2 2 2 2 2 2 1 2 1 2 2 \r\n"}, {"input": "2\r\n10 10 10 11\r\n", "output": "2\r\n1 2 1 2 \r\n"}]
| false
|
stdio
|
import sys
def main(input_path, output_path, submission_path):
# Read input
with open(input_path) as f:
n = int(f.readline())
cubes = list(map(int, f.readline().split()))
# Read reference output (to get the expected maximum)
with open(output_path) as f:
expected_max = int(f.readline().strip())
# Read submission output
with open(submission_path) as f:
lines = f.readlines()
if len(lines) < 2:
print(0)
return
submission_max_line = lines[0].strip()
submission_partition_line = lines[1].strip()
try:
submission_max = int(submission_max_line)
submission_partition = list(map(int, submission_partition_line.split()))
except:
print(0)
return
# Check submission_max matches expected_max
if submission_max != expected_max:
print(0)
return
# Check partition is valid
if len(submission_partition) != 2 * n:
print(0)
return
count_1 = submission_partition.count(1)
count_2 = submission_partition.count(2)
if count_1 != n or count_2 != n:
print(0)
return
# Separate heaps
heap1 = [cube for cube, heap in zip(cubes, submission_partition) if heap == 1]
heap2 = [cube for cube, heap in zip(cubes, submission_partition) if heap == 2]
# Generate all possible four-digit numbers
numbers = set()
for x in heap1:
for y in heap2:
numbers.add(x * 100 + y)
actual_count = len(numbers)
if actual_count == submission_max:
print(1)
else:
print(0)
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
|
246/B
|
246
|
B
|
PyPy 3-64
|
TESTS
| 15
| 154
| 7,680,000
|
151045036
|
n = int(input())
if n == 1:
print(1)
else:
arr = [int(x) for x in input().split()]
arr.sort()
for i in range(n-1):
if arr[i] >= 0:
arr[-1] += arr[i]
else:
arr[-1] -= arr[i]
arr[i] = 0
q,r = divmod(arr[-1], n)
# print(arr)
if r == 0:
print(n)
else:
print(n-1)
| 30
| 92
| 2,969,600
|
189736051
|
n=int(input()) ; print(n-1 if sum(list(map(int,input().split())))%n else n)
|
Codeforces Round 151 (Div. 2)
|
CF
| 2,012
| 2
| 256
|
Increase and Decrease
|
Polycarpus has an array, consisting of n integers a1, a2, ..., an. Polycarpus likes it when numbers in an array match. That's why he wants the array to have as many equal numbers as possible. For that Polycarpus performs the following operation multiple times:
- he chooses two elements of the array ai, aj (i ≠ j);
- he simultaneously increases number ai by 1 and decreases number aj by 1, that is, executes ai = ai + 1 and aj = aj - 1.
The given operation changes exactly two distinct array elements. Polycarpus can apply the described operation an infinite number of times.
Now he wants to know what maximum number of equal array elements he can get if he performs an arbitrary number of such operation. Help Polycarpus.
|
The first line contains integer n (1 ≤ n ≤ 105) — the array size. The second line contains space-separated integers a1, a2, ..., an (|ai| ≤ 104) — the original array.
|
Print a single integer — the maximum number of equal array elements he can get if he performs an arbitrary number of the given operation.
| null | null |
[{"input": "2\n2 1", "output": "1"}, {"input": "3\n1 4 1", "output": "3"}]
| 1,300
|
["greedy", "math"]
| 30
|
[{"input": "2\r\n2 1\r\n", "output": "1\r\n"}, {"input": "3\r\n1 4 1\r\n", "output": "3\r\n"}, {"input": "4\r\n2 -7 -2 -6\r\n", "output": "3\r\n"}, {"input": "4\r\n2 0 -2 -1\r\n", "output": "3\r\n"}, {"input": "6\r\n-1 1 0 0 -1 -1\r\n", "output": "5\r\n"}, {"input": "5\r\n0 0 0 0 0\r\n", "output": "5\r\n"}]
| false
|
stdio
| null | true
|
904/C
|
906
|
A
|
Python 3
|
TESTS
| 36
| 670
| 5,939,200
|
33572160
|
l=['q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m']
n=int(input())
shok=0
z=0
j=0
for i in range(n):
if len(l)==1 and z==0:
z=shok
j=0
s=input()
if s[0]=='.' or s[0]=='?':
if s[0]=='?':
shok+=1
for j in range(2,len(s)):
if s[j] in l:
l.pop(l.index(s[j]))
if s[0]=='!':
shok+=1
while j < len(l):
c=0
for k in range(2,len(s)):
if l[j]==s[k]:
c+=1
if c==0:
l.pop(j)
j-=1
j+=1
if z==0:
print(0)
else:
print(shok-1-z)
| 38
| 233
| 26,828,800
|
33568040
|
def main():
from sys import stdin, stdout
n = int(stdin.readline().strip())
alphas = list(map(lambda i: chr(i + ord('a')), range(0, 26)))
se = set(alphas)
count = 0
excess = 0
found = False
for i in range(n):
(a, b) = stdin.readline().strip().split(' ')
if a == '!':
se = se.intersection(set(b))
count += 1
if found:
excess += 1
elif a == '.':
se = se.difference(set(b))
elif a == '?':
if i != n - 1:
count += 1
se = se.difference(set(b))
if found:
excess += 1
if len(se) == 1:
found = True
stdout.write('{}\n'.format(excess))
main()
|
Технокубок 2018 - Отборочный Раунд 4
|
CF
| 2,017
| 2
| 256
|
Shockers
|
Valentin participates in a show called "Shockers". The rules are quite easy: jury selects one letter which Valentin doesn't know. He should make a small speech, but every time he pronounces a word that contains the selected letter, he receives an electric shock. He can make guesses which letter is selected, but for each incorrect guess he receives an electric shock too. The show ends when Valentin guesses the selected letter correctly.
Valentin can't keep in mind everything, so he could guess the selected letter much later than it can be uniquely determined and get excessive electric shocks. Excessive electric shocks are those which Valentin got after the moment the selected letter can be uniquely determined. You should find out the number of excessive electric shocks.
|
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of actions Valentin did.
The next n lines contain descriptions of his actions, each line contains description of one action. Each action can be of one of three types:
1. Valentin pronounced some word and didn't get an electric shock. This action is described by the string ". w" (without quotes), in which "." is a dot (ASCII-code 46), and w is the word that Valentin said.
2. Valentin pronounced some word and got an electric shock. This action is described by the string "! w" (without quotes), in which "!" is an exclamation mark (ASCII-code 33), and w is the word that Valentin said.
3. Valentin made a guess about the selected letter. This action is described by the string "? s" (without quotes), in which "?" is a question mark (ASCII-code 63), and s is the guess — a lowercase English letter.
All words consist only of lowercase English letters. The total length of all words does not exceed 105.
It is guaranteed that last action is a guess about the selected letter. Also, it is guaranteed that Valentin didn't make correct guesses about the selected letter before the last action. Moreover, it's guaranteed that if Valentin got an electric shock after pronouncing some word, then it contains the selected letter; and also if Valentin didn't get an electric shock after pronouncing some word, then it does not contain the selected letter.
|
Output a single integer — the number of electric shocks that Valentin could have avoided if he had told the selected letter just after it became uniquely determined.
| null |
In the first test case after the first action it becomes clear that the selected letter is one of the following: a, b, c. After the second action we can note that the selected letter is not a. Valentin tells word "b" and doesn't get a shock. After that it is clear that the selected letter is c, but Valentin pronounces the word cd and gets an excessive electric shock.
In the second test case after the first two electric shocks we understand that the selected letter is e or o. Valentin tries some words consisting of these letters and after the second word it's clear that the selected letter is e, but Valentin makes 3 more actions before he makes a correct hypothesis.
In the third example the selected letter can be uniquely determined only when Valentin guesses it, so he didn't get excessive electric shocks.
|
[{"input": "5\n! abc\n. ad\n. b\n! cd\n? c", "output": "1"}, {"input": "8\n! hello\n! codeforces\n? c\n. o\n? d\n? h\n. l\n? e", "output": "2"}, {"input": "7\n! ababahalamaha\n? a\n? b\n? a\n? b\n? a\n? h", "output": "0"}]
| 1,600
|
["strings"]
| 38
|
[{"input": "5\r\n! abc\r\n. ad\r\n. b\r\n! cd\r\n? c\r\n", "output": "1\r\n"}, {"input": "8\r\n! hello\r\n! codeforces\r\n? c\r\n. o\r\n? d\r\n? h\r\n. l\r\n? e\r\n", "output": "2\r\n"}, {"input": "7\r\n! ababahalamaha\r\n? a\r\n? b\r\n? a\r\n? b\r\n? a\r\n? h\r\n", "output": "0\r\n"}, {"input": "4\r\n! abcd\r\n! cdef\r\n? d\r\n? c\r\n", "output": "0\r\n"}, {"input": "1\r\n? q\r\n", "output": "0\r\n"}, {"input": "15\r\n. r\r\n? e\r\n. s\r\n. rw\r\n? y\r\n. fj\r\n. zftyd\r\n? r\r\n! wq\r\n? w\r\n? p\r\n. ours\r\n. dto\r\n. lbyfru\r\n? q\r\n", "output": "2\r\n"}, {"input": "3\r\n. abcdefghijklmnopqrstuvwxy\r\n? a\r\n? z\r\n", "output": "1\r\n"}, {"input": "3\r\n. abcdefghijklmnopqrstuvwxy\r\n! z\r\n? z\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
904/C
|
906
|
A
|
PyPy 3
|
TESTS
| 7
| 93
| 22,016,000
|
126446311
|
n = int(input())
ans = 0
wshot = False
a = set()
for i in range(n):
b = list(input().split())
if i == n - 1:
break
if b[0] == '!':
if wshot == False:
s = set(b[1])
s.difference_update(a)
wshot = True
if len(s) <= 1:
ans += 1
else:
if len(s) <= 1:
ans += 1
s.intersection_update(set(b[1]))
elif b[0] == '.':
if not wshot:
a.add(b[1])
else:
s.difference_update(set(b[1]))
else:
if not wshot:
a.add(b[1])
else:
if len(s) <= 1:
ans += 1
else:
s.difference_update(set(b[1]))
print(ans)
| 38
| 249
| 409,600
|
109740860
|
s,a=set('abcdefghijklmnopqrstuvwxyz'),0
for _ in range(int(input())-1):
c,w=input().split()
if len(s)==1 and c!='.':a+=1
w=set(w)
if c=='!':s&=w
else:s-=w
print(a)
|
Технокубок 2018 - Отборочный Раунд 4
|
CF
| 2,017
| 2
| 256
|
Shockers
|
Valentin participates in a show called "Shockers". The rules are quite easy: jury selects one letter which Valentin doesn't know. He should make a small speech, but every time he pronounces a word that contains the selected letter, he receives an electric shock. He can make guesses which letter is selected, but for each incorrect guess he receives an electric shock too. The show ends when Valentin guesses the selected letter correctly.
Valentin can't keep in mind everything, so he could guess the selected letter much later than it can be uniquely determined and get excessive electric shocks. Excessive electric shocks are those which Valentin got after the moment the selected letter can be uniquely determined. You should find out the number of excessive electric shocks.
|
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of actions Valentin did.
The next n lines contain descriptions of his actions, each line contains description of one action. Each action can be of one of three types:
1. Valentin pronounced some word and didn't get an electric shock. This action is described by the string ". w" (without quotes), in which "." is a dot (ASCII-code 46), and w is the word that Valentin said.
2. Valentin pronounced some word and got an electric shock. This action is described by the string "! w" (without quotes), in which "!" is an exclamation mark (ASCII-code 33), and w is the word that Valentin said.
3. Valentin made a guess about the selected letter. This action is described by the string "? s" (without quotes), in which "?" is a question mark (ASCII-code 63), and s is the guess — a lowercase English letter.
All words consist only of lowercase English letters. The total length of all words does not exceed 105.
It is guaranteed that last action is a guess about the selected letter. Also, it is guaranteed that Valentin didn't make correct guesses about the selected letter before the last action. Moreover, it's guaranteed that if Valentin got an electric shock after pronouncing some word, then it contains the selected letter; and also if Valentin didn't get an electric shock after pronouncing some word, then it does not contain the selected letter.
|
Output a single integer — the number of electric shocks that Valentin could have avoided if he had told the selected letter just after it became uniquely determined.
| null |
In the first test case after the first action it becomes clear that the selected letter is one of the following: a, b, c. After the second action we can note that the selected letter is not a. Valentin tells word "b" and doesn't get a shock. After that it is clear that the selected letter is c, but Valentin pronounces the word cd and gets an excessive electric shock.
In the second test case after the first two electric shocks we understand that the selected letter is e or o. Valentin tries some words consisting of these letters and after the second word it's clear that the selected letter is e, but Valentin makes 3 more actions before he makes a correct hypothesis.
In the third example the selected letter can be uniquely determined only when Valentin guesses it, so he didn't get excessive electric shocks.
|
[{"input": "5\n! abc\n. ad\n. b\n! cd\n? c", "output": "1"}, {"input": "8\n! hello\n! codeforces\n? c\n. o\n? d\n? h\n. l\n? e", "output": "2"}, {"input": "7\n! ababahalamaha\n? a\n? b\n? a\n? b\n? a\n? h", "output": "0"}]
| 1,600
|
["strings"]
| 38
|
[{"input": "5\r\n! abc\r\n. ad\r\n. b\r\n! cd\r\n? c\r\n", "output": "1\r\n"}, {"input": "8\r\n! hello\r\n! codeforces\r\n? c\r\n. o\r\n? d\r\n? h\r\n. l\r\n? e\r\n", "output": "2\r\n"}, {"input": "7\r\n! ababahalamaha\r\n? a\r\n? b\r\n? a\r\n? b\r\n? a\r\n? h\r\n", "output": "0\r\n"}, {"input": "4\r\n! abcd\r\n! cdef\r\n? d\r\n? c\r\n", "output": "0\r\n"}, {"input": "1\r\n? q\r\n", "output": "0\r\n"}, {"input": "15\r\n. r\r\n? e\r\n. s\r\n. rw\r\n? y\r\n. fj\r\n. zftyd\r\n? r\r\n! wq\r\n? w\r\n? p\r\n. ours\r\n. dto\r\n. lbyfru\r\n? q\r\n", "output": "2\r\n"}, {"input": "3\r\n. abcdefghijklmnopqrstuvwxy\r\n? a\r\n? z\r\n", "output": "1\r\n"}, {"input": "3\r\n. abcdefghijklmnopqrstuvwxy\r\n! z\r\n? z\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
353/B
|
353
|
B
|
Python 3
|
TESTS
| 10
| 92
| 204,800
|
5178022
|
n, t = 2 * int(input()), map(int, input().split())
d = [[] for i in range(100)]
for i, j in enumerate(t):
d[j].append(i)
y, x = [], False
p, q = [], ['1'] * n
for i in d[10: 100]:
if i:
if len(i) == 1:
if x: y.append(i[0])
x = not x
else:
y.append(i[0])
p += i[2: ]
k, l = len(p), len(y)
print(l * (n - k - l))
for i in y:
q[i] = '2'
for i in (p[: k // 2] if x else p[k // 2: ]):
q[i] = '2'
print(' '.join(q))
| 40
| 248
| 23,040,000
|
20167729
|
def s():
input()
l = [[]for _ in range(100)]
a = list(map(int,input().split()))
for i,v in enumerate(a):
l[v].append(i)
c = 0
cc = 0
fs = [0,0]
for i in l:
if len(i) == 0:
continue
if len(i) == 1:
a[i[0]] = c+1
fs[c]+=1
c = 1 - c
continue
fs[c] += 1
fs[c-1] += 1
for e in i[:len(i)//2]:
a[e] = 1 + cc
for e in i[len(i)//2:]:
a[e] = 2 - cc
if len(i) % 2 == 1:
cc = 1 - cc
print(fs[0]*fs[1])
print(*a)
s()
|
Codeforces Round 205 (Div. 2)
|
CF
| 2,013
| 1
| 256
|
Two Heaps
|
Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
|
The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.
|
In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
| null |
In the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
|
[{"input": "1\n10 99", "output": "1\n2 1"}, {"input": "2\n13 24 13 45", "output": "4\n1 2 2 1"}]
| 1,900
|
["combinatorics", "constructive algorithms", "greedy", "implementation", "math", "sortings"]
| 40
|
[{"input": "1\r\n10 99\r\n", "output": "1\r\n2 1 \r\n"}, {"input": "2\r\n13 24 13 45\r\n", "output": "4\r\n1 2 2 1 \r\n"}, {"input": "5\r\n21 60 18 21 17 39 58 74 62 34\r\n", "output": "25\r\n1 1 1 2 2 1 2 1 2 2 \r\n"}, {"input": "10\r\n26 43 29 92 22 27 95 56 72 55 93 51 91 30 70 77 32 69 87 98\r\n", "output": "100\r\n1 2 1 2 2 2 2 1 2 2 1 1 1 2 1 1 1 2 2 1 \r\n"}, {"input": "20\r\n80 56 58 61 75 60 25 49 59 15 43 39 21 73 67 13 75 31 18 87 32 44 53 15 53 76 79 94 85 80 27 25 48 78 32 18 20 78 46 37\r\n", "output": "400\r\n1 2 1 2 1 1 1 1 2 1 1 2 2 2 1 2 2 2 1 2 1 2 1 2 2 1 2 1 1 2 1 2 2 1 2 2 1 2 1 1 \r\n"}, {"input": "50\r\n49 13 81 20 73 62 19 49 65 95 32 84 24 96 51 57 53 83 40 44 26 65 78 80 92 87 87 95 56 46 22 44 69 80 41 61 97 92 58 53 42 78 53 19 47 36 25 77 65 81 14 61 38 99 27 58 67 37 67 80 77 51 32 43 31 48 19 79 31 91 46 97 91 71 27 63 22 84 73 73 89 44 34 84 70 23 45 31 56 73 83 38 68 45 99 33 83 86 87 80\r\n", "output": "1936\r\n1 2 1 2 1 2 1 2 1 1 1 1 2 1 1 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 2 1 2 2 1 1 2 1 2 1 2 1 2 1 1 1 1 1 2 1 2 1 1 1 2 1 2 2 1 2 2 2 2 1 2 1 2 2 1 2 2 2 1 2 1 2 2 2 1 2 1 2 2 2 1 1 2 2 2 2 2 2 2 2 1 2 1 2 2 \r\n"}, {"input": "2\r\n10 10 10 11\r\n", "output": "2\r\n1 2 1 2 \r\n"}]
| false
|
stdio
|
import sys
def main(input_path, output_path, submission_path):
# Read input
with open(input_path) as f:
n = int(f.readline())
cubes = list(map(int, f.readline().split()))
# Read reference output (to get the expected maximum)
with open(output_path) as f:
expected_max = int(f.readline().strip())
# Read submission output
with open(submission_path) as f:
lines = f.readlines()
if len(lines) < 2:
print(0)
return
submission_max_line = lines[0].strip()
submission_partition_line = lines[1].strip()
try:
submission_max = int(submission_max_line)
submission_partition = list(map(int, submission_partition_line.split()))
except:
print(0)
return
# Check submission_max matches expected_max
if submission_max != expected_max:
print(0)
return
# Check partition is valid
if len(submission_partition) != 2 * n:
print(0)
return
count_1 = submission_partition.count(1)
count_2 = submission_partition.count(2)
if count_1 != n or count_2 != n:
print(0)
return
# Separate heaps
heap1 = [cube for cube, heap in zip(cubes, submission_partition) if heap == 1]
heap2 = [cube for cube, heap in zip(cubes, submission_partition) if heap == 2]
# Generate all possible four-digit numbers
numbers = set()
for x in heap1:
for y in heap2:
numbers.add(x * 100 + y)
actual_count = len(numbers)
if actual_count == submission_max:
print(1)
else:
print(0)
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
|
904/C
|
906
|
A
|
Python 3
|
TESTS
| 7
| 61
| 5,939,200
|
33650047
|
n = int(input().strip())
shocks = 0
limit = 0
letters = set([chr(x) for x in range(65, 123)])
while n:
action, val = input().strip().split(' ')
if action == '.':
letters = letters.difference(val)
elif action == '!':
letters = letters.intersection(val)
shocks += 1
elif action == '?':
letters = letters.difference(val)
shocks += 1 if n > 1 else 0
n -= 1
if limit == 0 and len(letters) == 1:
limit = shocks
print(shocks - limit if limit else 0)
| 38
| 249
| 5,939,200
|
33563803
|
n = int(input().strip())
#print(n)
alpha = set(())
for i in range(ord("a"), ord("z")+1):
alpha.add(chr(i))
#print(alpha)
def noshock(s):
global alpha
letters = set(s)
alpha = alpha - letters
def shock(s):
global alpha
letters = set(s)
alpha = alpha&letters
def ask(c):
global alpha
if c in alpha:
alpha.remove(c)
ind = 0
for i in range(n):
temp = input().split(" ")
# print(temp)
if temp[0] == ".":
noshock(temp[1])
elif temp[0] == "!":
shock(temp[1])
elif temp[0] == "?":
ask(temp[1])
# print(alpha)
if len(alpha) == 1:
ind = i+1
break
#print(ind)
tot = 0
if ind == 0:
tot = 0
else:
for i in range(ind+1, n):
temp = input().split(" ")
if temp[0] == "!":
tot += 1
elif temp[0] == "?":
if temp[1] not in alpha:
tot += 1
print(tot)
|
Технокубок 2018 - Отборочный Раунд 4
|
CF
| 2,017
| 2
| 256
|
Shockers
|
Valentin participates in a show called "Shockers". The rules are quite easy: jury selects one letter which Valentin doesn't know. He should make a small speech, but every time he pronounces a word that contains the selected letter, he receives an electric shock. He can make guesses which letter is selected, but for each incorrect guess he receives an electric shock too. The show ends when Valentin guesses the selected letter correctly.
Valentin can't keep in mind everything, so he could guess the selected letter much later than it can be uniquely determined and get excessive electric shocks. Excessive electric shocks are those which Valentin got after the moment the selected letter can be uniquely determined. You should find out the number of excessive electric shocks.
|
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of actions Valentin did.
The next n lines contain descriptions of his actions, each line contains description of one action. Each action can be of one of three types:
1. Valentin pronounced some word and didn't get an electric shock. This action is described by the string ". w" (without quotes), in which "." is a dot (ASCII-code 46), and w is the word that Valentin said.
2. Valentin pronounced some word and got an electric shock. This action is described by the string "! w" (without quotes), in which "!" is an exclamation mark (ASCII-code 33), and w is the word that Valentin said.
3. Valentin made a guess about the selected letter. This action is described by the string "? s" (without quotes), in which "?" is a question mark (ASCII-code 63), and s is the guess — a lowercase English letter.
All words consist only of lowercase English letters. The total length of all words does not exceed 105.
It is guaranteed that last action is a guess about the selected letter. Also, it is guaranteed that Valentin didn't make correct guesses about the selected letter before the last action. Moreover, it's guaranteed that if Valentin got an electric shock after pronouncing some word, then it contains the selected letter; and also if Valentin didn't get an electric shock after pronouncing some word, then it does not contain the selected letter.
|
Output a single integer — the number of electric shocks that Valentin could have avoided if he had told the selected letter just after it became uniquely determined.
| null |
In the first test case after the first action it becomes clear that the selected letter is one of the following: a, b, c. After the second action we can note that the selected letter is not a. Valentin tells word "b" and doesn't get a shock. After that it is clear that the selected letter is c, but Valentin pronounces the word cd and gets an excessive electric shock.
In the second test case after the first two electric shocks we understand that the selected letter is e or o. Valentin tries some words consisting of these letters and after the second word it's clear that the selected letter is e, but Valentin makes 3 more actions before he makes a correct hypothesis.
In the third example the selected letter can be uniquely determined only when Valentin guesses it, so he didn't get excessive electric shocks.
|
[{"input": "5\n! abc\n. ad\n. b\n! cd\n? c", "output": "1"}, {"input": "8\n! hello\n! codeforces\n? c\n. o\n? d\n? h\n. l\n? e", "output": "2"}, {"input": "7\n! ababahalamaha\n? a\n? b\n? a\n? b\n? a\n? h", "output": "0"}]
| 1,600
|
["strings"]
| 38
|
[{"input": "5\r\n! abc\r\n. ad\r\n. b\r\n! cd\r\n? c\r\n", "output": "1\r\n"}, {"input": "8\r\n! hello\r\n! codeforces\r\n? c\r\n. o\r\n? d\r\n? h\r\n. l\r\n? e\r\n", "output": "2\r\n"}, {"input": "7\r\n! ababahalamaha\r\n? a\r\n? b\r\n? a\r\n? b\r\n? a\r\n? h\r\n", "output": "0\r\n"}, {"input": "4\r\n! abcd\r\n! cdef\r\n? d\r\n? c\r\n", "output": "0\r\n"}, {"input": "1\r\n? q\r\n", "output": "0\r\n"}, {"input": "15\r\n. r\r\n? e\r\n. s\r\n. rw\r\n? y\r\n. fj\r\n. zftyd\r\n? r\r\n! wq\r\n? w\r\n? p\r\n. ours\r\n. dto\r\n. lbyfru\r\n? q\r\n", "output": "2\r\n"}, {"input": "3\r\n. abcdefghijklmnopqrstuvwxy\r\n? a\r\n? z\r\n", "output": "1\r\n"}, {"input": "3\r\n. abcdefghijklmnopqrstuvwxy\r\n! z\r\n? z\r\n", "output": "1\r\n"}]
| false
|
stdio
| null | true
|
172/C
|
172
|
C
|
Python 3
|
TESTS
| 3
| 92
| 0
|
5356158
|
from collections import defaultdict
n, m = map(int, input().split())
d = j = 0
r = [0] * n
p = defaultdict(list)
for i in range(n):
t, x = map(int, input().split())
p[x].append(i)
j += 1
if j == m:
d = max(t, d)
y = sorted(p.keys())
for x in y:
d += x
for j in p[x]: r[j] = d
d += 1 + len(p[x]) // 2
d += y[-1]
p.clear()
j = 0
if p:
d = max(t, d)
y = sorted(p.keys())
for x in y:
d += x
for j in p[x]: r[j] = d
d += 1 + len(p[x]) // 2
print(' '.join(map(str, r)))
| 66
| 546
| 8,294,400
|
154860930
|
n, m = map(int, input().split())
w, T = [0]*n, 0
for i in range(0, n, m):
l = dict()
mt = 0
for s in range(i, min(n, m+i)):
t, x = map(int, input().split())
l.setdefault(x, []).append(s)
mt = max(mt, t)
T = max(T, mt)
p = 0
for x in sorted(l):
T += (x-p)
p = x
for s in l[x]:
w[s] = T
T += 1+len(l[x])//2
T += p
print(' '.join(map(str, w)))
|
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
|
246/B
|
246
|
B
|
PyPy 3
|
TESTS
| 12
| 216
| 5,120,000
|
108964044
|
n = int(input())
a = list(map(int, input().split()))
ans = 1
a.sort()
if n%2 == 0:
ans = n-1
else:
ans = n
print(ans)
| 30
| 92
| 3,072,000
|
176975299
|
n = int(input())
a = [int(i) for i in input().split()]
if sum(a) % n == 0:
print(n)
else:
print(n - 1)
|
Codeforces Round 151 (Div. 2)
|
CF
| 2,012
| 2
| 256
|
Increase and Decrease
|
Polycarpus has an array, consisting of n integers a1, a2, ..., an. Polycarpus likes it when numbers in an array match. That's why he wants the array to have as many equal numbers as possible. For that Polycarpus performs the following operation multiple times:
- he chooses two elements of the array ai, aj (i ≠ j);
- he simultaneously increases number ai by 1 and decreases number aj by 1, that is, executes ai = ai + 1 and aj = aj - 1.
The given operation changes exactly two distinct array elements. Polycarpus can apply the described operation an infinite number of times.
Now he wants to know what maximum number of equal array elements he can get if he performs an arbitrary number of such operation. Help Polycarpus.
|
The first line contains integer n (1 ≤ n ≤ 105) — the array size. The second line contains space-separated integers a1, a2, ..., an (|ai| ≤ 104) — the original array.
|
Print a single integer — the maximum number of equal array elements he can get if he performs an arbitrary number of the given operation.
| null | null |
[{"input": "2\n2 1", "output": "1"}, {"input": "3\n1 4 1", "output": "3"}]
| 1,300
|
["greedy", "math"]
| 30
|
[{"input": "2\r\n2 1\r\n", "output": "1\r\n"}, {"input": "3\r\n1 4 1\r\n", "output": "3\r\n"}, {"input": "4\r\n2 -7 -2 -6\r\n", "output": "3\r\n"}, {"input": "4\r\n2 0 -2 -1\r\n", "output": "3\r\n"}, {"input": "6\r\n-1 1 0 0 -1 -1\r\n", "output": "5\r\n"}, {"input": "5\r\n0 0 0 0 0\r\n", "output": "5\r\n"}]
| false
|
stdio
| null | true
|
246/B
|
246
|
B
|
PyPy 3
|
TESTS
| 15
| 248
| 25,292,800
|
125184248
|
def main():
n = int(input())
arr = list(map(int, input().split()))
for i in range(n - 1):
if arr[i] < 0:
arr[n - 1] -= arr[i]
arr[i] = 0
elif arr[i] > 0:
arr[n - 1] += arr[i]
arr[i] = 0
else:
pass
if arr[n - 1] % n == 0:
print(n)
else:
print(n - 1)
main()
| 30
| 92
| 3,174,400
|
146273371
|
n = int(input())
numbers = input().split()
lst = [int(x) for x in numbers]
print(n if sum(lst) % n == 0 else n-1)
|
Codeforces Round 151 (Div. 2)
|
CF
| 2,012
| 2
| 256
|
Increase and Decrease
|
Polycarpus has an array, consisting of n integers a1, a2, ..., an. Polycarpus likes it when numbers in an array match. That's why he wants the array to have as many equal numbers as possible. For that Polycarpus performs the following operation multiple times:
- he chooses two elements of the array ai, aj (i ≠ j);
- he simultaneously increases number ai by 1 and decreases number aj by 1, that is, executes ai = ai + 1 and aj = aj - 1.
The given operation changes exactly two distinct array elements. Polycarpus can apply the described operation an infinite number of times.
Now he wants to know what maximum number of equal array elements he can get if he performs an arbitrary number of such operation. Help Polycarpus.
|
The first line contains integer n (1 ≤ n ≤ 105) — the array size. The second line contains space-separated integers a1, a2, ..., an (|ai| ≤ 104) — the original array.
|
Print a single integer — the maximum number of equal array elements he can get if he performs an arbitrary number of the given operation.
| null | null |
[{"input": "2\n2 1", "output": "1"}, {"input": "3\n1 4 1", "output": "3"}]
| 1,300
|
["greedy", "math"]
| 30
|
[{"input": "2\r\n2 1\r\n", "output": "1\r\n"}, {"input": "3\r\n1 4 1\r\n", "output": "3\r\n"}, {"input": "4\r\n2 -7 -2 -6\r\n", "output": "3\r\n"}, {"input": "4\r\n2 0 -2 -1\r\n", "output": "3\r\n"}, {"input": "6\r\n-1 1 0 0 -1 -1\r\n", "output": "5\r\n"}, {"input": "5\r\n0 0 0 0 0\r\n", "output": "5\r\n"}]
| false
|
stdio
| null | true
|
246/B
|
246
|
B
|
PyPy 3
|
TESTS
| 15
| 216
| 5,734,400
|
108964064
|
n = int(input())
a = list(map(int, input().split()))
ans = 1
if n%2 == 0:
if len(set(a)) != 1:
ans = n-1
else:
ans = n
else:
ans = n
print(ans)
| 30
| 92
| 3,174,400
|
158395722
|
i=int(input())
l=list(map(int,input().split()))
if sum(l)%i==0:
print(i)
else:
print(i-1)
|
Codeforces Round 151 (Div. 2)
|
CF
| 2,012
| 2
| 256
|
Increase and Decrease
|
Polycarpus has an array, consisting of n integers a1, a2, ..., an. Polycarpus likes it when numbers in an array match. That's why he wants the array to have as many equal numbers as possible. For that Polycarpus performs the following operation multiple times:
- he chooses two elements of the array ai, aj (i ≠ j);
- he simultaneously increases number ai by 1 and decreases number aj by 1, that is, executes ai = ai + 1 and aj = aj - 1.
The given operation changes exactly two distinct array elements. Polycarpus can apply the described operation an infinite number of times.
Now he wants to know what maximum number of equal array elements he can get if he performs an arbitrary number of such operation. Help Polycarpus.
|
The first line contains integer n (1 ≤ n ≤ 105) — the array size. The second line contains space-separated integers a1, a2, ..., an (|ai| ≤ 104) — the original array.
|
Print a single integer — the maximum number of equal array elements he can get if he performs an arbitrary number of the given operation.
| null | null |
[{"input": "2\n2 1", "output": "1"}, {"input": "3\n1 4 1", "output": "3"}]
| 1,300
|
["greedy", "math"]
| 30
|
[{"input": "2\r\n2 1\r\n", "output": "1\r\n"}, {"input": "3\r\n1 4 1\r\n", "output": "3\r\n"}, {"input": "4\r\n2 -7 -2 -6\r\n", "output": "3\r\n"}, {"input": "4\r\n2 0 -2 -1\r\n", "output": "3\r\n"}, {"input": "6\r\n-1 1 0 0 -1 -1\r\n", "output": "5\r\n"}, {"input": "5\r\n0 0 0 0 0\r\n", "output": "5\r\n"}]
| false
|
stdio
| null | true
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.