description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
class Solution: def sumFourDivisors(self, nums: List[int]) -> int: def four_divisors3(n): div = set() i = 1 while i * i < n: if n % i == 0: div.add(i) div.add(n // i) if len(div) > 4: return 0 i += 1 return sum(div) if len(div) == 4 else 0 def four_divisors(n): div = set() for i in range(1, int(n**0.5) + 1): if n % i == 0: div.add(i) div.add(n // i) if len(div) > 4: return 0 return sum(div) if len(div) == 4 else 0 def four_divisors2(n): cnt = 0 sums = 0 div = set() if n != 0: for i in range(1, int(n**0.5) + 1): if n % i == 0: cnt += 2 sums += i + n // i if cnt > 4: return 0 return sums if cnt == 4 else 0 if not nums: return 0 nums.sort() total = 0 past = [None, None] for i, v in enumerate(nums): if i > 0 and v == nums[i - 1] and v == past[0]: total += past[1] continue tmp = four_divisors(v) total += tmp past = [v, tmp] return total
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR NUMBER VAR NUMBER IF VAR RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NONE NONE FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR RETURN VAR VAR
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
class Solution: def __init__(self): self.divisors = {} def generate_result(self, n): counter = 1 quo = n // counter while counter <= quo: if n % counter == 0: yield counter if quo != counter: yield quo counter += 1 quo = n // counter def count_divisors(self, n): if n in self.divisors: return self.divisors[n] result = list(self.generate_result(n)) self.divisors[n] = result return result def sumFourDivisors(self, nums: List[int]) -> int: divisors = list(map(self.count_divisors, nums)) four_divisors = list([x for x in divisors if len(x) == 4]) return sum(map(sum, four_divisors))
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR IF BIN_OP VAR VAR NUMBER EXPR VAR IF VAR VAR EXPR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
class Solution: def sumFourDivisors(self, nums: List[int], c={}) -> int: r = 0 for n in nums: if n in c: r += c[n] continue s = n + 1 cnt = 2 end = sqrt(n) if end == int(end): s += end cnt += 1 end -= 1 for i in range(2, int(end) + 1): if n % i == 0: cnt += 2 if cnt > 4: s = 0 break s += i s += n // i if cnt == 4: c.update({n: s}) r += s else: c.update({n: 0}) return r
CLASS_DEF FUNC_DEF VAR VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR DICT VAR VAR VAR VAR EXPR FUNC_CALL VAR DICT VAR NUMBER RETURN VAR VAR
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
class Solution: def sumFourDivisors(self, nums: List[int]) -> int: def check(n): i = 1 cnt = 0 res = 0 while i * i < n: if n % i == 0: cnt += 2 res += i res += n // i i += 1 if cnt > 4: return 0 if i * i == n: cnt += 1 res += i if cnt == 4: return res else: return 0 res = sum(check(n) for n in nums) return res
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER RETURN VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
class Solution: def sumFourDivisors(self, nums: List[int]) -> int: ans = 0 for n in nums: d = set() for cnd in range(1, floor(sqrt(n)) + 1): q, r = divmod(n, cnd) if not r: d.add(q) d.add(cnd) if len(d) == 4: ans += sum(d) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
class Solution: def sumFourDivisors(self, nums: List[int]) -> int: res = 0 for i in range(len(nums)): curr = nums[i] counter = 1 divisors = [] while counter <= sqrt(nums[i]) and len(divisors) < 5: if nums[i] % counter == 0: if counter not in divisors: divisors.append(counter) if nums[i] // counter not in divisors: divisors.append(nums[i] // counter) counter += 1 if len(divisors) == 4: res += sum(divisors) print(divisors) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
class Solution: def sumFourDivisors(self, nums: List[int]) -> int: def check(x): v = set() i = 1 while i * i <= x: if x % i == 0: v.add(i) v.add(x // i) if len(v) > 4: return 0 i += 1 if len(v) == 4: return sum(v) return 0 return sum([check(x) for x in nums])
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
class Solution: def sumFourDivisors(self, nums: List[int]) -> int: return sum([self.sumofDivisors(num) for num in nums]) def sumofDivisors(self, num): s = set() for i in range(1, int(sqrt(num)) + 1): if num % i == 0: s.add(i) s.add(num // i) if len(s) > 4: return 0 return sum(s) if len(s) == 4 else 0
CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
class Solution: def sumFourDivisors(self, nums: List[int]) -> int: res = 0 for n in nums: divisors = self.getDivisors(n) if len(divisors) == 4: res += sum(divisors) return res def getDivisors(self, n): divisors = set() for i in range(1, n): if i**2 > n: break if n % i == 0: divisors.add(i) divisors.add(n // i) if len(divisors) > 4: break return divisors
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
class Solution: def sumFourDivisors(self, nums: List[int]) -> int: ans = 0 for num in nums: ans += self.fourDivisors(num) return ans def fourDivisors(self, num): memo = set() for i in range(1, num + 1): if i * i > num: break if num % i == 0: memo.add(i) memo.add(num // i) if len(memo) > 4: return 0 if len(memo) == 4: return sum(memo) return 0
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR RETURN NUMBER
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
class Solution: def sumFourDivisors(self, nums: List[int]) -> int: ans = 0 def get_divisor(num): val = set() i = 1 while i < math.sqrt(num) + 1: if num % i == 0: val.add(i) val.add(num // i) if len(val) > 4: return val i += 1 return val for num in nums: a = get_divisor(num) if len(a) == 4: ans += sum(a) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR NUMBER RETURN VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
class Solution: def sumFourDivisors(self, nums: List[int]) -> int: def NOD(x): divisor = set() for i in range(1, int(sqrt(x)) + 1): if not x % i: divisor.add(i) divisor.add(x // i) return divisor res = 0 for num in nums: divisor = NOD(num) if len(divisor) == 4: res += sum(divisor) return res
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
class Solution: def sumFourDivisors(self, nums: List[int]) -> int: def find_divisors(n): i = 1 divisors = [] while i * i < n: if n % i == 0: divisors.append(i) divisors.append(n // i) i += 1 if i * i == n: divisors.append(i) return divisors ans = 0 for n in nums: divisors = find_divisors(n) if len(divisors) == 4: ans += sum(divisors) return ans
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
class Solution: def sumFourDivisors(self, nums: List[int]) -> int: ret = 0 for num in nums: ret += self.has_four_divisors(num) return int(ret) def has_four_divisors(self, num): divisor_sum = 0 divisors = 0 for i in range(1, int(sqrt(num)) + 1): if num % i == 0: if i != num / i: divisors += 2 divisor_sum += i divisor_sum += num / i else: divisors += 1 divisor_sum += i if divisors == 4: return divisor_sum return 0
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER RETURN VAR RETURN NUMBER
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
class Solution: def sumFourDivisors(self, nums: List[int]) -> int: res = 0 ls = len(nums) for i in range(ls): divs = set() for j in range(1, floor(sqrt(nums[i])) + 1): if nums[i] % j == 0: divs.add(nums[i] // j) divs.add(j) if len(divs) == 4: res = res + sum(divs) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
class Solution: def sumFourDivisors(self, nums: List[int]) -> int: res = 0 for i in range(len(nums)): curSum, curAns = 1 + nums[i], 2 for j in range(2, int(sqrt(nums[i])) + 1): if nums[i] % j == 0: if j == nums[i] // j: curSum += nums[i] // j curAns += 1 else: curSum += j + nums[i] // j curAns += 2 if curAns == 4: res += curSum return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR RETURN VAR VAR
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
class Solution: def sumFourDivisors(self, nums: List[int]) -> int: def make_divisors(n): divisors = [] for i in range(1, int(n**0.5) + 1): if n % i == 0: divisors.append(i) if i != n // i: divisors.append(n // i) return len(divisors), divisors ret = [0] for n in nums: l, d = make_divisors(n) if l == 4: ret.append(sum(d)) return sum(ret)
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
class Solution: def helper(self, n): if n == 1: return 0 d = int(math.sqrt(n)) cnt = 2 sm = 1 + n while d > 1: if n % d == 0: d1 = n // d if d1 != d: sm += d + d1 cnt += 2 else: sm += d cnt += 1 if cnt > 4: return 0 d -= 1 if cnt == 4: return sm return 0 def sumFourDivisors(self, nums: List[int]) -> int: res = 0 for n in nums: res += self.helper(n) return res
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR RETURN NUMBER FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
class Solution: def divs(self, x): memo = self.memo if x in memo: return memo[x] res = 2 if x > 1 else 1 B = {1, x} for a in range(2, x): if x < a**2: break if not x % a: res += 1 if x == a**2 else 2 B.update({a, x // a}) if res > 4: break a += 1 memo[x] = res, B return res, B def sumFourDivisors(self, A): self.memo = {} res = 0 for x in A: r, B = self.divs(x) if r == 4: res += sum(B) return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
class Solution: def sumFourDivisors(self, nums: List[int]) -> int: def getDivs(num): result = [] for div in range(1, int(num ** (1 / 2)) + 1): if num % div == 0: result.append(div) result.append(num // div) if len(result) > 4: print(num, result) return 0 if int(num ** (1 / 2)) * int(num ** (1 / 2)) == num: result.pop() if len(result) == 4: return sum(result) else: return 0 total = 0 for num in nums: total += getDivs(num) return total
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER NUMBER VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
class Solution: def sumFourDivisors(self, nums: List[int]) -> int: ans = 0 for num in nums: cnt = 0 for i in range(2, int(num**0.5) + 1): if num % i == 0: cnt += 1 d = i if cnt > 1: break if cnt == 1 and d != num // d: ans += 1 + d + num // d + num return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR VAR RETURN VAR VAR
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
class Solution: cache = {} def factors(self, n): if n in self.cache: return self.cache[n] result = set() for i in range(1, int(n**0.5) + 1): div, mod = divmod(n, i) if mod == 0: result |= {i, div} self.cache[n] = result return result def sumFourDivisors(self, nums: List[int]) -> int: factors = [self.factors(f) for f in nums] return sum([sum(f) for f in factors if len(f) == 4])
CLASS_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
def div(n): c = 0 i = 1 k = 0 if sqrt(n).is_integer(): return 0 while i * i < n and c <= 3: if n % i == 0: c += 1 k += i + n // i i += 1 if c == 2: return k else: return 0 class Solution: def sumFourDivisors(self, nums: List[int]) -> int: ans = 0 for i in nums: ans += div(i) return ans
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL FUNC_CALL VAR VAR RETURN NUMBER WHILE BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR RETURN NUMBER CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
class Solution: def sumFourDivisors(self, nums: List[int]) -> int: if not nums: return 0 ans = 0 for n in nums: rangemax = int(math.sqrt(n)) factsum = n + 1 factcount = 2 for f1 in range(2, rangemax + 1): if not n % f1: f2 = n // f1 factcount += 1 factsum += f1 if f1 != f2: factcount += 1 factsum += f2 if factcount > 4 or factcount % 2: break if factcount == 4: ans += factsum return ans
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR RETURN VAR VAR
Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0.   Example 1: Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.   Constraints: 1 <= nums.length <= 10^4 1 <= nums[i] <= 10^5
class Solution: def sumFourDivisors(self, nums: List[int]) -> int: ret_count = {} ret_sum = {} for n in nums: if n in ret_sum: if ret_sum[n] is not None: ret_count[n] += 1 continue cur_div = 2 hit_div = None while cur_div * cur_div <= n: if n % cur_div == 0: if hit_div is None: hit_div = cur_div else: hit_div = None break cur_div += 1 if hit_div is not None and hit_div != n // hit_div: res = 1 + n + hit_div + n // hit_div ret_count[n] = 1 else: res = None ret_sum[n] = res ret = sum(ret_sum[k] * c for k, c in list(ret_count.items())) return ret
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR IF VAR VAR IF VAR VAR NONE VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE VAR NUMBER IF VAR NONE VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) arr = list(map(int, input().split())) d = [0] * 10 max_ = 1 for i in range(n): d[arr[i] - 1] += 1 lol = sorted([x for x in d if x > 0]) kek = True for j in range(len(lol) - 2): if lol[j] != lol[j + 1]: kek = False break if kek and len(lol) == 1: max_ = max(max_, i + 1) continue elif kek and lol[-1] - 1 == lol[-2]: max_ = max(max_, i + 1) continue kek = True for j in range(1, len(lol) - 1): if lol[j] != lol[j + 1]: kek = False break if kek and lol[0] - 1 == 0: max_ = max(max_, i + 1) print(max_)
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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = [int(x) for x in input().split()] d = {} ans = 0 t = 0 if n == 1: print("1") exit() for i in a: if i in d: d[i] += 1 else: d[i] = 1 x = [d[i] for i in d] t += 1 for i in range(len(x)): x[i] -= 1 tt = list(set(x)) c = 1 if 0 in tt: c = 2 if len(tt) == c: ans = t x[i] += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF NUMBER VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = list(map(int, input().split())) ans = n s = set() x = {i: (0) for i in range(1, 100001)} y = {} for i in range(n): e = a[i] c = x[e] if c: y[c].remove(e) if len(y[c]) == 0: s.remove(c) x[e] += 1 c += 1 if c not in y: y[c] = set({e}) else: y[c].add(e) s.add(c) if len(s) == 2: l = list(s) p = l[0] q = l[1] py = len(y[p]) qy = len(y[q]) if py == 1: if p == q + 1 or p == 1: ans = i + 1 if qy == 1: if q == p + 1 or q == 1: ans = i + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) A = list(map(int, input().split())) ans = 1 colors = {} for i in range(1, 11): colors[i] = 0 for j in range(n): i = A[j] colors[i] += 1 q = [] for elem in colors: if colors[elem]: q.append(colors[elem]) q.sort() if len(q) == 1: ans = j + 1 elif q[-1] == q[-2] + 1 and q[0] == q[-2]: ans = j + 1 elif q[-1] == q[1] and q[0] == 1: ans = j + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) d = list(map(int, input().split())) MAX = 10**5 + 1 colors = 0 nofc = [0] * MAX cofn = [0] * MAX m = 0 r = 1 for p, i in enumerate(d): nofc[i] += 1 cofn[nofc[i] - 1] -= 1 cofn[nofc[i]] += 1 m = max(m, nofc[i]) if cofn[1] == p + 1 or cofn[p + 1] == 1: r = p + 1 elif cofn[1] == 1 and m * cofn[m] == p: r = p + 1 elif cofn[m] == 1 and (m - 1) * cofn[m - 1] == p + 1 - m: r = p + 1 print(r)
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 BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
N = int(100000.0 + 10) n = int(input().strip()) arr = list(map(int, input().strip().split())) counts = [0] * N freq = [0] * N maxVal = 0 for i in range(1, n + 1): color = arr[i - 1] counts[freq[color]] -= 1 freq[color] += 1 counts[freq[color]] += 1 maxVal = max(maxVal, freq[color]) ok = False if counts[1] == i: ok = True elif counts[i] == 1: ok = True elif counts[1] == 1 and counts[maxVal] * maxVal == i - 1: ok = True elif counts[maxVal - 1] * (maxVal - 1) == i - maxVal and counts[maxVal] == 1: ok = True if ok: ans = i print(ans)
ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER 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 ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = list(map(int, input().split())) d = {} for i in range(n): d[a[i]] = 0 x = 1 for i in range(1, n): if a[i] == a[i - 1]: x = i + 1 else: break ll = [(0) for i in range(100001)] l = [] for i in range(n): if d[a[i]] != 0: if ll[d[a[i]]] == 1: for c in range(len(l)): if l[c] == d[a[i]]: l.pop(c) break ll[d[a[i]]] -= 1 d[a[i]] += 1 if d[a[i]] not in l: l.append(d[a[i]]) l.sort() ll[d[a[i]]] += 1 if len(l) > 0 and l[0] == 0: l.pop(l[0]) if len(l) == 2: if abs(l[0] - l[1]) == 1 and ll[l[1]] == 1: x = max(x, i + 1) elif l[0] == 1 and ll[1] == 1: x = max(x, i + 1) if len(l) == 1 and l[0] == 1: x = max(x, i + 1) print(x)
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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = [*map(int, input().split())] num = [0] * (10**5 + 1) num_num = [0] * (n + 1) ans, ma = 1, 0 for i in range(n): num_num[num[a[i]]] = num_num[num[a[i]]] - 1 num[a[i]] = num[a[i]] + 1 ma = max(ma, num[a[i]]) num_num[num[a[i]]] = num_num[num[a[i]]] + 1 if ( ma == 1 or num_num[ma] == -num_num[0] - 1 and num_num[1] == 1 or num_num[ma - 1] == -num_num[0] - 1 and (num_num[1] == 1 or num_num[ma] == 1) ): ans = i + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = list(map(int, input().split())) d = {} for i in range(n): d[a[i]] = 0 x = 1 for i in range(1, n): if a[i] == a[i - 1]: x = i + 1 else: break for i in range(n): d[a[i]] += 1 l = list(d.values()) l = list(set(l)) ll = list(d.values()) l.sort() if l[0] == 0: l.pop(l[0]) if len(l) == 2: if abs(l[0] - l[1]) == 1 and ll.count(l[1]) == 1: x = max(x, i + 1) elif l[0] == 1 and ll.count(1) == 1: x = max(x, i + 1) if len(l) == 1 and l[0] == 1: x = max(x, i + 1) print(x)
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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) u = list(map(int, input().split())) cnts = [(0) for _ in range(10**5 + 1)] cntcnt = [10**5 + 1] + [(0) for _ in range(10**5)] b = 1 for i in range(n): cnts[u[i]] += 1 cntcnt[cnts[u[i]] - 1] -= 1 cntcnt[cnts[u[i]]] += 1 nz = 10**5 + 1 - cntcnt[0] j = i + 1 if nz == 1: b = j continue if j % nz == 1: c = j // nz if cntcnt[c] + 1 == nz: b = j continue if ( cntcnt[1] and (j - 1) % (nz - 1) == 0 and cntcnt[(j - 1) // (nz - 1)] == nz - 1 + (j == nz) ): b = j print(b)
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 VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST BIN_OP BIN_OP NUMBER NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR IF VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
maxn = 10**5 + 11 n = int(input()) m = list(map(int, input().split())) f = [(0) for i in range(maxn)] g = [set() for i in range(maxn)] ans = 1 known = set() for j in range(n): i = m[j] if i not in known: known.add(i) f[i] = 0 g[f[i]].add(i) g[f[i]].remove(i) f[i] += 1 g[f[i]].add(i) if f[i] - 1 > 0 and len(g[f[i] - 1]) * (f[i] - 1) + len(g[f[i]]) == j: ans = j + 1 if j + 1 < n and len(g[f[i]]) * f[i] == j + 1: ans = j + 2 if len(g[f[i]]) == j + 1: ans = j + 1 if len(g[f[i] + 1]) == 1 and len(g[f[i]]) * f[i] + f[i] == j: ans = j + 1 if len(g[f[i]]) * f[i] == j: ans = j + 1 print(ans)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER 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 NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) arr = [int(i) for i in input().split()] arr1 = [(0) for i in range(10**5 + 1)] arr2 = [(0) for i in range(10**5 + 1)] streaker = 0 maxa = 0 for i in range(n): arr2[arr1[arr[i]]] -= 1 arr1[arr[i]] += 1 arr2[arr1[arr[i]]] += 1 maxa = max(maxa, arr1[arr[i]]) if arr2[1] == i + 1: streaker = i + 1 elif arr2[i + 1] == 1: streaker = i + 1 elif arr2[1] == 1 and maxa * arr2[maxa] == i: streaker = i + 1 elif (maxa - 1) * arr2[maxa - 1] == i + 1 - maxa and arr2[maxa] == 1: streaker = i + 1 print(streaker)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
def main(): n = int(input()) colors = list(map(int, input().split())) dist_colors = {} x = 0 for i in range(n): if colors[i] not in dist_colors.keys(): dist_colors[colors[i]] = 1 else: dist_colors[colors[i]] += 1 keys = [] for j in dist_colors.keys(): keys.append(j) for j in range(len(dist_colors)): curr = dist_colors[keys[j]] dist_colors[keys[j]] -= 1 k = 0 while k < len(keys) and dist_colors[keys[k]] == 0: k += 1 if k < len(keys): c = max(dist_colors[keys[k]], 0) else: c = 0 count = 0 for k in range(len(dist_colors)): if dist_colors[keys[k]] == c or dist_colors[keys[k]] == 0: count += 1 else: break dist_colors[keys[j]] = curr if count == len(dist_colors): x = i + 1 break print(x) 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 DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = list(map(int, input().split())) if n == 1 or n == 2: print(n) return notpar = 0 occ = [(0) for i in range(10**5 + 1)] base = 1 gotone = False leader = -1 leadby = 0 best = 0 occ[a[0]] += 1 cnt = 1 at1 = 1 for i in range(1, n): occ[a[i]] += 1 if occ[a[i]] == 1: notpar += 1 at1 += 1 cnt += 1 elif occ[a[i]] == 2: at1 -= 1 if occ[a[i]] == base: notpar -= 1 elif occ[a[i]] > base: if gotone: if a[i] != leader: base += 1 notpar = cnt - 2 leadby -= 1 if leadby == 0: gotone = False leader = -1 else: leadby += 1 else: gotone = True leader = a[i] leadby = 1 if ( notpar == 0 and (leadby == 1 or leadby == 0 and base == 1) or notpar == 1 and leadby == 0 and at1 > 0 ): best = i + 1 if cnt == 1: best = i + 1 print(best)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) u = list(map(int, input().split())) x = [0] * 10 mx = 0 if n == 1: print(1) exit(0) for i in range(n): x[u[i] - 1] += 1 fl = False for j in range(10): x1 = x[:] if x1[j] != 0: x1[j] -= 1 x2 = -1 k2 = 0 for l in x1: if l != 0: if k2 == 0 or x2 != l: k2 += 1 x2 = l if k2 == 1: fl = True if fl: mx = i + 1 print(mx)
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 NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = [int(x) for x in input().split()] hsh = [(-1) for i in range(10)] fin = -1 for i in range(n): if hsh[a[i] - 1] == -1: hsh[a[i] - 1] = i for i in range(len(hsh)): h = [0] * 10 ans = 0 if hsh[i] != -1: for j in range(n): if a[j] - 1 == i and j == hsh[a[j] - 1]: pass else: h[a[j] - 1] += 1 ln = len(set(h)) - (0 in h) if ln == 1: ans = j + 1 fin = max(fin, ans) print(max(1, fin))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = [] dctt = {} maxx = 0 ind = 0 for i in input().split(): ind += 1 if i not in dctt: dctt[i] = 0 dctt[i] += 1 countss = {} for color, count in dctt.items(): if count not in countss: countss[count] = 0 countss[count] += 1 if len(countss) > 2: continue elif len(countss) == 2: a = countss.popitem() b = countss.popitem() if a[0] == 1 and a[1] == 1: maxx = ind elif b[0] == 1 and b[1] == 1: maxx = ind elif abs(a[0] - b[0]) == 1: if a[0] > b[0]: if a[1] == 1: maxx = ind elif b[1] == 1: maxx = ind else: a = countss.popitem() if a[0] == 1: maxx = ind elif a[1] == 1: maxx = ind print(maxx)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = [int(x) for x in input().split()] counter = [0] * 10**5 how = [0] * (10**5 + 1) answer = big = 0 for i in range(n): counter[a[i] - 1] += 1 big = max(big, counter[a[i] - 1]) how[counter[a[i] - 1]] += 1 how[counter[a[i] - 1] - 1] -= 1 if how[1] == i + 1: answer = i + 1 elif how[big] == 1 and how[big - 1] * (big - 1) == i + 1 - big: answer = i + 1 elif how[big] == 1 and big == i + 1: answer = i + 1 elif how[1] == 1 and how[big] * big == i: answer = i + 1 print(answer)
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 NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a, b = {}, {} ans = 1 t = [0] + list(map(int, input().split())) for i in range(1, n + 1): x = t[i] a[x] = a.get(x, 0) + 1 b[a[x]] = b.get(a[x], 0) + 1 if a[x] * b[a[x]] == i and i != n: ans = i + 1 elif a[x] * b[a[x]] == i - 1: ans = i print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR DICT DICT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = list(map(int, input().split())) k = set() b = 0 ind = {} for i in a: if i not in k: k.add(i) ind[i] = b b += 1 l = set() occ1 = 0 ans = 1 s = [(-1) for i in set(a)] for i in range(n): if a[i] in l: j = ind[a[i]] s[j] += 1 else: s[ind[a[i]]] = 1 l.add(a[i]) an = set(s) - set([-1]) if len(an) == 2: p = [i for i in an] p1 = s.count(p[0]) p2 = s.count(p[1]) if p1 == 1: if p[0] == p[1] + 1 or p[0] == 1: ans = i + 1 if p2 == 1: if p[1] == p[0] + 1 or p[1] == 1: ans = i + 1 elif len(an) == 1: p = [i for i in an] p1 = s.count(p[0]) if p[0] == 1 or p1 == 1: ans = i + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR LIST NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
ii = lambda: int(input()) kk = lambda: map(int, input().split()) k2 = lambda: map(lambda x: int(x) - 1, input().split()) ll = lambda: list(kk()) n = ii() d = [0] * 10 maxi = 2 for j, v in enumerate(k2()): d[v] += 1 vs = {} for i in range(10): if d[i] == 0: continue if d[i] not in vs: vs[d[i]] = 0 vs[d[i]] += 1 if len(vs) > 2: continue if len(vs) == 2: val = max(list(vs.keys())) if 1 in vs and vs[1] == 1 or val - 1 in vs and vs[val] == 1: maxi = j elif len(vs) == 1 and (1 in vs or d.count(0) == 9): maxi = j print(max(maxi + 1, min(2, n)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER 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 ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF NUMBER VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
import sys input = sys.stdin.readline count = [(0) for i in range(100001)] lengthCount = [(0) for i in range(100001)] n = int(input()) ans = 0 a = list(map(int, input().split())) allLengthsSet = set() for i in range(n): lengthCount[count[a[i]]] -= 1 if lengthCount[count[a[i]]] == 0: allLengthsSet.remove(count[a[i]]) count[a[i]] += 1 lengthCount[count[a[i]]] += 1 allLengthsSet.add(count[a[i]]) if len(allLengthsSet) == 1: allLengths = list(allLengthsSet) if lengthCount[allLengths[0]] == 1 or allLengths[0] == 1: ans = i + 1 elif len(allLengthsSet) == 2: allLengths = list(allLengthsSet) if lengthCount[allLengths[0]] == 1 and ( allLengths[0] == 1 or allLengths[0] == allLengths[1] + 1 ): ans = i + 1 elif lengthCount[allLengths[1]] == 1 and ( allLengths[1] == 1 or allLengths[1] == allLengths[0] + 1 ): ans = i + 1 print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER 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 VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) u = [int(s) for s in input().split()] a = [(0) for i in range(100000)] im = 0 d = dict() for i in range(n): auiold = a[u[i] - 1] if auiold != 0: d[auiold] -= 1 if d[auiold] == 0: d.pop(auiold) a[u[i] - 1] += 1 auinew = a[u[i] - 1] d[auinew] = d.setdefault(auinew, 0) + 1 if len(d.keys()) == 1 and 1 in d.keys(): im = i elif len(d.keys()) == 1 and len(d.values()) == 1 and 1 in d.values(): im = i elif len(d.keys()) == 2 and 1 in d.keys() and d[1] == 1: im = i elif ( len(d.keys()) == 2 and max(d.keys()) == min(d.keys()) + 1 and d[max(d.keys())] == 1 ): im = i print(im + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR ASSIGN VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR ASSIGN VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = list(map(int, input().split())) b = [0] * (max(a) + 1) for i in a: b[i] += 1 ans = 1 for i in range(n - 1, -1, -1): c = [j for j in b] c.sort() ind = c.count(0) if ( c.count(c[ind]) == len(c) - ind - 1 and c[-1] - 1 == c[ind] or c.count(c[-1]) == len(c) - 1 - ind and c[ind] == 1 or c.count(c[-1]) == len(c) - ind and c[-1] == 1 or len(c) - ind == 1 ): ans = i + 1 break else: b[a[i]] -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) ans = 0 a = list(map(int, input().split())) count = [0] * 10 k = 0 for each in a: k += 1 count[each - 1] += 1 f = [k for k in count if k > 0] fmin = min(f) fmax = max(f) lf, lsf = len(f), len(set(f)) if lsf > 2: continue if lf == 1: ans = k elif fmax - fmin == 1 and f.count(fmax) == 1: ans = k elif fmax == fmin == 1: ans = k elif fmin == 1 and f.count(fmin) == 1: ans = k print(n if not ans else ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
def streak(arr, l): x = [] for i in arr: if i != 0: x.append(i) mini = min(x) maxi = max(x) mincount = x.count(mini) maxcount = x.count(maxi) if mini == 1 and mincount == l: return True elif mini == 1 and mincount == 1 and maxi * maxcount == l - 1: return True elif mini + 1 == maxi and maxcount == 1 and maxi * maxcount + mini * mincount == l: return True elif len(x) == 1: return True return False n = int(input()) arr = [int(i) for i in input().split()] arr1 = [(0) for i in range(10)] streaker = 0 for i in range(n): arr1[arr[i] - 1] += 1 for i in range(n - 1, -1, -1): if streak(arr1, i + 1): streaker = i + 1 break arr1[arr[i] - 1] -= 1 print(streaker)
FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) u = [int(item) for item in input().split()] d = {key: (0) for key in u} for x in u: d[x] += 1 nb_occs = [0] * (n + 1) for x in d: nb_occs[d[x]] += 1 s = set() for i in range(len(nb_occs)): if nb_occs[i] != 0: s.add(i) x = n - 1 count = len(s) while 1: if len(s) == 1: items = list(s) if nb_occs[items[0]] == 1 or items[0] == 1: break if len(s) == 2: items = sorted(list(s)) if ( items[0] == 1 and nb_occs[1] == 1 or items[1] == items[0] + 1 and nb_occs[items[1]] == 1 ): break v = u[x] nb_occs[d[v]] -= 1 if nb_occs[d[v]] == 0: s.remove(d[v]) d[v] -= 1 if d[v] != 0: nb_occs[d[v]] += 1 s.add(d[v]) x -= 1 print(x + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
def solve(colors): n = len(colors) color_count = {} count_count = {} max_x = 1 for c in colors: color_count[c] = 0 distinct_counts = set() for i in range(0, n): color = colors[i] old_count = color_count[color] color_count[color] += 1 if old_count > 0: count_count[old_count] -= 1 r = count_count[old_count] if r == 0 and old_count in distinct_counts: distinct_counts.remove(old_count) if old_count + 1 in count_count: count_count[old_count + 1] += 1 else: count_count[old_count + 1] = 1 distinct_counts.add(old_count + 1) if len(distinct_counts) > 2: continue interesting_counts = sorted(list(distinct_counts)) if len(interesting_counts) == 1: if interesting_counts[0] == 1 or old_count + 1 == i + 1: max_x = i + 1 continue k1, k2 = interesting_counts if k1 + 1 == k2 and count_count[k2] == 1: max_x = i + 1 if k1 == 1 and count_count[k1] == 1 or k2 == 1 and count_count[k2] == 1: max_x = i + 1 print(max_x) def main(): n = int(input().strip()) colors = list(map(int, input().strip().split())) solve(colors) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF 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 VAR EXPR FUNC_CALL VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) l1 = list(map(int, input().split())) d1 = {} d2 = {} total = 0 l2 = [0] * 10 i = 0 ans = 1 for item in l1: i += 1 l2[item - 1] += 1 x = sum(l2) y = 10 - l2.count(0) if (x - 1) % y == 0: z = (x - 1) // y else: z = -1 if y > 1 and (x - 1) % (y - 1) == 0: f = (x - 1) // (y - 1) else: f = -1 c1 = 0 c2 = 0 if z == -1 and f == -1: continue for item in l2: if item != 0: if z != -1 and item == z: c1 += 1 if f != -1 and item == f: c2 += 1 if c1 == y - 1 or c2 == y - 1: ans = i if z == 1: if c1 == y: ans = i if f == 1: if c2 == y: ans = i print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
input() a, b = {}, {} r = i = 0 for u in input().split(): i += 1 c = a.get(u, 0) if c: b[c] -= 1 if b[c] == 0: del b[c] c += 1 a[u] = c b[c] = b.get(c, 0) + 1 l = len(b) if l == next(iter(b)) == 1 or len(a) == 1: r = i if l == 2: x, y = sorted(b) if x == b[x] == 1 or y - x == b[y] == 1: r = i print(r)
EXPR FUNC_CALL VAR ASSIGN VAR VAR DICT DICT ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
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): return sys.stdin.readline().split() def next_ints(self): return [int(x) for x in sys.stdin.readline().split()] def next_int(self): return int(self.next()) def solve(self): n = self.next_int() x = self.next_ints() cnt = [(0) for _ in range(0, max(x) + 1)] cc = [(0) for _ in range(0, n + 1)] mx = 0 r = 1 for i, xx in enumerate(x): cc[cnt[xx]] -= 1 cnt[xx] += 1 cc[cnt[xx]] += 1 mx = max(cnt[xx], mx) if cc[mx] * mx == i and cc[1] == 1: r = i + 1 if cc[mx] == 1 and cc[mx - 1] * (mx - 1) + mx - 1 == i: r = i + 1 if mx == 1: r = i + 1 print(r) Main().solve()
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 RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL FUNC_CALL VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) u = list(map(int, input().split())) freq = [(0) for i in range(10**5)] for i in u: freq[i - 1] += 1 my_dict = {} def is_good(freq, my_dict, t): if t == 0: for i in freq: if i != 0: if i not in my_dict.keys(): my_dict[i] = 1 else: my_dict[i] += 1 else: if freq[u[-t] - 1] in my_dict.keys(): my_dict[freq[u[-t] - 1]] += 1 elif freq[u[-t] - 1] != 0: my_dict[freq[u[-t] - 1]] = 1 my_dict[freq[u[-t] - 1] + 1] -= 1 if my_dict[freq[u[-t] - 1] + 1] == 0: my_dict.pop(freq[u[-t] - 1] + 1, None) N = len(my_dict.keys()) if N > 2: return False elif N == 2: if 1 in my_dict.keys(): if my_dict[1] == 1: return True a, b = list(my_dict.keys()) if (abs(a - b) == 1) & (my_dict[max(a, b)] == 1): return True else: return False elif N == 1: v = list(my_dict.values())[0] k = list(my_dict.keys())[0] if k == 1: return True elif v > 1: return False else: return True else: return True for i in range(n): N = n - i if is_good(freq, my_dict, i): print(N) break else: freq[u[-1 - i] - 1] -= 1
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 VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER NONE ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER IF NUMBER FUNC_CALL VAR IF VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) l = [int(x) for x in input().split()] d = [0] * 10 ans = 0 for i in range(n): ll = {} d[l[i] - 1] += 1 for j in range(10): if d[j] != 0: if d[j] not in ll: ll[d[j]] = 0 ll[d[j]] += 1 if len(ll) == 1: if 1 in ll: ans = i + 1 else: for x in ll: if ll[x] == 1: ans = i + 1 if len(ll) == 2: lll = [] for x in ll: lll.append(x) lll.sort() if 1 in ll and ll[1] == 1: ans = i + 1 elif abs(lll[0] - lll[1]) == 1 and ll[lll[1]] == 1: ans = i + 1 print(ans)
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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) u = [int(u) for u in input().split()] NMAX = 100005 suma = [(0) for _ in range(NMAX)] total = [(0) for _ in range(NMAX)] diferentes = 0 sol = 0 maximo = 1 for i, v in enumerate(u): if total[v] == 0: diferentes += 1 else: suma[total[v]] -= 1 total[v] += 1 suma[total[v]] += 1 maximo = max(maximo, total[v]) if diferentes <= 1: sol = i if suma[maximo - 1] == diferentes - 1 and suma[maximo] == 1: sol = i if suma[maximo] == diferentes - 1 and suma[1] == 1: sol = i if suma[1] == diferentes: sol = i print(sol + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) u = list(map(int, input().split())) true = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] big = 1 for i in range(n): true[u[i] - 1] += 1 vals = set(true) try: vals.remove(0) except: pass if len(vals) == 2 and 1 in vals and true.count(1) == 1: big = max(big, i + 1) elif len(vals) == 1 and 1 in vals: big = max(big, i + 1) elif len(vals) == 1 and true.count(max(vals)) == 1: big = max(big, i + 1) elif len(vals) == 2: a = max(vals) if true.count(a) == 1 and min(vals) == a - 1: big = max(big, i + 1) print(big)
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 NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) l = list(map(int, input().split())) count = [0] * (pow(10, 5) + 2) num = [0] * (pow(10, 5) + 1) mx = 0 s = 0 for i in range(n): v = l[i] count[num[v]] -= 1 num[v] += 1 mx = max(mx, num[v]) count[num[v]] += 1 f = 0 if count[1] == i + 1: f = 1 elif count[1] == 1 and count[mx] * mx == i: f = 1 elif count[mx] == 1 and count[mx - 1] * (mx - 1) == i - mx + 1: f = 1 elif mx == i + 1: f = 1 if f == 1: s = i + 1 print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = list(map(int, input().split())) n += 1 f = [0] * (10**5 + 10) x = 0 mx = 0 cnt = [0] * (10**5 + 10) i = 1 ans = 0 for c in a: cnt[f[c]] -= 1 f[c] += 1 cnt[f[c]] += 1 mx = max(mx, f[c]) ok = False if cnt[1] == i: ok = True elif cnt[1] == 1 and cnt[mx] * mx == i - 1: ok = True elif cnt[mx - 1] * (mx - 1) == i - mx and cnt[mx] == 1: ok = True if ok: ans = i i += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = list(map(int, input().split())) c = [0] * 11 ma = 1 for i in range(n): c[a[i]] += 1 for k in range(1, 11): if c[k] > 0: c[k] -= 1 s = {} for j in range(1, 11): if c[j] > 0: if c[j] not in s: s[c[j]] = 1 else: s[c[j]] += 1 c[k] += 1 if len(s) == 1: ma = max(ma, i + 1) break print(ma)
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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
def check(): mn = float("inf") mx = 0 k = 0 for i in mas: if i != 0: k += 1 mx = max(mx, i) mn = min(mn, i) if ( mx - mn <= 1 and mas.count(mx) == 1 or mn == 1 and k == 1 + mas.count(mx) or mn == mx == 1 ): return True return False n = int(input()) l = list(map(int, input().split())) mas = [0] * 10 ans = 0 for i in range(n): mas[l[i] - 1] += 1 if check(): ans = i print(ans + 1)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) arr = map(int, input().split()) occ = {} occ_count = {} ans = 1 for i, u in enumerate(arr): if u in occ: occ[u] += 1 if occ[u] in occ_count: occ_count[occ[u]] += 1 else: occ_count[occ[u]] = 1 if occ_count[occ[u] - 1] == 1: del occ_count[occ[u] - 1] else: occ_count[occ[u] - 1] -= 1 else: occ[u] = 1 if 1 in occ_count: occ_count[1] += 1 else: occ_count[1] = 1 if len(occ_count) == 1: if 1 in occ_count or occ_count[list(occ_count.keys())[0]] == 1: ans = i + 1 if len(occ_count) == 2: a, b = sorted(occ_count.keys()) if a == 1 and occ_count[a] == 1: ans = i + 1 if a + 1 == b and occ_count[b] == 1: ans = i + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER IF NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
def check(c): d = {} for i in range(10): if c[i] != 0: if c[i] not in d: d[c[i]] = 1 else: d[c[i]] += 1 l = list(d.keys()) if len(l) == 1 and (l[0] == 1 or l[0] != 1 and d[l[0]] == 1): return True elif len(l) == 2: if l[0] == 1 and d[l[0]] == 1 or l[1] == 1 and d[l[1]] == 1: return True elif l[0] == l[1] + 1: if d[l[0]] == 1: return True else: return False elif l[1] == l[0] + 1: if d[l[1]] == 1: return True else: return False else: return False else: return False n = int(input()) l = list(map(int, input().split())) c = [(0) for _ in range(10)] t = 1 c[l[0] - 1] += 1 for i in range(1, n): c[l[i] - 1] += 1 if check(c): t = i + 1 print(t)
FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER RETURN NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER RETURN NUMBER RETURN 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 NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a, b = {}, {} r = i = 0 for u in input().split(): i += 1 c = a[u] = a.get(u, 0) + 1 d = b[c] = b.get(c, 0) + c r = (r, d)[d in (i - (i == n), i - 1)] print(r + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR DICT DICT ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) array = list(map(int, input().split())) d = [0] * 100010 v = [0] * 100010 answer = 0 m = 0 for x in range(len(array)): c = array[x] v[d[c]] -= 1 d[c] += 1 v[d[c]] += 1 m = max(m, d[c]) f = False if v[1] == x + 1: f = True if v[x + 1] == 1: f = True if v[1] == 1 and v[m] * m == x: f = True if v[m - 1] * (m - 1) == x + 1 - m and v[m] == 1: f = True if f: answer = x + 1 print(answer)
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 NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = list(map(int, input().split())) mem = {} mark = [(0) for _ in range(11)] max_step = 0 c = 0 for v in a: c += 1 word2 = str(mark[v]) mark[v] += 1 word = str(mark[v]) mem[word] = mem.setdefault(word, 0) + 1 if word2 != "0": if mem.get(word2) == 1: del mem[word2] else: mem[word2] -= 1 if len(mem) == 1 and "1" in mem: max_step = c elif len(mem) == 1: for k, v in mem.items(): if v == 1: max_step = c elif len(mem) == 2: temp = [] for k, v in mem.items(): temp.append([int(k), v]) if k == "1" and v == 1: max_step = c if max_step != c: temp.sort() if temp[1][0] - temp[0][0] == 1 and temp[1][1] == 1: max_step = c print(max_step)
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 VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER STRING VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) arr = list(map(int, input().split())) dp = [0] * 11 ans = 0 for i in range(n): dp[arr[i]] += 1 d = {} for j in range(1, 11): if dp[j] > 0: d[dp[j]] = d.get(dp[j], 0) + 1 if len(d) == 2: s1 = [] for j in d: s1.append(j) a, b = s1[0], s1[1] if a - 1 == 0 and d[a] == 1: ans = max(ans, i + 1) if a - 1 > 0 and a - 1 == b and d[a] == 1: ans = max(ans, i + 1) if b - 1 == 0 and d[b] == 1: ans = max(ans, i + 1) if b - 1 > 0 and b - 1 == a and d[b] == 1: ans = max(ans, i + 1) if len(d) == 1: if dp.count(0) == 10: ans = max(ans, i + 1) if d.get(1, 0) > 0: ans = max(ans, i + 1) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
def convert(string): return string.split(sep=None, maxsplit=-1) def toint(lst): result = [] for i in lst: result.append(int(i)) return result def printeach(lst): for i in lst: print(i, end="") def valid(dic): val = {} for i in dic: if dic[i] != 0: try: val[dic[i]] += 1 except: val[dic[i]] = 1 if len(val) > 2: return False elif len(val) < 2: for i in val: if i == 1: return True elif val[i] == 1: return True else: return False else: keys = list(val.keys()) values = list(val.values()) if keys[0] == 1 and values[0] == 1: return True elif keys[1] == 1 and values[1] == 1: return True elif keys[0] - keys[1] == 1 and values[0] == 1: return True elif keys[0] - keys[1] == -1 and values[1] == 1: return True return False n = int(input()) s = toint(convert(input())) dic = {} for i in s: try: dic[i] += 1 except: dic[i] = 1 if valid(dic): print(n) else: for i in range(n - 1, -1, -1): last = s[i] dic[last] -= 1 if valid(dic): print(i) break
FUNC_DEF RETURN FUNC_CALL VAR NONE NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = list(map(lambda x: int(x), input().split())) freq_map = {} freq = {} maximum = 0 for i in range(0, len(a)): if a[i] not in freq: freq[a[i]] = 0 freq[a[i]] += 1 if len(freq_map) > 0: if freq[a[i]] - 1 in freq_map: freq_map[freq[a[i]] - 1] -= 1 if freq_map[freq[a[i]] - 1] == 0: del freq_map[freq[a[i]] - 1] if freq[a[i]] not in freq_map: freq_map[freq[a[i]]] = 0 freq_map[freq[a[i]]] += 1 else: freq_map[freq[a[i]]] = 1 if len(freq_map) == 1: key = list(freq_map.keys())[0] if key == 1: maximum = i + 1 if key > 1 and freq_map[key] == 1: maximum = i + 1 if len(freq_map) == 2 and 1 in list(freq_map.values()): keys = list(freq_map.keys()) values = list(freq_map.values()) if 1 in keys and freq_map[1] == 1: maximum = i + 1 if ( keys[0] - keys[1] == 1 and freq_map[keys[1]] >= freq_map[keys[0]] or keys[1] - keys[0] == 1 and freq_map[keys[0]] >= freq_map[keys[1]] ): maximum = i + 1 print(maximum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) apr, l, f, ans = 0, [0] * 100001, [0] * 100001, 1 i = 0 for x in input().split(): x = int(x) if l[x] == 0: l[x] += 1 apr += 1 f[1] += 1 else: f[l[x]] -= 1 l[x] += 1 f[l[x]] += 1 if i % apr == 0: tmp = i // apr if f[tmp] == apr - 1: ans = max(ans, i + 1) if apr > 1 and i % (apr - 1) == 0: tmp = i // (apr - 1) if f[tmp] == apr - 1 or f[tmp] == apr: ans = max(ans, i + 1) i += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP LIST NUMBER NUMBER BIN_OP LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) u = map(int, input().split()) max_x = 1 counts = [0] * 10**5 first_u = next(u) counts[first_u - 1] = 1 count_counts = {(1): 1} for i, v in enumerate(u): v -= 1 counts[v] += 1 if counts[v] > 1: count_counts[counts[v] - 1] -= 1 if count_counts[counts[v] - 1] == 0: del count_counts[counts[v] - 1] if counts[v] in count_counts: count_counts[counts[v]] += 1 else: count_counts[counts[v]] = 1 if len(count_counts) == 2: a, b = sorted(count_counts.keys()) if b == a + 1 and count_counts[b] == 1 or a == 1 and count_counts[a] == 1: max_x = i + 2 elif len(count_counts) == 1: a = next(iter(count_counts.keys())) if a == 1 or count_counts[a] == 1: max_x = i + 2 print(max_x)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
def check(d): if len(d) == 2: cmp = [] for k in d.keys(): cmp.append(k) to = max(cmp) be = min(cmp) if to == be + 1 and d[to] == 1: return True elif be == 1 and d[be] == 1: return True elif len(d) == 1: for k, v in d.items(): if k == 1 or v == 1: return True return False n = int(input()) a = list(map(int, input().split())) d = {} c = [(0) for _ in range(100001)] Max = -1 for i, x in enumerate(a): c[x] += 1 if c[x] not in d: d[c[x]] = 0 d[c[x]] += 1 if c[x] - 1 != 0: d[c[x] - 1] -= 1 if d[c[x] - 1] == 0: del d[c[x] - 1] if check(d) == True: Max = i + 1 print(Max)
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN 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 DICT ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
N = int(100000.0 + 1) input() ns = list(map(int, input().split())) counts = {} us = {} ans = 0 for i, u in enumerate(ns): c = us.get(u, 0) if c: counts[c] -= 1 if counts[c] == 0: del counts[c] c += 1 us[u] = c counts[c] = counts.get(c, 0) + 1 if len(counts) == next(iter(counts)) == 1 or len(us) == 1: ans = i + 1 if len(counts) == 2: x, y = sorted(counts) if x == counts[x] == 1 or y - x == counts[y] == 1: ans = i + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) A = list(map(int, input().split())) B = [n] + [0] * (n + 1) C = {} calcset = set() good = 0 next = 0 for i in range(n): C[A[i]] = C.get(A[i], 0) + 1 B[C[A[i]] - 1] -= 1 B[C[A[i]]] += 1 if C[A[i]] > next: next = C[A[i]] calcset.add(A[i]) if ( B[next] + B[next - 1] == len(calcset) and B[next] == 1 or B[1] == 1 and B[next] == len(calcset) - 1 or next == 1 and B[next] == len(calcset) ): good = max(good, i) print(good + 1)
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 VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = list(map(int, input().split())) u = [0] * 10 sl = 0 for i in range(n): u[a[i] - 1] += 1 s = sorted(u) for j in range(10): if s[j] != 0: break if s[j] == s[8] and s[9] - s[8] == 1: sl = max(sl, i + 1) elif s[j] == 1 and s[min(j + 1, 9)] == s[9]: sl = max(sl, i + 1) elif j == 9: sl = max(sl, i + 1) print(sl)
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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
def main(): n = int(input()) u = [int(x) for x in input().split()] c = [0] * 11 ans = 0 for i in range(n): c[u[i]] += 1 f = True common_amount = 0 common_occurrences = 0 more_occurrences = 0 one_occurrences = 0 for j in range(1, 11): if c[j] > 0: c[j] -= 1 else: continue common = 0 flag = True for ci in c: if ci > 0: if common == 0: common = ci elif common != ci: flag = False break if flag: ans = i + 1 c[j] += 1 break c[j] += 1 print(ans) main()
FUNC_DEF 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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) l = list(map(int, input().split())) d = dict() m = 0 c = [0] * (10**5 + 1) ans = 0 for i in range(n): if l[i] in d: d[l[i]] += 1 else: d.update({l[i]: 1}) m = max(m, d[l[i]]) mw = d[l[i]] c[mw] += 1 c[mw - 1] -= 1 if c[1] * 1 == i + 1: ans = i + 1 elif c[1] * 1 == 1 and c[m] * m == i: ans = i + 1 elif c[m] * m + c[m - 1] * (m - 1) == i + 1 and c[m] == 1: ans = i + 1 elif c[m] * m == i + 1 and c[m] == 1: ans = i + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR DICT VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) u = [int(i) for i in input().split()] d, s = dict(), dict() m = 0 for i, v in enumerate(u): t = d.get(v, 0) d[v] = t + 1 s[t + 1] = s.get(t + 1, 0) + 1 if t > 0: if s.get(t, 0) > 1: s[t] -= 1 else: del s[t] if ( len(s) == 1 and (1 in s.keys() or 1 in s.values()) or len(s) == 2 and ( s.get(1, 0) == 1 or max(s.keys()) == min(s.keys()) + 1 and s[max(s.keys())] == 1 ) ): m = i print(m + 1)
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 FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR NUMBER NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) l = list(map(int, input().split())) c = [0] * 10 ans = 1 c[l[0] - 1] += 1 for i in range(1, n): if i != 0: max = -10 min = 10**18 for j in range(10): if c[j] != 0: if c[j] > max: max = c[j] if c[j] < min: min = c[j] boo = 0 ba = 0 flag = 0 for j in c: if j == min: boo += 1 if j == max: ba += 1 if min < j < max: flag = 1 break if boo == 1 and min == 1 or ba == 1 and max - min == 1: if flag == 0: ans = i if abs(max - min) == 0: ans = i + 1 c[l[i] - 1] += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) lst = list(map(int, input().split())) d, length, res, ones, mx, col_mx = {}, 0, 1, 0, 0, 0 for i, x in enumerate(lst): if d.get(x) == None: d[x] = 0 ones += 1 length += 1 if d[x] == 1: ones -= 1 d[x] += 1 e = d[x] if e > mx: mx = e col_mx = 0 if e == mx: col_mx += 1 if ones == 1 and i // mx == length - 1 and i % mx == 0: res = i + 1 elif mx == 1: res = i + 1 elif length == 1: res = i + 1 elif i // (mx - 1) == length and i % (mx - 1) == 0 and col_mx == 1: res = i + 1 print(res)
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 VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) l = list(map(int, input().split())) m = [(0) for i in range(11)] varr = 0 for i in range(n): m[l[i]] += 1 listt = [m[i] for i in range(11) if m[i] != 0] listt = sorted(listt) if len(listt) == 1: varr = i + 1 if len(listt) > 1: if listt[0] == 1 and sum(listt) == 1 + len(listt) * listt[1] - listt[1]: varr = i + 1 elif listt[0] == listt[-2] and listt[-1] == listt[0] + 1: varr = i + 1 print(varr)
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 VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
def check(cnt): cnt_vals = {} nz_cnt = 0 for c in cnt: if c > 0: nz_cnt += 1 if c not in cnt_vals: cnt_vals[c] = 1 else: cnt_vals[c] += 1 if nz_cnt == 1: return True if len(cnt_vals) == 1 and 1 in cnt_vals: return True if len(cnt_vals) > 2: return False if 1 in cnt_vals and cnt_vals[1] == 1: return True keys = list(cnt_vals.keys()) if max(keys) == min(keys) + 1 and cnt_vals[max(keys)] == 1: return True return False n = int(input()) colors = input().split() colors = [(int(k) - 1) for k in colors] cnt = [(0) for _ in range(10)] ans = 0 for i in range(n): cnt[colors[i]] += 1 mn = min(cnt) mx_found = False uniq_found = False is_valid = True if check(cnt): ans = i + 1 print(ans)
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF NUMBER VAR VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = [int(x) for x in input().split()] dic = {} counter = 0 i = 1 for item in a: dic[item] = 0 for item in a: dic[item] += 1 b = [] for key in dic: if dic[key] > 0: b.append(dic[key]) b.sort() if len(b) > 1: if b[-1] - 1 == b[-2] == b[0] or b[0] == 1 and b[1] == b[-1]: counter = i else: counter = i i += 1 print(counter)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) lis = [0] + list(map(int, input().split())) d = [0] * 100005 ans = 0 c = 0 mx = 0 cnt = [0] * 100005 for i in range(1, n + 1): cnt[d[lis[i]]] -= 1 d[lis[i]] += 1 cnt[d[lis[i]]] += 1 mx = max(mx, d[lis[i]]) if cnt[1] == i: ans = i elif cnt[1] == 1 and cnt[mx] * mx == i - 1: ans = i elif cnt[i] == 1: ans = i elif cnt[mx - 1] * (mx - 1) == i - mx and cnt[mx] == 1: ans = i print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR IF VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) l = list(map(int, input().split())) d = [0] * 10 h = 0 for i in range(n): d[l[i] - 1] += 1 d1 = [] + d d1.sort() if max(d1) == 1: h = max(h, i + 1) else: t = 0 for j in range(10): if d1[j] == 1: if j == 9 or d1[j + 1] == d1[9]: t = 1 break else: break q = 0 for j in range(10): if d1[j] != 0 and j == 9: q = 1 break elif d1[j] != 0: if d1[j + 1] == d1[8] == d1[9] - 1 == d1[j]: q = 1 break elif j == 8 and d1[8] == d1[9] - 1: q = 1 break else: break if q == 1 or t == 1: h = max(h, i + 1) print(h)
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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
def check(arr): tmp = [] for i in arr: if i != 0: tmp.append(i) mi = min(tmp) ma = max(tmp) mic = tmp.count(mi) mac = tmp.count(ma) l = len(tmp) if mi == 1 and mic == l: return True elif mic == l - 1 and mac == 1 and ma - mi == 1: return True elif mi == 1 and mic == 1 and mac == l - 1: return True elif l == 1: return True else: return False n = int(input()) arr = list(map(int, input().split())) cnt = [0] * 11 for i in range(n): cnt[arr[i]] += 1 for i in range(n - 1, -1, -1): if check(cnt): print(i + 1) break cnt[arr[i]] -= 1
FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
def go(): n = int(input()) x = 10**5 + 5 colors = [0] * x count = [0] * x m = 0 answer = 0 for i, c in enumerate(input().split(" ")): color = int(c) index = i + 1 colors[color] += 1 count[colors[color] - 1] -= 1 count[colors[color]] += 1 m = max(m, colors[color]) if count[1] == index: answer = index elif count[index] == 1: answer = index elif count[1] == 1 and m * count[m] == index - 1: answer = index elif count[m - 1] * (m - 1) == index - m and count[m] == 1: answer = index return answer print(go())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) u = list(map(int, input().split())) colour = [0] * (10**5 + 1) count = [0] * (10**5 + 1) x = 1 max_c = 0 for i in range(1, n + 1): if count[colour[u[i - 1]]]: count[colour[u[i - 1]]] = count[colour[u[i - 1]]] - 1 colour[u[i - 1]] += 1 count[colour[u[i - 1]]] += 1 max_c = max(max_c, colour[u[i - 1]]) if count[max_c] == 1 and (max_c - 1) * count[max_c - 1] + max_c - 1 == i - 1: x = i elif count[i] == i: x = i elif count[1] == 1 and max_c * count[max_c] == i - 1: x = i elif count[1] == i: x = i print(x)
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 BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = list(map(int, input().split())) p = [([0] * 10) for i in range(n + 1)] for i in range(1, n + 1): p[i][a[i - 1] - 1] += 1 for j in range(10): p[i][j] += p[i - 1][j] for i in range(n, 0, -1): k = set(p[i]) if 0 in k: k.remove(0) if len(k) == 2: if p[i].count(max(k)) == 1 and min(k) == max(k) - 1: print(i) break if p[i].count(min(k)) == 1 and min(k) == 1: print(i) break elif len(k) == 1: m = k.pop() if m == 1: print(i) break if p[i].count(m) == 1: print(i) break
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 NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
def gns(): return [int(x) for x in input().split()] def gn(): return int(input()) n = gn() ns = gns() sm = [[0] * 11] for i in range(n): c = ns[i] sm.append(list(sm[-1])) sm[-1][c] += 1 def check(x): x = [i for i in x if i != 0] if len(x) == 1: return True if sum(x) <= len(x) + 1: return True if 1 in x: l1 = x.index(1) ot = x[(l1 + 1) % len(x)] for i in range(len(x)): if i == l1: continue if x[i] != ot: return False return True m = max(x) lm = x.index(m) for i in range(len(x)): if i == lm: continue if x[i] != m - 1: return False return True i = n while i >= 0: if check(sm[i]): print(i) quit() i -= 1
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
from sys import stdin n = int(stdin.readline()) ls = list(map(int, stdin.readline().split())) dic = {} occ = [(0) for __ in range(100005)] for i in ls: occ[i] += 1 occ_i = occ[i] if occ_i not in dic: dic[occ_i] = set() dic[occ_i].add(i) occ_i -= 1 if occ_i != 0: dic[occ_i].remove(i) if len(dic[occ_i]) == 0: del dic[occ_i] def adjust(number): occ_number = occ[number] dic[occ_number].remove(number) if len(dic[occ_number]) == 0: del dic[occ_number] occ_number = occ_number - 1 if occ_number != 0: if occ_number not in dic: dic[occ_number] = set() dic[occ_number].add(number) occ[number] = occ_number def sol(): for i in range(n - 1, -1, -1): number = ls[i] if len(dic) == 2: r = max(dic) l = min(dic) if len(dic[r]) == 1 and r - l == 1 or len(dic[l]) == 1 and l == 1: print(i + 1) return else: adjust(number) elif len(dic) == 1: level = max(dic) if level == 1 or len(dic[level]) == 1: print(i + 1) return else: adjust(number) else: adjust(number) print() sol()
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 VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
N = int(input()) arr = [int(x) for x in input().split()] cnt = dict() brr = set() crr = [(0) for _ in range(100001)] for i in range(1, N + 1): cnt[i] = set() answer = 1 for i in range(N): u = arr[i] crr[u] += 1 if crr[u] > 1: cnt[crr[u] - 1].remove(u) if len(cnt[crr[u] - 1]) == 0: brr.remove(crr[u] - 1) cnt[crr[u]].add(u) brr.add(crr[u]) if len(brr) == 1: drr = list(brr) if drr[0] == 1 or len(cnt[drr[0]]) == 1: answer = i + 1 elif len(brr) == 2: drr = list(brr) drr.sort() if drr[0] == 1 and len(cnt[1]) == 1: answer = i + 1 elif drr[1] == drr[0] + 1 and len(cnt[drr[1]]) == 1: answer = i + 1 print(answer)
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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) l = list(map(int, input().split())) m = [(0) for i in range(10**5 + 5)] wyn = 0 duz = [(0) for i in range(10**5 + 5)] ile = [(0) for i in range(10**5 + 5)] zajete = 0 praw = [] jedynki = 0 mini = 100000000000 maksi = -100000000000000000 for i in range(n): if m[l[i]] == 1: jedynki -= 1 if m[l[i]] == 0: zajete += 1 jedynki += 1 ile[m[l[i]]] -= 1 m[l[i]] += 1 ile[m[l[i]]] += 1 maksi = max(m[l[i]], maksi) if jedynki > 1: if zajete == jedynki or zajete == jedynki + 1 and maksi == 2: wyn = i + 1 if jedynki == 1: if i + 1 == zajete * maksi - maksi + 1: wyn = i + 1 if jedynki == 0: if ile[maksi - 1] == zajete - 1: wyn = i + 1 print(wyn)
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 VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) u = list(map(int, input().split())) cl = [0] * 10 cl[u[0] - 1] += 1 md = 1 for i in range(1, n): cc = u[i] - 1 cl[cc] += 1 czu = cl.count(0) cou = cl.count(1) cmu = cl.count(max(cl)) clu = cl.count(max(cl) - 1) if cmu == 1 and clu == 9 - czu or cou == 1 and cmu == 9 - czu or cou == 10 - czu: md = i + 1 print(md)
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 NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) array = list(map(int, input().split())) d = [0] * 100001 max_count = 0 count_of_1 = 0 count_of_max_count = 0 count_of_max_count_minus1 = 0 max_pref = 0 unique_count = 0 for i in range(n): if d[array[i]] == 0: count_of_1 += 1 unique_count += 1 d[array[i]] += 1 if d[array[i]] == 2: count_of_1 -= 1 if d[array[i]] > max_count: max_count = d[array[i]] count_of_max_count_minus1 = count_of_max_count - 1 count_of_max_count = 1 elif d[array[i]] == max_count: count_of_max_count += 1 elif d[array[i]] == max_count - 1: count_of_max_count_minus1 += 1 if count_of_max_count == unique_count - 1 and count_of_1 == 1: max_pref = i elif count_of_max_count == unique_count and max_count == 1: max_pref = i elif count_of_max_count == 1 and count_of_max_count_minus1 == unique_count - 1: max_pref = i print(max_pref + 1)
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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) d = 0 c = [0] * 100100 e = 0 f = [0] * 100100 f[0] = n for i, x in enumerate(map(int, input().split())): f[c[x]] -= 1 c[x] += 1 f[c[x]] += 1 if 1 == c[x]: e += 1 if 1 == e or 1 + i == e: d = max(d, i + 1) if e > 1 and 0 == i % (e - 1) and 1 == f[1]: if f[i // (e - 1)] == e - 1: d = max(d, i + 1) if e > 0 and 0 == i % e: y = i // e if 1 == f[y + 1] and e - 1 == f[y]: d = max(d, i + 1) print(d)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER IF NUMBER VAR VAR VAR NUMBER IF NUMBER VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$ — the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = input().split() b = {} c = [] q = 1 max1 = 1 prevmax = 0 countprev = 0 countmax = 1 countmin = 0 for i in range(n): if max1 == prevmax and i > n // max1 + max1: break if a[i] in b: b[a[i]] += 1 if b[a[i]] == 2: countmin -= 1 if b[a[i]] == max1: countmax += 1 elif b[a[i]] == prevmax: countprev += 1 elif b[a[i]] > max1: prevmax = max1 max1 = b[a[i]] countprev = countmax - 1 countmax = 1 else: b[a[i]] = 1 minb = 1 countmin += 1 if len(b) == i + 1 or len(b) == i: q = i + 1 elif countprev == len(b) - 1 and countmax == 1 and max1 == prevmax + 1 and max1 > 1: q = i + 1 elif countmax == len(b) - 1 and countmin == 1 and max1 > 1: q = i + 1 print(q)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR