출처: https://cses.fi/problemset/task/1090


문제 설명

There are n children who want to go to a Ferris wheel, and your task is to find a gondola for each child.

Each gondola may have one or two children in it, and in addition, the total weight in a gondola may not exceed xx. You know the weight of every child.

What is the minimum number of gondolas needed for the children?

입력 

The first input line contains two integers n and x: the number of children and the maximum allowed weight.

The next line contains n integers p_1,p_2,,p_n: the weight of each child.

출력

Print one integer: the minimum number of gondolas.

입력 예

4 10
7 2 3 9

출력 예

3

제약조건

  • 1 <= n <= 2x1e5  
  • 1 <= x <= 1e9 
  • 1 <= p_i <= x

문제 풀이

무게를 입력받아서 정렬하고, 앞쪽(left)와 뒤쪽(right) 아이의 몸무게를 합쳐서 제한무게와 비교한다.

작으면, 곤돌라 하나에 탑승시키고(left+1, right-1), 무거우면 무거운 아이 혼자 타도록 한다.(right-1)  

프로그램 내용

더보기
...
    int nChild, weightL;
    cin >> nChild >> weightL ;

    long childWeight[nChild] = {0};

    for (int in=0; in < nChild; ++in)
        cin >> childWeight[in];

    sort(childWeight, childWeight+nChild);

    int idx=0, id = nChild-1;
    int gondola_count=0;

    while( idx <= id )
        if (childWeight[idx] + childWeight[id] > weightL) id--;
        else { idx++; id--;}
        gondola_count++;

	cout << gondola_count << endl;

 

Sorting and Searching link )

'CSES' 카테고리의 다른 글

CSES 2. Restaurant Customers (1619)  (0) 2019.09.25
CSES 2. Concert Tickets (1091)  (0) 2019.09.25
CSES 2. Apartments (1084)  (0) 2019.09.24
CSES 2. Distinct Numbers (1621)  (0) 2019.09.24
CSES 2. Sorting and Searching  (0) 2019.09.24

출처: https://cses.fi/problemset/task/1084


문제 설명

There are n applicants and m free apartments. Your task is to distribute the apartments so that as many applicants as possible will get an apartment.

Each applicant has a desired apartment size, and they will accept any apartment whose size is close enough to the desired size.

입력 

The first input line has three integers n, m, and k: the number of applicants, the number of apartments, and the maximum allowed difference.

The next line contains n integers a_1,a_2,,a_n: the desired apartment size of each applicant. If the desired size of an applicant is x, he or she will accept any apartment whose size is between xk and x+k.

The last line contains mm integers b_1,b_2,,b_m: the size of each apartment.

출력

Print one integer: the number of applicants who will get an apartment.

입력 예

4 3 5
60 45 80 60
30 60 75

출력 예

2

제약조건

  • 1 <= n,m <= 2x10^5
  • 0 <= k <= 10^
  • 1 <= a_i, b_i <= 10^9

문제 풀이

아파트 크기와 원하는 크기를 입렵 받아서 작은 순서로 정렬한다.

원하는 크기와 감당할 수 있는 크기차이와 주어진 아파트의 크기를 비교해서, 가능한 방들을 출력한다.

프로그램 내용

더보기
#define MAX_N 2*100000
#define MAX_M 2*100000
#define MAX_K 1000000000
#define MAX_A 1000000000
#define MAX_B 1000000000

int main()
{
    int nApplicant, nApart, maxDiff;
    cin >> nApplicant >> nApart >> maxDiff;

    long desired[nApplicant];
    long aptSize[nApart];

    for (int idx=0; idx<nApplicant; ++idx)
        cin >> desired[idx];

    for (int idx=0; idx<nApart ; ++idx)
        cin >> aptSize[idx];

    sort(desired, desired+nApplicant);
    sort(aptSize, aptSize+nApart);

    int idx=0, id=0;
    int room_count=0;

    while(idx < nApplicant && id < nApart)
        if (desired[idx]+maxDiff < aptSize[id]) 
        	idx++;
        else if (desired[idx]-maxDiff > aptSize[id]) 
        	id++;
        else 
        	idx++; 
            id++; 
            room_count++;

	cout << room_count << endl;

 

Sorting and Searching link )

'CSES' 카테고리의 다른 글

CSES 2. Concert Tickets (1091)  (0) 2019.09.25
CSES 2. Ferris Wheel (1090)  (0) 2019.09.25
CSES 2. Distinct Numbers (1621)  (0) 2019.09.24
CSES 2. Sorting and Searching  (0) 2019.09.24
CSES 1. Chessboard and Queens (1624)  (0) 2019.09.23

출처: https://cses.fi/problemset/task/1621


문제 설명

You are given a list of n integers, and your task is to calculate the number of distinct values in the list.

입력 양식

The first input line has an integer n: the number of values.

The second line has n integers x_1,x_2,,x_

출력

Print one integers: the number of distinct values.

입력 예

5
2 3 2 2 3

출력 예

2

제약조건

  • 1 <= n <= 2x10^5
  • 1 <= x_i <= 10^9

문제 풀이 내용

입력을 읽어 들여서 set에 입력하여 중복을 제거하고,  set size를 출력한다.

프로그램 내용

더보기
...
    set <long> distinct;

    for (int idx=0; idx<nInput; ++idx)
        distinct.insert(numbers);

	cout << distinct.size() << endl;

 

Sorting and Searching ( link )

'CSES' 카테고리의 다른 글

CSES 2. Ferris Wheel (1090)  (0) 2019.09.25
CSES 2. Apartments (1084)  (0) 2019.09.24
CSES 2. Sorting and Searching  (0) 2019.09.24
CSES 1. Chessboard and Queens (1624)  (0) 2019.09.23
CSES 1. Apple Division (1623)  (0) 2019.09.23

+ Recent posts