문제
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.
Solved.ac 레벨
실버 V
풀이
#include<bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, s;
vector<int> cow;
cin >> n >> s;
for(int i = 0; i < n; i++) {
int input;
cin >> input;
cow.push_back(input);
}
sort(cow.begin(), cow.end());
int ans = 0;
for(int i = 0; i < cow.size(); i++) {
for(int j = i + 1; j < cow.size(); j++) {
if(cow[i] + cow[j] <= s) {
//두 마리 소 길이의 합이 s보다 작다면 답을 1 증가시킴
ans++;
}
}
}
cout << ans << "\n";
return 0;
}
'Study (etc) > Problem Solving' 카테고리의 다른 글
[BOJ / C++] 14916번 : 거스름돈 (0) | 2023.07.15 |
---|---|
[BOJ / C++] 1863번 : 스카이라인 쉬운거 (0) | 2023.07.13 |
[BOJ / C++] 2167번 : 2차원 배열의 합 (0) | 2023.07.13 |
[BOJ / C++] 4677번 : Oil Deposits (0) | 2023.06.05 |
[BOJ / C++] 9024번 : 두 수의 합 (0) | 2023.06.05 |