algorithm/BOJ
BOJ 11652번 카드
_JunHo
2020. 1. 11. 00:15
BOJ : https://www.acmicpc.net/problem/11652
처음에는 2^62 라는 수때문에 숫자타입형으로 표현이 안되는줄 알았다.
그래서 string 으로 N^2 라는 시간복잡도에 풀어냈는데
2^62라는 범위가 숫자타입형으로 표현이 되는줄 알게 되어서 다시 풀어보았다.
첫번째 if문은 숫자가 달라질 때 최대수가 더 적으면 갱신을 해주는 작업이고
두번째 if문은 숫자가 달라질 때 최대수가 현재 수와 같다면 갱신을 해줄 필요가 없는 부분이고
세번째 if문은 카운트해주는 부분이다.
마지막조건은 끝까지 탐색 후 맥스가 갱신되지 않았을 때 최대수가 현재수보다 작다면 갱신해주는 부분이다.
더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
#include <iostream>
#include <algorithm>
using namespace std;
long long arr[1000001];
int main() {
ios::sync_with_stdio(false);
int N; cin >> N;
int Maxcnt = 0, cnt = 1;
long long Maxnum;
for (int i = 0; i < N; i++) cin >> arr[i];
sort(arr, arr + N);
for (int i = 1; i < N; i++) {
if (arr[i - 1] != arr[i] && Maxcnt < cnt) {
Maxnum = arr[i - 1];
Maxcnt = cnt;
cnt = 1;
}
else if (arr[i - 1] != arr[i] && Maxcnt >= cnt) {
cnt = 1;
}
else cnt++;
}
if (Maxcnt < cnt) Maxnum = arr[N - 1];
cout << Maxnum;
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|