# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1178822 | ericl23302 | Aliens (IOI16_aliens) | C++20 | 0 ms | 0 KiB |
#include "aliens.h"
#include <iostream>
#include <algorithm>
#include <utility>
#include <climits>
#include <set>
using namespace std;
long long sq(long long a) {
return a * a;
}
long long sq(int a) {
return (long long)(a) * a;
}
long long getArea(pair<int, int> a) {
return sq(a.second - a.first + 1);
}
long long getArea(int a, int b) {
return sq(b - a + 1);
}
long long intersection(pair<int, int> a, pair<int, int> b) {
return sq(max(0, min(a.second, b.second) - max(a.first, b.first) + 1));
}
long long unite(pair<int, int> a, pair<int, int> b) {
return getArea(min(a.first, b.first), max(a.second, b.second));
}
long long take_photos(int n, int m, int k, std::vector<int> r, std::vector<int> c) {
set<pair<int, int>> curPoints;
for (int i = 0; i < n; ++i) curPoints.emplace(min(r[i], c[i]), max(r[i], c[i]));
long long leastCost = LLONG_MAX / 2;
while (true) {
leastCost = LLONG_MAX / 2;
auto curStart = curPoints.end(), curEnd = curPoints.end();
for (auto i = curPoints.begin(); i != curPoints.end(); ++i) {
for (auto j = next(i); j != curPoints.end(); ++j) {
auto a = *i, b = *j;
long long curCost = unite(a, b) - getArea(a) - getArea(b) + intersection(a, b);
if (curCost < leastCost) {
leastCost = curCost;
curStart = i;
curEnd = j;
}
}
}
if (curPoints.size() > k || leastCost == 0) {
auto a = *curStart, b = *curEnd;
pair<int, int> newRange = make_pair(min(a.first, b.first), max(a.second, b.second));
curPoints.erase(curStart);
curPoints.erase(curEnd);
curPoints.insert(newRange);
} else break;
}
// vector<pair<int, int>> condensedPoints;
// int curBegin = -1, curEnd = -1;
// for (auto i = curPoints.begin(); i != curPoints.end(); ++i) {
// auto a = *i;
// if (a.first == curBegin) curEnd = max(curEnd, a.second);
// else {
// if (curBegin != -1) condensedPoints.emplace_back(curBegin, curEnd);
// curBegin = a.first;
// curEnd = a.second;
// }
// }
// condensedPoints.emplace_back(curBegin, curEnd);
// auto a = condensedPoints[0];
// long long res = getArea(a);
// for (int i = 1; i < condensedPoints.size(); ++i) {
// auto b = condensedPoints[i];
// if (b.second <= a.second) {
// continue;
// } else {
// res += getArea(b) - intersection(a, b);
// a = b;
// }
// }
auto a = *curPoints.begin();
long long res = getArea(a);
for (auto i = next(curPoints.begin()); i != curPoints.end(); ++i) {
auto b = *i;
// if (b.second <= a.second) {
// continue;
// } else {
res += getArea(b) - intersection(a, b);
a = b;
// }
}
return res;
}