Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 알고리즘
- 다익스트라
- DFS
- 시뮬레이션
- 백준
- 자료구조
- 우선순위 큐
- VR
- c++
- BFS
- Unreal Engine 5
- 브루트포스
- Team Fortress 2
- 수학
- 트리
- 다이나믹 프로그래밍
- 그리디 알고리즘
- 재귀
- 정렬
- 투 포인터
- 유니티
- XR Interaction Toolkit
- 백트래킹
- 누적 합
- 구현
- 문자열
- 유니온 파인드
- 그래프
- ue5
- 스택
Archives
- Today
- Total
1일1알
백준 27172번 수 나누기 게임 C++ (골드5) 본문
https://www.acmicpc.net/problem/27172
다른 수와 비교하여 내가 다른 수의 약수이면 1점을 얻고, 내가 다른 수의 배수이면 1점을 잃는 게임이다.
n이 최대 10만이기때문에 두개씩 짝지어서 전부 비교하면 시간초과가 날 것이기 때문에 다른 방법을 사용해야 한다.
모든 수를 순회하며 약수를 구하고, 약수를 순회하며 만약 약수가 다른 카드에 존재하면 해당 카드는 1점을 얻고 본인은 1점을 잃는다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
vector<int> v(n);
map<int, int> ans;
for (int i = 0; i < n; i++)
{
cin >> v[i];
ans.insert({ v[i],0 });
}
for (int i = 0; i < n; i++)
{
int value = v[i];
set<int> s;
for (int j = 1; j * j <= value; j++) {
if (j * j == value) {
s.insert(j);
}
else if (value % j == 0) {
s.insert(j);
s.insert(value / j);
}
}
for (auto a : s)
{
if (ans.find(a) == ans.end()) continue;
ans[value]--;
ans[a]++;
}
}
for (int i = 0; i < n; i++)
{
cout << ans[v[i]] << " ";
}
}
'알고리즘' 카테고리의 다른 글
백준 1766번 문제집 C++ (골드2) (0) | 2024.05.27 |
---|---|
백준 9328번 열쇠 C++ (골드1) (0) | 2024.05.26 |
백준 20529번 가장 가까운 세 사람의 심리적 거리 C++ (실버1) (0) | 2024.05.26 |
백준 3184번 양 C++ (0) | 2023.06.30 |
백준 3187번 양치기 꿍 C++ (0) | 2023.06.29 |