結果
| 問題 |
No.2221 Set X
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-02-17 22:20:45 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 1,658 ms / 2,000 ms |
| コード長 | 1,242 bytes |
| コンパイル時間 | 230 ms |
| コンパイル使用メモリ | 82,184 KB |
| 実行使用メモリ | 111,692 KB |
| 最終ジャッジ日時 | 2024-07-19 13:29:50 |
| 合計ジャッジ時間 | 31,706 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 40 |
ソースコード
import sys,random,bisect
from collections import deque,defaultdict
from heapq import heapify,heappop,heappush
from itertools import permutations
from math import gcd,log
input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())
N = int(input())
A = li()
arg_x = 1
min_val = 2*N
B = int((2*N)**.5)
for x in range(1,B):
res = 1
for i in range(1,N):
if A[i]//x != A[i-1]//x:
res += 1
if res * (x+1) < min_val:
arg_x,min_val = x,res*(x+1)
plus = set([i for i in range(N)])
minus = set([i for i in range(N)])
diff = [(A[i+1]-A[i],i) for i in range(N-1)]
diff.sort(reverse=True)
for x in range(B,2*N+1):
"""
種類数は [2N/(x+1)] 以下
→ A_i-A_{i-1} >= x が成り立つiは [2N/(x+1)]個以下
"""
while diff and diff[-1][0] <= x:
d,i = diff.pop()
plus.remove(i)
minus.remove(i+1)
#print(x,plus,minus)
if len(plus) * (x+1) < min_val:
tmp = 0
for i in plus:
tmp += A[i]//x
for i in minus:
tmp -= A[i]//x
tmp += len(plus)
tmp *= (x+1)
if tmp < min_val:
arg_x,min_val = x,tmp
print(arg_x)
print(min_val)