This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<v> vv;
typedef vector<vv> vvv;
typedef pair<ll, ll> p;
typedef vector<p> vp;
typedef vector<vp> vvp;
typedef vector<vvp> vvvp;
typedef pair<ll, p> tri;
typedef vector<tri> vtri;
typedef vector<vtri> vvtri;
typedef vector<vvtri> vvvtri;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vvb> vvvb;
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()
const ll INF = 1e18;
const ll mod = 1e9 + 7;
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll n, t;
cin >> n >> t;
vp cells(n);
for (ll i= 0; i < n; i++) cin >> cells[i].f >> cells[i].s;
v relevantX, relevantY;
for (ll i = 0; i < n; i++)
{
relevantX.pb(cells[i].f - 1);
relevantX.pb(cells[i].f);
relevantX.pb(cells[i].f + 1);
relevantY.pb(cells[i].s - 1);
relevantY.pb(cells[i].s);
relevantY.pb(cells[i].s + 1);
}
sort(all(relevantX)); sort(all(relevantY));
relevantX.erase(unique(all(relevantX)), relevantX.end()); relevantY.erase(unique(all(relevantY)), relevantY.end());
map<ll, ll> xShrink;
map<ll, ll> yShrink;
for (ll i = 0; i < relevantX.size(); i++) xShrink[relevantX[i]] = i + 1;
for (ll i = 0; i < relevantY.size(); i++) yShrink[relevantY[i]] = i + 1;
for (ll i = 0; i < n; i++) cells[i] = p(xShrink[cells[i].f], yShrink[cells[i].s]);
ll maxX = relevantX.size() + 2, maxY = relevantY.size() + 2;
vvb occupied(maxX, vb(maxY, false));
for (ll i = 0; i < n; i++) occupied[cells[i].f][cells[i].s] = true;
set<ll, greater<ll>> available;
for (ll i = 0; i < n; i++) available.insert(i);
v order;
vb possible(n, false);
vv adjacentStructures(n);
for (ll i = 0; i < n; i++)
{
for (ll j = i + 1; j < n; j++)
{
if (abs(cells[i].f - cells[j].f) <= 1 && abs(cells[i].s - cells[j].s) <= 1)
{
adjacentStructures[i].pb(j);
adjacentStructures[j].pb(i);
}
}
}
v supporting(n);
for (ll i = 0; i < n; i++) supporting[i] = adjacentStructures[i].size();
for (ll i = n - 1; i >= 0; i--)
{
bool done = false;
for (ll x : available)
{
vv supplyComp(maxX, v(maxY));
vvvp newSupplyGraph(maxX, vvp(maxY));
occupied[cells[x].f][cells[x].s] = false;
for (ll i = 0; i < maxX; i++)
{
for (ll j = 0; j < maxY; j++)
{
if (occupied[i][j]) continue;
if (i + 1 < maxX && !occupied[i + 1][j])
{
newSupplyGraph[i][j].eb(i+1,j);
newSupplyGraph[i+1][j].eb(i,j);
}
if (j + 1 < maxY && !occupied[i][j + 1])
{
newSupplyGraph[i][j].eb(i,j+1);
newSupplyGraph[i][j+1].eb(i,j);
}
}
}
vv component(maxX, v(maxY, -1));
ll comp = 0;
for (ll i = 0; i < maxX; i++)
{
for (ll j = 0; j < maxY; j++)
{
if (component[i][j] != -1) continue;
stack<p> dfs;
dfs.emplace(i, j);
while (!dfs.empty())
{
auto top = dfs.top();
dfs.pop();
if (component[top.f][top.s] != -1) continue;
component[top.f][top.s] = comp;
for (auto x : newSupplyGraph[top.f][top.s]) dfs.push(x);
}
comp++;
}
}
bool poss = true;
if (component[cells[x].f][cells[x].s] != component[maxX - 1][maxY - 1]) poss = false;
for (ll x : adjacentStructures[x]) supporting[x]--;
for (ll y : available)
{
if (y != x && supporting[y] == 0 && i != 1)
{
poss = false;
}
}
if (!poss)
{
occupied[cells[x].f][cells[x].s] = true;
for (ll x : adjacentStructures[x]) supporting[x]++;
continue;
}
available.erase(x);
order.pb(x);
done = true;
break;
}
if (!done)
{
cout << "NO\n";
return 0;
}
}
reverse(all(order));
cout << "YES\n";
for (ll x : order) cout << x + 1 << "\n";
return 0;
}
Compilation message (stderr)
skyscrapers.cpp: In function 'int main()':
skyscrapers.cpp:51:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for (ll i = 0; i < relevantX.size(); i++) xShrink[relevantX[i]] = i + 1;
| ~~^~~~~~~~~~~~~~~~~~
skyscrapers.cpp:52:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (ll i = 0; i < relevantY.size(); i++) yShrink[relevantY[i]] = i + 1;
| ~~^~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |