description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
for _ in range(int(input())):
s = input()
t = input()
for c in t[::-1]:
i = len(s) - 1
while i >= 0:
if s[i] != c:
i -= 2
else:
s = s[:i]
break
else:
print("NO")
break
else:
print("YES")
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
rint = lambda: int(input())
rli = lambda: list(map(int, input().split()))
rl = lambda: input().split()
def strm(m):
s = ""
for i in range(len(m)):
for j in range(len(m[0])):
s += str(m[i][j])
s += "\n"
return s
def solve():
s = input()
t = input()
if len(s) < len(t):
return "NO"
if s == t:
return "YES"
if s[-len(t) :] == t:
return "YES"
k = len(s) - 1
j = len(t) - 1
while j > -1 and k >= 0:
if s[k] == t[j]:
k -= 1
j -= 1
else:
k -= 2
if j == -1:
return "YES"
return "NO"
res = ""
for test_number in range(1, rint() + 1):
r = solve()
print(r)
res += r + "\n"
|
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 FUNC_CALL VAR FUNC_DEF ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR STRING RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN STRING IF VAR VAR RETURN STRING IF VAR FUNC_CALL VAR VAR VAR RETURN STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN STRING RETURN STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR STRING
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
import sys
input = sys.stdin.buffer.readline
def solve():
si = len(S) - 1
ti = len(T) - 1
while si >= 0 and ti >= 0:
if S[si] != T[ti]:
si -= 2
continue
ti -= 1
si -= 1
return "YES" if ti == -1 else "NO"
test_cases = int(input())
for test_case in range(test_cases):
S = input()
T = input()
print(solve())
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR NUMBER STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
import sys
input = sys.stdin.readline
for _ in range(int(input())):
s = input().strip("\n")
t = input().strip("\n")
i = len(s) - 1
ans = "YES"
j = len(t) - 1
while j >= 0:
if i < 0:
ans = "NO"
break
if s[i] == t[j]:
i -= 1
j -= 1
else:
i -= 2
print(ans)
|
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER ASSIGN VAR STRING IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
ntests = int(input())
for _ in range(ntests):
s = input()
t = input()
ns = len(s)
nt = len(t)
if ns < nt:
print("NO")
else:
answer = "YES"
while nt > 0:
nt -= 1
ns -= 1
while t[nt] != s[ns]:
if ns < 2 or ns < nt:
answer = "NO"
nt = -1
break
ns -= 2
print(answer)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING WHILE VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
import sys
input = sys.stdin.readline
t = int(input())
for tests in range(t):
S = input().strip()
T = input().strip()
if len(T) > len(S):
print("NO")
continue
if T == S[len(S) - len(T) :]:
print("YES")
continue
LEN = len(S)
NEXT = [([-10] * 26) for i in range(LEN + 2)]
for i in range(LEN - 1, -1, -1):
for j in range(26):
NEXT[i][j] = NEXT[i + 2][j]
NEXT[i][ord(S[i]) - 97] = i
NOW = 0
for t in T:
NOW = NEXT[NOW][ord(t) - 97] + 1
if NOW < 0:
break
if NOW >= 0 and (LEN - NOW) % 2 == 0:
print("YES")
continue
NOW = 1
for t in T:
NOW = NEXT[NOW][ord(t) - 97] + 1
if NOW < 0:
break
if NOW >= 1 and (LEN - NOW) % 2 == 0:
print("YES")
continue
print("NO")
|
IMPORT ASSIGN VAR VAR 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 FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
for _ in range(int(input())):
s = input()
t = input()
ss = len(s) - 1
tt = len(t) - 1
flag = 1
while tt >= 0 and flag == 1:
while ss >= 0 and s[ss] != t[tt]:
ss = ss - 2
if ss < 0:
print("NO")
flag = 0
break
ss = ss - 1
tt = tt - 1
if flag == 1:
print("YES")
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
q = int(input())
for x in range(q):
s = list(input()[::-1])
t = list(input()[::-1])
temp = []
it = 0
while it < len(s):
if s[it] == t[len(temp)]:
temp.append(s[it])
else:
it += 1
if temp == t:
break
it += 1
if temp == t:
print("YES")
else:
print("NO")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
def solver(s, t):
si = len(s)
ti = len(t)
while si and ti:
si -= 1
if s[si] == t[ti - 1]:
ti -= 1
elif si:
si -= 1
print("NO" if ti else "YES")
for _ in range(int(input())):
s = input()
t = input()
solver(s, t)
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
def solve():
s = input()
t = input()
n = len(s) - 1
m = len(t) - 1
while n >= 0 and m >= 0:
if s[n] == t[m]:
m -= 1
n -= 1
else:
n -= 2
if m >= 0:
print("NO")
else:
print("YES")
def main():
t = int(input())
for _ in range(t):
solve()
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
def can_type(S, T):
S, T = S[::-1], T[::-1]
s, t = 0, 0
while True:
if s < len(S) and t < len(T) and S[s] == T[t]:
s += 1
t += 1
else:
s += 2
if t == len(T):
return True
if s > len(S):
return False
for k in range(int(input())):
print("YES" if can_type(input(), input()) else "NO")
|
FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR STRING STRING
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
def canSproduceT(s, t):
n = len(s)
m = len(t)
if m == 0:
return "YES"
if n < m:
return "NO"
i = n - 1
j = m - 1
while i >= 0 and j >= 0:
if s[i] == t[j]:
i -= 1
j -= 1
else:
i -= 2
if j == -1:
return "YES"
return "NO"
tests = int(input())
results = []
while tests:
s = input()
t = input()
print(canSproduceT(s, t))
tests -= 1
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN STRING IF VAR VAR RETURN STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
def main():
for _ in range(int(input())):
s, t = list(input()), list(input())
while s and t:
if s[-1] == t[-1]:
del s[-1]
del t[-1]
else:
del s[-2:]
print(("NO", "YES")[not t])
main()
|
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING STRING VAR EXPR FUNC_CALL VAR
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
import sys
input = sys.stdin.readline
for _ in range(int(input())):
s = input()[:-1]
t = input()[:-1]
n, m = len(s), len(t)
i, j = n - 1, m - 1
cnt = 0
while i >= 0 and j >= 0:
while cnt:
i -= 1
cnt -= 1
if i < 0:
break
if s[i] == t[j]:
i -= 1
j -= 1
else:
cnt += 1
i -= 1
print("YES" if j < 0 else "NO")
|
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING STRING
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
from sys import stdin as sin
def aint():
return int(input())
def amap():
return map(int, sin.readline().split())
def alist():
return list(map(int, sin.readline().split()))
def astr():
return input()
for _ in range(aint()):
s = input()
t = input()
if len(s) >= len(t):
j = len(s) - 1
c = 0
i = len(t) - 1
while i >= 0:
if j >= 0:
if s[j] != t[i]:
j -= 2
i += 1
else:
c += 1
j -= 1
else:
break
i -= 1
if j >= -1 and c == len(t):
print("YES")
else:
print("NO")
else:
print("NO")
|
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
import sys
input = sys.stdin.readline
I = lambda: list(map(int, input().split()))
(t,) = I()
for _ in range(t):
s = list(input().strip())
t = list(input().strip())
ns = ""
nt = ""
n = len(s)
m = len(t)
tf = 1
i = n - 1
j = m - 1
while i >= 0 and j >= 0:
if s[i] == t[j]:
i -= 1
j -= 1
nt += t[j]
continue
elif i >= 1:
nt = nt + s[i] + s[i - 1]
i -= 2
else:
tf = 0
break
if not tf:
print("NO")
elif j == -1:
print("YES")
else:
print("NO")
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
def main():
S = input()[::-1]
T = input()[::-1]
Skip = 0
j = 0
for i in range(len(S)):
if Skip:
Skip = 0
elif S[i] == T[j]:
j += 1
if j == len(T):
break
else:
Skip = 1
else:
return print("NO")
print("YES")
T = int(input())
for _ in range(T):
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
def delete_backspace(s: str, t: str) -> bool:
n = len(s)
m = len(t)
if n < m:
return False
i = n - m & 1
j = 0
while i < n:
if j < m and t[j] == s[i]:
j += 1
else:
i += 1
i += 1
return j == m
n_test_cases = int(input())
for _ in range(n_test_cases):
s = input()
t = input()
ans = delete_backspace(s, t)
output = "YES" if ans else "NO"
print(output)
|
FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR STRING STRING EXPR FUNC_CALL VAR VAR
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
def isPosible(s, t):
string = s[::-1]
wanted = t[::-1]
i_string = 0
i_wanted = 0
while i_wanted < len(wanted) and i_string < len(string):
if string[i_string] == wanted[i_wanted]:
i_string += 1
i_wanted += 1
else:
i_string += 2
if i_wanted == len(wanted):
return "YES"
else:
return "NO"
for _ in range(int(input())):
print(isPosible(input(), input()))
|
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
def check(s, t):
s += "$"
t += "$"
j = len(s) - 1
for i in range(len(t) - 1, -1, -1):
while j >= 0 and s[j] != t[i]:
j -= 2
if j < 0:
return False
j -= 1
return True
def main():
t = int(input())
for _ in range(t):
s = input()
t = input()
if check(s, t):
print("YES")
else:
print("NO")
main()
|
FUNC_DEF VAR STRING VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
t = int(input())
while t > 0:
t -= 1
s = input()
a = input()
last = len(s)
fl = True
j = len(a) - 1
for i in range(len(s) - 1, -1, -1):
if a[j] == s[i]:
if (last - i - 1) % 2 == 0:
last = i
j -= 1
if j < 0:
break
if j < 0:
print("YES")
else:
print("NO")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
t = int(input())
for k in range(t):
s = input()
seq = input()
i, j = len(s) - 1, len(seq) - 1
o = 0
while i >= 0 and o != len(seq):
if s[i] == seq[j]:
i -= 1
j -= 1
o += 1
if s[i] != seq[j]:
i -= 2
if o == len(seq):
print("YES")
else:
print("NO")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
import sys
def solver(source, target):
if len(target) > len(source):
return False
elif len(target) == len(source):
return source == target
elif len(target) == 0:
return True
else:
c_source = len(source) - 1
for i_target in range(len(target) - 1, -1, -1):
found = False
g = target[i_target]
for i_source in range(c_source, -1, -2):
if source[i_source] == g:
c_source = i_source - 1
found = True
break
if not found:
return False
return True
stdin_iter = iter(sys.stdin)
num_tests = int(next(stdin_iter))
for _ in range(num_tests):
source = next(stdin_iter)
target = next(stdin_iter)
print("YES" if solver(source, target) else "NO")
|
IMPORT FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING STRING
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
q = int(input())
def solve(s, t):
m = len(s)
n = len(t)
if m % 2 != n % 2:
s = s[1:]
m -= 1
if m < n:
return False
u = []
cnt = 0
for i in range(m):
if cnt == 0:
u.append(s[i])
cnt += 1
elif cnt == n + 1:
u.pop()
cnt -= 1
elif u[cnt - 1] == t[cnt - 1]:
u.append(s[i])
cnt += 1
elif t[cnt - 1] != u[cnt - 1]:
u.pop()
cnt -= 1
return "".join(u) == t
for ind in range(q):
s = input()
t = input()
print("Yes" if solve(s, t) else "No")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL STRING VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING STRING
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
import sys
input = sys.stdin.readline
def solve():
s = input().strip()
t = input().strip()
n, m = len(s), len(t)
even, odd = -1, -1
for i in range(n):
if t[0] == s[i]:
if i % 2 == 0:
if even == -1:
even = i
elif odd == -1:
odd = i
if even != -1 and odd != -1:
break
def possible(i):
j = 0
L = 0
while i < n:
to_skip = False
if j < m and s[i] == t[j] and L % 2 == 0:
j += 1
L = 0
to_skip = True
i += 1
if not to_skip:
L += 1
return j == m and L % 2 == 0
ans = False
if even != -1:
ans = ans or possible(even)
if odd != -1:
ans = ans or possible(odd)
if ans:
print("YES")
else:
print("NO")
return -1
for nt in range(int(input())):
solve()
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR NUMBER RETURN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
def get_solution(s, t):
n = len(s)
n_2 = len(t)
if n_2 > n:
print("NO")
return
i = n - 1
j = n_2 - 1
while j >= 0 and i >= 0:
if s[i] == t[j]:
i -= 1
j -= 1
else:
i -= 2
if j == -1:
print("YES")
return
print("NO")
for _ in range(int(input())):
get_solution(input(), input())
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
r = int(input())
for _ in range(r):
s = input()
t = input()
try:
for c in t[::-1]:
no = True
i = len(s) - 1
while i >= 0:
if s[i] != c:
i -= 2
else:
s = s[:i]
no = False
break
if no:
print("NO")
break
if not no:
print("YES")
except ValueError:
print("NO")
continue
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING IF VAR EXPR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
for _ in range(int(input())):
s = str(input())
t = str(input())
if len(s) < len(t):
print("NO")
else:
a = len(s) - 1
b = len(t) - 1
while 1:
if a < 0 or b < 0:
break
if s[a] == t[b]:
b -= 1
a -= 1
else:
a -= 2
print("YES" if b < 0 else "NO")
|
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 IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING STRING
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
def run(*tasks):
value = None
def tick():
nonlocal value
task = tasks[-1]
try:
subtask = task.send(value)
tasks.append(subtask)
value = None
except TypeError as error:
if error.args == ("can't send non-None value to a just-started generator",):
subtask = task.send(None)
tasks.append(subtask)
else:
raise error
except StopIteration as stop:
value = stop.value
tasks.pop()
tasks = list(tasks)
while len(tasks) != 0:
tick()
return value
def shortest_walk(keys, target):
if len(target) == 0:
return keys
first, rest = target[0], target[1:]
keys1 = yield shortest_walk(keys, rest)
if keys1 == None:
return None
while keys1 != "":
if keys1[-1] == first:
return keys1[:-1]
keys1 = keys1[:-2]
else:
return None
def shortest_walk_iter(keys, target):
for first in target[::-1]:
while keys != "":
if keys[-1] == first:
keys = keys[:-1]
break
keys = keys[:-2]
else:
return None
return keys
ntests = int(input())
for t in range(ntests):
keys = input()
target = input()
possible = shortest_walk_iter(keys, target) != None
print("YES" if possible else "NO")
|
FUNC_DEF ASSIGN VAR NONE FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NONE VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR NONE EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN NONE WHILE VAR STRING IF VAR NUMBER VAR RETURN VAR NUMBER ASSIGN VAR VAR NUMBER RETURN NONE FUNC_DEF FOR VAR VAR NUMBER WHILE VAR STRING IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NONE EXPR FUNC_CALL VAR VAR STRING STRING
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
from sys import stdin
q = int(stdin.readline())
for loop in range(q):
s = list(stdin.readline()[:-1])
t = list(stdin.readline()[:-1])
while len(s) > 0 and len(t) > 0:
if s[-1] == t[-1]:
del s[-1]
del t[-1]
else:
del s[-1]
if len(s) > 0:
del s[-1]
if len(t) == 0:
print("YES")
else:
print("NO")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
ans = []
for _ in range(int(input())):
s = list(input())
t = list(input())
if len(s) % 2 != len(t) % 2:
s = s[1:]
if len(t) > len(s):
ans.append("NO")
continue
i = 0
j = 0
while i < len(s):
if s[i] == t[j]:
i += 1
j += 1
if j == len(t):
ans.append("YES")
break
else:
i += 2
else:
ans.append("NO")
print("\n".join(ans))
|
ASSIGN VAR LIST 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 IF BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
q = int(input())
for _ in range(q):
s = input()
t = input()
m, n = len(s), len(t)
if n > m:
print("NO")
continue
q = n - 1
k = 0
for i in range(m - 1, -1, -1):
if k == 1:
k = 0
continue
if q >= 0 and s[i] == t[q]:
q -= 1
else:
k += 1
if q == -1:
print("YES")
else:
print("NO")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
def backspace(s, t):
if len(s) < len(t):
return "NO"
if s == t:
return "YES"
if len(s) == len(t) and s != t:
return "NO"
i = len(s) - 1
j = len(t) - 1
while i > -1 and j > -1:
if s[i] == t[j]:
if j == 0:
return "YES"
else:
i -= 1
j -= 1
else:
i -= 2
return "NO"
num_tests = int(input())
for i in range(num_tests):
s = input()
t = input()
print(backspace(s, t))
|
FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN STRING IF VAR VAR RETURN STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF VAR NUMBER RETURN STRING VAR NUMBER VAR NUMBER VAR NUMBER RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
from sys import gettrace, stdin
if gettrace():
def inputi():
return input()
else:
def input():
return next(stdin)[:-1]
def inputi():
return stdin.buffer.readline()
def solve(ss, tt):
dp = [0] * len(ss)
p = (len(ss) - len(tt)) % 2
for t in tt:
while p < len(ss) and ss[p] != t:
p += 2
if p >= len(ss):
break
p += 1
else:
return True
return False
def main():
t = int(input())
sstt = [(input(), input()) for _ in range(t)]
for ss, tt in sstt:
if solve(ss, tt):
print("YES")
else:
print("NO")
main()
|
IF FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
def solve():
s = input()
t = input()
cur = len(s) & 1 ^ len(t) & 1
for i, a in enumerate(t):
while True:
if cur >= len(s):
return "NO"
if s[cur] == a:
break
cur += 2
cur += 1
return "YES"
t = int(input())
for __ in range(t):
print(solve())
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE NUMBER IF VAR FUNC_CALL VAR VAR RETURN STRING IF VAR VAR VAR VAR NUMBER VAR NUMBER RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
for _ in range(int(input())):
s1 = input()
s2 = input()
tr = 0
for i in range(len(s1) - 1, -1, -1):
if s1[i] == s2[-1] and (len(s1) - i - tr) % 2 == 1:
tr += 1
s2 = s2[:-1]
if len(s2) == 0:
break
if len(s2) == 0:
print("YES")
else:
print("NO")
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
def solution(s, t):
s = list(s)
t = list(t)
while len(s) >= len(t):
if not t:
return "YES"
if s[-1] == t[-1]:
s.pop()
t.pop()
else:
s.pop()
if s:
s.pop()
return "NO"
def main():
for _ in range(int(input())):
s = input()
t = input()
print(solution(s, t))
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR RETURN STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR RETURN STRING FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
zzzz = int(input())
for jj in range(zzzz):
s = input()
win = 1
ans = ""
t = input()
if len(s) % 2 != len(t) % 2:
s = s[1:]
i = -1
for k in range(len(s)):
if s[k] == t[0] and k % 2 == 0:
i = k
break
if i == -1:
print("NO")
continue
for j in range(1, len(t)):
if i >= len(s) - 1:
win = 0
break
if s[i + 1] == t[j]:
i = i + 1
continue
if i >= len(s) - 3:
win = 0
break
i = i + 3
while s[i] != t[j] and i < len(s) - 2:
i += 2
if s[i] != t[j]:
win = 0
break
if win == 0:
print("NO")
else:
print("YES")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
|
You are given two strings $s$ and $t$, both consisting of lowercase English letters. You are going to type the string $s$ character by character, from the first character to the last one.
When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $s$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $s$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a".
Your task is to determine whether you can obtain the string $t$, if you type the string $s$ and press "Backspace" instead of typing several (maybe zero) characters of $s$.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^5$) β the number of test cases.
The first line of each test case contains the string $s$ ($1 \le |s| \le 10^5$). Each character of $s$ is a lowercase English letter.
The second line of each test case contains the string $t$ ($1 \le |t| \le 10^5$). Each character of $t$ is a lowercase English letter.
It is guaranteed that the total number of characters in the strings over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print "YES" if you can obtain the string $t$ by typing the string $s$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot.
You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
-----Examples-----
Input
4
ababa
ba
ababa
bb
aaa
aaaa
aababa
ababa
Output
YES
NO
NO
YES
-----Note-----
Consider the example test from the statement.
In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters.
There's no way to obtain "bb" while typing "ababa".
There's no way to obtain "aaaa" while typing "aaa".
In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters.
|
def solve(s, t, start):
z = start
cnt = 0
n = len(s)
m = len(t)
latest = n - 1
while z < n and cnt < m:
if s[z] == t[cnt]:
latest = z
z += 1
cnt += 1
else:
z += 2
return cnt == m and (n - latest) % 2
def main():
q = int(input())
for _ in range(q):
s = input()
t = input()
if solve(s, t, 0) or solve(s, t, 1):
print("YES")
else:
print("NO")
main()
|
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
|
Read problems statements in English, Mandarin Chinese and Russian as well.
Given an array A_{1},A_{2}...A_{N}, you have to print the size of the largest contiguous subarray such that
LCM of all integers in that subarray is equal to the product of all integers in that subarray.
Formally,
For a subarray A_{i},A_{i+1}...A_{j} where 1 β€ i < j β€ N to be valid: LCM(A_{i},A_{i+1}...A_{j}) should be equal to A_{i}*A_{i+1}*...*A_{j}. You have to print the size of the largest valid subarray.
If no subarray satisfies output -1.
Note:A single element is not considered a subarray according to definition in this problem.
------ Input ------
First line contains T, the number of testcases. Each testcase consists of N in one line followed by N integers in next line.
------ Output ------
For each testcase, print the required answer in one line.
------ Constraints ------
$1 β€ T β€ 50$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{6}$
------ Example ------
Input:
3
2
7 2
4
2 2 3 4
3
2 2 4
Output:
2
2
-1
------ Explanation ------
Example case 1.LCM(2,7)=2*7. Therefore, subarray A_{1} to A_{2} satisfies.
Example case 2.The subarrays A_{2} to A_{3} and A_{3} to A_{4} are the maximum size possible.
Example case 3.No subarray will satisfy.
Warning: Use fast input/output. Large input files. Solutions may not pass in slower languages.
Update: Time limit for python=10s
|
seive = [1] * (10**6 + 1)
prev = [1] * (10**6 + 1)
for i in range(2, len(seive)):
if seive[i] > 1:
continue
t = 1
for j in range(i, len(seive), i):
seive[j] = i
prev[j] = t
t += 1
t = int(input())
for _ in range(t):
n = int(input())
arr = [int(c) for c in input().split()]
v = [0] * (10**6 + 1)
ans = -1
l = 0
for r in range(len(arr)):
j = arr[r]
z = -1
while j > 1:
d = seive[j]
if d != z:
l = max(l, v[d])
v[d] = r + 1
j = prev[j]
z = d
if l < r:
ans = max(ans, r - l + 1)
print(ans)
|
ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR 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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problems statements in English, Mandarin Chinese and Russian as well.
Given an array A_{1},A_{2}...A_{N}, you have to print the size of the largest contiguous subarray such that
LCM of all integers in that subarray is equal to the product of all integers in that subarray.
Formally,
For a subarray A_{i},A_{i+1}...A_{j} where 1 β€ i < j β€ N to be valid: LCM(A_{i},A_{i+1}...A_{j}) should be equal to A_{i}*A_{i+1}*...*A_{j}. You have to print the size of the largest valid subarray.
If no subarray satisfies output -1.
Note:A single element is not considered a subarray according to definition in this problem.
------ Input ------
First line contains T, the number of testcases. Each testcase consists of N in one line followed by N integers in next line.
------ Output ------
For each testcase, print the required answer in one line.
------ Constraints ------
$1 β€ T β€ 50$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{6}$
------ Example ------
Input:
3
2
7 2
4
2 2 3 4
3
2 2 4
Output:
2
2
-1
------ Explanation ------
Example case 1.LCM(2,7)=2*7. Therefore, subarray A_{1} to A_{2} satisfies.
Example case 2.The subarrays A_{2} to A_{3} and A_{3} to A_{4} are the maximum size possible.
Example case 3.No subarray will satisfy.
Warning: Use fast input/output. Large input files. Solutions may not pass in slower languages.
Update: Time limit for python=10s
|
p = 10**6 + 5
def sieve():
l = [1] * p
i = 2
l[0] = l[1] = 0
spf = [i for i in range(p)]
while i * i <= p:
if l[i]:
for j in range(2 * i, p, i):
l[j] = 0
if spf[j] == j:
spf[j] = i
i += 1
return spf, [i for i in range(p) if l[i]]
spf, prms = sieve()
def get(x):
s = set()
while x > 1:
s.add(spf[x])
x = x // spf[x]
return s
for _ in range(int(input())):
n = int(input())
prev = {}
for x in prms:
prev[x] = -1
l = [int(i) for i in input().split()]
maxi = 0
curr = -1
for i in range(n):
g = get(l[i])
for pf in g:
curr = max(curr, prev[pf])
if curr == -1:
maxi = max(maxi, i - curr)
else:
maxi = max(maxi, i - curr)
for pf in g:
prev[pf] = i
print(maxi if maxi > 1 else -1)
|
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP 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 DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
|
Read problems statements in English, Mandarin Chinese and Russian as well.
Given an array A_{1},A_{2}...A_{N}, you have to print the size of the largest contiguous subarray such that
LCM of all integers in that subarray is equal to the product of all integers in that subarray.
Formally,
For a subarray A_{i},A_{i+1}...A_{j} where 1 β€ i < j β€ N to be valid: LCM(A_{i},A_{i+1}...A_{j}) should be equal to A_{i}*A_{i+1}*...*A_{j}. You have to print the size of the largest valid subarray.
If no subarray satisfies output -1.
Note:A single element is not considered a subarray according to definition in this problem.
------ Input ------
First line contains T, the number of testcases. Each testcase consists of N in one line followed by N integers in next line.
------ Output ------
For each testcase, print the required answer in one line.
------ Constraints ------
$1 β€ T β€ 50$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{6}$
------ Example ------
Input:
3
2
7 2
4
2 2 3 4
3
2 2 4
Output:
2
2
-1
------ Explanation ------
Example case 1.LCM(2,7)=2*7. Therefore, subarray A_{1} to A_{2} satisfies.
Example case 2.The subarrays A_{2} to A_{3} and A_{3} to A_{4} are the maximum size possible.
Example case 3.No subarray will satisfy.
Warning: Use fast input/output. Large input files. Solutions may not pass in slower languages.
Update: Time limit for python=10s
|
import sys
maxd = pow(10, 6) + 1
factors = [[] for i in range(maxd)]
for i in range(2, maxd):
if len(factors[i]) == 0:
for j in range(i, maxd, i):
factors[j].append(i)
t = int(sys.stdin.readline())
for _ in range(t):
n = int(sys.stdin.readline())
arr = list(map(int, sys.stdin.readline().split()))
last = [-1] * maxd
start = [-1] * maxd
ans = -1
for i in range(n):
val = arr[i]
x = -1
for fact in factors[val]:
if last[fact] != -1:
x = max(x, last[fact])
last[fact] = i
if i > 0:
start[i] = max(start[i - 1], x)
ans = max(ans, i - start[i])
print(ans if ans > 1 else -1)
|
IMPORT ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR 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 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 FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
|
Read problems statements in English, Mandarin Chinese and Russian as well.
Given an array A_{1},A_{2}...A_{N}, you have to print the size of the largest contiguous subarray such that
LCM of all integers in that subarray is equal to the product of all integers in that subarray.
Formally,
For a subarray A_{i},A_{i+1}...A_{j} where 1 β€ i < j β€ N to be valid: LCM(A_{i},A_{i+1}...A_{j}) should be equal to A_{i}*A_{i+1}*...*A_{j}. You have to print the size of the largest valid subarray.
If no subarray satisfies output -1.
Note:A single element is not considered a subarray according to definition in this problem.
------ Input ------
First line contains T, the number of testcases. Each testcase consists of N in one line followed by N integers in next line.
------ Output ------
For each testcase, print the required answer in one line.
------ Constraints ------
$1 β€ T β€ 50$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{6}$
------ Example ------
Input:
3
2
7 2
4
2 2 3 4
3
2 2 4
Output:
2
2
-1
------ Explanation ------
Example case 1.LCM(2,7)=2*7. Therefore, subarray A_{1} to A_{2} satisfies.
Example case 2.The subarrays A_{2} to A_{3} and A_{3} to A_{4} are the maximum size possible.
Example case 3.No subarray will satisfy.
Warning: Use fast input/output. Large input files. Solutions may not pass in slower languages.
Update: Time limit for python=10s
|
maxlimt = int(1000000.0 + 5)
factors = [list() for i in range(maxlimt)]
def sieve():
factors[0], factors[1] = list(), list()
for i in range(2, maxlimt):
if len(factors[i]) == 0:
for j in range(i, maxlimt, i):
factors[j].append(i)
sieve()
t = int(input())
while t:
n = int(input().strip())
arr = [int(x) for x in input().strip().split()]
indices = [-1] * maxlimt
left, ans = 0, 0
for right in range(n):
val = arr[right]
for x in factors[val]:
left = max(left, indices[x] + 1)
indices[x] = right
dis = right - left + 1
ans = max(ans, dis)
if ans < 2:
print(-1)
else:
print(ans)
t -= 1
|
ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
|
Read problems statements in English, Mandarin Chinese and Russian as well.
Given an array A_{1},A_{2}...A_{N}, you have to print the size of the largest contiguous subarray such that
LCM of all integers in that subarray is equal to the product of all integers in that subarray.
Formally,
For a subarray A_{i},A_{i+1}...A_{j} where 1 β€ i < j β€ N to be valid: LCM(A_{i},A_{i+1}...A_{j}) should be equal to A_{i}*A_{i+1}*...*A_{j}. You have to print the size of the largest valid subarray.
If no subarray satisfies output -1.
Note:A single element is not considered a subarray according to definition in this problem.
------ Input ------
First line contains T, the number of testcases. Each testcase consists of N in one line followed by N integers in next line.
------ Output ------
For each testcase, print the required answer in one line.
------ Constraints ------
$1 β€ T β€ 50$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{6}$
------ Example ------
Input:
3
2
7 2
4
2 2 3 4
3
2 2 4
Output:
2
2
-1
------ Explanation ------
Example case 1.LCM(2,7)=2*7. Therefore, subarray A_{1} to A_{2} satisfies.
Example case 2.The subarrays A_{2} to A_{3} and A_{3} to A_{4} are the maximum size possible.
Example case 3.No subarray will satisfy.
Warning: Use fast input/output. Large input files. Solutions may not pass in slower languages.
Update: Time limit for python=10s
|
temp = [(1) for i in range(10**6 + 1)]
for i in range(2, 10**6 + 1):
for j in range(i, 10**6 + 1, i):
if temp[j] == 1:
temp[j] = i
def getFactor(num):
last = None
factors = []
while num > 1:
if temp[num] != last:
factors.append(temp[num])
last = temp[num]
num //= last
return factors
def find():
n = int(input())
a = list(map(int, input().split()))
dic = {}
big = 0
index = -1
for i in range(n):
factors = getFactor(a[i])
for f in factors:
if f in dic:
index = max(index, dic[f])
dic[f] = i
big = max(big, i - index)
if big == 1:
print(-1)
else:
print(big)
t = int(input())
for i in range(t):
find()
|
ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR NONE ASSIGN VAR LIST WHILE VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Read problems statements in English, Mandarin Chinese and Russian as well.
Given an array A_{1},A_{2}...A_{N}, you have to print the size of the largest contiguous subarray such that
LCM of all integers in that subarray is equal to the product of all integers in that subarray.
Formally,
For a subarray A_{i},A_{i+1}...A_{j} where 1 β€ i < j β€ N to be valid: LCM(A_{i},A_{i+1}...A_{j}) should be equal to A_{i}*A_{i+1}*...*A_{j}. You have to print the size of the largest valid subarray.
If no subarray satisfies output -1.
Note:A single element is not considered a subarray according to definition in this problem.
------ Input ------
First line contains T, the number of testcases. Each testcase consists of N in one line followed by N integers in next line.
------ Output ------
For each testcase, print the required answer in one line.
------ Constraints ------
$1 β€ T β€ 50$
$2 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{6}$
------ Example ------
Input:
3
2
7 2
4
2 2 3 4
3
2 2 4
Output:
2
2
-1
------ Explanation ------
Example case 1.LCM(2,7)=2*7. Therefore, subarray A_{1} to A_{2} satisfies.
Example case 2.The subarrays A_{2} to A_{3} and A_{3} to A_{4} are the maximum size possible.
Example case 3.No subarray will satisfy.
Warning: Use fast input/output. Large input files. Solutions may not pass in slower languages.
Update: Time limit for python=10s
|
from sys import stdin
input = stdin.readline
size = 10**6 + 1
primef = [[] for i in range(size)]
prime = []
for i in range(2, size):
if len(primef[i]) == 0:
for j in range(i, size, i):
primef[j].append(i)
prime.append(i)
def nextpossible(j, length, x):
while their[ind[x]]:
for i in primef[a[j]]:
their[ind[i]] -= 1
j += 1
length -= 1
return [j, length]
def answer():
for i in range(len(prime)):
ind[prime[i]] = i
j, ans, length = 0, -1, 0
for i in range(n):
for x in primef[a[i]]:
if their[ind[x]]:
j, length = nextpossible(j, length, x)
their[ind[x]] += 1
length += 1
if length > 1:
ans = max(ans, length)
return ans
for T in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
ind = dict()
their = [0] * len(prime)
print(answer())
|
ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF WHILE VAR VAR VAR FOR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN LIST VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
Analyzing the mistakes people make while typing search queries is a complex and an interesting work. As there is no guaranteed way to determine what the user originally meant by typing some query, we have to use different sorts of heuristics.
Polycarp needed to write a code that could, given two words, check whether they could have been obtained from the same word as a result of typos. Polycarpus suggested that the most common typo is skipping exactly one letter as you type a word.
Implement a program that can, given two distinct words S and T of the same length n determine how many words W of length n + 1 are there with such property that you can transform W into both S, and T by deleting exactly one character. Words S and T consist of lowercase English letters. Word W also should consist of lowercase English letters.
Input
The first line contains integer n (1 β€ n β€ 100 000) β the length of words S and T.
The second line contains word S.
The third line contains word T.
Words S and T consist of lowercase English letters. It is guaranteed that S and T are distinct words.
Output
Print a single integer β the number of distinct words W that can be transformed to S and T due to a typo.
Examples
Input
7
reading
trading
Output
1
Input
5
sweet
sheep
Output
0
Input
3
toy
try
Output
2
Note
In the first sample test the two given words could be obtained only from word "treading" (the deleted letters are marked in bold).
In the second sample test the two given words couldn't be obtained from the same word by removing one letter.
In the third sample test the two given words could be obtained from either word "tory" or word "troy".
|
from sys import stdin
read = stdin.readline
n = int(read())
S = read()
T = read()
for i in range(n):
if S[i] != T[i]:
break
a = b = 1
for j in range(i, n - 1):
if S[j] != T[j + 1]:
a = S[j + 1 :] == T[j + 1 :]
break
for j in range(i, n - 1):
if S[j + 1] != T[j]:
b = S[j + 1 :] == T[j + 1 :]
break
print(a + b)
|
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Analyzing the mistakes people make while typing search queries is a complex and an interesting work. As there is no guaranteed way to determine what the user originally meant by typing some query, we have to use different sorts of heuristics.
Polycarp needed to write a code that could, given two words, check whether they could have been obtained from the same word as a result of typos. Polycarpus suggested that the most common typo is skipping exactly one letter as you type a word.
Implement a program that can, given two distinct words S and T of the same length n determine how many words W of length n + 1 are there with such property that you can transform W into both S, and T by deleting exactly one character. Words S and T consist of lowercase English letters. Word W also should consist of lowercase English letters.
Input
The first line contains integer n (1 β€ n β€ 100 000) β the length of words S and T.
The second line contains word S.
The third line contains word T.
Words S and T consist of lowercase English letters. It is guaranteed that S and T are distinct words.
Output
Print a single integer β the number of distinct words W that can be transformed to S and T due to a typo.
Examples
Input
7
reading
trading
Output
1
Input
5
sweet
sheep
Output
0
Input
3
toy
try
Output
2
Note
In the first sample test the two given words could be obtained only from word "treading" (the deleted letters are marked in bold).
In the second sample test the two given words couldn't be obtained from the same word by removing one letter.
In the third sample test the two given words could be obtained from either word "tory" or word "troy".
|
n = int(input())
s, t = input(), input()
i = 0
while s[i] == t[i]:
i += 1
j = n - 1
while s[j] == t[j]:
j -= 1
print(int(s[i + 1 : j + 1] == t[i:j]) + int(s[i:j] == t[i + 1 : j + 1]))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
|
Analyzing the mistakes people make while typing search queries is a complex and an interesting work. As there is no guaranteed way to determine what the user originally meant by typing some query, we have to use different sorts of heuristics.
Polycarp needed to write a code that could, given two words, check whether they could have been obtained from the same word as a result of typos. Polycarpus suggested that the most common typo is skipping exactly one letter as you type a word.
Implement a program that can, given two distinct words S and T of the same length n determine how many words W of length n + 1 are there with such property that you can transform W into both S, and T by deleting exactly one character. Words S and T consist of lowercase English letters. Word W also should consist of lowercase English letters.
Input
The first line contains integer n (1 β€ n β€ 100 000) β the length of words S and T.
The second line contains word S.
The third line contains word T.
Words S and T consist of lowercase English letters. It is guaranteed that S and T are distinct words.
Output
Print a single integer β the number of distinct words W that can be transformed to S and T due to a typo.
Examples
Input
7
reading
trading
Output
1
Input
5
sweet
sheep
Output
0
Input
3
toy
try
Output
2
Note
In the first sample test the two given words could be obtained only from word "treading" (the deleted letters are marked in bold).
In the second sample test the two given words couldn't be obtained from the same word by removing one letter.
In the third sample test the two given words could be obtained from either word "tory" or word "troy".
|
def aux(s, t):
n = len(s)
lpr = 0
for i in range(n):
if s[i] != t[i]:
break
lpr += 1
lsf = 0
for i in range(n - 1, -1, -1):
if s[i] != t[i]:
break
lsf += 1
if lpr == n:
return 2
return (s[lpr : n - lsf - 1] == t[lpr + 1 : n - lsf]) + (
t[lpr : n - lsf - 1] == s[lpr + 1 : n - lsf]
)
input()
print(aux(input(), input()))
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
|
Analyzing the mistakes people make while typing search queries is a complex and an interesting work. As there is no guaranteed way to determine what the user originally meant by typing some query, we have to use different sorts of heuristics.
Polycarp needed to write a code that could, given two words, check whether they could have been obtained from the same word as a result of typos. Polycarpus suggested that the most common typo is skipping exactly one letter as you type a word.
Implement a program that can, given two distinct words S and T of the same length n determine how many words W of length n + 1 are there with such property that you can transform W into both S, and T by deleting exactly one character. Words S and T consist of lowercase English letters. Word W also should consist of lowercase English letters.
Input
The first line contains integer n (1 β€ n β€ 100 000) β the length of words S and T.
The second line contains word S.
The third line contains word T.
Words S and T consist of lowercase English letters. It is guaranteed that S and T are distinct words.
Output
Print a single integer β the number of distinct words W that can be transformed to S and T due to a typo.
Examples
Input
7
reading
trading
Output
1
Input
5
sweet
sheep
Output
0
Input
3
toy
try
Output
2
Note
In the first sample test the two given words could be obtained only from word "treading" (the deleted letters are marked in bold).
In the second sample test the two given words couldn't be obtained from the same word by removing one letter.
In the third sample test the two given words could be obtained from either word "tory" or word "troy".
|
import sys
try:
while True:
n = int(input())
s1 = input()
s2 = input()
L = 0
R = n - 1
while s1[L] == s2[L]:
L += 1
while s1[R] == s2[R]:
R -= 1
res = 0
i = L
while i < R and s1[i] == s2[i + 1]:
i += 1
if i >= R:
res += 1
s1, s2 = s2, s1
i = L
while i < R and s1[i] == s2[i + 1]:
i += 1
if i >= R:
res += 1
print(res)
except EOFError:
pass
|
IMPORT WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
Analyzing the mistakes people make while typing search queries is a complex and an interesting work. As there is no guaranteed way to determine what the user originally meant by typing some query, we have to use different sorts of heuristics.
Polycarp needed to write a code that could, given two words, check whether they could have been obtained from the same word as a result of typos. Polycarpus suggested that the most common typo is skipping exactly one letter as you type a word.
Implement a program that can, given two distinct words S and T of the same length n determine how many words W of length n + 1 are there with such property that you can transform W into both S, and T by deleting exactly one character. Words S and T consist of lowercase English letters. Word W also should consist of lowercase English letters.
Input
The first line contains integer n (1 β€ n β€ 100 000) β the length of words S and T.
The second line contains word S.
The third line contains word T.
Words S and T consist of lowercase English letters. It is guaranteed that S and T are distinct words.
Output
Print a single integer β the number of distinct words W that can be transformed to S and T due to a typo.
Examples
Input
7
reading
trading
Output
1
Input
5
sweet
sheep
Output
0
Input
3
toy
try
Output
2
Note
In the first sample test the two given words could be obtained only from word "treading" (the deleted letters are marked in bold).
In the second sample test the two given words couldn't be obtained from the same word by removing one letter.
In the third sample test the two given words could be obtained from either word "tory" or word "troy".
|
i, j = 0, int(input()) - 1
a, b = input(), input()
while a[i] == b[i]:
i += 1
while a[j] == b[j]:
j -= 1
print((a[i + 1 : j + 1] == b[i:j]) + (b[i + 1 : j + 1] == a[i:j]))
|
ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
def maxSubsequenceSubstring(x, y, n, m):
dp = [[(0) for i in range(n + 1)] for i in range(n + 1)]
for i in range(1, m + 1):
for j in range(1, n + 1):
if x[j - 1] == y[i - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
else:
dp[i][j] = dp[i][j - 1]
ans = 0
for i in range(1, m + 1):
ans = max(ans, dp[i][n])
return ans
t = int(input())
while t:
t -= 1
n = int(input())
l = list(map(int, input().split()))
q = sorted(l)
print(n - maxSubsequenceSubstring(l, q, len(l), len(q)))
|
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
T = int(input())
while T > 0:
n = int(input())
arr = list(map(int, input().split()))
b = arr.copy()
b.sort()
dic = {}
dp = [0] * (n + 1)
for i in range(n):
dic[b[i]] = i + 1
for i in range(n):
dp[dic[arr[i]]] = dp[dic[arr[i]] - 1] + 1
ma = max(dp)
print(n - ma)
T -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
import sys
def minp():
return sys.stdin.readline().strip()
def mint():
return int(minp())
def mints():
return list(map(int, minp().split()))
def solve():
n = mint()
a = list(mints())
b = list(enumerate(a))
b.sort(key=lambda a: a[1])
c1 = [0] * n
c = 0
h = 0
p = -1
for i, v in b:
if i > p:
c += 1
else:
c = 1
h += 1
c1[i] = h - c
p = i
c = 0
h = 0
p = -1
ans = n + n
for z in range(len(b) - 1, -1, -1):
i, v = b[z]
if i < p:
c += 1
else:
c = 1
h += 1
ans = min(ans, h - c + c1[i])
p = i
print(ans)
for i in range(mint()):
solve()
|
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR 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 FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
b = sorted(range(n), key=lambda x: a[x])
ans = 0
c = 1
for i in range(1, n):
if b[i] > b[i - 1]:
c += 1
else:
ans = max(ans, c)
c = 1
print(n - max(ans, c))
|
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 VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
import sys
input = sys.stdin.readline
def main():
t = int(input())
for _ in range(t):
N = int(input())
A = [int(x) for x in input().split()]
Asort = sorted(A)
dp = [0] * (N + 1)
dic = {}
for i, a in enumerate(Asort):
dic[a] = i + 1
for a in A:
dp[dic[a]] = dp[dic[a] - 1] + 1
print(N - max(dp))
main()
|
IMPORT ASSIGN VAR VAR FUNC_DEF 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 ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
def main():
n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
d = dict()
f = dict()
for i in range(n - 1, -1, -1):
d[b[i]] = i
for i in range(n):
where = d[a[i]]
if where == 0 or b[where - 1] not in f:
f[a[i]] = 1
else:
f[a[i]] = 1 + f[b[where - 1]]
print(n - max(f.values()))
tn = int(input())
for _ in range(tn):
main()
|
FUNC_DEF 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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
t = int(input())
for _ in range(t):
n = int(input())
l = list(map(int, input().split()))
id = list(zip(l, list(range(n))))
id.sort()
val, pos = zip(*id)
best = 1
i = 1
count = 1
while True:
if i >= n:
break
if pos[i] > pos[i - 1]:
count += 1
best = max(count, best)
else:
count = 1
i += 1
print(n - best)
|
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 VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
def remainderSorted(x, n, a, b):
mn = b[x[0]]
mx = b[x[1]]
prev = None
for e in a:
if e >= mn and e <= mx:
if prev is not None and prev > e:
return False
prev = e
return True
def count(n, a):
mincnt = 0
for i in range(1, n):
if a[i - 1] > a[i]:
mincnt += 1
b = sorted(a)
seen = {(0, -1): 1}
list = [(0, -1)]
while True:
x = list.pop(0)
cnt = x[0] - x[1] - 1
if cnt >= mincnt and remainderSorted(x, n, a, b):
return cnt
else:
xnew = x[0] + 1, x[1]
if xnew not in seen:
list.append(xnew)
seen[xnew] = 1
xnew = x[0], x[1] - 1
if xnew not in seen:
list.append(xnew)
seen[xnew] = 1
def count2(n, a):
b = sorted(a)
h = {a[i]: i for i in range(n)}
ms = [int(h[b[i]] < h[b[i - 1]]) for i in range(1, n)]
gap = 0
maxgap = 0
for m in ms:
if m == 0:
gap += 1
maxgap = max(gap, maxgap)
else:
gap = 0
return n - maxgap - 1
t = int(input().strip())
for _ in range(t):
n = int(input().strip())
a = list(map(int, input().strip().split()))
print(count2(n, a))
|
FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NONE FOR VAR VAR IF VAR VAR VAR VAR IF VAR NONE VAR VAR RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
def read_list():
return list(map(int, input().strip().split(" ")))
def print_list(l):
print(" ".join(map(str, l)))
N = int(input())
for _ in range(N):
n = int(input())
nums = read_list()
dic = {}
for i in range(n):
dic[nums[i]] = i
nums.sort()
res = 0
tmp = 1
for i in range(1, n):
if dic[nums[i]] < dic[nums[i - 1]]:
res = max(res, tmp)
tmp = 1
else:
tmp += 1
res = max(res, tmp)
print(n - res)
|
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR 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 ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
l = list(map(int, input().split()))
id = list(zip(l, list(range(n))))
id.sort()
val, pos = zip(*id)
blok = []
cur = [pos[0]]
for i in range(1, n):
if val[i] == val[i - 1]:
cur.append(pos[i])
else:
cur.sort()
blok.append(cur)
cur = [pos[i]]
cur.sort()
blok.append(cur)
best = 0
m = len(blok)
for j in range(m):
best = max(len(blok[j]), best)
i = 0
while True:
if i >= m - 2:
break
cyk = min(blok[i + 1])
j = -1
while j + 1 < len(blok[i]) and blok[i][j + 1] < cyk:
j += 1
su = j + 1
ii = i + 2
while ii < m:
if min(blok[ii]) > max(blok[ii - 1]):
su += len(blok[ii - 1])
ii += 1
else:
break
if ii == m:
su += len(blok[-1])
best = max(best, su)
else:
xxx = max(blok[ii - 1])
su += len(blok[ii - 1])
inde = len(blok[ii]) - 1
while inde >= 0 and blok[ii][inde] >= xxx:
su += 1
inde -= 1
best = max(best, su)
i = max(i + 1, ii - 1)
for i in range(1, m):
b1 = blok[i]
b0 = blok[i - 1]
l0 = len(b0)
l1 = len(b1)
i1 = 0
for ind in range(l0):
while True:
if i1 < l1 and b1[i1] <= b0[ind]:
i1 += 1
else:
break
if l1 == i1:
break
best = max(best, ind + 1 + (l1 - i1))
print(n - best)
|
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
for p in range(int(input())):
n = int(input())
a = [int(i) for i in input().split()][:n]
b = a.copy()
b.sort()
c = []
e = 0
while True:
x = b.index(a[e])
m = 1
for j in range(e, len(a)):
if x <= len(a) - 2:
if a[j] == b[x + 1]:
x += 1
m += 1
c.append(m)
e += 1
if e == len(a):
break
print(n - max(c))
|
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 EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
from sys import setrecursionlimit, stdin, stdout
for _ in range(int(input())):
n = int(input())
a = [[int(i)] for i in input().split()]
for i in range(n):
a[i].append(i)
a = sorted(a)
ans = 1
cur = 1
for i in range(1, n):
if a[i][1] > a[i - 1][1]:
cur += 1
else:
ans = max(ans, cur)
cur = 1
if i == n - 1:
ans = max(ans, cur)
cur = 1
print(n - ans)
|
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 VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
T = int(input())
for _ in range(T):
n = int(input())
ls = list(map(int, input().split()))
cc = {u: i for i, u in enumerate(sorted(ls))}
ls = [cc[u] for u in ls]
def lcount(i, u):
cc = 0
while i >= 0:
if ls[i] == u:
cc += 1
u -= 1
i -= 1
return cc
def rcount(i, u):
cc = 0
while i < n:
if ls[i] == u:
cc += 1
u += 1
i += 1
return cc
mn = n
for i, u in enumerate(ls):
cnt = lcount(i - 1, u - 1) + 1 + rcount(i + 1, u + 1)
mn = min(mn, n - cnt)
print(mn)
|
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 VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
import sys
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II():
return int(sys.stdin.readline())
def MI():
return map(int, sys.stdin.readline().split())
def LI():
return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
def SI():
return sys.stdin.readline()[:-1]
def main():
for _ in range(II()):
n = II()
aa = LI()
s = set(aa)
enc = {a: i for i, a in enumerate(sorted(s))}
aa = [enc[a] for a in aa]
first = [False] * n
fin = [False] * len(s)
for i in range(n):
if fin[aa[i]]:
continue
first[i] = True
fin[aa[i]] = True
end = [False] * n
fin = [False] * len(s)
for i in range(n - 1, -1, -1):
if fin[aa[i]]:
continue
end[i] = True
fin[aa[i]] = True
last = [-1] * len(s)
dp = [([0] * 3) for _ in range(n)]
mx = 0
for i, a in enumerate(aa):
val = 1
i0 = last[a]
if i0 != -1:
val = dp[i0][0] + 1
dp[i][0] = val
mx = max(mx, val)
val = 1
if i0 != -1:
val = dp[i0][1] + 1
i1 = -1
if a:
i1 = last[a - 1]
if i1 != -1:
val = max(val, dp[i1][0] + 1)
if end[i1] and first[i]:
val = max(val, dp[i1][1] + 1)
dp[i][1] = val
mx = max(mx, val)
val = 1
if i0 != -1:
val = dp[i0][2] + 1
if i1 != -1:
val = max(val, dp[i1][0] + 1)
if end[i1]:
val = max(val, dp[i1][1] + 1)
dp[i][2] = val
mx = max(mx, val)
last[a] = i
print(n - mx)
main()
|
IMPORT ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
from sys import stdin, stdout
t = int(stdin.readline().strip())
for _ in range(t):
n = int(stdin.readline().strip())
a = [(int(b), i) for i, b in enumerate(stdin.readline().split())]
a.sort()
ans, c = 0, 1
for i in range(1, n):
if a[i][1] > a[i - 1][1]:
c += 1
else:
ans = max(ans, c)
c = 1
else:
ans = max(ans, c)
stdout.write("{}\n".format(n - ans))
|
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 FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
import sys
def input():
return sys.stdin.readline()[:-1]
def main():
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
b = [[a[k], k] for k in range(n)]
b = sorted(b)
c = [b[k][1] for k in range(n)]
c = [-1] + c + [100000]
t = 0
ans = 0
for k in range(1, n + 1):
if c[k - 1] < c[k]:
t += 1
else:
t = 1
ans = max(ans, t)
print(n - ans)
main()
|
IMPORT FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_DEF 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 LIST VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
tt = int(input())
for loop in range(tt):
n = int(input())
a = list(map(int, input().split()))
ai = []
for i in range(n):
ai.append((a[i], i))
ai.sort()
ind = 0
b = [0] * n
non = [0] * (n + 1)
for i in range(n):
if i != 0 and ai[i][0] != ai[i - 1][0]:
ind += 1
b[ai[i][1]] = ind
non[ind] += 1
dp = [0] * (n + 1)
for i in range(n):
dp[b[i]] += max(dp[b[i]], dp[b[i] - 1] + 1)
print(n - max(dp))
|
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 LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
t = int(input())
for you in range(t):
n = int(input())
l = input().split()
li = [int(i) for i in l]
z = list(li)
z.sort()
hashi = dict()
for i in range(n):
hashi[z[i]] = i + 1
for i in range(n):
li[i] = hashi[li[i]]
hashi = dict()
dp = [(0) for i in range(n)]
for i in range(n):
if li[i] - 1 in hashi:
dp[i] = hashi[li[i] - 1] + 1
hashi[li[i]] = dp[i]
else:
dp[i] = 1
hashi[li[i]] = 1
z = max(dp)
print(n - z)
|
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
from sys import stdin
def search(lis, idx, val):
low = 0
high = idx
while low <= high:
mid = (low + high) // 2
if lis[mid] == val:
return mid
if lis[mid] > val:
high = mid - 1
else:
low = mid + 1
def ans(arr):
lis_f = sorted(arr)
ans_arr = [0] * len(arr)
for i in range(len(arr)):
ans_arr[search(lis_f, len(arr) - 1, arr[i])] = i
c = 1
p = 0
for j in range(1, len(arr)):
if ans_arr[j] > ans_arr[j - 1]:
c += 1
elif c > p:
p = c
c = 1
else:
c = 1
if c > p:
p = c
print(len(arr) - p)
for i in range(int(stdin.readline())):
stdin.readline()
ans(list(map(int, stdin.readline().split())))
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
from sys import stdin, stdout
def findops(l):
ans = 0
count = 1
for i in range(1, len(l)):
if l[i] > l[i - 1]:
count += 1
else:
ans = max(ans, count)
count = 1
return len(l) - max(ans, count)
for _ in range(int(stdin.readline())):
stdin.readline()
l = list(map(int, stdin.readline().split()))
print(findops(sorted(range(len(l)), key=lambda x: l[x])))
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
from itertools import islice
for t in range(int(input())):
n = int(input())
aa = list(map(int, input().split(" ")))
asort = sorted(aa)
ls = [None] * n
for i, a in enumerate(asort):
ai = aa.index(a)
l = 1
if i >= 1:
pi = aa.index(asort[i - 1])
if pi < ai:
l = ls[pi] + 1
ls[ai] = l
print(n - max(ls))
|
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 STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
narr = dict()
for i in range(n):
narr[arr[i]] = i
arr.sort()
ct = ans = 0
for i in range(n):
if i > 0 and narr[arr[i]] < narr[arr[i - 1]]:
ct = 0
ct += 1
ans = max(ct, ans)
print(n - ans)
|
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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
def flyingsort(L):
d = {}
for i in range(len(L)):
d[L[i]] = i
L.sort()
prev = -1
run = 0
maxRun = -1
for i in L:
if d[i] > prev:
run += 1
else:
run = 1
maxRun = max(maxRun, run)
prev = d[i]
return len(L) - maxRun
T = int(input())
for i in range(T):
N = int(input())
L = list(map(int, input().split(" ")))
print(flyingsort(L))
|
FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR 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 STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
I = lambda: list(map(int, input().split()))
def lis(arr):
n = len(arr)
lis = [1] * n
for i in range(1, n):
for j in range(0, i):
if arr[i] == arr[j] + 1 and lis[i] < lis[j] + 1:
lis[i] = lis[j] + 1
maximum = 0
for i in range(n):
maximum = max(maximum, lis[i])
return maximum
for tc in range(int(input())):
(n,) = I()
l = I()
ar = l.copy()
ar.sort()
f = {}
for i in range(n):
f[ar[i]] = i + 1
for i in range(n):
l[i] = f[l[i]]
print(n - lis(l))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
d = {}
for x in sorted(a):
d[x] = len(d)
b = [d[x] for x in a]
l = [0] * n
r = [0] * n
for x in b:
l[x] = 1
if x - 1 >= 0:
l[x] += l[x - 1]
for x in b[::-1]:
r[x] = 1
if x + 1 < n:
r[x] += r[x + 1]
print(n - max([(x + y - 1) for x, y in zip(l, 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 DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
test_case = int(input())
for _ in range(test_case):
input()
arr = list(map(int, input().split()))
brr = sorted(arr)
crr = [0] * (len(arr) + 1)
for i in arr:
index = brr.index(i)
crr[index] = 1 + crr[index - 1]
print(len(arr) - max(crr))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n):
b.append([a[i], i])
b = sorted(b, key=lambda R: R[0])
positions = [0] * n
for i in range(n):
positions[b[i][1]] = i
leftmin = []
rightmin = []
for i in range(n):
leftmin.append(0)
rightmin.append(0)
for i in range(n - 1):
for j in range(i + 1):
if a[j] > a[i + 1]:
l = positions[i + 1] + 1
r = n - positions[j]
for k in range(l - 1, -1, -1):
if leftmin[k] >= r:
break
leftmin[k] = r
for k in range(r - 1, -1, -1):
if rightmin[k] >= l:
break
rightmin[k] = l
maximum = n
for i in range(n):
maximum = min(maximum, leftmin[i] + i)
maximum = min(maximum, rightmin[i] + i)
print(maximum)
|
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 LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
indexOf = {}
for i in range(n):
indexOf[a[i]] = i
values = sorted(list(set(a)))
visited = [(False) for _ in range(n)]
ng = [n for _ in range(n)]
for i in range(n):
lo = 0
hi = len(values) - 1
while lo < hi:
mid = (lo + hi) // 2
if values[mid] < a[i]:
lo = mid + 1
else:
hi = mid
if lo != len(values) - 1:
ng[i] = indexOf[values[lo + 1]]
ans = n
for i in range(n):
if visited[i]:
continue
tempAns = n
at = i
while at != n:
visited[at] = True
tempAns -= 1
if ng[at] > at:
at = ng[at]
else:
at = n
ans = min(ans, tempAns)
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR 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 IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
a[i] = a[i] * n + i
a.sort()
p = [(0) for _ in range(n)]
for i in range(n):
p[a[i] % n] = i
q = [(0) for _ in range(n)]
for i in range(n):
q[p[i]] = i
cnt = 1
m = 0
for i in range(1, n):
if q[i - 1] < q[i]:
cnt += 1
else:
m = max(cnt, m)
cnt = 1
m = max(cnt, m)
print(n - m)
|
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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
t = int(input())
for _ in range(t):
n = int(input())
a = [(int(x), i) for i, x in enumerate(input().split())]
a.sort()
mx = 0
curl = 0
prev = -1000
for x, i in a:
if i > prev:
curl += 1
else:
curl = 1
prev = i
if curl > mx:
mx = curl
print(n - mx)
|
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 VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
b = sorted(list(set(a)))
max_r = 0
for i in range(n):
a[i] = b.index(a[i])
while a != []:
r = 0
last = a[-1]
for i in range(len(a) - 1, -1, -1):
if a[i] == last or a[i] + 1 == last:
r += 1
last = a[i]
a.pop(i)
if r > max_r:
max_r = r
print(n - max_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 FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR WHILE VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
t = int(input())
for _ in range(t):
n = int(input())
l = [[x, i] for i, x in enumerate(map(int, input().split()))]
l.sort()
c = 0
count = 0
now = -1
for i in range(n):
if l[i][1] > now:
now = l[i][1]
count += 1
else:
c = max(c, count)
count = 1
now = l[i][1]
c = max(c, count)
print(n - c)
|
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 VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
c = {}
for i in range(n - 1):
c[b[i + 1]] = b[i]
c[b[0]] = -1
li = {}
for i in a:
if c[i] in li:
li[i] = li[c[i]] + 1
else:
li[i] = 1
print(n - max(li.values()))
|
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 FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
for p in range(int(input())):
n = int(input())
a = [int(i) for i in input().split()][:n]
b = a.copy()
b.sort()
c = -1
for e in range(len(a)):
m = 0
g = e
for j in range(len(a)):
if g < len(a) and a[j] == b[g]:
g += 1
m += 1
c = max(c, m)
print(n - c)
|
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 EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
import sys
def ii():
return int(sys.stdin.readline())
def mi():
return map(int, sys.stdin.readline().split())
def li():
return list(map(int, sys.stdin.readline().split()))
def lli(rows_number):
return [li() for _ in range(rows_number)]
def si():
return sys.stdin.readline()[:-1]
for p in range(ii()):
n = ii()
a = li()
b = a.copy()
b.sort()
c = -1
for e in range(len(a)):
m = 0
g = e
for j in range(len(a)):
if g < len(a) and a[j] == b[g]:
g += 1
m += 1
c = max(c, m)
print(n - c)
|
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
t = int(input())
for i in range(t):
l = int(input())
s = list(map(int, input().split()))
for j in range(l):
s[j] += j / 100000.0
sor = sorted(s)
b = []
for j in s:
b.append(sor.index(j))
dp = [0] * (l + 1)
for j in range(l):
dp[b[j]] = 1 + dp[b[j] - 1]
print(l - max(dp))
|
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 FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
p = []
for i in range(n):
p.append((a[i], i))
p.sort(key=lambda x: x[0])
aOrder = [-1] * n
for i in range(n):
aOrder[p[i][1]] = i
maxSubseq = []
for i in range(n):
maxSubseqcur = 0
for j in range(i):
if aOrder[i] - aOrder[j] == 1 and maxSubseqcur < maxSubseq[j]:
maxSubseqcur = maxSubseq[j]
maxSubseq.append(maxSubseqcur + 1)
print(n - max(maxSubseq))
|
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 LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
import sys
input = sys.stdin.readline
for nt in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
l = {}
for i in range(n):
l[b[i]] = i
if a == b:
print(0)
continue
dp = [0] * (n + 1)
for i in range(n):
dp[l[a[i]]] = 1 + dp[l[a[i]] - 1]
print(n - max(dp))
|
IMPORT ASSIGN VAR 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 VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
import sys
def input():
return sys.stdin.readline().strip()
def list2d(a, b, c):
return [([c] * b) for i in range(a)]
def list3d(a, b, c, d):
return [[([d] * c) for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e):
return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1):
return int(-(-x // y))
def INT():
return int(input())
def MAP():
return map(int, input().split())
def LIST(N=None):
return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes():
print("Yes")
def No():
print("No")
def YES():
print("YES")
def NO():
print("NO")
INF = 10**19
MOD = 10**9 + 7
def compress(S):
zipped, unzipped = {}, {}
for i, a in enumerate(sorted(S)):
zipped[a] = i
unzipped[i] = a
return zipped, unzipped
for _ in range(INT()):
N = INT()
A = LIST()
zipped, _ = compress(A)
for i in range(N):
A[i] = zipped[A[i]]
B = [0] * N
for i, a in enumerate(A):
B[a] = i
mx = cnt = 1
for i in range(N - 1):
if B[i] < B[i + 1]:
cnt += 1
else:
cnt = 1
mx = max(mx, cnt)
print(N - mx)
|
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR DICT DICT FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
This is an easy version of the problem. In this version, all numbers in the given array are distinct and the constraints on $n$ are less than in the hard version of the problem.
You are given an array $a$ of $n$ integers (there are no equals elements in the array). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array.
For example, if $n = 5$, $a = [4, 7, 2, 3, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 3, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 3, 9, 4]$.
You can perform operations of any type any number of times in any order.
Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations that must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$)Β β the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing an integer $n$ ($1 \le n \le 3000$)Β β length of the array $a$.
Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β an array that needs to be sorted by the given operations. All numbers in the given array are distinct.
The sum of $n$ for all test cases in one test does not exceed $3000$.
-----Output-----
For each test case output one integerΒ β the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order.
-----Example-----
Input
4
5
4 7 2 3 9
5
3 5 8 1 7
5
1 4 5 7 12
4
0 2 1 3
Output
2
2
0
2
-----Note-----
In the first test case, you first need to move 3, and then 2 to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 3, 9] \rightarrow [3, 4, 7, 2, 9] \rightarrow [2, 3, 4, 7, 9]$.
In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$.
In the third test case, the array is already sorted.
|
t = int(input())
for _ in range(t):
n = int(input())
a = [int(x) for x in input().split()]
indices = {}
for i in range(n):
indices[a[i]] = i
ordered = sorted(a)
seq_length = [1] * n
for i in range(1, n):
if indices[ordered[i - 1]] < indices[ordered[i]]:
seq_length[i] = seq_length[i - 1] + 1
print(n - max(seq_length))
|
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 DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
|
There are $n$ people in a row. The height of the $i$-th person is $a_i$. You can choose any subset of these people and try to arrange them into a balanced circle.
A balanced circle is such an order of people that the difference between heights of any adjacent people is no more than $1$. For example, let heights of chosen people be $[a_{i_1}, a_{i_2}, \dots, a_{i_k}]$, where $k$ is the number of people you choose. Then the condition $|a_{i_j} - a_{i_{j + 1}}| \le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|a_{i_1} - a_{i_k}| \le 1$ should be also satisfied. $|x|$ means the absolute value of $x$. It is obvious that the circle consisting of one person is balanced.
Your task is to choose the maximum number of people and construct a balanced circle consisting of all chosen people. It is obvious that the circle consisting of one person is balanced so the answer always exists.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of people.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the height of the $i$-th person.
-----Output-----
In the first line of the output print $k$ β the number of people in the maximum balanced circle.
In the second line print $k$ integers $res_1, res_2, \dots, res_k$, where $res_j$ is the height of the $j$-th person in the maximum balanced circle. The condition $|res_{j} - res_{j + 1}| \le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|res_{1} - res_{k}| \le 1$ should be also satisfied.
-----Examples-----
Input
7
4 3 5 1 2 2 1
Output
5
2 1 1 2 3
Input
5
3 7 5 1 5
Output
2
5 5
Input
3
5 1 4
Output
2
4 5
Input
7
2 2 3 2 1 2 2
Output
7
1 2 2 2 2 3 2
|
def solve():
n = int(input())
a = sorted(map(int, input().split()))
ans = 1, 0
cur = [a[0]]
for i in range(1, n):
if a[i] - a[i - 1] > 1:
ans = max(ans, (len(cur), i - 1))
cur.clear()
elif a[i - 1] < a[i] and (i == n - 1 or a[i] < a[i + 1]):
cur.append(a[i])
ans = max(ans, (len(cur), i))
cur.clear()
cur.append(a[i])
ans = max(ans, (len(cur), n - 1))
print(ans[0])
ans1 = []
ans2 = []
for i in a[ans[1] - ans[0] + 1 : ans[1] + 1]:
if not ans1 or ans1[-1] != i:
ans1.append(i)
else:
ans2.append(i)
print(*(ans1 + ans2[::-1]))
for _ in range(1):
solve()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
|
There are $n$ people in a row. The height of the $i$-th person is $a_i$. You can choose any subset of these people and try to arrange them into a balanced circle.
A balanced circle is such an order of people that the difference between heights of any adjacent people is no more than $1$. For example, let heights of chosen people be $[a_{i_1}, a_{i_2}, \dots, a_{i_k}]$, where $k$ is the number of people you choose. Then the condition $|a_{i_j} - a_{i_{j + 1}}| \le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|a_{i_1} - a_{i_k}| \le 1$ should be also satisfied. $|x|$ means the absolute value of $x$. It is obvious that the circle consisting of one person is balanced.
Your task is to choose the maximum number of people and construct a balanced circle consisting of all chosen people. It is obvious that the circle consisting of one person is balanced so the answer always exists.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of people.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the height of the $i$-th person.
-----Output-----
In the first line of the output print $k$ β the number of people in the maximum balanced circle.
In the second line print $k$ integers $res_1, res_2, \dots, res_k$, where $res_j$ is the height of the $j$-th person in the maximum balanced circle. The condition $|res_{j} - res_{j + 1}| \le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|res_{1} - res_{k}| \le 1$ should be also satisfied.
-----Examples-----
Input
7
4 3 5 1 2 2 1
Output
5
2 1 1 2 3
Input
5
3 7 5 1 5
Output
2
5 5
Input
3
5 1 4
Output
2
4 5
Input
7
2 2 3 2 1 2 2
Output
7
1 2 2 2 2 3 2
|
import sys
__version__ = "0.1"
__date__ = "2019-04-26"
def solve(n, a):
max_a = max(a)
counter = [(0) for i in range(max_a + 1)]
for ai in a:
counter[ai] += 1
best_group = []
best_total = 0
curr = 1
while curr <= max_a:
if counter[curr] > 0:
curr_group = [curr]
curr_total = counter[curr]
curr += 1
if curr > max_a:
break
while counter[curr] > 1:
curr_group.append(curr)
curr_total += counter[curr]
curr += 1
if curr > max_a:
break
if curr > max_a:
break
if counter[curr] > 0:
curr_group.append(curr)
curr_total += counter[curr]
if curr_total > best_total:
best_group = curr_group
best_total = curr_total
else:
curr += 1
if curr_total > best_total:
best_group = curr_group
best_total = curr_total
seq = best_group
for i in best_group[::-1]:
seq += [i] * (counter[i] - 1)
return best_total, seq
def main(argv=None):
n = int(input())
a = list(map(int, input().split()))
total, seq = solve(n, a)
print(total)
print(" ".join(map(str, seq)))
return 0
def __starting_point():
STATUS = main()
return STATUS
__starting_point()
|
IMPORT ASSIGN VAR STRING ASSIGN VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR NUMBER VAR BIN_OP LIST VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR FUNC_DEF NONE 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 FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN VAR EXPR FUNC_CALL VAR
|
There are $n$ people in a row. The height of the $i$-th person is $a_i$. You can choose any subset of these people and try to arrange them into a balanced circle.
A balanced circle is such an order of people that the difference between heights of any adjacent people is no more than $1$. For example, let heights of chosen people be $[a_{i_1}, a_{i_2}, \dots, a_{i_k}]$, where $k$ is the number of people you choose. Then the condition $|a_{i_j} - a_{i_{j + 1}}| \le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|a_{i_1} - a_{i_k}| \le 1$ should be also satisfied. $|x|$ means the absolute value of $x$. It is obvious that the circle consisting of one person is balanced.
Your task is to choose the maximum number of people and construct a balanced circle consisting of all chosen people. It is obvious that the circle consisting of one person is balanced so the answer always exists.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of people.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the height of the $i$-th person.
-----Output-----
In the first line of the output print $k$ β the number of people in the maximum balanced circle.
In the second line print $k$ integers $res_1, res_2, \dots, res_k$, where $res_j$ is the height of the $j$-th person in the maximum balanced circle. The condition $|res_{j} - res_{j + 1}| \le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|res_{1} - res_{k}| \le 1$ should be also satisfied.
-----Examples-----
Input
7
4 3 5 1 2 2 1
Output
5
2 1 1 2 3
Input
5
3 7 5 1 5
Output
2
5 5
Input
3
5 1 4
Output
2
4 5
Input
7
2 2 3 2 1 2 2
Output
7
1 2 2 2 2 3 2
|
def create(s, e, cnt):
arr = []
for i in range(s, e + 1):
arr.extend([i for _ in range(cnt[i] - 1)])
arr.extend(list(range(e, s - 1, -1)))
return arr
n = int(input())
a = list(map(int, input().split()))
def solve(a):
cnt = [0] * 200001
for x in a:
cnt[x] += 1
block = []
max_ = 0
i = 0
s, e = None, None
while i < len(cnt):
if cnt[i] > 1:
s = i
while i + 1 < len(cnt) and cnt[i + 1] > 1:
i += 1
e = i
if s >= 1 and cnt[s - 1] > 0:
s -= 1
if e + 1 < len(cnt) and cnt[e + 1] > 0:
e += 1
block.append([s, e])
i = e + 1
else:
i += 1
pos = -1
for i, [s, e] in enumerate(block):
sum_ = 0
for j in range(s, e + 1):
sum_ += cnt[j]
if max_ < sum_:
max_ = sum_
pos = i
if max_ >= 2:
arr = create(block[pos][0], block[pos][1], cnt)
return len(arr), " ".join([str(x) for x in arr])
else:
for i in range(1, 200000):
if cnt[i] == 1 and cnt[i + 1] == 1:
return 2, str(i) + " " + str(i + 1)
return 1, str(a[0])
length, arr = solve(a)
print(length)
print(arr)
|
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NONE NONE WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR LIST VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR RETURN FUNC_CALL VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR BIN_OP VAR NUMBER RETURN NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
There are $n$ people in a row. The height of the $i$-th person is $a_i$. You can choose any subset of these people and try to arrange them into a balanced circle.
A balanced circle is such an order of people that the difference between heights of any adjacent people is no more than $1$. For example, let heights of chosen people be $[a_{i_1}, a_{i_2}, \dots, a_{i_k}]$, where $k$ is the number of people you choose. Then the condition $|a_{i_j} - a_{i_{j + 1}}| \le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|a_{i_1} - a_{i_k}| \le 1$ should be also satisfied. $|x|$ means the absolute value of $x$. It is obvious that the circle consisting of one person is balanced.
Your task is to choose the maximum number of people and construct a balanced circle consisting of all chosen people. It is obvious that the circle consisting of one person is balanced so the answer always exists.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of people.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the height of the $i$-th person.
-----Output-----
In the first line of the output print $k$ β the number of people in the maximum balanced circle.
In the second line print $k$ integers $res_1, res_2, \dots, res_k$, where $res_j$ is the height of the $j$-th person in the maximum balanced circle. The condition $|res_{j} - res_{j + 1}| \le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|res_{1} - res_{k}| \le 1$ should be also satisfied.
-----Examples-----
Input
7
4 3 5 1 2 2 1
Output
5
2 1 1 2 3
Input
5
3 7 5 1 5
Output
2
5 5
Input
3
5 1 4
Output
2
4 5
Input
7
2 2 3 2 1 2 2
Output
7
1 2 2 2 2 3 2
|
kk = lambda: map(int, input().split())
ll = lambda: list(kk())
n = int(input())
d = {}
for v in kk():
if v not in d:
d[v] = 0
d[v] += 1
k = list(d.keys())
k.sort()
i = 0
m = 0
while i < len(k):
start = x = k[i]
total = d[start]
while i + 1 < len(k) and k[i] + 1 == k[i + 1]:
i += 1
total += d[k[i]]
if d[k[i]] == 1:
if total > m:
m, mi = total, (start, k[i])
break
else:
i += 1
if total > m:
m, mi = total, (start, k[i - 1])
print(m)
tbp = []
for i in range(mi[0], mi[1] + 1):
tbp.extend([i] * (d[i] - 1))
print()
for i in range(mi[1], mi[0] - 1, -1):
tbp.append(i)
print(*tbp)
|
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP LIST VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
There are $n$ people in a row. The height of the $i$-th person is $a_i$. You can choose any subset of these people and try to arrange them into a balanced circle.
A balanced circle is such an order of people that the difference between heights of any adjacent people is no more than $1$. For example, let heights of chosen people be $[a_{i_1}, a_{i_2}, \dots, a_{i_k}]$, where $k$ is the number of people you choose. Then the condition $|a_{i_j} - a_{i_{j + 1}}| \le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|a_{i_1} - a_{i_k}| \le 1$ should be also satisfied. $|x|$ means the absolute value of $x$. It is obvious that the circle consisting of one person is balanced.
Your task is to choose the maximum number of people and construct a balanced circle consisting of all chosen people. It is obvious that the circle consisting of one person is balanced so the answer always exists.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of people.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the height of the $i$-th person.
-----Output-----
In the first line of the output print $k$ β the number of people in the maximum balanced circle.
In the second line print $k$ integers $res_1, res_2, \dots, res_k$, where $res_j$ is the height of the $j$-th person in the maximum balanced circle. The condition $|res_{j} - res_{j + 1}| \le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|res_{1} - res_{k}| \le 1$ should be also satisfied.
-----Examples-----
Input
7
4 3 5 1 2 2 1
Output
5
2 1 1 2 3
Input
5
3 7 5 1 5
Output
2
5 5
Input
3
5 1 4
Output
2
4 5
Input
7
2 2 3 2 1 2 2
Output
7
1 2 2 2 2 3 2
|
def unique(a):
if len(a) == 0:
return a
i = 1
j = 1
last = a[0]
while j < len(a):
if a[j] > last:
last = a[j]
a[i], a[j] = a[j], a[i]
i += 1
j += 1
return a[:i]
def expand(cir):
e = []
for i in range(len(cir)):
e.append(cir[i][0])
for i in range(-1, -len(cir) - 1, -1):
for _ in range(cir[i][1] - 1):
e.append(cir[i][0])
return e
n = int(input())
a = list(map(int, input().split(" ")))
c = dict()
for x in a:
if x in c:
c[x] += 1
else:
c[x] = 1
a.sort()
a = unique(a)
n = len(a)
cir = []
cirsize = 0
i = 0
while i < n:
j = i + 1
while j < n and a[j] == a[j - 1] + 1 and c[a[j]] > 1:
j += 1
if j < n and a[j] == a[j - 1] + 1:
j += 1
sz = 0
for p in range(i, j):
sz += c[a[p]]
if sz > cirsize:
cir.clear()
for p in range(i, j):
cir.append((a[p], c[a[p]]))
cirsize = sz
if c[a[j - 1]] == 1 and i != j - 1:
i = j - 1
else:
i = j
print(cirsize)
print(*expand(cir))
|
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
There are $n$ people in a row. The height of the $i$-th person is $a_i$. You can choose any subset of these people and try to arrange them into a balanced circle.
A balanced circle is such an order of people that the difference between heights of any adjacent people is no more than $1$. For example, let heights of chosen people be $[a_{i_1}, a_{i_2}, \dots, a_{i_k}]$, where $k$ is the number of people you choose. Then the condition $|a_{i_j} - a_{i_{j + 1}}| \le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|a_{i_1} - a_{i_k}| \le 1$ should be also satisfied. $|x|$ means the absolute value of $x$. It is obvious that the circle consisting of one person is balanced.
Your task is to choose the maximum number of people and construct a balanced circle consisting of all chosen people. It is obvious that the circle consisting of one person is balanced so the answer always exists.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of people.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the height of the $i$-th person.
-----Output-----
In the first line of the output print $k$ β the number of people in the maximum balanced circle.
In the second line print $k$ integers $res_1, res_2, \dots, res_k$, where $res_j$ is the height of the $j$-th person in the maximum balanced circle. The condition $|res_{j} - res_{j + 1}| \le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|res_{1} - res_{k}| \le 1$ should be also satisfied.
-----Examples-----
Input
7
4 3 5 1 2 2 1
Output
5
2 1 1 2 3
Input
5
3 7 5 1 5
Output
2
5 5
Input
3
5 1 4
Output
2
4 5
Input
7
2 2 3 2 1 2 2
Output
7
1 2 2 2 2 3 2
|
n = int(input())
a = [int(x) for x in input().split()]
c = [0] * 2 * 100007
s = [0] * 2 * 100007
result = 0
dis = 0
result_list = []
for i in a:
c[i] += 1
for i in range(len(c)):
if c[i] > 0:
s[i] = c[i]
result = c[i]
dis = i
break
for i in range(dis + 1, len(c)):
if c[i] > 1:
s[i] = max(s[i - 1] + c[i], c[i - 1] + c[i])
else:
s[i] = 0
for i in range(1, len(s)):
if c[i] + s[i - 1] > result:
result = c[i] + s[i - 1]
dis = i
if c[i] + c[i - 1] > result:
result = c[i] + c[i - 1]
dis = i
for i in range(dis - 1, -1, -1):
if c[i] == 1:
pos = i
break
if c[i] == 0:
pos = i + 1
break
print(result)
for i in range(pos, dis + 1):
print(i, end=" ")
for i in range(dis, pos - 1, -1):
for j in range(1, c[i]):
print(i, end=" ")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR STRING
|
There are $n$ people in a row. The height of the $i$-th person is $a_i$. You can choose any subset of these people and try to arrange them into a balanced circle.
A balanced circle is such an order of people that the difference between heights of any adjacent people is no more than $1$. For example, let heights of chosen people be $[a_{i_1}, a_{i_2}, \dots, a_{i_k}]$, where $k$ is the number of people you choose. Then the condition $|a_{i_j} - a_{i_{j + 1}}| \le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|a_{i_1} - a_{i_k}| \le 1$ should be also satisfied. $|x|$ means the absolute value of $x$. It is obvious that the circle consisting of one person is balanced.
Your task is to choose the maximum number of people and construct a balanced circle consisting of all chosen people. It is obvious that the circle consisting of one person is balanced so the answer always exists.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of people.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the height of the $i$-th person.
-----Output-----
In the first line of the output print $k$ β the number of people in the maximum balanced circle.
In the second line print $k$ integers $res_1, res_2, \dots, res_k$, where $res_j$ is the height of the $j$-th person in the maximum balanced circle. The condition $|res_{j} - res_{j + 1}| \le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|res_{1} - res_{k}| \le 1$ should be also satisfied.
-----Examples-----
Input
7
4 3 5 1 2 2 1
Output
5
2 1 1 2 3
Input
5
3 7 5 1 5
Output
2
5 5
Input
3
5 1 4
Output
2
4 5
Input
7
2 2 3 2 1 2 2
Output
7
1 2 2 2 2 3 2
|
import sys
class Main:
def __init__(self):
self.buff = None
self.index = 0
def __next__(self):
if self.buff is None or self.index == len(self.buff):
self.buff = self.next_line()
self.index = 0
val = self.buff[self.index]
self.index += 1
return val
def next_line(self, _map=str):
return list(map(_map, sys.stdin.readline().split()))
def next_int(self):
return int(next(self))
def solve(self):
n = self.next_int()
x = sorted([self.next_int() for _ in range(0, n)])
ml = -1
_i = 0
_j = 0
j = 0
for i in range(0, n):
j = max(j, i)
while j + 1 < n and x[j + 1] == x[i]:
j += 1
while j + 2 < n and x[j + 2] == x[j] + 1:
j += 2
while j + 1 < n and x[j + 1] == x[j]:
j += 1
jj = j
if j + 1 < n and x[j + 1] == x[j] + 1:
jj += 1
if jj - i > ml:
ml = jj - i
_i = i
_j = jj
a = []
b = []
i = _i
while i <= _j:
a.append(x[i])
i += 1
while i <= _j and x[i] == a[-1]:
b.append(x[i])
i += 1
print(ml + 1)
print(" ".join([str(x) for x in a + b[::-1]]))
def __starting_point():
Main().solve()
__starting_point()
|
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR
|
There are $n$ people in a row. The height of the $i$-th person is $a_i$. You can choose any subset of these people and try to arrange them into a balanced circle.
A balanced circle is such an order of people that the difference between heights of any adjacent people is no more than $1$. For example, let heights of chosen people be $[a_{i_1}, a_{i_2}, \dots, a_{i_k}]$, where $k$ is the number of people you choose. Then the condition $|a_{i_j} - a_{i_{j + 1}}| \le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|a_{i_1} - a_{i_k}| \le 1$ should be also satisfied. $|x|$ means the absolute value of $x$. It is obvious that the circle consisting of one person is balanced.
Your task is to choose the maximum number of people and construct a balanced circle consisting of all chosen people. It is obvious that the circle consisting of one person is balanced so the answer always exists.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of people.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the height of the $i$-th person.
-----Output-----
In the first line of the output print $k$ β the number of people in the maximum balanced circle.
In the second line print $k$ integers $res_1, res_2, \dots, res_k$, where $res_j$ is the height of the $j$-th person in the maximum balanced circle. The condition $|res_{j} - res_{j + 1}| \le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|res_{1} - res_{k}| \le 1$ should be also satisfied.
-----Examples-----
Input
7
4 3 5 1 2 2 1
Output
5
2 1 1 2 3
Input
5
3 7 5 1 5
Output
2
5 5
Input
3
5 1 4
Output
2
4 5
Input
7
2 2 3 2 1 2 2
Output
7
1 2 2 2 2 3 2
|
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
m = 200005
cnt = [0] * m
for i in a:
cnt[i] += 1
k = 0
lr = [-1, -1]
r = -1
for i in range(m):
if i < r:
continue
if 2 <= cnt[i]:
l, r = i, i
x = 0
while 2 <= cnt[r]:
x += cnt[r]
r += 1
r -= 1
x += cnt[l - 1] + cnt[r + 1]
if cnt[l - 1]:
l -= 1
if cnt[r + 1]:
r += 1
if k < x:
k = x
lr = [l, r]
if k == 0:
for i in range(m - 1):
if cnt[i] and cnt[i + 1]:
k = 2
lr = [i, i + 1]
break
if k == 0:
k = 1
lr = [a[0], a[0]]
l, r = lr
a = []
for i in range(l, r + 1):
a.append(i)
cnt[i] -= 1
for i in range(r, l - 1, -1):
for _ in range(cnt[i]):
a.append(i)
print(k)
print(*a)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF NUMBER VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
There are $n$ people in a row. The height of the $i$-th person is $a_i$. You can choose any subset of these people and try to arrange them into a balanced circle.
A balanced circle is such an order of people that the difference between heights of any adjacent people is no more than $1$. For example, let heights of chosen people be $[a_{i_1}, a_{i_2}, \dots, a_{i_k}]$, where $k$ is the number of people you choose. Then the condition $|a_{i_j} - a_{i_{j + 1}}| \le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|a_{i_1} - a_{i_k}| \le 1$ should be also satisfied. $|x|$ means the absolute value of $x$. It is obvious that the circle consisting of one person is balanced.
Your task is to choose the maximum number of people and construct a balanced circle consisting of all chosen people. It is obvious that the circle consisting of one person is balanced so the answer always exists.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of people.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the height of the $i$-th person.
-----Output-----
In the first line of the output print $k$ β the number of people in the maximum balanced circle.
In the second line print $k$ integers $res_1, res_2, \dots, res_k$, where $res_j$ is the height of the $j$-th person in the maximum balanced circle. The condition $|res_{j} - res_{j + 1}| \le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|res_{1} - res_{k}| \le 1$ should be also satisfied.
-----Examples-----
Input
7
4 3 5 1 2 2 1
Output
5
2 1 1 2 3
Input
5
3 7 5 1 5
Output
2
5 5
Input
3
5 1 4
Output
2
4 5
Input
7
2 2 3 2 1 2 2
Output
7
1 2 2 2 2 3 2
|
def process(A):
d = {}
for x in A:
if x not in d:
d[x] = 0
d[x] += 1
my_max = [0, None, None]
current = None
for x in sorted(d):
if current is None or x - 1 not in d:
current = [d[x], x, x]
my_max = max(my_max, current)
elif d[x] > 1 and x - 1 in d:
current[0] += d[x]
current[2] = x
my_max = max(my_max, current)
elif d[x] == 1 and x - 1 in d:
current[0] += d[x]
current[2] = x
my_max = max(my_max, current)
current = [d[x], x, x]
else:
current = None
if current is not None:
my_max = max(my_max, current)
answer_c = my_max[0]
answer_l = []
for i in range(my_max[1], my_max[2] + 1):
answer_l.append(i)
d[i] -= 1
for i in range(my_max[2], my_max[1] - 1, -1):
for j in range(d[i]):
answer_l.append(i)
return [answer_c, answer_l]
n = int(input())
A = [int(x) for x in input().split()]
a1, a2 = process(A)
print(a1)
print(" ".join(map(str, a2)))
|
FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST NUMBER NONE NONE ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR IF VAR NONE BIN_OP VAR NUMBER VAR ASSIGN VAR LIST VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR ASSIGN VAR NONE IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN LIST VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
|
There are $n$ people in a row. The height of the $i$-th person is $a_i$. You can choose any subset of these people and try to arrange them into a balanced circle.
A balanced circle is such an order of people that the difference between heights of any adjacent people is no more than $1$. For example, let heights of chosen people be $[a_{i_1}, a_{i_2}, \dots, a_{i_k}]$, where $k$ is the number of people you choose. Then the condition $|a_{i_j} - a_{i_{j + 1}}| \le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|a_{i_1} - a_{i_k}| \le 1$ should be also satisfied. $|x|$ means the absolute value of $x$. It is obvious that the circle consisting of one person is balanced.
Your task is to choose the maximum number of people and construct a balanced circle consisting of all chosen people. It is obvious that the circle consisting of one person is balanced so the answer always exists.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of people.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the height of the $i$-th person.
-----Output-----
In the first line of the output print $k$ β the number of people in the maximum balanced circle.
In the second line print $k$ integers $res_1, res_2, \dots, res_k$, where $res_j$ is the height of the $j$-th person in the maximum balanced circle. The condition $|res_{j} - res_{j + 1}| \le 1$ should be satisfied for all $j$ from $1$ to $k-1$ and the condition $|res_{1} - res_{k}| \le 1$ should be also satisfied.
-----Examples-----
Input
7
4 3 5 1 2 2 1
Output
5
2 1 1 2 3
Input
5
3 7 5 1 5
Output
2
5 5
Input
3
5 1 4
Output
2
4 5
Input
7
2 2 3 2 1 2 2
Output
7
1 2 2 2 2 3 2
|
rint = lambda: int(input())
rmint = lambda: map(int, input().split())
rlist = lambda: list(rmint())
n = rint()
a = [0, 0] * (10**5 + 1)
for c in rlist():
a[c] += 1
pr = 0
t = 0
ans = 0
g = 0
for i in range(1, 2 * 10**5 + 1):
if a[i] < 2:
t = 0
pr = a[i]
else:
t += a[i]
nx = a[i + 1]
if pr + t + nx > ans:
ans = pr + t + nx
g = i
r = g + 1
l = g + 1
while a[l - 1] > 1:
l -= 1
while a[r] > 1:
r += 1
print(ans)
def out(x, y=1):
while y:
print(x, end=" ")
y -= 1
for i in range(l, r):
out(i)
out(r, a[r])
for i in range(r - 1, l - 1, -1):
out(i, a[i] - 1)
out(l - 1, a[l - 1])
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF NUMBER WHILE VAR EXPR FUNC_CALL VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.