티스토리 뷰
6515번: Frequent values
The input consists of several test cases. Each test case starts with a line containing two integers n and q (1 ≤ n, q ≤ 100000). The next line contains n integers a1 , ... , an (-100000 ≤ ai ≤ 100000, for each i ∈ {1, ..., n}) separated by spaces
www.acmicpc.net
백준 소스코드 [C++] 6515 Frequent values
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
#define all(v) v.begin(), v.end()
#define pii pair<int,int>
typedef long long ll;
using namespace std;
int n, m,sqrtn;
int arr[100001];
int cnt[200001];
int table[200001];
int maxcount=0;
void add(int a) {
if (table[cnt[a]] != 0) table[cnt[a]]--;
cnt[a]++;
table[cnt[a]]++;
maxcount = max(maxcount, cnt[a]);
}
void sub(int a) {
table[cnt[a]]--;
if (maxcount == cnt[a] && table[cnt[a]] == 0) maxcount--;
cnt[a]--;
table[cnt[a]]++;
}
struct query {
int idx, i, j;
};
bool compare(query& x,query& y) {
if (x.i / sqrtn != y.i / sqrtn) return x.i / sqrtn < y.i / sqrtn;
return x.j < y.j;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
while (true) {
maxcount = 0;
cin >> m;
sqrtn = (int)sqrt(n);
memset(arr, 0, sizeof(arr));
memset(cnt, 0, sizeof(cnt));
memset(table, 0, sizeof(table));
int val;
for (int i = 0;i < n;i++) {
cin >> val;
arr[i] = val + 100000;
}
vector<query> q(m);
vector<int>res(m);
int x, y;
for (int a = 0;a < m;a++) {
cin >> x >> y;
q[a] = { a,x - 1,y - 1 };
}
sort(q.begin(), q.end(), compare);
int s = q[0].i, e = q[0].j;
for (int a = s;a <= e;a++) add(arr[a]);
res[q[0].idx] = maxcount;
for (int a = 1;a < m;a++) {
while (q[a].i < s)add(arr[--s]);
while (q[a].j > e)add(arr[++e]);
while (q[a].i > s)sub(arr[s++]);
while (q[a].j < e)sub(arr[e--]);
res[q[a].idx] = maxcount;
}
for (int i = 0;i < m;i++) cout << res[i] << '\n';
cin >> n;
if (n == 0) break;
}
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 14413 Poklon (0) | 2020.12.25 |
---|---|
백준 소스코드 [C++] 14897 서로 다른 수와 쿼리 1 (0) | 2020.12.25 |
백준 소스코드 [C++] 12986 화려한 마을 2 (2) | 2020.12.24 |
백준 소스코드 [C++] 12999 화려한 마을 3 (0) | 2020.12.24 |
백준 소스코드 [C++] 8462 배열의 힘 (0) | 2020.12.24 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- test coverage
- MeTal
- 에드몬드 카프 알고리즘
- 네트워크 유량
- 강한 순환 참조
- State Restoration
- 최단경로 알고리즘
- 코딩대회
- 최대 매칭
- 컴퓨터 추상화
- rxswift
- WWDC17
- 최단경로문제
- mach-o
- WWDC16
- observeOn
- 부스트캠프 6기
- CPU와 Memory
- 다익스트라 시간복잡도
- 벨만포드 알고리즘
- WWDC19
- 최단경로 문제
- WWDC21
- 포드 풀커슨 알고리즘
- 벨만포드 시간복잡도
- HIG
- 네트워크 플로우
- CompositionalLayout
- Testable
- IOS
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함