반응형
문제
It's Halloween! Farmer John is taking the cows to a costume party, but unfortunately he only has one costume. The costume fits precisely two cows with a length of S (1 <= S <= 1,000,000). FJ has N cows (2 <= N <= 20,000) conveniently numbered 1..N; cow i has length L_i (1 <= L_i <= 1,000,000). Two cows can fit into the costume if the sum of their lengths is no greater than the length of the costume. FJ wants to know how many pairs of two distinct cows will fit into the costume.
입력
- Line 1: Two space-separated integers: N and S
- Lines 2..N+1: Line i+1 contains a single integer: L_i
출력
- Line 1: A single integer representing the number of pairs of cows FJ can choose. Note that the order of the two cows does not matter.
힌트
The four pairs are as follows: cow 1 and cow 3; cow 1 and cow 4; cow 2 and cow 4; and finally cow 3 and cow 4.
풀이
import sys
input = sys.stdin.readline
n, s = map(int,input().split())
cow = [int(input()) for _ in range(n)]
ans = 0
for i in range(n-1):
for k in range(i+1, n):
if cow[i]+cow[k] <= s:
ans += 1
print(ans)
파이썬으로 제출하면 시간초과가 나니 pypy3로 제출하면 된다.
반응형
'Develop > 알고리즘' 카테고리의 다른 글
[백준/Python] Bronze I #6996 애너그램 (0) | 2023.08.23 |
---|---|
[백준/Python] Bronze I #9506 약수들의 합 (0) | 2023.08.22 |
[백준/Python] Silver V #4158 CD (0) | 2023.08.22 |
[백준/Python] Gold IV #16472 고냥이 (0) | 2023.08.22 |
[백준/Python] Silver IV #1337 올바른 배열 (0) | 2023.08.22 |
Comment