description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
vowels = "aeoiu" d = {} d2 = {} n = int(input()) pairs1 = [] pairs2 = [] for i in range(n): w = input() cnt = 0 lst = "-" for c in w: if c in vowels: lst = c cnt += 1 key = str(cnt) + lst if key in d: pairs2.append((w, d[key])) del d[key] else: d[key] = w for k in d: k2 = k[:-1] if k2 in d2: pairs1.append((d[k], d2[k2])) del d2[k2] else: d2[k2] = d[k] l2 = len(pairs2) r1 = min(len(pairs1), l2) r2 = (l2 - r1) // 2 r2 = r2 if r2 >= 0 else 0 print(r1 + r2) for i in range(r1): print(pairs1[i][0], pairs2[i][0]) print(pairs1[i][1], pairs2[i][1]) j = r1 while j + 1 < l2: print(pairs2[j][0], pairs2[j + 1][0]) print(pairs2[j][1], pairs2[j + 1][1]) j += 2
ASSIGN VAR STRING ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
n = int(input()) words = {} for i in range(n): wrd = input() count = 0 latest = None for letter in wrd: if ( letter == "a" or letter == "e" or letter == "i" or letter == "o" or letter == "u" ): count += 1 latest = letter if count not in words: words[count] = {} if latest not in words[count]: words[count][latest] = [] words[count][latest].append(wrd) first_pairs = [] second_paird = [] for length, letter_dct in words.items(): nosecond_curlength = [] for words_array in letter_dct.values(): cnt = len(words_array) for i in range(cnt // 2): second_paird.append([words_array.pop(), words_array.pop()]) if cnt % 2 == 1: nosecond_curlength.append(words_array.pop()) cnt = len(nosecond_curlength) for i in range(cnt // 2): first_pairs.append([nosecond_curlength.pop(), nosecond_curlength.pop()]) secondcnt = len(second_paird) firstcnt = len(first_pairs) if secondcnt < firstcnt: print(secondcnt) else: print(firstcnt + (secondcnt - firstcnt) // 2) while 1: if first_pairs: fpair = first_pairs.pop() elif second_paird: fpair = second_paird.pop() else: break if second_paird: spair = second_paird.pop() else: break print(fpair[0], spair[0]) print(fpair[1], spair[1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR VAR IF VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR DICT IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR IF VAR ASSIGN VAR FUNC_CALL VAR IF VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
def countvowels(s): count = 0 for i in s: if i in vowels: count += 1 last = i return count, last n = int(input()) a = [] count = {} vowels = {"a", "e", "i", "o", "u"} last = {} for i in range(n): s = input() d, l = countvowels(s) if d not in count: count[d] = [s] else: count[d].append(s) last[s] = l lword = [] fword = [] for i in count: d = {} for j in count[i]: if last[j] not in d: d[last[j]] = j else: lword.append((j, d[last[j]])) del d[last[j]] if len(d) > 1: k = [] for j in d: k.append(d[j]) for j in range(0, len(k) - 1, 2): fword.append((k[j], k[j + 1])) if len(fword) <= len(lword): ans = len(fword) + (len(lword) - len(fword)) // 2 print(ans) for i in range(len(fword)): print(fword[i][0], lword[i][0]) print(fword[i][1], lword[i][1]) for i in range(len(fword), len(lword) - 1, 2): print(lword[i][0], lword[i + 1][0]) print(lword[i][1], lword[i + 1][1]) else: ans = len(lword) print(ans) for i in range(ans): print(fword[i][0], lword[i][0]) print(fword[i][1], lword[i][1])
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR STRING STRING STRING STRING STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR DICT FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
n = int(input()) ns = [] d = {} def nm(s): ans = 0 lst = None for c in s: if c == "a" or c == "e" or c == "i" or c == "o" or c == "u": lst = c ans += 1 return ans, lst for i in range(n): s = input() x = nm(s) if x not in d: d[x] = [] d[x].append(s) ans2 = [] ans1 = [] dn = {} for x in d.keys(): ans2 += list(d[x]) num, lst = x if len(ans2) % 2 == 1: y = ans2.pop() if num not in dn: dn[num] = [] dn[num].append(y) for num in dn.keys(): ans1 += list(dn[num]) if len(ans1) % 2 == 1: ans1.pop() if len(ans1) > len(ans2): print(len(ans2) // 2) for i in range(len(ans2)): print(ans1[i], ans2[i]) else: ans = [] l1 = len(ans1) l2 = len(ans2) for i in range(l1): ans.append(ans1[i] + " " + ans2[i]) i = l1 while i + 3 < l2: ans.append(ans2[i] + " " + ans2[i + 2]) ans.append(ans2[i + 1] + " " + ans2[i + 3]) i += 4 print(len(ans) // 2) for c in ans: print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR VAR IF VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR STRING VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
import sys n = int(input()) setA = {"a", "e", "i", "o", "u"} a = [] mem = [] dic = {} dic2 = {} count = {} for i in range(n): a.append(sys.stdin.readline()) for word in a: word = word[:-1] c = 0 last = "" for v in word: if v in setA: c += 1 last = v m = str(c) mv = last + m if m not in dic: dic[m] = [1, [word]] else: dic[m][0] += 1 dic[m][1].append(word) if mv not in dic2: dic2[mv] = [1, [word]] else: dic2[mv][0] += 1 dic2[mv][1].append(word) if word not in count: count[word] = 1 else: count[word] += 1 same = 0 spare = 0 mem = set() col2 = [] col1 = [] for k, v in dic2.items(): minus = v[0] % 2 same += v[0] - minus for v1 in v[1][: v[0] - minus]: col2.append(v1) count[v1] -= 1 for k, v in dic.items(): minus = v[0] % 2 spare += v[0] - minus temp = [] for v1 in v[1][: v[0] - minus]: if count.get(v1): col1.append(v1) count[v1] -= 1 if len(col1) % 2 == 1: col1.pop(-1) leftElement = n - same lastChar = same first = len(col1) ans = 0 if lastChar > first: left = (lastChar - first) // 2 if left % 2 == 1: left -= 1 col1 = col1 + col2[:left] col2 = col2[left:] ans = first + left else: ans = lastChar if len(col2) % 2 == 1: col2.pop(0) if len(col1) % 2 == 1: col1.pop(-1) print(ans // 2) for i in range(ans // 2 * 2): print(col1[i], col2[i])
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR LIST NUMBER LIST VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR LIST NUMBER LIST VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST FOR VAR VAR NUMBER BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
n = int(input()) words = [input() for i in range(n)] d = {} for w in words: vs = "".join([(c if c in "aeiou" else "") for c in w]) v = len(vs) last = vs[-1] if not v in d: d[v] = {} dd = d[v] if not last in dd: dd[last] = [] dd[last].append(w) pairs = [] e = {} for v, dd in list(d.items()): for c, l in list(dd.items()): while len(l) > 1: p = l.pop(), l.pop() pairs.append(p) if len(l) > 0: if v in e: e[v] += l else: e[v] = l ans = [] for c, l in list(e.items()): while len(l) > 1 and len(pairs) > 0: a = l.pop(), l.pop() for i, j in zip(a, pairs.pop()): ans.append((i, j)) while len(pairs) > 1: for i, j in zip(pairs.pop(), pairs.pop()): ans.append((i, j)) print(len(ans) // 2) for i, j in ans: print(i, j)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR STRING VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR DICT ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
import sys sys.setrecursionlimit(10**9) input = sys.stdin.readline N = int(input()) words = [input()[:-1] for _ in range(N)] vowels = "a", "i", "u", "e", "o" counted_word = [] for word in words: last_vowel = "" num_vowel = 0 for c in word: if c in vowels: num_vowel += 1 last_vowel = c counted_word.append([word, num_vowel, last_vowel]) counted_word = sorted(counted_word, key=lambda x: (x[1], x[2])) cand_second = [] unused = [] used = [False] * N for i, w1, w2 in zip(list(range(N)), counted_word, counted_word[1:]): if used[i]: continue if w1[1] == w2[1] and w1[2] == w2[2]: cand_second.append([w1, w2]) used[i] = True used[i + 1] = True else: unused.append(w1) if not used[-1]: unused.append(counted_word[-1]) cand_first = [] used = [0] * len(unused) for i, w1, w2 in zip(list(range(len(unused))), unused, unused[1:]): if used[i]: continue if w1[1] == w2[1]: cand_first.append([w1, w2]) used[i] = True used[i + 1] = True len_first = len(cand_first) len_second = len(cand_second) diff = len_second - len_first if diff > 1: for i in range(diff // 2): cand_first.append(cand_second[-i - 1]) ans = min(len_second, len(cand_first)) print(ans) for first, second in zip(cand_first, cand_second): print(first[0][0], second[0][0]) print(first[1][0], second[1][0])
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR STRING STRING STRING STRING STRING ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
vowel = ["a", "e", "i", "o", "u"] rare = [] common = [] res = [] stor = {} def ins(w): cnum, lc = 0, "" for c in w: for v in vowel: if c == v: cnum += 1 lc = c break stor.setdefault(cnum, {}) stor[cnum].setdefault(lc, []) stor[cnum][lc].append(w) def srt(): for N in stor: cstr = rstr = "" for C in stor[N]: for st in stor[N][C]: if rstr == "": rstr = st else: rare.append((st, rstr)) rstr = "" if cstr != "" and rstr != "": common.append((cstr, rstr)) cstr = rstr = "" if rstr != "": cstr = rstr rstr = "" def mkstn(): while len(common): if not len(rare): break cp = common.pop() rp = rare.pop() res.append(cp[0] + " " + rp[0] + "\n" + cp[1] + " " + rp[1]) while len(rare): if len(rare) >= 2: rp1, rp2 = rare.pop(), rare.pop() res.append(rp1[0] + " " + rp2[0] + "\n" + rp1[1] + " " + rp2[1]) else: break T = int(input()) for t in range(T): ins(input()) srt() mkstn() print(len(res)) for S in res: print(S)
ASSIGN VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT FUNC_DEF ASSIGN VAR VAR NUMBER STRING FOR VAR VAR FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR DICT EXPR FUNC_CALL VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF FOR VAR VAR ASSIGN VAR VAR STRING FOR VAR VAR VAR FOR VAR VAR VAR VAR IF VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING IF VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR STRING IF VAR STRING ASSIGN VAR VAR ASSIGN VAR STRING FUNC_DEF WHILE FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER WHILE FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
n = int(input()) a = [None] * n d = dict() vov = list("aeiou") for i in range(n): a[i] = input() vc = 0 lv = "a" for j in a[i]: if j in vov: vc += 1 lv = j try: d[vc][lv].append(a[i]) except: d[vc] = {"a": [], "e": [], "i": [], "o": [], "u": []} d[vc][lv].append(a[i]) ans = [[(-1) for i in range(4)] for j in range(n)] si = 0 fi = 0 for i in d.values(): if fi != int(fi): fi -= 0.5 for j in i.values(): for k in range(len(j) // 2): ans[si][1] = j[2 * k] ans[si][3] = j[2 * k + 1] si += 1 for k in range(len(j) // 2 * 2, len(j)): if int(fi) == fi: ans[int(fi)][0] = j[k] else: ans[int(fi)][2] = j[k] fi += 0.5 fa = [] icans = [] for i in range(len(ans)): if ans[i].count(-1) == 0: fa.append(ans[i][:]) elif ans[i][1] != -1 and ans[i][3] != -1: icans.append([ans[i][1], ans[i][3]]) print(len(fa) + len(icans) // 2) for i in range(len(fa)): print(fa[i][0], fa[i][1]) print(fa[i][2], fa[i][3]) for i in range(len(icans) // 2): print(icans[i * 2][0], icans[i * 2 + 1][0]) print(icans[i * 2][1], icans[i * 2 + 1][1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR DICT STRING STRING STRING STRING STRING LIST LIST LIST LIST LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
n = int(input()) vowels = {"a", "e", "o", "i", "u"} words = [] for i in range(n): words.append(input()) all_pairs = {} sec_pairs = {} for item in words: cnt, last = 0, -1 for i in item: if i in vowels: cnt += 1 last = i if cnt not in all_pairs: all_pairs[cnt] = {item: 1} elif item in all_pairs[cnt]: all_pairs[cnt][item] += 1 else: all_pairs[cnt][item] = 1 if cnt not in sec_pairs: sec_pairs[cnt] = {last: {item: 1}} elif last not in sec_pairs[cnt]: sec_pairs[cnt][last] = {item: 1} elif item not in sec_pairs[cnt][last]: sec_pairs[cnt][last][item] = 1 else: sec_pairs[cnt][last][item] += 1 s_p = 0 s_list = [] for cnt in list(sec_pairs.keys()): for last in list(sec_pairs[cnt].keys()): s = 0 cnt_l_list = [] for word in list(sec_pairs[cnt][last].keys()): s += sec_pairs[cnt][last][word] cnt_l_list += sec_pairs[cnt][last][word] * [word] s_p += s // 2 s_list.extend(cnt_l_list[: 2 * (s // 2)]) all_p = 0 for cnt in list(all_pairs.keys()): s = 0 for word in list(all_pairs[cnt].keys()): s += all_pairs[cnt][word] all_p += s // 2 ans = min(s_p, all_p // 2) s_list = s_list[: 2 * ans] for item in s_list: cnt = 0 for i in item: if i in vowels: cnt += 1 all_pairs[cnt][item] -= 1 f_list = [] for cnt in list(all_pairs.keys()): cnt_l = [] s = 0 for word in list(all_pairs[cnt].keys()): cnt_l += all_pairs[cnt][word] * [word] s += all_pairs[cnt][word] f_list.extend(cnt_l[: 2 * (s // 2)]) f_list = f_list[: 2 * ans] print(ans) for i in range(ans): print(f_list[2 * i], s_list[2 * i]) print(f_list[2 * i + 1], s_list[2 * i + 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING STRING STRING STRING STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR DICT VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR DICT VAR DICT VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR DICT VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR LIST VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
VOWELS = "a", "e", "i", "o", "u" def main(): buf = input() n = int(buf) s = [] for i in range(n): buf = input() s.append(buf) word_info = [] for i, x in enumerate(s): vowel_count = 0 last_vowel = None for c in x: if c in VOWELS: vowel_count += 1 last_vowel = c word_info.append((i, vowel_count, last_vowel)) word_info.sort(key=lambda x: (x[1], x[2])) word_pair = [] word_pair_first_only = [] last_vowel_count = 0 last_last_vowel = None last_word = None last_word_leftover = None for i, x in enumerate(word_info): if x[1] > last_vowel_count: if last_word and last_word_leftover: word_pair_first_only.append((last_word, last_word_leftover)) last_word_leftover = None last_word = None last_vowel_count = x[1] last_last_vowel = x[2] if x[2] != last_last_vowel: if last_word: if last_word_leftover: word_pair_first_only.append((last_word, last_word_leftover)) last_word_leftover = None else: last_word_leftover = last_word last_word = None last_last_vowel = x[2] if last_word: word_pair.append((last_word, x)) last_word = None else: last_word = x if last_word and last_word_leftover: word_pair_first_only.append((last_word, last_word_leftover)) lyrics = [] first_only_pointer = 0 last_word_pair = None for i, x in enumerate(word_pair): if first_only_pointer < len(word_pair_first_only): last_word_pair = word_pair_first_only[first_only_pointer] first_only_pointer += 1 if last_word_pair: lyrics.append((last_word_pair, x)) last_word_pair = None else: last_word_pair = x print(len(lyrics)) for x in lyrics: print(s[x[0][0][0]], s[x[1][0][0]]) print(s[x[0][1][0]], s[x[1][1][0]]) def __starting_point(): main() __starting_point()
ASSIGN VAR STRING STRING STRING STRING STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR IF VAR IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
def count_vowels(s): return sum(1 if c in {"a", "e", "i", "o", "u"} else 0 for c in s) def last_vowel(s): answer = s[0] for c in s: if c in {"a", "e", "i", "o", "u"}: answer = c return answer n = int(input()) ss = [input() for _ in range(n)] d = {} for s in ss: c, l = count_vowels(s), last_vowel(s) if (c, l) not in d: d[c, l] = [] d[c, l].append(s) p2 = [] for c, l in d: ldcl = len(d[c, l]) while ldcl > 1: p2.append((d[c, l][-2], d[c, l][-1])) d[c, l].pop() d[c, l].pop() ldcl -= 2 p1 = [] d2 = {} for c, l in d: if len(d[c, l]) > 0: if c not in d2: d2[c] = [] d2[c].append(d[c, l][0]) for c in d2: ld2c = len(d2[c]) while ld2c > 1: p1.append((d2[c][-2], d2[c][-1])) d2[c].pop() d2[c].pop() ld2c -= 2 pp = [] lp1, lp2 = len(p1), len(p2) while lp1 > 0 and lp2 > 0: pp.append(((p1[-1][0], p2[-1][0]), (p1[-1][1], p2[-1][1]))) p1.pop() p2.pop() lp1 -= 1 lp2 -= 1 while lp2 > 1: pp.append(((p2[-2][0], p2[-1][0]), (p2[-2][1], p2[-1][1]))) p2.pop() p2.pop() lp2 -= 2 print(len(pp)) for (a, b), (c, d) in pp: print(f"{a} {b}\n{c} {d}")
FUNC_DEF RETURN FUNC_CALL VAR VAR STRING STRING STRING STRING STRING NUMBER NUMBER VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR STRING STRING STRING STRING STRING ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR STRING VAR STRING VAR
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
vowelList = ["a", "e", "i", "o", "u"] r = int(input()) stringArr = [] for i in range(r): string = input() properties = [] properties.append(string) backStr = string[::-1] numVowel = 0 for j in range(len(string)): if backStr[j] in vowelList: if numVowel == 0: properties.append(backStr[j]) numVowel += 1 properties.append(numVowel) stringArr.append(properties) stringArr.sort(key=lambda arr: arr[2]) n = 1 left = 0 equalNumVowels = [] correspondingSecondPairs = [] correspondingFirstPairs = [] currentArr = [] for i in range(r): if stringArr[i][2] > n: equalNumVowels.append(currentArr) n = stringArr[i][2] currentArr = [] currentArr.append(stringArr[i]) equalNumVowels.append(currentArr) for ar in equalNumVowels: aArr = [] eArr = [] iArr = [] oArr = [] uArr = [] for item in ar: if item[1] == "a": aArr.append(item) elif item[1] == "e": eArr.append(item) elif item[1] == "i": iArr.append(item) elif item[1] == "o": oArr.append(item) else: uArr.append(item) while len(aArr) > 1: m = aArr.pop() n = aArr.pop() correspondingSecondPairs.append([m, n]) while len(eArr) > 1: m = eArr.pop() n = eArr.pop() correspondingSecondPairs.append([m, n]) while len(iArr) > 1: m = iArr.pop() n = iArr.pop() correspondingSecondPairs.append([m, n]) while len(oArr) > 1: m = oArr.pop() n = oArr.pop() correspondingSecondPairs.append([m, n]) while len(uArr) > 1: m = uArr.pop() n = uArr.pop() correspondingSecondPairs.append([m, n]) leftOver = [] if len(aArr) == 1: leftOver.append(aArr[0]) if len(eArr) == 1: leftOver.append(eArr[0]) if len(iArr) == 1: leftOver.append(iArr[0]) if len(oArr) == 1: leftOver.append(oArr[0]) if len(uArr) == 1: leftOver.append(uArr[0]) while len(leftOver) > 1: m = leftOver.pop() n = leftOver.pop() correspondingFirstPairs.append([m, n]) print( min( len(correspondingSecondPairs), len(correspondingFirstPairs) + (len(correspondingSecondPairs) - len(correspondingFirstPairs)) // 2, ) ) while len(correspondingFirstPairs) >= 1 and len(correspondingSecondPairs) >= 1: a = correspondingFirstPairs.pop() b = correspondingSecondPairs.pop() print(str(a[0][0]) + " " + str(b[0][0]) + "\n" + str(a[1][0]) + " " + str(b[1][0])) while len(correspondingSecondPairs) >= 2: a = correspondingSecondPairs.pop() b = correspondingSecondPairs.pop() print(str(a[0][0]) + " " + str(b[0][0]) + "\n" + str(a[1][0]) + " " + str(b[1][0]))
ASSIGN VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER STRING FUNC_CALL VAR VAR NUMBER NUMBER STRING FUNC_CALL VAR VAR NUMBER NUMBER STRING FUNC_CALL VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER STRING FUNC_CALL VAR VAR NUMBER NUMBER STRING FUNC_CALL VAR VAR NUMBER NUMBER STRING FUNC_CALL VAR VAR NUMBER NUMBER
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
n = int(input()) vowels = "aeoiu" words = [] vocab1 = {} i = 0 complete_pairs = [] for _ in range(n): word = input() words.append(word) counter = 0 last_letter = "a" for letter in word: if letter in vowels: counter += 1 last_letter = letter if (counter, last_letter) in vocab1: vocab1[counter, last_letter].append(i) else: vocab1[counter, last_letter] = [i] if len(vocab1[counter, last_letter]) >= 2: complete_pairs.append( (vocab1[counter, last_letter].pop(), vocab1[counter, last_letter].pop()) ) if len(vocab1[counter, last_letter]) == 0: vocab1.pop((counter, last_letter)) i += 1 semicomplete_pairs = [] vocab2 = {} for key in vocab1: if key[0] in vocab2: vocab2[key[0]].extend(vocab1[key]) else: vocab2[key[0]] = vocab1[key] while len(vocab2[key[0]]) >= 2: semicomplete_pairs.append((vocab2[key[0]].pop(), vocab2[key[0]].pop())) if len(vocab2[key[0]]) == 0: vocab2.pop(key[0]) result = [] while len(semicomplete_pairs) >= 1 and len(complete_pairs) >= 1: complete_pair = complete_pairs.pop() semicomplete_pair = semicomplete_pairs.pop() result.append( (semicomplete_pair[0], complete_pair[0], semicomplete_pair[1], complete_pair[1]) ) while len(complete_pairs) >= 2: complete_pair1 = complete_pairs.pop() complete_pair2 = complete_pairs.pop() result.append( (complete_pair1[0], complete_pair2[0], complete_pair1[1], complete_pair2[1]) ) print(len(result)) for r in result: print("{} {}".format(words[r[0]], words[r[1]])) print("{} {}".format(words[r[2]], words[r[3]]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR WHILE FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR NUMBER VAR VAR NUMBER
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
n = int(input()) dic = {} vowel = {"a": 1, "e": 2, "i": 3, "o": 4, "u": 5} first_pair = [] second_pair = [] for i in range(n): tmp = input() first_vowel = "" cnt = 0 for j in range(len(tmp) - 1, -1, -1): if tmp[j] in vowel: if first_vowel == "": first_vowel = tmp[j] cnt += 1 if str(cnt) + first_vowel not in dic: dic[str(cnt) + first_vowel] = tmp else: second_pair.append([tmp, dic.pop(str(cnt) + first_vowel)]) dic_cnt = {} for i in dic.keys(): if int(i[:-1]) not in dic_cnt: dic_cnt[int(i[:-1])] = 1 else: dic_cnt[int(i[:-1])] += 1 for i in dic_cnt.keys(): cnt = 0 tmp = "" if dic_cnt[i] // 2 == 1: for j in vowel.keys(): if str(i) + j in dic: if cnt % 2 == 0: tmp = dic[str(i) + j] cnt += 1 else: first_pair.append([tmp, dic[str(i) + j]]) cnt += 1 if cnt == 2: break elif dic_cnt[i] // 2 == 2: for j in vowel.keys(): if str(i) + j in dic: if cnt % 2 == 0: tmp = dic[str(i) + j] cnt += 1 else: first_pair.append([tmp, dic[str(i) + j]]) cnt += 1 if cnt == 4: break len1 = len(first_pair) len2 = len(second_pair) if len1 >= len2: if len2 == 0: print(len2) else: print(len2) for i in range(len2): print(first_pair[i][0], second_pair[i][0]) print(first_pair[i][1], second_pair[i][1]) elif len2 <= 1: print(0) else: print(len1 + (len2 - len1) // 2) for i in range(len1): print(first_pair[i][0], second_pair[i][0]) print(first_pair[i][1], second_pair[i][1]) for i in range(len1, len2, 2): if (len2 - len1) % 2 == 1: if len2 - i == 1: break print(second_pair[i][0], second_pair[i + 1][0]) print(second_pair[i][1], second_pair[i + 1][1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR IF VAR STRING ASSIGN VAR VAR VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING IF BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
def fun(string): last = "" amount = 0 saw = False r = len(string) for i in range(r - 1, -1, -1): if string[i] in gay: amount += 1 if not saw: saw = True last = string[i] return amount, last def main(): n = int(input()) words = {} sizesandlasts = {} sizes = set() for i in range(n): s = input() pair = size, last = fun(s) sizes.add(size) words[s] = pair if pair not in sizesandlasts: sizesandlasts[pair] = [] sizesandlasts[pair].append(s) second = [] first = {} for length in sizes: for ch in gay: text = sizesandlasts.get((length, ch), []) for val in range(len(text) // 2): second.append([text.pop(), text.pop()]) if len(text) > 0: size = words[text[0]][0] if size not in first: first[size] = [] first[size].append(text.pop()) ans = [] for size in first: if len(second) == 0: break else: while len(first[size]) > 1 and len(second) > 0: ans.append([[first[size].pop(), first[size].pop()], second.pop()]) while len(second) > 1: ans.append([second.pop(), second.pop()]) print(len(ans)) for i in range(len(ans)): print("{} {}".format(ans[i][0][0], ans[i][1][0])) print("{} {}".format(ans[i][0][1], ans[i][1][1])) gay = set() for char in ["a", "e", "o", "i", "u"]: gay.add(char) main()
FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR LIST STRING STRING STRING STRING STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
vowels = "aeiou" n = int(input()) s = [] for i in range(n): s.append(input()) def id(word): vowel_count = 0 for letter in word: if letter in vowels: vowel_count += 1 last_vowel = letter return vowel_count, last_vowel rhymer = {} for word in s: vowel_count, last_vowel = id(word) if not rhymer.get(vowel_count): rhymer[vowel_count] = {} if not rhymer[vowel_count].get(last_vowel): rhymer[vowel_count][last_vowel] = [] rhymer[vowel_count][last_vowel].append(word) solid_rhymes, weak_rhymes = [], [] for vowel_count in rhymer: weak_cache = [] for last_vowel in rhymer[vowel_count]: while len(rhymer[vowel_count][last_vowel]) > 1: solid_rhymes.append(rhymer[vowel_count][last_vowel][-2:]) rhymer[vowel_count][last_vowel].pop() rhymer[vowel_count][last_vowel].pop() weak_cache += rhymer[vowel_count][last_vowel] while len(weak_cache) > 1: weak_rhymes.append(weak_cache[-2:]) weak_cache.pop() weak_cache.pop() while len(solid_rhymes) > len(weak_rhymes): weak_rhymes.append(solid_rhymes.pop()) def print_lyric(solid_rhyme, weak_rhyme): for j in range(2): print(weak_rhyme[j] + " " + solid_rhyme[j]) m = len(solid_rhymes) print(m) for i in range(m): print_lyric(solid_rhymes[i], weak_rhymes[i])
ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR DICT IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR LIST LIST FOR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
import sys num = int(input()) vowels = "aeiou" numvdic = {} valdic = {} inpt = [] for i in range(num): word = sys.stdin.readline() inpt.append(word.strip()) a = sum([word.count(vowels[i]) for i in range(5)]) ind = -1 while True: b = vowels.find(word[ind]) if b != -1: break ind -= 1 val = a << 3 | b if a in numvdic: numvdic[a].add(i) else: numvdic[a] = {i} if val in valdic: valdic[val].add(i) else: valdic[val] = {i} bound = 0 for st in numvdic.values(): bound += len(st) // 2 bound //= 2 ids = list(valdic.values()) data = [] used = set() for i in range(len(ids)): st = ids[i] while len(st) > 1 and len(data) < bound: a = st.pop() b = st.pop() data.append((a, b)) used.add(a) used.add(b) if len(data) == bound: break print(len(data)) ind = len(data) - 1 for st in numvdic.values(): temp = [] while len(st) != 0: if ind == -1: break q = st.pop() if q not in used: temp.append(q) if len(temp) == 2: print(inpt[temp[0]], inpt[data[ind][0]]) print(inpt[temp[1]], inpt[data[ind][1]]) ind -= 1 temp = []
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
n = int(input()) la = [] le = [] li = [] lo = [] lu = [] ans = [] d1 = {} for i in range(n): s = input() x = s.count("a") + s.count("e") + s.count("i") + s.count("o") + s.count("u") if x > 0: for c in s[::-1]: if c == "a" or c == "e" or c == "i" or c == "o" or c == "u": break if (x, c) in d1: d1[x, c].append(s) else: d1[x, c] = [s] d2 = {} pairs1 = [] pairs2 = [] for k in d1: if len(d1[k]) % 2 == 1: if k[0] in d2: d2[k[0]].append(d1[k][0]) else: d2[k[0]] = [d1[k][0]] for i in range(1, len(d1[k]), 2): pairs1.append((d1[k][i], d1[k][i + 1])) else: for i in range(0, len(d1[k]), 2): pairs1.append((d1[k][i], d1[k][i + 1])) for k in d2: if len(d2[k]) > 1: if len(d2[k]) % 2 == 1: d2[k].pop() for i in range(0, len(d2[k]), 2): pairs2.append((d2[k][i], d2[k][i + 1])) if len(pairs1) < len(pairs2): print(len(pairs1)) for i in range(len(pairs1)): print(pairs2[i][0], pairs1[i][0]) print(pairs2[i][1], pairs1[i][1]) else: print(len(pairs2) + (len(pairs1) - len(pairs2)) // 2) for i in range(len(pairs2)): print(pairs2[i][0], pairs1[i][0]) print(pairs2[i][1], pairs1[i][1]) for j in range(len(pairs2), len(pairs1), 2): if j == len(pairs1) - 1: break else: print(pairs1[j][0], pairs1[j + 1][0]) print(pairs1[j][1], pairs1[j + 1][1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING IF VAR NUMBER FOR VAR VAR NUMBER IF VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER LIST VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
from sys import stdin s = [] z = int(input()) dict = {} for t in range(z): ss = stdin.readline().strip() s.append(ss) vow = "" got = False ct = 0 for i in range(len(ss) - 1, -1, -1): if ss[i] == "a" or ss[i] == "e" or ss[i] == "i" or ss[i] == "o" or ss[i] == "u": ct += 1 if not got: got = True vow = ss[i] key = vow + str(ct) if key in dict: dict[key].append(t) else: dict[key] = [t] same = [] dif = {} for key in dict: if len(dict[key]) & 1: while len(dict[key]) > 1: same.append(dict[key][-1]) dict[key].pop() k = key[1:] if k in dif: dif[k].append(dict[key][-1]) else: dif[k] = [dict[key][-1]] dict[key].pop() else: while len(dict[key]) > 0: same.append(dict[key][-1]) dict[key].pop() ans = [] for key in dif: if len(dif[key]) & 1: dif[key].pop() while len(dif[key]) > 0 and len(same) > 1: ans.append(dif[key][-1]) dif[key].pop() ans.append(same[-1]) same.pop() ans.append(dif[key][-1]) dif[key].pop() ans.append(same[-1]) same.pop() if len(same) <= 1: break while len(same) > 3: ans.append(same[-1]) ans.append(same[-3]) ans.append(same[-2]) ans.append(same[-4]) same.pop() same.pop() same.pop() same.pop() print(len(ans) // 4) for i in range(i, len(ans), 4): print(s[ans[i]], s[ans[i + 1]]) print(s[ans[i + 2]], s[ans[i + 3]])
ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR LIST VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
def is_vowel(c): return c == "a" or c == "e" or c == "i" or c == "o" or c == "u" def count_vowels(word): ans = 0 last_vowel = "" for i in range(len(word)): if is_vowel(word[i]): last_vowel = word[i] ans += 1 return ans, last_vowel N = int(input()) m = {} for n in range(N): word = input() num_vowels, last_vowel = count_vowels(word) if not num_vowels in m: m[num_vowels] = {} if not last_vowel in m[num_vowels]: m[num_vowels][last_vowel] = [] m[num_vowels][last_vowel].append(word) tot_pair = [] semi_pair = [] for key_num, value_num in m.items(): for key_last, value_last in value_num.items(): while len(value_last) >= 2: tot_pair.append(value_last[-1]) tot_pair.append(value_last[-2]) value_last.pop() value_last.pop() while len(value_last) > 0: semi_pair.append(value_last[-1]) value_last.pop() if len(semi_pair) % 2 == 1: semi_pair.pop() ans = [] count = 0 while len(tot_pair) >= 2 and len(semi_pair) >= 2: line_1 = [semi_pair[-1], tot_pair[-1]] line_2 = [semi_pair[-2], tot_pair[-2]] semi_pair.pop() semi_pair.pop() tot_pair.pop() tot_pair.pop() lyric = [line_1, line_2] ans.append(lyric) count += 1 while len(tot_pair) >= 4: line_1 = [tot_pair[-3], tot_pair[-1]] line_2 = [tot_pair[-4], tot_pair[-2]] tot_pair.pop() tot_pair.pop() tot_pair.pop() tot_pair.pop() lyric = [line_1, line_2] ans.append(lyric) count += 1 print(count) for lyric in ans: for line in lyric: print(line[0], line[1])
FUNC_DEF RETURN VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR DICT IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
import sys input = sys.stdin.readline def last_vowel(s): r = "" for c in s: if c in "aeiou": r = c return r def count_vowel(s): r = 0 for c in s: r += 1 if c in "aeiou" else 0 return r n = int(input()) words = {"a": {}, "e": {}, "i": {}, "o": {}, "u": {}} for i in range(n): s = input().rstrip() lv = last_vowel(s) cnt = count_vowel(s) if not cnt in words[lv]: words[lv][cnt] = [] words[lv][cnt].append(s) cpairs = [] twords = [] tpairs = [] for lv in "aeiou": for cnt in words[lv]: for i in range(len(words[lv][cnt]) // 2): cpairs.append((words[lv][cnt][2 * i], words[lv][cnt][2 * i + 1])) if len(words[lv][cnt]) % 2 == 1: twords.append((cnt, words[lv][cnt][-1])) twords.sort() t = None for w in twords: if t == None: t = w elif t[0] == w[0]: tpairs.append((t[1], w[1])) t = None else: t = w res = [] while len(tpairs) > 0 and len(cpairs) > 0: res.append((tpairs.pop(), cpairs.pop())) while len(cpairs) > 1: res.append((cpairs.pop(), cpairs.pop())) print(len(res)) for p in res: print("{} {}\n{} {}".format(p[0][0], p[1][0], p[0][1], p[1][1]))
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR STRING FOR VAR VAR IF VAR STRING ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR STRING NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING DICT DICT DICT DICT DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR STRING FOR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NONE FOR VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
GL = set(list("aeiou")) n = int(input()) di = dict() tails = dict() ends = 0 ends_words = [] starts = 0 starts_words = [] for i in range(n): word = input() counter = 0 for w in word: if w in GL: last = w counter += 1 if (last, counter) not in di: di[last, counter] = [word] else: di[last, counter].append(word) for i in list(di.keys()): ends += len(di[i]) // 2 * 2 while len(di[i]) > 1: fir = di[i].pop() sec = di[i].pop() ends_words.append([fir, sec]) for i in list(di.keys()): if di[i] == []: continue if i[1] in tails: starts += 2 starts_words.append([di[i][0], tails[i[1]]]) del tails[i[1]] else: tails[i[1]] = di[i][0] while len(ends_words) > len(starts_words): starts_words.append(ends_words.pop()) print(len(ends_words)) for i in range(len(ends_words)): end = ends_words.pop() st = starts_words.pop() print(st[0], end[0]) print(st[1], end[1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR LIST IF VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
n = int(input()) words = {} def getKey(word): vowels = "aeiou" nVowels = 0 indexLast = -1 for i, l in enumerate(word): if l in vowels: nVowels += 1 lVowel = l return str(lVowel), str(nVowels) for i in range(n): s = input() index, num = getKey(s) if num not in words: words[num] = {} if index not in words[num]: words[num][index] = [] words[num][index].append(s) nComp = [] nSemi = [] for i in words.values(): nRem = [] for j in i.values(): for k in range(0, len(j) - 1, 2): nComp.append((j[k], j[k + 1])) if len(j) % 2 != 0: nRem.append(j[-1]) for j in range(0, len(nRem) - 1, 2): nSemi.append((nRem[j], nRem[j + 1])) lComp = len(nComp) lSemi = len(nSemi) minim = min(lComp, lSemi) rem = (lComp - minim) // 2 print(minim + rem) for i in range(minim): for j in range(2): print(nSemi[i][j], nComp[i][j]) for i in range(rem): for j in range(2): print(nComp[minim + 2 * i][j], nComp[minim + 2 * i + 1][j])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR DICT IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
n = int(input()) d = {} ish = {"a", "e", "o", "i", "u"} def last_g(x): for el in x[::-1]: if el in ish: return el for i in range(n): s = input() count = s.count("a") + s.count("e") + s.count("o") + s.count("i") + s.count("u") if count in d: d[count].append(s) else: d[count] = [s] firsts = [] seconds = [] for e in d: gd = {"a": [], "e": [], "o": [], "i": [], "u": []} pairs = [] for s in d[e]: gd[last_g(s)].append(s) for b in gd: for i in range(0, len(gd[b]) - 1, 2): seconds.append((gd[b][i], gd[b][i + 1])) if len(gd[b]) % 2 == 1: pairs.append(gd[b][-1]) for i in range(0, len(pairs) - 1, 2): firsts.append((pairs[i], pairs[i + 1])) q = len(seconds) m = len(firsts) if m >= q: print(q) for i in range(q): print(firsts[i][0] + " " + seconds[i][0]) print(firsts[i][1] + " " + seconds[i][1]) else: print(m + (q - m) // 2) for i in range(m): print(firsts[i][0] + " " + seconds[i][0]) print(firsts[i][1] + " " + seconds[i][1]) for i in range(m, q - 1, 2): print(seconds[i][0] + " " + seconds[i + 1][0]) print(seconds[i][1] + " " + seconds[i + 1][1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR STRING STRING STRING STRING STRING FUNC_DEF FOR VAR VAR NUMBER IF VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING LIST LIST LIST LIST LIST ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR BIN_OP VAR NUMBER NUMBER
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
def countVowels(s): vowels = ["a", "e", "i", "o", "u"] count = 0 for i in range(len(s)): if s[i] in vowels: count += 1 lastVowel = s[i] return count, lastVowel n = int(input()) d = {} second = [] for i in range(n): word = input() x = countVowels(word) if x[0] in d: v = d[x[0]] if x[1] in v: arr = v[x[1]] if len(arr) == 1: item = arr.pop() second.append((item, word)) else: arr.append(word) else: v[x[1]] = [word] else: d[x[0]] = {x[1]: [word]} first = [] for n in d: v = d[n] prev = None for e in v: arr = v[e] if len(arr) == 1: if prev == None: prev = arr[0] else: first.append((prev, arr[0])) prev = None if len(first) >= len(second): print(len(second)) for i in range(len(second)): print(first[i][0] + " " + second[i][0]) print(first[i][1] + " " + second[i][1]) else: print(len(first) + int((len(second) - len(first)) / 2)) for i in range(len(first)): print(first[i][0] + " " + second[i][0]) print(first[i][1] + " " + second[i][1]) i = len(first) while i < len(second): if i + 1 < len(second): print(second[i][0] + " " + second[i + 1][0]) print(second[i][1] + " " + second[i + 1][1]) i += 2 else: break
FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER LIST VAR ASSIGN VAR VAR NUMBER DICT VAR NUMBER LIST VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NONE FOR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NONE ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NONE IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
n = int(input()) tuples = [] hashMap = {} used = {} for _ in range(n): word = input() noOfVowels = 0 last = 0 for i in word: if i == "a" or i == "e" or i == "i" or i == "o" or i == "u": last = i noOfVowels += 1 tuples.append((word, noOfVowels, last)) if noOfVowels not in hashMap: hashMap[noOfVowels] = {} hashMap[noOfVowels][last] = [] elif last not in hashMap[noOfVowels]: hashMap[noOfVowels][last] = [] hashMap[noOfVowels][last].append(word) CompleteDuos = [] SemiCompleteDuos = [] for SameNoOfVowels in hashMap: remaining = [] for sameLastVowel in hashMap[SameNoOfVowels]: while len(hashMap[SameNoOfVowels][sameLastVowel]) >= 2: CompleteDuos.append( ( hashMap[SameNoOfVowels][sameLastVowel].pop(), hashMap[SameNoOfVowels][sameLastVowel].pop(), ) ) while len(hashMap[SameNoOfVowels][sameLastVowel]) > 0: remaining.append(hashMap[SameNoOfVowels][sameLastVowel].pop()) while len(remaining) >= 2: SemiCompleteDuos.append((remaining.pop(), remaining.pop())) NoOfLyrics = 0 minValue = min(len(CompleteDuos), len(SemiCompleteDuos)) NoOfLyrics = minValue extra = len(CompleteDuos) - minValue NoOfLyrics += extra // 2 print(NoOfLyrics) for i in range(minValue): print(SemiCompleteDuos[i][0], CompleteDuos[i][0]) print(SemiCompleteDuos[i][1], CompleteDuos[i][1]) for j in range(minValue, minValue + extra - 1, 2): print(CompleteDuos[j][0], CompleteDuos[j + 1][0]) print(CompleteDuos[j][1], CompleteDuos[j + 1][1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR DICT ASSIGN VAR VAR VAR LIST IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
n = int(input()) word = [] endwith = [{} for i in range(5)] second = [] first = {} for i in range(n): w = input() word.append(w) c = 0 lastkind = 0 for ch in w: if ch == "a": lastkind = 0 c += 1 elif ch == "e": lastkind = 1 c += 1 elif ch == "i": lastkind = 2 c += 1 elif ch == "o": lastkind = 3 c += 1 elif ch == "u": lastkind = 4 c += 1 if c not in endwith[lastkind]: endwith[lastkind][c] = [i] else: endwith[lastkind][c].append(i) for i in range(5): for key, value in list(endwith[i].items()): while len(value) >= 2: second.append(value.pop()) second.append(value.pop()) if len(value) == 1: if key in first: first[key].append(value[0]) else: first[key] = [value[0]] ans = [] m = 0 for key, value in list(first.items()): while len(value) >= 2 and len(second) >= 2: ans.append((value.pop(), value.pop(), second.pop(), second.pop())) m += 1 while len(second) >= 4: ans.append((second.pop(), second.pop(), second.pop(), second.pop())) m += 1 print(m) for tup in ans: print(word[tup[0]], word[tup[2]]) print(word[tup[1]], word[tup[3]])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR LIST VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
def solve(counts): ans = [] for count in counts.keys(): words = counts[count] for i in range(5): ending_words = words[i] while True: if len(ending_words) > 1: line1 = [ending_words.pop()] line2 = [ending_words.pop()] ans.append([line1, line2]) else: break index = 0 for count in counts.keys(): words = counts[count] line = [] for i in range(5): ending_words = words[i] if len(ending_words) > 0: if not line: line.append(ending_words.pop()) else: line.append(ending_words.pop()) if not ans: print(0) return if index < len(ans): ans[index][0].append(line[0]) ans[index][1].append(line[1]) ans[index][0].reverse() ans[index][1].reverse() index += 1 line = [] new_ans = [] for i in range(index): new_ans.append(ans[i]) while True: if index < len(ans): curr = ans[index] if index + 1 < len(ans): nexxt = ans[index + 1] temp = [[curr[0][0], nexxt[0][0]], [curr[1][0], nexxt[1][0]]] new_ans.append(temp) else: break index += 2 print(len(new_ans)) for i in new_ans: for j in range(2): print(i[j][0], i[j][1]) def getVowelCount(word): count = 0 vowels = ["a", "e", "i", "o", "u"] last = "" for i in word: if i in vowels: count += 1 last = i return count, last def main(): index = {} index["a"] = 0 index["e"] = 1 index["i"] = 2 index["o"] = 3 index["u"] = 4 n = int(input()) counts = {} for i in range(n): word = input() count, last = getVowelCount(word) if count not in counts.keys(): counts[count] = [[] for j in range(5)] counts[count][index[last]].append(word) else: counts[count][index[last]].append(word) solve(counts) main()
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR WHILE NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR NUMBER RETURN IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST LIST VAR NUMBER NUMBER VAR NUMBER NUMBER LIST VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR STRING NUMBER ASSIGN VAR STRING NUMBER ASSIGN VAR STRING NUMBER ASSIGN VAR STRING NUMBER ASSIGN VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
n = int(input()) d = [] for _ in range(n): s1 = input() s2 = "" for c in s1: if c in ("a", "e", "o", "i", "u"): s2 += c d.append((len(s2), s2[-1], s1)) d.sort(key=lambda x: (x[0], x[1])) r = [] ost = [] p_a, p_b, p_c = 0, "", "" for a, b, c in d: if a == p_a and b == p_b: r.append([p_c, c]) p_a, p_b, p_c = 0, "", "" continue if p_a != 0: ost.append((p_a, p_b, p_c)) p_a, p_b, p_c = a, b, c if p_a != 0: ost.append((p_a, p_b, p_c)) i = 0 p_a, p_c = 0, "" for a, b, c in ost: if a == p_a and i < len(r): r[i].append(p_c) r[i].append(c) p_a = 0 i += 1 continue p_a, p_b, p_c = a, b, c r_final = r[:i] for j in range(i, len(r), 2): if j < len(r) - 1: r[j].extend(r[j + 1]) r_final.append(r[j]) print(len(r_final)) for g in r_final: print(" ".join([g[2], g[0]])) print(" ".join([g[3], g[1]]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR IF VAR STRING STRING STRING STRING STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR VAR NUMBER STRING STRING FOR VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR NUMBER STRING STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER STRING FOR VAR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING LIST VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING LIST VAR NUMBER VAR NUMBER
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
n = int(input()) s = [] v = "aeiou" d1 = {} for _ in range(n): t1 = [*input()] t2 = [*filter(lambda x: t1[x] in v, range(len(t1)))] s.append(t1 + [len(t2), t1[t2[-1]]]) if s[-1][-2] in d1: if s[-1][-1] in d1[s[-1][-2]]: d1[s[-1][-2]][s[-1][-1]].append(s[-1]) else: d1[s[-1][-2]][s[-1][-1]] = [s[-1]] else: d1[s[-1][-2]] = {s[-1][-1]: [s[-1]]} d2 = {} for i, j in d1.items(): d2[i] = {"cd": [], "sd": []} for k, l in j.items(): d2[i]["cd"].extend(l[: len(l) - (len(l) & 1)]) if len(l) & 1: d2[i]["sd"].append(l[-1]) ans = [] for i, j in d2.items(): t = j["sd"][: len(j["sd"]) - (len(j["sd"]) & 1)] for k in range(0, len(t), 2): ans.append([t[k], t[k + 1]]) t = len(ans) l = p = 0 for i, j in d2.items(): k = 0 while k < len(j["cd"]): if l < t: ans[l].extend([j["cd"][k], j["cd"][k + 1]]) else: break k += 2 l += 1 while k < len(j["cd"]): if p % 4 == 0: ans.append([]) ans[-1].append(j["cd"][k]) p += 1 k += 1 ans = [*filter(lambda x: len(x) == 4, ans)] print(len(ans)) for i in ans: print("".join(i[0][:-2]), "".join(i[2][:-2])) print("".join(i[1][:-2]), "".join(i[3][:-2]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR LIST FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER NUMBER VAR IF VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER NUMBER LIST VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER DICT VAR NUMBER NUMBER LIST VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR DICT STRING STRING LIST LIST FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR STRING BIN_OP FUNC_CALL VAR VAR STRING BIN_OP FUNC_CALL VAR VAR STRING NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR STRING VAR VAR STRING BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR NUMBER VAR STRING VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER FUNC_CALL STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER FUNC_CALL STRING VAR NUMBER NUMBER
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
from sys import setrecursionlimit, stdin setrecursionlimit(10**7) def iin(): return int(stdin.readline()) def lin(): return list(map(int, stdin.readline().split())) def main(): n = iin() dc = {} vowel = {"a", "e", "i", "o", "u"} a1 = [] for i in range(n): w = input() a1.append(w) ch, v = 0, "z" for j in w: if j in vowel: ch += 1 v = j try: dc[ch, v].append(i) except: dc[ch, v] = [i] left = {} select = [] ans = [] for i in dc: j = 0 l = len(dc[i]) while j < l - 1: select.append([dc[i][j], dc[i][j + 1]]) j += 2 if l % 2: x, y = i try: left[x].append(dc[i][-1]) except: left[x] = [dc[i][-1]] select2 = [] for i in left: j = 0 l = len(left[i]) while j < l - 1: select2.append([left[i][j], left[i][j + 1]]) j += 2 i, j = 0, 0 c1, c2 = len(select), len(select2) while i < c1 and j < c2: ans.append(select[i] + select2[j]) i += 1 j += 1 while i < c1 - 1: ans.append(select[i] + select[i + 1]) i += 2 print(len(ans)) for i, j, k, l in ans: print(a1[k], a1[i]) print(a1[l], a1[j]) main()
EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR STRING STRING STRING STRING STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER STRING FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR LIST VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
n = int(input()) lyrics = [] seq = {} vowels = ["a", "e", "i", "o", "u"] for i in range(n): tmp = input() lyrics.append(tmp) cnt = 0 last = "-" for s in tmp: if s in vowels: last = vowels.index(s) cnt += 1 try: seq[cnt] except KeyError: seq[cnt] = [[] for x in range(5)] seq[cnt][last].append(i) odd = [] pair = [] for key, val in seq.items(): for x in val: i = len(x) if i % 2 == 1: odd.append(x[-1]) x = x[:-1] pair.extend(x) if len(odd) % 2 == 1: odd = odd[:-1] lo = len(odd) // 2 lp = len(pair) // 2 m = min((lo + lp) // 2, lp) print(m) ans = [(-1) for x in range(m * 4)] for i in range(len(odd)): try: ans[i * 2] = odd[i] except IndexError: break cnt = 0 i = 0 while i < m * 4: if i < len(odd) * 2: if ans[i] == -1: ans[i] = pair[cnt] cnt += 1 else: ans[i] = pair[cnt] ans[i + 1] = pair[cnt + 2] ans[i + 2] = pair[cnt + 1] ans[i + 3] = pair[cnt + 3] cnt += 4 i += 3 i += 1 for i in range(len(ans) // 2): print(lyrics[ans[2 * i]], lyrics[ans[2 * i + 1]])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR LIST STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR VAR VAR VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER
You are given $n$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man" "iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. -----Input----- The first line contains single integer $n$ ($1 \le n \le 10^{5}$) — the number of words. The $i$-th of the next $n$ lines contains string $s_{i}$ consisting lowercase alphabet letters — the $i$-th word. It is guaranteed that the sum of the total word length is equal or less than $10^{6}$. Each word contains at least one vowel. -----Output----- In the first line, print $m$ — the number of maximum possible beautiful lyrics. In next $2m$ lines, print $m$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. -----Examples----- Input 14 wow this is the first mcdics codeforces round hooray i am proud about that Output 3 about proud hooray round wow first this is i that mcdics am Input 7 arsijo suggested the idea for this problem Output 0 Input 4 same same same differ Output 1 same differ same same -----Note----- In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
n = int(input()) words = [] for i in range(n): words.append(input()) num_v = {} last_v = {} for i in words: cnt = 0 for j in i: if j in ["a", "e", "i", "o", "u"]: cnt += 1 num_v[i] = cnt for j in range(len(i) - 1, -1, -1): if i[j] in ["a", "e", "i", "o", "u"]: break last_v[i] = ord(i[j]) words.sort(key=lambda x: 10**6 * num_v[x] + last_v[x]) w_in_second = {} p_in_second = [] w_in_first = {} p_in_first = [] for i in range(len(words) - 1): if ( num_v[words[i]] == num_v[words[i + 1]] and last_v[words[i]] == last_v[words[i + 1]] ): try: if w_in_second[i]: pass except: w_in_second[i] = True w_in_second[i + 1] = True p_in_second.append((i, i + 1)) new = [] for i in range(len(words)): try: if w_in_second[i]: pass except: new.append(words[i]) for i in range(len(new) - 1): if num_v[new[i]] == num_v[new[i + 1]]: try: if w_in_first[i]: pass except: w_in_first[i] = True w_in_first[i + 1] = True p_in_first.append((i, i + 1)) p_in_first = [(new[i[0]], new[i[1]]) for i in p_in_first] p_in_second = [(words[i[0]], words[i[1]]) for i in p_in_second] while len(p_in_first) < len(p_in_second): pair = p_in_second.pop() p_in_first.append(pair) print(min(len(p_in_first), len(p_in_second))) for i in range(min(len(p_in_first), len(p_in_second))): print(p_in_first[i][0], p_in_second[i][0]) print(p_in_first[i][1], p_in_second[i][1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR LIST STRING STRING STRING STRING STRING VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER
Chef Palin, as his name suggests, is always very interested in palindromic strings. Recently, he made a pretty interesting discovery on palindromes and that made him feel really Lucky. He came across something known as Lucky Palindromes. He defines a string as being a lucky palindrome if it is a palindrome containing the string "lucky" as a substring. As always, now he wants to turn every string he comes across into a lucky palindrome. Being a chef, he is a man of patience and creativity, so he knows the operation of replacing any character of the string with any other character very well and he can perform this action infinitely many times. He wants you to write a program that can help him convert a given string to a lucky palindrome using the minimum number of operations and if several such lucky palindromes are possible, then output the lexicographically smallest one. ------ Input Format ------ The first line contains a single integer T ≤ 100 the number of testcases. The following T lines each contain a string of length ≤ 1000 and only containing characters 'a'-'z'. ------ Output Format ------ For each line of testcase, your program should output on a single line, the required lucky palindrome along with the minimum number of operations, both separated by a single space. If there is no lucky palindrome possible, then just output "unlucky" in a single line. ----- Sample Input 1 ------ 3 laubcdkey luckycodechef aaaaaaaa ----- Sample Output 1 ------ luckykcul 8 luckycocykcul 6 unlucky
def inbOunds(index, l, r): if index >= l and index <= r: return True return False t = int(input()) for _ in range(t): s = input().strip() l = len(s) count = 0 mini = 10000000000 ans = "" for index in range(l - 5 + 1): copyStr = list(s) count = 0 if copyStr[index] != "l": copyStr[index] = "l" count += 1 if copyStr[index + 1] != "u": copyStr[index + 1] = "u" count += 1 if copyStr[index + 2] != "c": copyStr[index + 2] = "c" count += 1 if copyStr[index + 3] != "k": copyStr[index + 3] = "k" count += 1 if copyStr[index + 4] != "y": copyStr[index + 4] = "y" count += 1 i = 0 j = len(copyStr) - 1 l = index r = index + 4 found = True while i <= j: if copyStr[i] != copyStr[j]: if (i < l or i > r) and (j < l or j > r): if copyStr[i] < copyStr[j]: copyStr[j] = copyStr[i] count += 1 else: copyStr[i] = copyStr[j] count += 1 elif inbOunds(i, l, r): if j < l or j > r: copyStr[j] = copyStr[i] count += 1 else: found = False break elif inbOunds(j, l, r): if i < l or i > r: copyStr[i] = copyStr[j] count += 1 else: found = False break else: found = False break i += 1 j -= 1 if found: if count < mini: mini = count ans = "".join(copyStr) elif mini == count: string = "".join(copyStr) ans = min(ans, string) if mini == 10000000000: print("unlucky") else: print(ans, mini)
FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
t = int(input()) for _ in range(t): n = int(input()) l = [] for i in range(0, n): l1 = [int(l1) for l1 in input().split()] l.append(l1) s = max(l[-1]) m = max(l[-1]) g = 0 for i in range(n - 2, -1, -1): m1 = -1 f = 0 for j in l[i]: if j < m and j > m1: f = 1 m1 = j if f == 0: print(-1) g = 1 break s += m1 m = m1 if g == 0: print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
T = int(input()) for t in range(T): N = int(input()) matrix = [] for n in range(N): matrix.append([int(i) for i in input().split()]) E = [max(matrix[N - 1])] for i in range(N - 2, -1, -1): e = -1 for j in range(N - 1, -1, -1): if matrix[i][j] < E[len(E) - 1]: e = max(e, matrix[i][j]) if e == -1: print(-1) break else: E.append(e) else: print(sum(E))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
for _ in range(int(input())): n = int(input()) l = [] if n == 1: s = int(input()) print(s) else: for i in range(n): a = list(map(int, input().split())) a.sort() l.append(a) m = l[-1][-1] flag = 1 sum = m for j in range(n - 2, -1, -1): b = False for k in range(n - 1, -1, -1): if l[j][k] < m: m = l[j][k] sum += m b = True break if b == False: break if not b: print(-1) else: print(sum)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
for i in range(int(input())): n = int(input()) l = [] for i in range(n): x = list(map(int, input().split())) l.append(x) max1 = max(l[len(l) - 1]) sum1 = max1 c = len(l) - 2 while c >= 0: z = l[c] z.sort(reverse=True) p = 0 for i in z: if i < max1: sum1 += i max1 = i p = 1 break if p == 0: sum1 = -1 break c -= 1 print(sum1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
T = int(input()) for i in range(T): N = int(input()) c = [] for j in range(N): s = list(map(int, input().split())) s.sort() c.append(s) t = 1 k = max(c[N - 1]) r = k for i in range(N - 2, -1, -1): for j in range(N - 1, -1, -1): if k > c[i][j]: k = c[i][j] t += 1 r += k break if t == N: print(r) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
for _ in range(int(input())): n = int(input()) l = [] for i in range(n): a = list(map(int, input().split())) a.sort(reverse=True) l.append(a) su = l[n - 1][0] flag = True c = l[n - 1][0] j = 0 if n > 1: for i in range(n - 2, -1, -1): j = 0 while 1: if l[i][j] < su: su = l[i][j] c += l[i][j] break elif j + 1 < n: j += 1 else: print(-1) flag = False break if not flag: break if flag: print(c) elif n == 1: print(c)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
import sys ntests = int(sys.stdin.readline().strip()) while ntests > 0: ntests -= 1 N = int(sys.stdin.readline().strip()) state = [(0, 0)] for r in range(0, N): fields = list(map(int, sys.stdin.readline().strip().split())) fields.sort() i = len(state) - 1 j = len(fields) - 1 nstate = [] while i >= 0 and j >= 0: if fields[j] > state[i][0]: nstate.insert(0, (fields[j], fields[j] + state[i][1])) j -= 1 else: i -= 1 state = nstate if len(state) > 0: print(state[-1][1]) else: print("-1")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
for _ in range(int(input())): n = int(input()) l = [] for i in range(n): l1 = list(map(int, input().split())) l1.sort() l.append(l1) ans = l1[-1] p = ans for i in range(n - 2, -1, -1): flag = 0 for j in range(n - 1, -1, -1): if l[i][j] < p: p = l[i][j] ans += p flag = 1 break if flag == 0: print(-1) flag = 2 break if flag != 2: print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
T = int(input()) for _ in range(T): N = int(input()) l = [] for __ in range(N): l.append(sorted(list(map(int, input().split())))) M = l[N - 1][N - 1] S = M c = 0 f = 0 for i in range(N - 2, -1, -1): for j in range(N - 1, -1, -1): if l[i][j] < M: M = l[i][j] S += M c += 1 break if c == 0: print("-1") f += 1 break else: c -= 1 if f == 0: print(S)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
def largest_smaller_than(a, N, key): start = 0 end = N ans = -1 while end >= start: mid = (start + end) // 2 if mid >= N or mid < 0: break elif a[mid] < key: ans = mid start = mid + 1 elif a[mid] >= key: end = mid - 1 if ans != -1: return a[ans] else: return -1 T = int(input()) ans = [] for _ in range(T): N = int(input()) A = [] for i in range(N): x = [int(i) for i in input().split()] A.append(sorted(x)) start = float("inf") sum = 0 for i in range(N - 1, -1, -1): start = largest_smaller_than(A[i], N, start) if start == -1: sum = -1 break sum += start ans.append(sum) for i in ans: print(i)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
for _ in range(int(input())): n = int(input()) mat = [] for _ in range(n): mat.append(sorted([int(a) for a in input().split()])) mat = mat[::-1] ans = max(mat[0]) p = max(mat[0]) for k in range(1, len(mat)): test = mat[k][::-1] f = False for val in test: if val < p: ans = ans + val f = True p = val break if not f: print(-1) break if f: print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
t = int(input()) for _ in range(t): arr = [] n = int(input()) for _ in range(n): arr.append(list(map(int, input().split(" ")))) mx = max(arr[-1]) sem = mx try: for i in range(n - 2, -1, -1): mx = max([j for j in arr[i] if j < mx]) sem += mx print(sem) except ValueError: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
def abc(l, n): l.reverse() x = l[0][-1] sum = x for i in range(1, n): if l[i][0] >= x: return -1 for j in range(n - 1, -1, -1): if l[i][j] < x: sum += l[i][j] x = l[i][j] break return sum t = int(input()) for _ in range(t): n = int(input()) l = [] for i in range(n): x = list(map(int, input().split())) x.sort() l.append(x) print(abc(l, n))
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
for _ in range(int(input())): n = int(input()) s = 0 mat = [] for i in range(n): a = [] a = [int(i) for i in input().split()] a.sort() mat.append(a) ans = mat[n - 1][n - 1] last = mat[n - 1][n - 1] s = 1 for i in range(n - 2, -1, -1): for j in range(n - 1, -1, -1): if mat[i][j] < last: ans += mat[i][j] last = mat[i][j] s += 1 break if s == n: print(ans) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
T = int(input()) for _ in range(T): n = int(input()) seq = [] score_list = [] for _ in range(n): res = list(map(int, input().split())) seq.append(res) seq.reverse() for l in seq: if len(score_list) == 0: score_list.append(max(l)) else: l.sort(reverse=True) for i in l: if i < score_list[len(score_list) - 1]: score_list.append(i) break elif i == l[len(l) - 1] and i >= score_list[len(score_list) - 1]: score_list.append(-1) if min(score_list) == -1: print(-1) else: print(sum(score_list))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
for _ in range(int(input())): res = 0 mat = [] n = int(input()) for _ in range(n): a = list(map(int, input().split())) mat.append(sorted(a)) count = mat[n - 1][n - 1] temp = mat[n - 1][n - 1] c = 0 k = 0 for i in range(n - 2, -1, -1): for j in range(n - 1, -1, -1): if mat[i][j] < temp: count += mat[i][j] temp = mat[i][j] c += 1 break if c == 0: print(-1) k = 1 break else: c = 0 if k == 0: print(count)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
t = int(input()) for i in range(t): a = int(input()) b = [] c = [] f = 0 for j in range(a): b = list(map(int, input().split())) c.append(b) m = max(c[-1]) s = m for k in range(a - 2, -1, -1): d = [] for z in c[k]: if z < m: d.append(z) if len(d) > 0: m = max(d) else: f = -1 s += m print(s if f == 0 else -1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
def fun(a, n): t = max(a[n - 1]) s = 0 s += t for i in range(n - 2, -1, -1): l = [] for j in range(n): if t > a[i][j]: l.append(a[i][j]) if l == []: return -1 t = max(l) s += t return s t = int(input()) for p in range(t): n = int(input()) a = [] for i in range(n): a.append(list(map(int, input().strip().split()))[:n]) print(fun(a, n))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR LIST RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
test = int(input()) for _ in range(test): n = int(input()) ls = [] for _ in range(n): ls.append(sorted(list(map(int, input().split())), reverse=True)) m = max(ls[n - 1]) s = m d = 0 for i in range(n - 2, -1, -1): f = 0 for j in range(n): if ls[i][j] < m: s += ls[i][j] m = ls[i][j] f = 1 break if f == 0: d = 1 break if d == 0: print(s) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
def function(a, N): acc = max(a[N - 1]) en = acc for i in range(N - 2, -1, -1): max_yet = -1 for elem in a[i]: if elem > max_yet and elem < en: max_yet = elem if max_yet == -1: print(-1) return en = max_yet acc += en print(acc) T = int(input()) for testcase in range(T): N = int(input()) a = [] for i in range(N): ai = list(map(int, input().split(" "))) ai.sort() a.append(ai) function(a, N)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
T = int(input()) for _ in range(T): N = int(input()) l = [] S = 0 f = 0 for __ in range(N): l.append(list(map(int, input().split()))) y = max(l[N - 1]) S += y for i in range(N - 2, -1, -1): c = 0 for j in range(0, N): if l[i][j] > c and l[i][j] < y: c = l[i][j] if c == 0: f += 1 break else: S += c y = c if f > 0: print("-1") else: print(S)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
for j in range(int(input())): n = int(input()) x = [] for i in range(n): y = list(map(int, input().split())) y.sort() x.append(y) x.reverse() z = [] a = max(x[0]) z.append(a) v = 1 for i in range(1, n): x[i].append(a) x[i].sort() b = x[i].index(a) if b - 1 >= 0: z.append(x[i][b - 1]) a = x[i][b - 1] else: v = 0 print(-1) break if v != 0: print(sum(z))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
def check(m, arr): s = -1 for i in arr: if i < m: s = max(s, i) return s t = int(input()) for _ in range(t): n = int(input()) a = [] for i in range(n): a.append(list(map(int, input().split()))) s = max(a[-1]) m = s for i in range(n - 2, -1, -1): m = check(m, a[i]) if m != -1: s += m else: s = -1 break print(s)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
for _ in range(int(input())): n = int(input()) mat = [] for x in range(n): mat.append([int(i) for i in input().split()]) ans = [0] * n flag = True ans[-1] = max(mat[-1]) for i in range(n - 2, -1, -1): for ele in mat[i]: if ele < ans[i + 1]: ans[i] = max(ans[i], ele) if ans[i] == 0: flag = False break if flag: print(sum(ans)) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
try: for i in range(int(input())): nm = int(input()) lst = [] for j in range(nm): lst.append(sorted([int(x) for x in input().split()])) sm = lst[-1][-1] mx = lst[-1][-1] c = 1 for m in range(nm - 2, -1, -1): for n in range(nm - 1, -1, -1): if lst[m][n] < mx: sm += lst[m][n] mx = lst[m][n] c += 1 break if c == nm: print(sm) else: print(-1) except: pass
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
t = int(input()) def func(n, l, cnt, cur): for i in range(n - 2, -1, -1): if min(l[i]) >= cur: return -1 break else: l[i] = [k for k in l[i] if k < cur] tmp = max(l[i]) cnt += tmp cur = tmp return cnt for _ in range(t): l = [] n = int(input()) for _ in range(n): s = input().split(" ") arr = [int(i) for i in s] l.append(arr) cnt = 0 cnt += max(l[-1]) cur = max(l[-1]) print(func(n, l, cnt, cur))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
for _ in range(int(input())): n = int(input()) A = list() for i in range(n): A.append(sorted(list(map(int, input().split())), reverse=True)) Ans = A[n - 1][0] prev = Ans flag = False for i in range(n - 2, -1, -1): flag = True for j in A[i]: if j < prev: flag = False Ans += j prev = j break if flag: break if flag: print(-1) else: print(Ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
for i in range(int(input())): n = int(input()) b = [] counter = 0 for j in range(n): b.append(list(map(int, input().split()))) c = max(b[-1]) sum = c for k in range(1, n): for x in b[-k - 1]: if x < c and x > counter: counter = x if counter == 0: print(-1) sum = 0 break else: sum = sum + counter c = counter counter = 0 if sum != 0: print(sum)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
for _ in range(int(input())): n = int(input()) score = [] for i in range(n): a = list(map(int, input().split())) a.sort() score.append(a) score = score[::-1] maxx = max(score[0]) p = max(score[0]) for j in range(1, len(score)): b = score[j] b = sorted(b, reverse=True) for value in b: if value < p: maxx += value p = value break else: maxx = -1 break print(maxx)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
for _ in range(int(input())): a = [] n = int(input()) for t in range(n): p = list(map(int, input().split())) a.append(p) c = [0] * n c[n - 1] = max(a[n - 1]) f = 0 s = 0 for i in range(n - 2, -1, -1): c[i] = 0 for j in range(n): if a[i][j] < c[i + 1]: c[i] = max(c[i], a[i][j]) if c[i] == 0: f = 1 break else: s += c[i] if f == 1: print(-1) else: print(s + c[n - 1])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
t = int(input()) while t > 0: n = int(input()) mat = [] for _ in range(n): s = input().split() arr = [int(x) for x in s] arr.sort(reverse=True) mat.append(arr) mat.reverse() curr = mat[0][0] summ = curr counter = 1 for i in range(1, n): for el in mat[i]: if el < curr: summ += el curr = el counter += 1 break if counter == n: print(summ) else: print(-1) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
for _ in range(int(input())): n = int(input()) lis = [] for i in range(n): a = list(map(int, input().split())) lis.append(a) for i in range(n): lis[i].sort() sum = lis[n - 1][n - 1] tmp = lis[n - 1][n - 1] f = 0 g = 0 i = n - 2 while i >= 0: j = n - 1 f = 0 while j >= 0: if lis[i][j] < tmp: sum += lis[i][j] tmp = lis[i][j] f = 1 break j -= 1 if f == 0: g = 1 break i -= 1 if g == 0: print(sum) else: print("-1")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
def fun(n, arr): s = 0 temp = float("infinity") for i in range(n - 1, -1, -1): a = 0 for j in range(0, n): if arr[i][j] > a and arr[i][j] < temp: a = arr[i][j] if a == 0: return -1 temp = a s += a return s for _ in range(int(input())): n = int(input()) arr = [0] * n for i in range(n): arr[i] = list(map(int, input().strip().split())) print(fun(n, arr))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
t = int(input()) for T in range(t): l = [] n = int(input()) c = 1 for i in range(n): a = [int(x) for x in input().split()] a.sort() l.append(a) ans = l[n - 1][n - 1] ele = ans for i in range(len(l) - 2, -1, -1): c = 1 for j in range(n - 1, -1, -1): if l[i][j] < ele: ele = l[i][j] ans += ele c = 0 break if c == 1: ans = -1 break print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
t = int(input()) while t > 0: l = [] n = int(input()) for i in range(n): l.append(list(map(int, input().split()))) x = max(l[-1]) w = 0 s = x for i in range(n - 2, -1, -1): l[i].sort(reverse=True) q = 0 for j in l[i]: if j < x: x = j q = 1 s += x break if q == 0: w = 1 print(-1) break if w == 0: print(s) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
for _ in range(int(input())): n = int(input()) a = [] for i in range(n): t = list(map(int, input().split())) t.sort(reverse=True) a.append(t) a.reverse() f = a[0][0] count = 1 mx = f for i in range(1, n): for j in range(n): if a[i][j] < mx: f += a[i][j] count += 1 mx = a[i][j] break if count == n: print(f) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
for _ in range(int(input())): n = int(input()) l = [] while n > 0: k = [] k = [int(i) for i in input().split()] k = sorted(k) l.append(k) n -= 1 count = [l[-1][-1]] flag = 0 for i in range(len(l) - 2, -1, -1): for j in range(len(l) - 1, -1, -1): if l[i][j] < count[-1]: count.append(l[i][j]) break else: flag = 1 break if flag == 1: break if flag == 1: print(-1) else: print(sum(count))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
T = int(input()) for i in range(T): n = int(input()) grid = [] for j in range(n): temp = [] temp = list(map(int, input().strip().split())) temp.sort() grid.append(temp) curr = max(grid[n - 1]) total = curr for i in range(n - 2, 0 - 1, -1): flag = 0 for j in range(n - 1, 0 - 1, -1): if grid[i][j] < curr: flag = 1 curr = grid[i][j] total += curr break if flag == 0: total = -1 break print(total)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
def maxScore(): for t in range(int(input())): n = int(input()) l = [] for i in range(n): temp = sorted([int(x) for x in input().split()]) l.append(temp) s = l[n - 1][n - 1] cdt_o = l[n - 1][n - 1] for i in range(n - 2, -1, -1): success = 1 for j in range(n - 1, -1, -1): cdt_t = l[i][j] if cdt_t < cdt_o: s += cdt_t cdt_o = cdt_t break elif j == 0: success = 0 break elif cdt_t >= cdt_o: continue if success == 0: break if success == 0: print(-1) else: print(s) maxScore()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
for i in range(int(input())): b = [] for j in range(int(input())): a = [int(x) for x in input().split()] b.append(a) next = max(b[-1]) sum = next for k in reversed(b[:-1]): su = max(y if y < next else -1 for y in k) if su == -1: sum = -1 break sum += su next = su print(sum)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
for _ in range(int(input())): k = 0 x = int(input()) a = list() p = [0] * x for i in range(x): a.append(list(map(int, input().split()))) p[-1] = max(a[-1]) flag = 0 for i in range(x - 2, -1, -1): q = sorted(a[i]) m = 0 for j in range(x - 1, -1, -1): if q[j] < p[i + 1]: p[i] = q[j] m = 1 break if m == 0: flag = 1 break if flag == 1: print(-1) else: print(sum(p))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
for _ in range(int(input())): n = int(input()) l = [[*map(int, input().split())] for i in range(n)] for i in range(n): l[i].sort() val = l[-1][-1] ans = val flag = True for i in range(n - 2, -1, -1): j = n - 1 flag1 = False while j >= 0: if l[i][j] < val: val = l[i][j] flag1 = True break else: j -= 1 if flag1: ans += l[i][j] val = l[i][j] else: flag = False break if flag: print(ans) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
tc = int(input().strip()) for t in range(tc): n = int(input().strip()) arr = [] for i in range(n): arr.append(list(map(int, input().strip().split()))) ans = 0 ulim = 1000000000.0 + 1 flag = True for i in range(n - 1, -1, -1): mx = -1 for j in range(n): if ulim > arr[i][j]: if arr[i][j] > mx: mx = arr[i][j] if mx == -1: flag = False break else: ans += mx ulim = mx print(ans) if flag else print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER
You are given N integer sequences A1, A2, ..., AN. Each of these sequences contains N elements. You should pick N elements, one from each sequence; let's denote the element picked from sequence Ai by Ei. For each i (2 ≤ i ≤ N), Ei should be strictly greater than Ei-1. Compute the maximum possible value of E1 + E2 + ... + EN. If it's impossible to pick the elements E1, E2, ..., EN, print -1 instead. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains a single integer N. - N lines follow. For each valid i, the i-th of these lines contains N space-separated integers Ai1, Ai2, ..., AiN denoting the elements of the sequence Ai. -----Output----- For each test case, print a single line containing one integer — the maximum sum of picked elements. -----Constraints----- - 1 ≤ T ≤ 10 - 1 ≤ N ≤ 700 - 1 ≤ sum of N in all test-cases ≤ 3700 - 1 ≤ Aij ≤ 109 for each valid i, j -----Subtasks----- Subtask #1 (18 points): 1 ≤ Aij ≤ N for each valid i, j Subtask #2 (82 points): original constraints -----Example----- Input: 1 3 1 2 3 4 5 6 7 8 9 Output: 18 -----Explanation----- Example case 1: To maximise the score, pick 3 from the first row, 6 from the second row and 9 from the third row. The resulting sum is E1+E2+E3 = 3+6+9 = 18.
for _ in range(int(input())): n = int(input()) lst = [] for i in range(n): lst.append(list(map(int, input().split()))) l = [] prev = 1000000001 for i in lst[::-1]: m = 0 bl = False for j in i: if j < prev: m = max(j, m) bl = True if bl: l.append(m) prev = m else: break if len(l) == n: print(sum(l)) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
In fact, the problems E1 and E2 do not have much in common. You should probably think of them as two separate problems. You are given an integer array $a[1 \ldots n] = [a_1, a_2, \ldots, a_n]$. Let us consider an empty deque (double-ended queue). A deque is a data structure that supports adding elements to both the beginning and the end. So, if there are elements $[3, 4, 4]$ currently in the deque, adding an element $1$ to the beginning will produce the sequence $[{1}, 3, 4, 4]$, and adding the same element to the end will produce $[3, 4, 4, {1}]$. The elements of the array are sequentially added to the initially empty deque, starting with $a_1$ and finishing with $a_n$. Before adding each element to the deque, you may choose whether to add it to the beginning or to the end. For example, if we consider an array $a = [3, 7, 5, 5]$, one of the possible sequences of actions looks like this: $\quad$ 1. add $3$ to the beginning of the deque: deque has a sequence $[{3}]$ in it; $\quad$ 2. add $7$ to the end of the deque: deque has a sequence $[3, {7}]$ in it; $\quad$ 3. add $5$ to the end of the deque: deque has a sequence $[3, 7, {5}]$ in it; $\quad$ 4. add $5$ to the beginning of the deque: deque has a sequence $[{5}, 3, 7, 5]$ in it; Find the minimal possible number of inversions in the deque after the whole array is processed. An inversion in sequence $d$ is a pair of indices $(i, j)$ such that $i < j$ and $d_i > d_j$. For example, the array $d = [5, 3, 7, 5]$ has exactly two inversions — $(1, 2)$ and $(3, 4)$, since $d_1 = 5 > 3 = d_2$ and $d_3 = 7 > 5 = d_4$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The next $2t$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — array size. The second line of the description contains $n$ space-separated integers $a_i$ ($-10^9 \le a_i \le 10^9$) — elements of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the minimal possible number of inversions in the deque after executing the described algorithm. -----Examples----- Input 6 4 3 7 5 5 3 3 2 1 3 3 1 2 4 -1 2 2 -1 4 4 5 1 3 5 1 3 1 3 2 Output 2 0 1 0 1 2 -----Note----- One of the ways to get the sequence $[5, 3, 7, 5]$ in the deque, containing only two inversions, from the initial array $[3, 7, 5, 5]$ (the first sample test case) is described in the problem statement. Also, in this example, you could get the answer of two inversions by simply putting each element of the original array at the end of the deque. In this case, the original sequence $[3, 7, 5, 5]$, also containing exactly two inversions, will be in the deque as-is.
import sys input = sys.stdin.readline def make_tree(n): tree = [0] * (n + 1) return tree def get_sum(i): s = 0 while i > 0: s += tree[i] i -= i & -i return s def get_sum_segment(s, t): ans = get_sum(t) - get_sum(s - 1) return ans def add(i, x): while i <= n: tree[i] += x i += i & -i t = int(input()) for _ in range(t): n = int(input()) p = list(map(int, input().split())) tree = make_tree(n + 5) s = set(p) s = list(s) s.sort() d = dict() for i in range(len(s)): d[s[i]] = i + 1 cnt = [0] * (n + 1) ans = 0 c0 = 0 for i in p: di = d[i] ci = cnt[di] s0 = get_sum(di - 1) ans += min(s0, c0 - ci - s0) add(di, 1) c0 += 1 cnt[di] += 1 print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
In fact, the problems E1 and E2 do not have much in common. You should probably think of them as two separate problems. You are given an integer array $a[1 \ldots n] = [a_1, a_2, \ldots, a_n]$. Let us consider an empty deque (double-ended queue). A deque is a data structure that supports adding elements to both the beginning and the end. So, if there are elements $[3, 4, 4]$ currently in the deque, adding an element $1$ to the beginning will produce the sequence $[{1}, 3, 4, 4]$, and adding the same element to the end will produce $[3, 4, 4, {1}]$. The elements of the array are sequentially added to the initially empty deque, starting with $a_1$ and finishing with $a_n$. Before adding each element to the deque, you may choose whether to add it to the beginning or to the end. For example, if we consider an array $a = [3, 7, 5, 5]$, one of the possible sequences of actions looks like this: $\quad$ 1. add $3$ to the beginning of the deque: deque has a sequence $[{3}]$ in it; $\quad$ 2. add $7$ to the end of the deque: deque has a sequence $[3, {7}]$ in it; $\quad$ 3. add $5$ to the end of the deque: deque has a sequence $[3, 7, {5}]$ in it; $\quad$ 4. add $5$ to the beginning of the deque: deque has a sequence $[{5}, 3, 7, 5]$ in it; Find the minimal possible number of inversions in the deque after the whole array is processed. An inversion in sequence $d$ is a pair of indices $(i, j)$ such that $i < j$ and $d_i > d_j$. For example, the array $d = [5, 3, 7, 5]$ has exactly two inversions — $(1, 2)$ and $(3, 4)$, since $d_1 = 5 > 3 = d_2$ and $d_3 = 7 > 5 = d_4$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The next $2t$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — array size. The second line of the description contains $n$ space-separated integers $a_i$ ($-10^9 \le a_i \le 10^9$) — elements of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the minimal possible number of inversions in the deque after executing the described algorithm. -----Examples----- Input 6 4 3 7 5 5 3 3 2 1 3 3 1 2 4 -1 2 2 -1 4 4 5 1 3 5 1 3 1 3 2 Output 2 0 1 0 1 2 -----Note----- One of the ways to get the sequence $[5, 3, 7, 5]$ in the deque, containing only two inversions, from the initial array $[3, 7, 5, 5]$ (the first sample test case) is described in the problem statement. Also, in this example, you could get the answer of two inversions by simply putting each element of the original array at the end of the deque. In this case, the original sequence $[3, 7, 5, 5]$, also containing exactly two inversions, will be in the deque as-is.
class FenwickTree: def __init__(self, n): self.n = n self.f = [0] * (n + 1) def update(self, x, d): while x <= self.n: self.f[x] += d x += x & -x def prefix(self, x): ans = 0 while x: ans += self.f[x] x &= x - 1 return ans def lower_bound(a, x): l = 0 r = len(a) while l < r: mid = l + r >> 1 if a[mid] >= x: r = mid else: l = mid + 1 return r def upper_bound(a, x): l = 0 r = len(a) while l < r: mid = l + r >> 1 if a[mid] > x: r = mid else: l = mid + 1 return r t = int(input()) for _ in range(t): n = int(input()) a = [int(x) for x in input().split()] b = sorted(a) for i in range(n): a[i] = lower_bound(b, a[i]) + 1 f = FenwickTree(n) ans = 0 for i in range(n): x = f.prefix(a[i] - 1) y = f.prefix(a[i]) ans += min(x, i - y) f.update(a[i], 1) print(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
In fact, the problems E1 and E2 do not have much in common. You should probably think of them as two separate problems. You are given an integer array $a[1 \ldots n] = [a_1, a_2, \ldots, a_n]$. Let us consider an empty deque (double-ended queue). A deque is a data structure that supports adding elements to both the beginning and the end. So, if there are elements $[3, 4, 4]$ currently in the deque, adding an element $1$ to the beginning will produce the sequence $[{1}, 3, 4, 4]$, and adding the same element to the end will produce $[3, 4, 4, {1}]$. The elements of the array are sequentially added to the initially empty deque, starting with $a_1$ and finishing with $a_n$. Before adding each element to the deque, you may choose whether to add it to the beginning or to the end. For example, if we consider an array $a = [3, 7, 5, 5]$, one of the possible sequences of actions looks like this: $\quad$ 1. add $3$ to the beginning of the deque: deque has a sequence $[{3}]$ in it; $\quad$ 2. add $7$ to the end of the deque: deque has a sequence $[3, {7}]$ in it; $\quad$ 3. add $5$ to the end of the deque: deque has a sequence $[3, 7, {5}]$ in it; $\quad$ 4. add $5$ to the beginning of the deque: deque has a sequence $[{5}, 3, 7, 5]$ in it; Find the minimal possible number of inversions in the deque after the whole array is processed. An inversion in sequence $d$ is a pair of indices $(i, j)$ such that $i < j$ and $d_i > d_j$. For example, the array $d = [5, 3, 7, 5]$ has exactly two inversions — $(1, 2)$ and $(3, 4)$, since $d_1 = 5 > 3 = d_2$ and $d_3 = 7 > 5 = d_4$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The next $2t$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — array size. The second line of the description contains $n$ space-separated integers $a_i$ ($-10^9 \le a_i \le 10^9$) — elements of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the minimal possible number of inversions in the deque after executing the described algorithm. -----Examples----- Input 6 4 3 7 5 5 3 3 2 1 3 3 1 2 4 -1 2 2 -1 4 4 5 1 3 5 1 3 1 3 2 Output 2 0 1 0 1 2 -----Note----- One of the ways to get the sequence $[5, 3, 7, 5]$ in the deque, containing only two inversions, from the initial array $[3, 7, 5, 5]$ (the first sample test case) is described in the problem statement. Also, in this example, you could get the answer of two inversions by simply putting each element of the original array at the end of the deque. In this case, the original sequence $[3, 7, 5, 5]$, also containing exactly two inversions, will be in the deque as-is.
import sys input = sys.stdin.readline def get_merge(l, r, n, a, res): if l == r: return [l] mid = (l + r) // 2 left = get_merge(l, mid, n, a, res) right = get_merge(mid + 1, r, n, a, res) ind = [] while True: if left and right: if a[left[-1]] > a[right[-1]]: res[left[-1]] += len(right) ind.append(left.pop()) else: ind.append(right.pop()) elif not right and left: ind.append(left.pop()) elif not left and right: ind.append(right.pop()) else: break return ind[::-1] for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) smaller_ = [0] * n get_merge(0, n - 1, n, a[::-1], smaller_) smaller_ = smaller_[::-1] larger_ = [0] * n for i in range(n): a[i] = -1 * a[i] get_merge(0, n - 1, n, a[::-1], larger_) larger_ = larger_[::-1] ans = 0 for i in range(n): ans += min(smaller_[i], larger_[i]) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR VAR RETURN LIST VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR LIST WHILE NUMBER IF VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
In fact, the problems E1 and E2 do not have much in common. You should probably think of them as two separate problems. You are given an integer array $a[1 \ldots n] = [a_1, a_2, \ldots, a_n]$. Let us consider an empty deque (double-ended queue). A deque is a data structure that supports adding elements to both the beginning and the end. So, if there are elements $[3, 4, 4]$ currently in the deque, adding an element $1$ to the beginning will produce the sequence $[{1}, 3, 4, 4]$, and adding the same element to the end will produce $[3, 4, 4, {1}]$. The elements of the array are sequentially added to the initially empty deque, starting with $a_1$ and finishing with $a_n$. Before adding each element to the deque, you may choose whether to add it to the beginning or to the end. For example, if we consider an array $a = [3, 7, 5, 5]$, one of the possible sequences of actions looks like this: $\quad$ 1. add $3$ to the beginning of the deque: deque has a sequence $[{3}]$ in it; $\quad$ 2. add $7$ to the end of the deque: deque has a sequence $[3, {7}]$ in it; $\quad$ 3. add $5$ to the end of the deque: deque has a sequence $[3, 7, {5}]$ in it; $\quad$ 4. add $5$ to the beginning of the deque: deque has a sequence $[{5}, 3, 7, 5]$ in it; Find the minimal possible number of inversions in the deque after the whole array is processed. An inversion in sequence $d$ is a pair of indices $(i, j)$ such that $i < j$ and $d_i > d_j$. For example, the array $d = [5, 3, 7, 5]$ has exactly two inversions — $(1, 2)$ and $(3, 4)$, since $d_1 = 5 > 3 = d_2$ and $d_3 = 7 > 5 = d_4$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The next $2t$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — array size. The second line of the description contains $n$ space-separated integers $a_i$ ($-10^9 \le a_i \le 10^9$) — elements of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the minimal possible number of inversions in the deque after executing the described algorithm. -----Examples----- Input 6 4 3 7 5 5 3 3 2 1 3 3 1 2 4 -1 2 2 -1 4 4 5 1 3 5 1 3 1 3 2 Output 2 0 1 0 1 2 -----Note----- One of the ways to get the sequence $[5, 3, 7, 5]$ in the deque, containing only two inversions, from the initial array $[3, 7, 5, 5]$ (the first sample test case) is described in the problem statement. Also, in this example, you could get the answer of two inversions by simply putting each element of the original array at the end of the deque. In this case, the original sequence $[3, 7, 5, 5]$, also containing exactly two inversions, will be in the deque as-is.
from sys import stdin, stdout def update_bit(i, bit_a, v): while i < len(bit_a): bit_a[i] += v i += i & -i def query_bit(i, bit_a): r = 0 while i > 0: r += bit_a[i] i -= i & -i return r def solve(n, a_a): sa_a = list(set(a_a)) sa_a.sort() dic = {} for i in range(1, len(sa_a) + 1): dic[sa_a[i - 1]] = i res = 0 ttl = 0 bit_a = [0] * (len(sa_a) + 1) for a in a_a: idx = dic[a] res += min(query_bit(idx - 1, bit_a), ttl - query_bit(idx, bit_a)) ttl += 1 update_bit(idx, bit_a, 1) return res t = int(stdin.readline()) for _ in range(t): n = int(stdin.readline()) a_a = list(map(int, stdin.readline().split())) res = solve(n, a_a) stdout.write(str(res) + "\n")
FUNC_DEF WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
In fact, the problems E1 and E2 do not have much in common. You should probably think of them as two separate problems. You are given an integer array $a[1 \ldots n] = [a_1, a_2, \ldots, a_n]$. Let us consider an empty deque (double-ended queue). A deque is a data structure that supports adding elements to both the beginning and the end. So, if there are elements $[3, 4, 4]$ currently in the deque, adding an element $1$ to the beginning will produce the sequence $[{1}, 3, 4, 4]$, and adding the same element to the end will produce $[3, 4, 4, {1}]$. The elements of the array are sequentially added to the initially empty deque, starting with $a_1$ and finishing with $a_n$. Before adding each element to the deque, you may choose whether to add it to the beginning or to the end. For example, if we consider an array $a = [3, 7, 5, 5]$, one of the possible sequences of actions looks like this: $\quad$ 1. add $3$ to the beginning of the deque: deque has a sequence $[{3}]$ in it; $\quad$ 2. add $7$ to the end of the deque: deque has a sequence $[3, {7}]$ in it; $\quad$ 3. add $5$ to the end of the deque: deque has a sequence $[3, 7, {5}]$ in it; $\quad$ 4. add $5$ to the beginning of the deque: deque has a sequence $[{5}, 3, 7, 5]$ in it; Find the minimal possible number of inversions in the deque after the whole array is processed. An inversion in sequence $d$ is a pair of indices $(i, j)$ such that $i < j$ and $d_i > d_j$. For example, the array $d = [5, 3, 7, 5]$ has exactly two inversions — $(1, 2)$ and $(3, 4)$, since $d_1 = 5 > 3 = d_2$ and $d_3 = 7 > 5 = d_4$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The next $2t$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — array size. The second line of the description contains $n$ space-separated integers $a_i$ ($-10^9 \le a_i \le 10^9$) — elements of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the minimal possible number of inversions in the deque after executing the described algorithm. -----Examples----- Input 6 4 3 7 5 5 3 3 2 1 3 3 1 2 4 -1 2 2 -1 4 4 5 1 3 5 1 3 1 3 2 Output 2 0 1 0 1 2 -----Note----- One of the ways to get the sequence $[5, 3, 7, 5]$ in the deque, containing only two inversions, from the initial array $[3, 7, 5, 5]$ (the first sample test case) is described in the problem statement. Also, in this example, you could get the answer of two inversions by simply putting each element of the original array at the end of the deque. In this case, the original sequence $[3, 7, 5, 5]$, also containing exactly two inversions, will be in the deque as-is.
import sys input = sys.stdin.readline t = int(input()) for tests in range(t): n = int(input()) A = list(map(int, input().split())) compression_dict = {a: (ind + 2) for ind, a in enumerate(sorted(set(A)))} A = [compression_dict[a] for a in A] LEN = len(A) + 10 BIT = [0] * (LEN + 1) def update(v, w): while v <= LEN: BIT[v] += w v += v & -v def getvalue(v): ANS = 0 while v != 0: ANS += BIT[v] v -= v & -v return ANS ALL = 0 ANS = 0 for a in A: x = getvalue(a - 1) y = getvalue(a) z = ALL - y ANS += min(x, z) update(a, 1) ALL += 1 print(ANS)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
In fact, the problems E1 and E2 do not have much in common. You should probably think of them as two separate problems. You are given an integer array $a[1 \ldots n] = [a_1, a_2, \ldots, a_n]$. Let us consider an empty deque (double-ended queue). A deque is a data structure that supports adding elements to both the beginning and the end. So, if there are elements $[3, 4, 4]$ currently in the deque, adding an element $1$ to the beginning will produce the sequence $[{1}, 3, 4, 4]$, and adding the same element to the end will produce $[3, 4, 4, {1}]$. The elements of the array are sequentially added to the initially empty deque, starting with $a_1$ and finishing with $a_n$. Before adding each element to the deque, you may choose whether to add it to the beginning or to the end. For example, if we consider an array $a = [3, 7, 5, 5]$, one of the possible sequences of actions looks like this: $\quad$ 1. add $3$ to the beginning of the deque: deque has a sequence $[{3}]$ in it; $\quad$ 2. add $7$ to the end of the deque: deque has a sequence $[3, {7}]$ in it; $\quad$ 3. add $5$ to the end of the deque: deque has a sequence $[3, 7, {5}]$ in it; $\quad$ 4. add $5$ to the beginning of the deque: deque has a sequence $[{5}, 3, 7, 5]$ in it; Find the minimal possible number of inversions in the deque after the whole array is processed. An inversion in sequence $d$ is a pair of indices $(i, j)$ such that $i < j$ and $d_i > d_j$. For example, the array $d = [5, 3, 7, 5]$ has exactly two inversions — $(1, 2)$ and $(3, 4)$, since $d_1 = 5 > 3 = d_2$ and $d_3 = 7 > 5 = d_4$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The next $2t$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — array size. The second line of the description contains $n$ space-separated integers $a_i$ ($-10^9 \le a_i \le 10^9$) — elements of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the minimal possible number of inversions in the deque after executing the described algorithm. -----Examples----- Input 6 4 3 7 5 5 3 3 2 1 3 3 1 2 4 -1 2 2 -1 4 4 5 1 3 5 1 3 1 3 2 Output 2 0 1 0 1 2 -----Note----- One of the ways to get the sequence $[5, 3, 7, 5]$ in the deque, containing only two inversions, from the initial array $[3, 7, 5, 5]$ (the first sample test case) is described in the problem statement. Also, in this example, you could get the answer of two inversions by simply putting each element of the original array at the end of the deque. In this case, the original sequence $[3, 7, 5, 5]$, also containing exactly two inversions, will be in the deque as-is.
import sys input = sys.stdin.buffer.readline def queries(l, r): count = 0 while l <= r: if l % 2 == 1: count += sgn_tree[l] l += 1 if r % 2 == 0: count += sgn_tree[r] r -= 1 l = l >> 1 r = r >> 1 return count def update(indx): sgn_tree[indx] += 1 new_indx = indx >> 1 while new_indx >= 1: sgn_tree[new_indx] = sgn_tree[2 * new_indx] + sgn_tree[2 * new_indx + 1] new_indx = new_indx >> 1 for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) brr = arr[:] arr.sort() count = 0 d = {} for i in range(n): if arr[i] not in d: d[arr[i]] = count count += 1 sgn_tree = [(0) for i in range(2 * n)] ans = 0 for i in range(n): left_tree = queries(n, d[brr[i]] + n - 1) right_tree = queries(d[brr[i]] + 1 + n, 2 * n - 1) if left_tree <= right_tree: ans += left_tree else: ans += right_tree update(d[brr[i]] + n) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
In fact, the problems E1 and E2 do not have much in common. You should probably think of them as two separate problems. You are given an integer array $a[1 \ldots n] = [a_1, a_2, \ldots, a_n]$. Let us consider an empty deque (double-ended queue). A deque is a data structure that supports adding elements to both the beginning and the end. So, if there are elements $[3, 4, 4]$ currently in the deque, adding an element $1$ to the beginning will produce the sequence $[{1}, 3, 4, 4]$, and adding the same element to the end will produce $[3, 4, 4, {1}]$. The elements of the array are sequentially added to the initially empty deque, starting with $a_1$ and finishing with $a_n$. Before adding each element to the deque, you may choose whether to add it to the beginning or to the end. For example, if we consider an array $a = [3, 7, 5, 5]$, one of the possible sequences of actions looks like this: $\quad$ 1. add $3$ to the beginning of the deque: deque has a sequence $[{3}]$ in it; $\quad$ 2. add $7$ to the end of the deque: deque has a sequence $[3, {7}]$ in it; $\quad$ 3. add $5$ to the end of the deque: deque has a sequence $[3, 7, {5}]$ in it; $\quad$ 4. add $5$ to the beginning of the deque: deque has a sequence $[{5}, 3, 7, 5]$ in it; Find the minimal possible number of inversions in the deque after the whole array is processed. An inversion in sequence $d$ is a pair of indices $(i, j)$ such that $i < j$ and $d_i > d_j$. For example, the array $d = [5, 3, 7, 5]$ has exactly two inversions — $(1, 2)$ and $(3, 4)$, since $d_1 = 5 > 3 = d_2$ and $d_3 = 7 > 5 = d_4$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The next $2t$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — array size. The second line of the description contains $n$ space-separated integers $a_i$ ($-10^9 \le a_i \le 10^9$) — elements of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the minimal possible number of inversions in the deque after executing the described algorithm. -----Examples----- Input 6 4 3 7 5 5 3 3 2 1 3 3 1 2 4 -1 2 2 -1 4 4 5 1 3 5 1 3 1 3 2 Output 2 0 1 0 1 2 -----Note----- One of the ways to get the sequence $[5, 3, 7, 5]$ in the deque, containing only two inversions, from the initial array $[3, 7, 5, 5]$ (the first sample test case) is described in the problem statement. Also, in this example, you could get the answer of two inversions by simply putting each element of the original array at the end of the deque. In this case, the original sequence $[3, 7, 5, 5]$, also containing exactly two inversions, will be in the deque as-is.
class FenwickTree: def __init__(self, a): self.bit = [0] * len(a) for i, x in enumerate(a): if x: self.add(i, x) def query(self, i): s = 0 bit = self.bit while i >= 0: s += bit[i] i = (i & i + 1) - 1 return s def add(self, i, delta=1): n = len(self.bit) bit = self.bit while i < n: bit[i] += delta i |= i + 1 for _ in range(int(input())): input() a = list(map(int, input().split())) order = {} i = 0 for x in sorted(a): if x not in order: order[x] = i i += 1 inversions = 0 tree = FenwickTree([0] * len(order)) for i, k in enumerate(map(order.__getitem__, a)): inversions += min(tree.query(k - 1), i - tree.query(k)) tree.add(k) print(inversions)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
In fact, the problems E1 and E2 do not have much in common. You should probably think of them as two separate problems. You are given an integer array $a[1 \ldots n] = [a_1, a_2, \ldots, a_n]$. Let us consider an empty deque (double-ended queue). A deque is a data structure that supports adding elements to both the beginning and the end. So, if there are elements $[3, 4, 4]$ currently in the deque, adding an element $1$ to the beginning will produce the sequence $[{1}, 3, 4, 4]$, and adding the same element to the end will produce $[3, 4, 4, {1}]$. The elements of the array are sequentially added to the initially empty deque, starting with $a_1$ and finishing with $a_n$. Before adding each element to the deque, you may choose whether to add it to the beginning or to the end. For example, if we consider an array $a = [3, 7, 5, 5]$, one of the possible sequences of actions looks like this: $\quad$ 1. add $3$ to the beginning of the deque: deque has a sequence $[{3}]$ in it; $\quad$ 2. add $7$ to the end of the deque: deque has a sequence $[3, {7}]$ in it; $\quad$ 3. add $5$ to the end of the deque: deque has a sequence $[3, 7, {5}]$ in it; $\quad$ 4. add $5$ to the beginning of the deque: deque has a sequence $[{5}, 3, 7, 5]$ in it; Find the minimal possible number of inversions in the deque after the whole array is processed. An inversion in sequence $d$ is a pair of indices $(i, j)$ such that $i < j$ and $d_i > d_j$. For example, the array $d = [5, 3, 7, 5]$ has exactly two inversions — $(1, 2)$ and $(3, 4)$, since $d_1 = 5 > 3 = d_2$ and $d_3 = 7 > 5 = d_4$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The next $2t$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — array size. The second line of the description contains $n$ space-separated integers $a_i$ ($-10^9 \le a_i \le 10^9$) — elements of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the minimal possible number of inversions in the deque after executing the described algorithm. -----Examples----- Input 6 4 3 7 5 5 3 3 2 1 3 3 1 2 4 -1 2 2 -1 4 4 5 1 3 5 1 3 1 3 2 Output 2 0 1 0 1 2 -----Note----- One of the ways to get the sequence $[5, 3, 7, 5]$ in the deque, containing only two inversions, from the initial array $[3, 7, 5, 5]$ (the first sample test case) is described in the problem statement. Also, in this example, you could get the answer of two inversions by simply putting each element of the original array at the end of the deque. In this case, the original sequence $[3, 7, 5, 5]$, also containing exactly two inversions, will be in the deque as-is.
import sys input = sys.stdin.readline def update(inp, add, ar): global n while inp < n + 1: ar[inp] += add inp += inp & -inp def summation(inp, ar): ans = 0 while inp: ans += ar[inp] inp -= inp & -inp return ans for _ in range(int(input())): n = int(input()) br = list(map(int, input().split())) li = list(set(br)) li.sort() dic = {} for i in range(len(li)): dic[li[i]] = i sort_ind = [(dic[i] + 1) for i in br] ar = [0] * (n + 1) ans = 0 update(sort_ind[0], 1, ar) for i in range(1, n): sind = sort_ind[i] tot = i sm = summation(sind - 1, ar) la = tot - summation(sind, ar) ans += min(sm, la) update(sind, 1, ar) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
In fact, the problems E1 and E2 do not have much in common. You should probably think of them as two separate problems. You are given an integer array $a[1 \ldots n] = [a_1, a_2, \ldots, a_n]$. Let us consider an empty deque (double-ended queue). A deque is a data structure that supports adding elements to both the beginning and the end. So, if there are elements $[3, 4, 4]$ currently in the deque, adding an element $1$ to the beginning will produce the sequence $[{1}, 3, 4, 4]$, and adding the same element to the end will produce $[3, 4, 4, {1}]$. The elements of the array are sequentially added to the initially empty deque, starting with $a_1$ and finishing with $a_n$. Before adding each element to the deque, you may choose whether to add it to the beginning or to the end. For example, if we consider an array $a = [3, 7, 5, 5]$, one of the possible sequences of actions looks like this: $\quad$ 1. add $3$ to the beginning of the deque: deque has a sequence $[{3}]$ in it; $\quad$ 2. add $7$ to the end of the deque: deque has a sequence $[3, {7}]$ in it; $\quad$ 3. add $5$ to the end of the deque: deque has a sequence $[3, 7, {5}]$ in it; $\quad$ 4. add $5$ to the beginning of the deque: deque has a sequence $[{5}, 3, 7, 5]$ in it; Find the minimal possible number of inversions in the deque after the whole array is processed. An inversion in sequence $d$ is a pair of indices $(i, j)$ such that $i < j$ and $d_i > d_j$. For example, the array $d = [5, 3, 7, 5]$ has exactly two inversions — $(1, 2)$ and $(3, 4)$, since $d_1 = 5 > 3 = d_2$ and $d_3 = 7 > 5 = d_4$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The next $2t$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — array size. The second line of the description contains $n$ space-separated integers $a_i$ ($-10^9 \le a_i \le 10^9$) — elements of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the minimal possible number of inversions in the deque after executing the described algorithm. -----Examples----- Input 6 4 3 7 5 5 3 3 2 1 3 3 1 2 4 -1 2 2 -1 4 4 5 1 3 5 1 3 1 3 2 Output 2 0 1 0 1 2 -----Note----- One of the ways to get the sequence $[5, 3, 7, 5]$ in the deque, containing only two inversions, from the initial array $[3, 7, 5, 5]$ (the first sample test case) is described in the problem statement. Also, in this example, you could get the answer of two inversions by simply putting each element of the original array at the end of the deque. In this case, the original sequence $[3, 7, 5, 5]$, also containing exactly two inversions, will be in the deque as-is.
import sys for _ in range(int(sys.stdin.readline())): n = int(sys.stdin.readline()) d = [int(i) for i in sys.stdin.readline().split()][:n] s = sorted(d) dd, last = {}, 1 for i in s: if not i in dd: dd[i] = last last += 1 d = [dd[i] for i in d] a = [0] * (n + 1) def upd(x): while x <= n: a[x] += 1 x += x & -x def qry(x): r = 0 while x > 0: r += a[x] x -= x & -x return r r = 0 dd = [0] * (n + 1) for i in range(n): q = qry(d[i]) r += min(q - dd[d[i]], i - q) dd[d[i]] += 1 upd(d[i]) sys.stdout.write("%d\n" % r)
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR DICT NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF WHILE VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR
In fact, the problems E1 and E2 do not have much in common. You should probably think of them as two separate problems. You are given an integer array $a[1 \ldots n] = [a_1, a_2, \ldots, a_n]$. Let us consider an empty deque (double-ended queue). A deque is a data structure that supports adding elements to both the beginning and the end. So, if there are elements $[3, 4, 4]$ currently in the deque, adding an element $1$ to the beginning will produce the sequence $[{1}, 3, 4, 4]$, and adding the same element to the end will produce $[3, 4, 4, {1}]$. The elements of the array are sequentially added to the initially empty deque, starting with $a_1$ and finishing with $a_n$. Before adding each element to the deque, you may choose whether to add it to the beginning or to the end. For example, if we consider an array $a = [3, 7, 5, 5]$, one of the possible sequences of actions looks like this: $\quad$ 1. add $3$ to the beginning of the deque: deque has a sequence $[{3}]$ in it; $\quad$ 2. add $7$ to the end of the deque: deque has a sequence $[3, {7}]$ in it; $\quad$ 3. add $5$ to the end of the deque: deque has a sequence $[3, 7, {5}]$ in it; $\quad$ 4. add $5$ to the beginning of the deque: deque has a sequence $[{5}, 3, 7, 5]$ in it; Find the minimal possible number of inversions in the deque after the whole array is processed. An inversion in sequence $d$ is a pair of indices $(i, j)$ such that $i < j$ and $d_i > d_j$. For example, the array $d = [5, 3, 7, 5]$ has exactly two inversions — $(1, 2)$ and $(3, 4)$, since $d_1 = 5 > 3 = d_2$ and $d_3 = 7 > 5 = d_4$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The next $2t$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — array size. The second line of the description contains $n$ space-separated integers $a_i$ ($-10^9 \le a_i \le 10^9$) — elements of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the minimal possible number of inversions in the deque after executing the described algorithm. -----Examples----- Input 6 4 3 7 5 5 3 3 2 1 3 3 1 2 4 -1 2 2 -1 4 4 5 1 3 5 1 3 1 3 2 Output 2 0 1 0 1 2 -----Note----- One of the ways to get the sequence $[5, 3, 7, 5]$ in the deque, containing only two inversions, from the initial array $[3, 7, 5, 5]$ (the first sample test case) is described in the problem statement. Also, in this example, you could get the answer of two inversions by simply putting each element of the original array at the end of the deque. In this case, the original sequence $[3, 7, 5, 5]$, also containing exactly two inversions, will be in the deque as-is.
for i in range(int(input())): n = int(input()) a = list(map(int, input().split())) z = a.copy() z.sort() p = dict() s = 0 for i in z: if i not in p: p[i] = s s += 1 C = [0] * s F = [0] * s r = 0 for i in range(n): v = a[i] c = 0 x = p[v] while x >= 0: c += F[x] x = (x & x + 1) - 1 x = p[v] r += min(c - C[x], i - c) C[x] += 1 while x < s: F[x] += 1 x = x | x + 1 print(r)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
In fact, the problems E1 and E2 do not have much in common. You should probably think of them as two separate problems. You are given an integer array $a[1 \ldots n] = [a_1, a_2, \ldots, a_n]$. Let us consider an empty deque (double-ended queue). A deque is a data structure that supports adding elements to both the beginning and the end. So, if there are elements $[3, 4, 4]$ currently in the deque, adding an element $1$ to the beginning will produce the sequence $[{1}, 3, 4, 4]$, and adding the same element to the end will produce $[3, 4, 4, {1}]$. The elements of the array are sequentially added to the initially empty deque, starting with $a_1$ and finishing with $a_n$. Before adding each element to the deque, you may choose whether to add it to the beginning or to the end. For example, if we consider an array $a = [3, 7, 5, 5]$, one of the possible sequences of actions looks like this: $\quad$ 1. add $3$ to the beginning of the deque: deque has a sequence $[{3}]$ in it; $\quad$ 2. add $7$ to the end of the deque: deque has a sequence $[3, {7}]$ in it; $\quad$ 3. add $5$ to the end of the deque: deque has a sequence $[3, 7, {5}]$ in it; $\quad$ 4. add $5$ to the beginning of the deque: deque has a sequence $[{5}, 3, 7, 5]$ in it; Find the minimal possible number of inversions in the deque after the whole array is processed. An inversion in sequence $d$ is a pair of indices $(i, j)$ such that $i < j$ and $d_i > d_j$. For example, the array $d = [5, 3, 7, 5]$ has exactly two inversions — $(1, 2)$ and $(3, 4)$, since $d_1 = 5 > 3 = d_2$ and $d_3 = 7 > 5 = d_4$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The next $2t$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — array size. The second line of the description contains $n$ space-separated integers $a_i$ ($-10^9 \le a_i \le 10^9$) — elements of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the minimal possible number of inversions in the deque after executing the described algorithm. -----Examples----- Input 6 4 3 7 5 5 3 3 2 1 3 3 1 2 4 -1 2 2 -1 4 4 5 1 3 5 1 3 1 3 2 Output 2 0 1 0 1 2 -----Note----- One of the ways to get the sequence $[5, 3, 7, 5]$ in the deque, containing only two inversions, from the initial array $[3, 7, 5, 5]$ (the first sample test case) is described in the problem statement. Also, in this example, you could get the answer of two inversions by simply putting each element of the original array at the end of the deque. In this case, the original sequence $[3, 7, 5, 5]$, also containing exactly two inversions, will be in the deque as-is.
import sys input = sys.stdin.readline class BIT: n = None A = None def __init__(self, n): self.n = n self.A = [0] * (n + 5) def update(self, x, val): while x <= self.n: self.A[x] += val x += x & -x def que(self, x): if x == 0: return 0 else: return self.A[x] + self.que(x - (x & -x)) def query(self, x, y): return self.que(y) - self.que(x - 1) t = int(input()) for nt in range(t): n = int(input()) A = [-1000000000000000.0] + list(map(int, input().split())) B = sorted(list(set(A))) for i in range(1, n + 1): kir, kan = 1, len(B) - 1 while kir < kan: mid = (kir + kan) // 2 if A[i] > B[mid]: kir = mid + 1 else: kan = mid A[i] = kir ft = BIT(n) res = 0 for x in A[1:]: mn = min(ft.query(1, x - 1), ft.query(x + 1, n)) res += mn ft.update(x, 1) print(res)
IMPORT ASSIGN VAR VAR CLASS_DEF ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
In fact, the problems E1 and E2 do not have much in common. You should probably think of them as two separate problems. You are given an integer array $a[1 \ldots n] = [a_1, a_2, \ldots, a_n]$. Let us consider an empty deque (double-ended queue). A deque is a data structure that supports adding elements to both the beginning and the end. So, if there are elements $[3, 4, 4]$ currently in the deque, adding an element $1$ to the beginning will produce the sequence $[{1}, 3, 4, 4]$, and adding the same element to the end will produce $[3, 4, 4, {1}]$. The elements of the array are sequentially added to the initially empty deque, starting with $a_1$ and finishing with $a_n$. Before adding each element to the deque, you may choose whether to add it to the beginning or to the end. For example, if we consider an array $a = [3, 7, 5, 5]$, one of the possible sequences of actions looks like this: $\quad$ 1. add $3$ to the beginning of the deque: deque has a sequence $[{3}]$ in it; $\quad$ 2. add $7$ to the end of the deque: deque has a sequence $[3, {7}]$ in it; $\quad$ 3. add $5$ to the end of the deque: deque has a sequence $[3, 7, {5}]$ in it; $\quad$ 4. add $5$ to the beginning of the deque: deque has a sequence $[{5}, 3, 7, 5]$ in it; Find the minimal possible number of inversions in the deque after the whole array is processed. An inversion in sequence $d$ is a pair of indices $(i, j)$ such that $i < j$ and $d_i > d_j$. For example, the array $d = [5, 3, 7, 5]$ has exactly two inversions — $(1, 2)$ and $(3, 4)$, since $d_1 = 5 > 3 = d_2$ and $d_3 = 7 > 5 = d_4$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The next $2t$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — array size. The second line of the description contains $n$ space-separated integers $a_i$ ($-10^9 \le a_i \le 10^9$) — elements of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the minimal possible number of inversions in the deque after executing the described algorithm. -----Examples----- Input 6 4 3 7 5 5 3 3 2 1 3 3 1 2 4 -1 2 2 -1 4 4 5 1 3 5 1 3 1 3 2 Output 2 0 1 0 1 2 -----Note----- One of the ways to get the sequence $[5, 3, 7, 5]$ in the deque, containing only two inversions, from the initial array $[3, 7, 5, 5]$ (the first sample test case) is described in the problem statement. Also, in this example, you could get the answer of two inversions by simply putting each element of the original array at the end of the deque. In this case, the original sequence $[3, 7, 5, 5]$, also containing exactly two inversions, will be in the deque as-is.
from sys import stdin input = stdin.readline class segtree: def __init__(self, n): self.st = [0] * (4 * n) self.size = n - 1 def update(self, ind, value): l, h = 0, self.size i = 0 while l < h: mid = (l + h) // 2 if ind > mid: l = mid + 1 i = 2 * i + 2 else: i = 2 * i + 1 h = mid self.st[i] += value while i > 0: i = (i - 1) // 2 self.st[i] = self.st[2 * i + 1] + self.st[2 * i + 2] def query(self, x, y, l=0, h=-1, i=0): if h == -1: h = self.size if l >= x and h <= y: return self.st[i] if l > y or h < x: return 0 mid = (l + h) // 2 left = self.query(x, y, l, mid, 2 * i + 1) right = self.query(x, y, mid + 1, h, 2 * i + 2) return left + right def answer(): x = [[a[i], i] for i in range(n)] x.sort() st = segtree(n) ans, prev = 0, 10000000000.0 for i in range(n): if prev == x[i][0]: same += 1 else: prev = x[i][0] same = 0 less = st.query(0, x[i][1]) - same greater = x[i][1] - less - same ans += min(less, greater) st.update(x[i][1], 1) return ans for T in range(int(input())): n = int(input()) a = list(map(int, input().split())) print(answer())
ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
In fact, the problems E1 and E2 do not have much in common. You should probably think of them as two separate problems. You are given an integer array $a[1 \ldots n] = [a_1, a_2, \ldots, a_n]$. Let us consider an empty deque (double-ended queue). A deque is a data structure that supports adding elements to both the beginning and the end. So, if there are elements $[3, 4, 4]$ currently in the deque, adding an element $1$ to the beginning will produce the sequence $[{1}, 3, 4, 4]$, and adding the same element to the end will produce $[3, 4, 4, {1}]$. The elements of the array are sequentially added to the initially empty deque, starting with $a_1$ and finishing with $a_n$. Before adding each element to the deque, you may choose whether to add it to the beginning or to the end. For example, if we consider an array $a = [3, 7, 5, 5]$, one of the possible sequences of actions looks like this: $\quad$ 1. add $3$ to the beginning of the deque: deque has a sequence $[{3}]$ in it; $\quad$ 2. add $7$ to the end of the deque: deque has a sequence $[3, {7}]$ in it; $\quad$ 3. add $5$ to the end of the deque: deque has a sequence $[3, 7, {5}]$ in it; $\quad$ 4. add $5$ to the beginning of the deque: deque has a sequence $[{5}, 3, 7, 5]$ in it; Find the minimal possible number of inversions in the deque after the whole array is processed. An inversion in sequence $d$ is a pair of indices $(i, j)$ such that $i < j$ and $d_i > d_j$. For example, the array $d = [5, 3, 7, 5]$ has exactly two inversions — $(1, 2)$ and $(3, 4)$, since $d_1 = 5 > 3 = d_2$ and $d_3 = 7 > 5 = d_4$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The next $2t$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — array size. The second line of the description contains $n$ space-separated integers $a_i$ ($-10^9 \le a_i \le 10^9$) — elements of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the minimal possible number of inversions in the deque after executing the described algorithm. -----Examples----- Input 6 4 3 7 5 5 3 3 2 1 3 3 1 2 4 -1 2 2 -1 4 4 5 1 3 5 1 3 1 3 2 Output 2 0 1 0 1 2 -----Note----- One of the ways to get the sequence $[5, 3, 7, 5]$ in the deque, containing only two inversions, from the initial array $[3, 7, 5, 5]$ (the first sample test case) is described in the problem statement. Also, in this example, you could get the answer of two inversions by simply putting each element of the original array at the end of the deque. In this case, the original sequence $[3, 7, 5, 5]$, also containing exactly two inversions, will be in the deque as-is.
import sys readline = sys.stdin.readline class FenwickTree: def __init__(self, n): self.size = n self.tree = [0] * (n + 1) def sum(self, i): out = 0 while i > 0: out += self.tree[i] i -= i & -i return out def add(self, i, x): while i <= self.size: self.tree[i] += x i += i & -i T = int(readline()) for t in range(T): N = int(readline()) A = list(map(int, readline().split())) za = sorted(list(set(A))) n = len(za) dic = {} for i, z in enumerate(za): dic[z] = i for i in range(len(A)): A[i] = dic[A[i]] + 1 BIT = FenwickTree(n + 1) ans = 0 for a in A: small = BIT.sum(a - 1) large = BIT.sum(n + 1) - BIT.sum(a) if small < large: ans += small else: ans += large BIT.add(a, 1) print(ans)
IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): <image> Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. Input The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. Output Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. Examples Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No Note The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: <image> In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: <image> In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: <image>
str = input() s = [] for c in str: if len(s) > 0 and c == s[-1]: del s[-1] else: s.append(c) if len(s) > 0: print("No") else: print("Yes")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): <image> Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. Input The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. Output Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. Examples Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No Note The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: <image> In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: <image> In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: <image>
list_inp = input() stack_jud = [] for i in list_inp: if len(stack_jud) > 0: if stack_jud[-1] == i: stack_jud.pop() else: stack_jud.append(i) else: stack_jud.append(i) if stack_jud == []: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR LIST EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): <image> Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. Input The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. Output Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. Examples Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No Note The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: <image> In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: <image> In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: <image>
seq = input() stack = [] for elem in seq: stack.append(elem) while len(stack) > 1 and stack[-1] == stack[-2]: stack.pop() stack.pop() if not stack: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): <image> Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. Input The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. Output Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. Examples Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No Note The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: <image> In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: <image> In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: <image>
a = input() if len(a) % 2 == 1: print("No") else: pm = 0 mp = 0 for i in range(len(a))[::2]: t = a[i] + a[i + 1] if t == "+-": pm += 1 if t == "-+": mp += 1 if pm == mp: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): <image> Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. Input The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. Output Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. Examples Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No Note The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: <image> In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: <image> In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: <image>
a = input() b = [] for i in range(len(a)): if b == []: b.append(a[i]) continue if b[-1] == a[i]: b.pop() else: b.append(a[i]) if len(b) == 0: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR LIST EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): <image> Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. Input The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. Output Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. Examples Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No Note The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: <image> In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: <image> In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: <image>
s = input() j, k = [], [] i, t = 0, 0 while i < len(s): while i < len(s) - 1 and s[i] == s[i + 1]: i = i + 2 if i < len(s): j.append(s[i]) i = i + 1 while t < len(j): if len(k) != 0 and k[-1] != j[t] or len(k) == 0: k.append(j[t]) t = t + 1 else: k.pop() t = t + 1 if len(k) == 0: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): <image> Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. Input The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. Output Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. Examples Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No Note The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: <image> In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: <image> In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: <image>
s = input() q = [] q.append(s[0]) for i in s[1:]: if len(q) == 0: q.append(i) elif q[-1] == i: q.pop() else: q.append(i) if len(q) == 0: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again. The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): <image> Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut. To understand the problem better please read the notes to the test samples. Input The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise. Output Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled. Examples Input -++- Output Yes Input +- Output No Input ++ Output Yes Input - Output No Note The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses. In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled: <image> In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher: <image> In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself: <image>
n = input() s = 0 for i in range(len(n)): if n[i] == "-": s += 2 * (i - 2 * (i // 2)) - 1 else: s += -2 * (i - 2 * (i // 2)) + 1 if s == 0: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP BIN_OP NUMBER BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING