본문 바로가기
Study (etc)/Problem Solving

[BOJ / C++] 6519번 : 코스튬 파티

by Haren 2023. 7. 13.

문제

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;
}

 

 

 

6159번: Costume Party

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 h

www.acmicpc.net