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>
#define ll int
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
using namespace std;
const ll maxn=150005;
ll x[maxn], y[maxn], z[maxn], cnt[maxn];
ll cntx[maxn], cnty[maxn], cntz[maxn];
vector <ll> px[maxn], py[maxn], pz[maxn];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n; cin >> n;
vector <ll> nx, ny, nz;
for (ll i=1; i<=n; i++)
{
cin >> x[i] >> y[i] >> z[i];
nx.pb(x[i]), ny.pb(y[i]), nz.pb(z[i]);
}
sort(nx.begin(), nx.end());
nx.resize(unique(nx.begin(), nx.end())-nx.begin());
sort(ny.begin(), ny.end());
ny.resize(unique(ny.begin(), ny.end())-ny.begin());
sort(nz.begin(), nz.end());
nz.resize(unique(nz.begin(), nz.end())-nz.begin());
for (ll i=1; i<=n; i++)
{
x[i]=lower_bound(nx.begin(), nx.end(), x[i])-nx.begin()+1;
y[i]=lower_bound(ny.begin(), ny.end(), y[i])-ny.begin()+1;
z[i]=lower_bound(nz.begin(), nz.end(), z[i])-nz.begin()+1;
px[x[i]].pb(i), py[y[i]].pb(i), pz[z[i]].pb(i);
cntx[x[i]]++, cnty[y[i]]++, cntz[z[i]]++;
}
ll mx=nx.size(), my=ny.size(), mz=nz.size();
for (ll i=1; i<=n; i++)
{
cnt[i]=(x[i]==mx)+(y[i]==my)+(z[i]==mz);
if (cnt[i]>=2) cntx[x[i]]--, cnty[y[i]]--, cntz[z[i]]--;
}
while (1)
{
bool ok=1;
if (!cntx[mx] && mx)
{
while (!cntx[mx] && mx) mx--;
for (ll i:px[mx])
{
cnt[i]++;
if (cnt[i]==2)
{
ok=0;
cntx[x[i]]--, cnty[y[i]]--, cntz[z[i]]--;
}
}
}
if (!cnty[my] && my)
{
while (!cnty[my] && my) my--;
for (ll i:py[my])
{
cnt[i]++;
if (cnt[i]==2)
{
ok=0;
cntx[x[i]]--, cnty[y[i]]--, cntz[z[i]]--;
}
}
}
if (!cntz[mz] && mz)
{
while (!cntz[mz] && mz) mz--;
for (ll i:pz[mz])
{
cnt[i]++;
if (cnt[i]==2)
{
ok=0;
cntx[x[i]]--, cnty[y[i]]--, cntz[z[i]]--;
}
}
}
if (ok) break;
}
if (mx==0 && my==0 && mz==0) cout << -1;
else cout << nx[mx-1]+ny[my-1]+nz[mz-1] << "\n";
}
# | 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... |