A - 최대 곱셈
두 수 a와 b의 곱이 최대가 되는 조건은 a=1일 때입니다.
#include <iostream>
using namespace std;
typedef long long ll;
#define endl '\n'
ll gcd(ll a, ll b) {
while (b) {
swap(a, b);
b %= a;
}
return a;
}
void solve() {
ll x, y;
cin >> x >> y;
cout << 1 << " " << (x * y) / (gcd(x, y) * gcd(x, y)) << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int t; cin >> t;
while (t--) solve();
return 0;
}
B - 삼각형 면적 계산
3개의 점을 선택하여 삼각형을 형성하고, 벡터 크로스积을 이용해 넓이를 계산합니다.
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
double cross(double x1, double y1, double x2, double y2, double x3, double y3) {
double v1x = x1 - x2, v1y = y1 - y2;
double v2x = x1 - x3, v2y = y1 - y3;
return abs(v1x * v2y - v2x * v1y) / 2;
}
void solve() {
int n; cin >> n;
vector> points(n);
for (auto& [x, y] : points) cin >> x >> y;
double min_area = 1e18;
for (int i=0; i= 1e-6) min_area = min(min_area, area);
}
cout << fixed << setprecision(6) << (min_area == 1e18 ? -1 : min_area) << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int t; cin >> t;
while (t--) solve();
return 0;
}
E - 가격 계산
특정 수량에 따라 두 가지 요금 중 하나를 적용하는 계산 로직입니다.
#include <iostream>
using namespace std;
typedef long long ll;
void solve() {
ll n, x, a, b;
cin >> n >> x >> a >> b;
cout << x * b + (n - x) * a << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int t; cin >> t;
while (t--) solve();
return 0;
}
G - 게임 상태 관리
그리드 내 객체의 상태 변화를 처리하며, 병합 및 분할 로직을 구현했습니다.
#include <iostream>
#include <vector>
#include <set>
using namespace std;
typedef long long ll;
struct Cell { int x, y, state; };
const int dx[] = {1,-1,0,0}, dy[] = {0,0,1,-1};
void solve() {
int n; cin >> n;
vector grid(20, vector<int>(20, 0));
vector<int> parent(20*20), size(20*20);
auto find = [&](int x) {
while (parent[x] != x) x = parent[x];
return x;
};
auto merge = [&](int u, int v) {
int rootU = find(u), rootV = find(v);
if (rootU == rootV) return;
parent[rootU] = rootV;
size[rootV] += size[rootU];
};
for (int t=1; t<=n; ++t) {
int x, y; cin >> x >> y;
// ... (상세 로직 생략)
}
// ... (결과 출력)
}
H - 코드 압축
격자 내 물체 개수를 4진법으로 인코딩하여 처리하는 알고리즘입니다.
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
void solve() {
int n, m, k; cin >> n >> m >> k;
vector<int> power(15, 1);
for (int i=1; i<10; ++i) power[i] = power[i-1]*4;
vector grid(n+1, vector<int>(m+1, 0));
vector<Cell> data(k+1);
int current = 0;
for (int i=1; i<=k; ++i) {
int x, y, cnt; cin >> x >> y >> cnt;
current += power[i-1] * cnt;
data[i] = {x,y,cnt};
grid[x][y] = i;
}
vector<int> dist(power[k], 1e18);
dist[current] = 0;
// ... (BFS 기반 최단 경로 계산)
cout << dist[0] << endl;
}