結果
問題 | No.2501 Maximum Inversion Number |
ユーザー | chineristAC |
提出日時 | 2023-10-13 21:42:13 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,116 ms / 2,000 ms |
コード長 | 1,975 bytes |
コンパイル時間 | 532 ms |
コンパイル使用メモリ | 81,844 KB |
実行使用メモリ | 121,380 KB |
最終ジャッジ日時 | 2024-09-15 17:19:53 |
合計ジャッジ時間 | 7,328 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 47 ms
56,064 KB |
testcase_01 | AC | 237 ms
77,696 KB |
testcase_02 | AC | 482 ms
80,784 KB |
testcase_03 | AC | 274 ms
83,588 KB |
testcase_04 | AC | 372 ms
121,380 KB |
testcase_05 | AC | 385 ms
121,332 KB |
testcase_06 | AC | 396 ms
120,244 KB |
testcase_07 | AC | 253 ms
100,068 KB |
testcase_08 | AC | 301 ms
102,172 KB |
testcase_09 | AC | 105 ms
93,780 KB |
testcase_10 | AC | 225 ms
82,136 KB |
testcase_11 | AC | 279 ms
102,472 KB |
testcase_12 | AC | 289 ms
104,820 KB |
testcase_13 | AC | 60 ms
66,816 KB |
testcase_14 | AC | 262 ms
121,240 KB |
testcase_15 | AC | 308 ms
85,128 KB |
testcase_16 | AC | 1,116 ms
97,120 KB |
ソースコード
import sys from itertools import permutations import heapq from collections import deque import random input = lambda :sys.stdin.readline().rstrip() mi = lambda :map(int,input().split()) li = lambda :list(mi()) def brute(n,m,L,R): if m < sum(L) or sum(R) < m: return -1 cnt = [L[i] for i in range(n)] while sum(cnt) < m: mini = 10**10 arg = -1 for i in range(n): if cnt[i]!=R[i] and cnt[i] < mini: mini = cnt[i] arg = i cnt[arg] += 1 res = m * (m-1)//2 for i in range(n): res -= cnt[i] * (cnt[i]-1) //2 return res def solve(n,m,L,R): if m < sum(L) or sum(R) < m: return -1 cnt = [L[i] for i in range(n)] rest = m - sum(cnt) def cond(k): check = 0 for i in range(n): if L[i] <= k: check += min(k,R[i]) - L[i] return check <= rest ok = min(L) ng = max(R) + 1 while ng-ok>1: mid = (ok+ng)//2 if cond(mid): ok = mid else: ng = mid for i in range(n): if L[i] <= ok: cnt[i] = min(R[i],ok) rest = m - sum(cnt) idx = [i for i in range(n)] idx.sort(key=lambda i:cnt[i]) for i in idx: if rest and cnt[i]!=R[i]: cnt[i] += 1 rest -= 1 res = m * (m-1)//2 for i in range(n): res -= cnt[i] * (cnt[i]-1) //2 return res while False: n = random.randint(1,10) m = random.randint(1,100) L = [random.randint(1,m) for i in range(n)] R = [random.randint(L[i],m) for i in range(n)] if solve(n,m,L,R) != brute(n,m,L,R): print(n,m) print(L) print(R) print(solve(n,m,L,R),brute(n,m,L,R)) exit() print("AC") for _ in range(int(input())): n,m = mi() L = li() R = li() print(solve(n,m,L,R))