※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 11670번 문제인 초등 수학이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/11670
11670번: 초등 수학
입력과 같은 순서대로 (a,b) 순서쌍이 유효한 방정식과 함께 출력된다. 각각의 방정식은 5개의 요소로 나뉜다. a와 3개의 연산자(+ 혹은 - 혹은 *)중 하나, b 그리고 = 와 연산결과이다. 모든 연
www.acmicpc.net
N(<=2500)개의 숫자쌍이 주어질 때, 이 숫자쌍들 사이에 +, -, *들을 적절히 넣어 각 계산식의 결과들이 다르게 만드는 문제이다.
나올 수 있는 세 가지 결과값들을 map 등을 이용하여 압축한 후 최대 이분매칭을 이용하여 문제를 해결하자.
아래는 제출한 소스코드이다.
#include <iostream>
#include <vector>
#include <map>
#include <cstring>
using namespace std;
typedef long long ll;
vector<int> G[2501];
map<ll, int> mp;
ll queries[2501][2];
int visited[2501];
int matching[7501];
int bipartite(int current) {
if (visited[current]) return 0;
visited[current] = 1;
for (auto node : G[current]) {
if (matching[node] == 0) {
matching[node] = current;
return 1;
}
if (bipartite(matching[node])) {
matching[node] = current;
return 1;
}
}
return 0;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int idx = 1;
int N; cin >> N;
for (int i = 1;i <= N;i++) {
ll x, y; cin >> x >> y;
queries[i][0] = x, queries[i][1] = y;
if (mp.find(x + y) == mp.end()) {
mp.insert({ x + y,idx });
idx++;
}
if (mp.find(x - y) == mp.end()) {
mp.insert({ x - y,idx });
idx++;
}
if (mp.find(x * y) == mp.end()) {
mp.insert({ x * y,idx });
idx++;
}
G[i].push_back(mp[x + y]);
G[i].push_back(mp[x - y]);
G[i].push_back(mp[x * y]);
}
int ans = 0;
for (int i = 1; i <= N; i++) {
memset(visited, 0, sizeof(visited));
ans += bipartite(i);
}
if (ans < N) cout << "impossible";
else {
for (int i = 1; i <= N; i++) {
ll x = queries[i][0], y = queries[i][1];
if (i == matching[mp[x + y]]) {
cout << x << " + " << y << " = " << x + y << '\n';
}
else if (i == matching[mp[x - y]]) {
cout << x << " - " << y << " = " << x - y << '\n';
}
else {
cout << x << " * " << y << " = " << x * y << '\n';
}
}
}
}
728x90
'BOJ' 카테고리의 다른 글
[BOJ 15701 // C++] 순서쌍 (0) | 2021.07.14 |
---|---|
[BOJ 20294 // C++] 에어컨 설치 (0) | 2021.07.13 |
[BOJ 13231 // C++] Lucky Tickets (0) | 2021.07.11 |
[BOJ 11695 // C++] 표 게임 (0) | 2021.07.10 |
[BOJ 3687 // C++] 성냥개비 (0) | 2021.07.09 |