#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v = {4, 1, 6, 3, 8, 2};
// 1. max_element: 최댓값의 위치를 찾아줍니다
auto max_it = max_element(v.begin(), v.end());
cout << "최댓값: " << *max_it << endl; // 8 출력
// 2. min_element: 최솟값의 위치를 찾아줍니다
auto min_it = min_element(v.begin(), v.end());
cout << "최솟값: " << *min_it << endl; // 1 출력
// 3. minmax_element: 최솟값과 최댓값을 한 번에! (구조적 바인딩 활용했어요)
auto [min_iter, max_iter] = minmax_element(v.begin(), v.end());
cout << "최댓값2: " << *max_iter << endl; // 8 출력
cout << "최소값2: " << *min_iter << endl; // 1 출력
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v = {1, 1, 2, 2, 3, 3};
// 값이 2인 원소 개수
int cnt = count(v.begin(), v.end(), 2);
// 짝수 개수
int even_cnt = count_if(v.begin(), v.end(), [](int x) { return x % 2 == 0; });
cout << "값이 2인 원소 개수: " << cnt << endl;
cout << "짝수인 원소 개수: " << even_cnt << endl;
}
시간 복잡도 요구 사항 파악
map / set 또는 unordered_map / unordered_setunordered_map / unordered_setpriority_queuequeuedeque정렬의 필요성
map, set, 혹은 sort 함수 사용lower_bound, upper_boundnth_element중복 제거 & 효율적 관리
unique + erase 패턴 (정렬 필요)set / unordered_setbits/stdc++.h 사용법
위 링크는 코테뿐아니라 로컬에 깔려있는 VS에도 사용할 수 있는 방법이다.
bits/stdc++.h헤더파일은 우리가 자주 쓰는 iostream, vector, algorithm 등등을 한 번에 불러올 수 있어서, 대회 현장에서 코드량을 줄이기 위해 자주 쓰인다.
입/출력을 하고 싶다면 대부분의 코딩 테스트 문제에서는 cin >> 변수, cout << 값; 만 잘 써도 된다. 다만, 속도가 조금 아쉽다면 이런 것도 있다.
ios::sync_with_stdio(false);cin.tie(nullptr); (or cin.tie(0);)#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); // 새로 추가!
cin.tie(nullptr); // 새로 추가!
int a, b;
cin >> a >> b;
cout << a + b << "\n";
}
여기서 \n 대신 endl을 쓰면 출력 버퍼가 강제로 flush 돼서 느려질 수 있으니, 큰 문제에서 반복 출력할 때는 endl보다 \n이 선호된다!
문자열 전체를 입력받으려면 getline 함수를 쓰시는 것이 좋다! 공백이나 탭 같은 구분 문자도 그대로 읽어들일 수 있다
getline(cin, s); // 한 줄 통째로 s에 저장