text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int mod = 1e9 + 7; int power(int a, long long b) { int res = 1; while (b) { if (b & 1) res = 1LL * res * a; a = 1LL * a * a; b >>= 1; } return res; } long long min(int a, long long b) { if (a > b) return b; return a; } long long max(int a, long long b) { if (a > b) return a; return b; } int n, m; vector<int> v[100005]; void solve() { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } for (int i = 0; i < n; i++) { v[arr[i]].push_back(i); } for (int i = 1; i <= 100005; i++) { if (v[i].size() < v[i + 1].size()) { cout << -1; return; } } int val = v[1].size(); for (int i = 1; i <= 100000; i++) { for (int j = 0; j < v[i].size(); j++) { arr[v[i][j]] = j + 1; } } cout << val << "\n"; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int mul = 0; if (mul == 1) { int t; cin >> t; while (t--) { solve(); cout << "\n"; } } else { solve(); } }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; bool prime[1000001]; vector<int> v1; void sieve() { memset(prime, true, sizeof(prime)); for (int i = 2; i * i <= 1000000; i++) if (prime[i]) for (int j = i * i; j <= 1000000; j += i) prime[j] = false; prime[0] = prime[1] = false; } int main() { int n; cin >> n; map<int, int> mp1; int max1 = 0; int mm = 0; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; mm = max(mm, a[i]); mp1[a[i]]++; max1 = max(max1, mp1[a[i]]); } int cnt1 = 1; for (int i = 2; i < mm + 1; i++) { if (mp1[i] <= mp1[i - 1]) { } else { cnt1 = 2; break; } } if (cnt1 == 2) { cout << "-1\n"; return 0; } cout << max1 << endl; for (int i = 0; i < n; i++) { cout << mp1[a[i]] << " "; mp1[a[i]]--; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int32_t main() { ios::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; vector<long long> v(n), ans(n); vector<vector<long long> > idxs(100100); for (long long i = 0; i < n; i++) { cin >> v[i]; idxs[v[i]].push_back(i); } long long up = 0; for (long long i = 1; i <= 100000; i++) { if (idxs[i].size()) up = i; if (idxs[i].size() < idxs[i + 1].size()) { cout << -1; return 0; } } long long cnt = 1; while (up) { while ((up > 0) && idxs[up].size() == 0) up--; if (!up) break; for (long long i = 1; i <= up; i++) { ans[idxs[i].back()] = cnt; idxs[i].pop_back(); } cnt++; } cout << (cnt - 1) << endl; for (long long i = 0; i < n; i++) cout << ans[i] << " "; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int k; cin >> k; vector<int> ans(k, -1); map<int, vector<int> > mapa; for (int i = 0; i < k; ++i) { int x; cin >> x; mapa[x].push_back(i); } int cc = 1; while (!mapa.empty()) { map<int, vector<int> >::iterator it = --mapa.end(); int x = it->first; for (int j = 1; j <= x; ++j) { if (!mapa.count(j)) { cout << "-1\n"; return 0; } it = mapa.lower_bound(j); int pos = it->second.back(); ans[pos] = cc; it->second.pop_back(); if (it->second.empty()) { mapa.erase(j); } } ++cc; } cout << cc - 1 << "\n"; for (int i = 0; i < k; ++i) { cout << ans[i] << " "; } cout << "\n"; }
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const long long INFLL = 0x3f3f3f3f3f3f3f3f; const int MOD = 1e9 + 7; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; int maior = -1; int p, pos[100005], qnt[100005]; memset(qnt, 0, sizeof(qnt)); for (int i = 0; i < n; i++) { cin >> p; qnt[p]++; pos[i + 1] = qnt[p]; maior = max(maior, p); } for (int i = 2; i <= maior; i++) { if (qnt[i] > qnt[i - 1]) { cout << "-1\n"; return 0; } } cout << qnt[1] << endl; for (int i = 0; i < n; i++) cout << pos[i + 1] << " "; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int arr[100005], permu[100005]; map<int, int> mp; int main() { int n, mx = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i], mp[arr[i]]++, permu[i] = mp[arr[i]], mx = max(mx, arr[i]); } for (int i = 2; i <= mx; i++) { if (mp[i] > mp[i - 1] || mp[i] == 0) { puts("-1"); return 0; } } if (mp[1] == 0) { puts("-1"); return 0; } cout << mp[1] << "\n"; for (int i = 0; i < n; i++) { cout << permu[i] << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 110000; int a[maxn], tot[maxn], ans[maxn]; int main() { int n; scanf("%d", &n); memset(tot, 0, sizeof(tot)); bool flag = true; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] > n) { flag = false; } else { ans[i] = ++tot[a[i]]; } } if (!flag) { puts("-1"); return 0; } for (int i = 1, tmp = 0; i <= n; i++) { tmp += tot[i]; if (tmp == n) { break; } if ((i > 1) && (tot[i] > tot[i - 1])) { puts("-1"); return 0; } } printf("%d\n", tot[1]); for (int i = 0; i < n; i++) { printf("%d", ans[i]); i < n - 1 ? putchar(' ') : puts(""); } return 0; }
#include <bits/stdc++.h> const int inf = 1000000000ll; const long long inf64 = 1ll * inf * inf; const long long base = inf + 7; const double pi = acos(-1.0); using namespace std; template <typename T, typename P> const ostream& operator<<(ostream& os, const pair<T, P>& a) { os.precision(10); os << fixed; os << a.first; os << ' '; os << a.second; os << '\n'; return os; } bool solve() { int n; scanf("%d", &n); vector<int> cnt(n + 1); vector<pair<int, int> > a(n + 1); for (int i(1); i <= n; i++) { scanf("%d", &a[i].first); if (a[i].first > n) { puts("-1"); return true; } cnt[a[i].first]++; a[i].second = i; } for (int i(1); i + 1 <= n; i++) { if (cnt[i] < cnt[i + 1]) { puts("-1"); return true; } } sort(a.begin() + 1, a.end()); vector<int> ans(n + 1); for (int i(1); i <= n; i++) { int j = i; while (j <= n && a[j].first == a[i].first) { ans[a[j].second] = j - i + 1; j++; } i = j - 1; } printf("%d\n", cnt[1]); for (int i(1); i <= n; i++) { printf("%d ", ans[i]); } puts(""); return true; } int main() { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> tab[100000 + 10]; void calc(vector<int>& v, vector<int>& xs) { int id = 1; int maxval = *max_element(v.begin(), v.end()); for (int i = maxval; i > 0; i--) { if (xs[i] == 0) continue; int n = xs[i]; for (int j = i; j > 0; j--) { if (xs[j] < n) { puts("-1"); return; } for (int k = 0; k < n; k++) tab[j].push_back(id + k); xs[j] -= n; } id += n; } printf("%d\n", id - 1); for (int i = 0; i < (int)(v.size()); i++) { if (tab[v[i]].empty()) break; assert(tab[v[i]].size() > 0); if (i > 0) putchar(' '); printf("%d", tab[v[i]].back()); tab[v[i]].pop_back(); } puts(""); } int main() { int n; scanf("%d", &n); vector<int> v(n), xs(100000 + 10); for (int i = 0; i < (int)(n); i++) { scanf("%d", &v[i]); xs[v[i]]++; } calc(v, xs); }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; bool b = true; scanf("%d", &n); int arr[100001]; for (int i = 0; i < 100001; i++) { arr[i] = 0; } vector<pair<int, int> > v; v.resize(n); for (int i = 0; i < n; i++) { scanf("%d", &v[i].first); arr[v[i].first]++; v[i].second = arr[v[i].first]; } for (int i = 1; i < 100000; i++) { if (arr[i] < arr[i + 1]) { b = false; break; } } if (!b) { cout << -1; } else { cout << arr[1] << endl; for (int i = 0; i < n; i++) { cout << v[i].second << " "; } } }
#include <bits/stdc++.h> using namespace std; const int MAXN = 100 * 1000 + 3; int n, t[MAXN]; pair<int, int> p[MAXN]; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> p[i].first; p[i].second = i; t[p[i].first]++; } sort(p, p + n); bool is_r = true; for (int i = 1; i < p[n - 1].first; i++) if (t[i + 1] > t[i]) is_r = false; if (is_r) { int c = 1, k = 1; for (int i = 0; i < n; i++) { if (p[i].first == c) { p[i].first = k; k++; } else { c++; k = 1; p[i].first = k; k++; } } for (int i = 0; i < n; i++) swap(p[i].first, p[i].second); sort(p, p + n); cout << t[1] << endl; for (int i = 0; i < n; i++) cout << p[i].second << " "; cout << endl; } else { cout << -1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); long long n, m, a, i, b, j, k, t, x, y, z, tc, u, v, w; cin >> n; long long arr[100005]; long long freq[100005] = {0}; long long maxx = 0; long long ans[100005]; set<long long> adj[100005]; set<long long>::iterator itr; for (i = 0; i < n; i++) { cin >> arr[i]; freq[arr[i]]++; adj[freq[arr[i]]].insert(arr[i]); ans[i + 1] = freq[arr[i]]; maxx = max(maxx, freq[arr[i]]); } long long flag = 0; for (i = 1; i <= maxx; i++) { itr = adj[i].end(); itr--; if (adj[i].size() != *itr) { flag = 1; break; } } if (flag == 1) cout << -1 << endl; else { cout << maxx << endl; for (i = 1; i <= n; i++) cout << ans[i] << " "; cout << endl; } }
#include <bits/stdc++.h> using namespace std; int p[100005], a[100005], g[100005]; int main() { int n, i, m, t = 0; memset(p, 0, sizeof(p)); memset(g, 0, sizeof(g)); scanf("%d", &n); m = 0; for (i = 0; i < n; i++) { scanf("%d", a + i); if (m < a[i]) m = a[i]; p[a[i]]++; } for (i = 2; i <= m; i++) if (p[i] > p[i - 1]) { t = 1; break; } if (t) printf("-1\n"); else { printf("%d\n", p[1]); for (i = 0; i < n - 1; i++) printf("%d ", ++g[a[i]]); printf("%d\n", ++g[a[n - 1]]); } return 0; }
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > A; int ANS[101000]; int main() { int N; scanf("%d", &N); A.resize(N); for (int i = 0; i < N; i++) { A[i].second = i; scanf("%d", &A[i].first); } sort(A.begin(), A.end()); int prev = 0; int count = 100000; int maxx = 100000; bool ok = 1; int maxcount = -1; for (int i = 0; i < N; i++) { if ((A[i].first != prev && count > maxx) || A[i].first > prev + 1) { ok = 0; break; } if (A[i].first == prev + 1) { maxx = count; count = 0; prev = A[i].first; } count++; ANS[A[i].second] = count; maxcount = max(maxcount, count); } if (count > maxx) ok = 0; if (ok) { printf("%d\n", maxcount); for (int i = 0; i < N; i++) printf("%d ", ANS[i]); printf("\n"); } else printf("-1\n"); }
#include <bits/stdc++.h> using namespace std; int n, a[100005], b[100005]; int main() { for (; cin >> a[n]; n++) if (n > 0) b[a[n]]++; for (int i = 2; i < 100005; i++) if (b[i] > b[i - 1]) b[1] = -1, n = 1; for (int i = 0; i < n; i++) cout << (i ? b[a[i]]-- : b[1]) << (i ? " " : "\n"); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 100005; vector<int> cnt[N]; int res[N]; int main() { int n; while (cin >> n) { for (int i = 1; i < N; i++) cnt[i].clear(); for (int i = 1; i <= n; i++) { int v; cin >> v; cnt[v].push_back(i); } bool ok = true; for (int i = 1; i < N; i++) { if (i > 1 && cnt[i].size() > cnt[i - 1].size()) ok = false; for (int j = 0; j < cnt[i].size(); j++) { res[cnt[i][j]] = j + 1; } } if (!ok) { printf("%d\n", -1); } else { printf("%d\n", cnt[1].size()); for (int i = 1; i <= n; i++) printf("%d%c", res[i], i == n ? '\n' : ' '); } } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const int maxn = 1e5 + 5; vector<int> v[maxn]; int ans[maxn]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for (int i = 0; i < n; i++) { int ai; cin >> ai; v[ai].push_back(i); } int cnt = 0; for (int i = 100000; i >= 1;) { if (v[i].empty()) { i--; } else { cnt++; for (int j = i; j >= 1; j--) { if (v[j].empty()) { cout << -1 << '\n'; return 0; } ans[v[j].back()] = cnt; v[j].pop_back(); } } } cout << cnt << '\n'; for (int i = 0; i < n; i++) { cout << ans[i] << ' '; } cout << '\n'; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 20; int n, a; int arr[N]; vector<vector<int> > ve(N); int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &a); ve[a].push_back(i); } for (int i = 2; i < N; ++i) if (ve[i].size() > ve[i - 1].size()) return printf("-1"), 0; bool sto = false; for (int i = 1; i < N; ++i) { if (ve[i].size() == 0) sto = true; if (ve[i].size() > 0) { if (sto) return printf("-1"), 0; } } printf("%d\n", ve[1].size()); for (int i = 1; i < N; ++i) { for (int j = 0; j < ve[i].size(); ++j) { arr[ve[i][j]] = j + 1; } } for (int i = 0; i < n; ++i) printf("%d ", arr[i]); return 0; }
#include <bits/stdc++.h> using namespace std; int a[110000]; int b[110000]; int main() { int n, i, j, k; scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d", &j), b[i] = j, a[j]++; for (i = 2; i <= 100000; i++) if (a[i] > a[i - 1]) { printf("-1\n"); return 0; } printf("%d\n", a[1]); for (i = 1; i <= n; i++) printf("%d ", a[b[i]]--); puts(""); }
#include <bits/stdc++.h> using namespace std; int main() { int t, n, m, i, j, k, l, flag; int b[100009], c[100009]; pair<int, int> pa[100009]; cin >> n; k = 0; flag = 1; for (i = 0; i < n; i++) { cin >> pa[i].first; pa[i].second = i; b[pa[i].first]++; if (k < pa[i].first) k = pa[i].first; } c[1] = b[1]; for (i = 2; i <= k; i++) c[i] = min(b[i], c[i - 1]); sort(pa, pa + n); for (i = k; i >= 2; i--) { if (c[i - 1] < b[i]) flag = 0; } if (flag) { m = 0; c[pa[n - 1].second] = 1; j = 1; for (i = n - 2; i >= 0; i--) { if (pa[i].first == pa[i + 1].first) j++; else { m = max(m, j); j = 1; } c[pa[i].second] = j; } m = max(m, j); cout << m << "\n"; for (i = 0; i < n; i++) cout << c[i] << " "; } else cout << "-1"; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 100 * 1000; int n; int per[MAXN]; int num[MAXN]; int main() { cin >> n; int maxPer = 0; for (int i = 0; i < n; i++) { cin >> per[i]; num[per[i] - 1]++; if (maxPer < per[i]) { maxPer = per[i]; } } for (int i = 1; i < maxPer; i++) { if (num[i] > num[i - 1]) { cout << -1 << endl; return 0; } } cout << num[0] << endl; for (int i = 0; i < n; i++) { cout << num[per[i] - 1]-- << ' '; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; signed main() { int n; cin >> n; int a[100005], b[100005] = {0}; int ans = 0, ma = 0; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] == 1) ans++; b[a[i]]++; ma = max(ma, a[i]); } for (int i = 1; i < ma; i++) { if (b[i] < b[i + 1]) { cout << -1; return 0; } } cout << ans << endl; map<int, int> mp; for (int i = 0; i < n; i++) { mp[a[i]]++; int an = mp[a[i]]; cout << an << " "; } return 0; }
#include <bits/stdc++.h> int main() { int n; std::cin >> n; std::vector<int> v(n); for (int i = 0; i < n; ++i) { std::cin >> v[i]; } std::vector<std::vector<int> > a(100001); for (int i = 0; i < n; ++i) { a[v[i]].push_back(i); } int num = a[1].size(); if (num == 0) { std::cout << -1 << std::endl; return 0; } for (int i = 2; i < (int)a.size(); ++i) { if (num < (int)a[i].size()) { std::cout << -1 << std::endl; return 0; } else { num = a[i].size(); } } std::vector<int> res(n); for (int i = 1; i < (int)a.size(); ++i) { for (int j = 0; j < (int)a[i].size(); ++j) { res[a[i][j]] = j + 1; } } std::cout << a[1].size() << std::endl; std::copy(res.begin(), res.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << std::endl; }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; const double pi = 3.141592653589793238462; const int nmax = 200000; const int hash_base = 119; const long long inf = 2e18; int n, val, ans[nmax], cnt = 1; vector<int> pos[nmax]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &val); pos[val].push_back(i); }; val = n; while (!pos[1].empty()) { for (int i = 1; !pos[i].empty(); i++) { ans[pos[i].back()] = cnt; pos[i].pop_back(); --val; }; ++cnt; }; if (val != 0) printf("-1\n"); else { printf("%d\n", cnt - 1); for (int i = 1; i <= n; i++) printf("%d ", ans[i]); }; return 0; }
#include <bits/stdc++.h> using namespace std; int a[100007], cnt[100007], be[100007], ma; int n; bool check() { for (int i = 1; i < ma; ++i) if (cnt[i] < cnt[i + 1]) { puts("-1"); return 0; } cout << cnt[1] << endl; for (int i = 0; i < n; ++i) { cout << (++be[a[i]]); if (i == n - 1) puts(""); else cout << " "; } return 1; } int main() { scanf("%d", &n); ma = 0; for (int i = 0; i < n; ++i) { scanf("%d", a + i); cnt[a[i]]++; ma = max(a[i], ma); } check(); }
#include <bits/stdc++.h> using namespace std; void open() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } int perm[100005]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; pair<int, int> sk[n]; for (int j = 0; j < n; j++) { cin >> sk[j].first; sk[j].second = j; } sort(sk, sk + n); int rez[n]; int l = 1; int an = 0; for (int j = 0; j < n; j++) { if (sk[j].first != l) { cout << -1; exit(0); } int cnt = 1; while (sk[j].first == l) { if (sk[j].first - perm[cnt] != 1) { cout << -1; exit(0); } rez[sk[j].second] = cnt; perm[cnt] = l; an = max(an, cnt); cnt++; j++; } j--; l++; } cout << an << "\n"; for (int j = 0; j < n; j++) cout << rez[j] << " "; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, a[100001], b[100001], uk = 0, d = 0, t = 0; vector<int> q, w[100001]; cin >> n; for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); b[i] = a[i]; } sort(a, a + n); for (int i = 0; i < n; ++i) { uk++; d++; t = 1; while (d == a[i]) { w[uk].push_back(t++); i++; } i--; } size_t ot = w[1].size(); for (int i = 2; i <= uk; ++i) { ot = max(w[i].size(), ot); if (w[i - 1].size() >= w[i].size()) { continue; } else { cout << -1; return 0; } } cout << ot << endl; for (int i = 0; i < n; ++i) { printf("%d ", w[b[i]].back()); w[b[i]].pop_back(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { vector<int> *v = new vector<int>(); int n, a; cin >> n; int arr[n], res[n]; for (int i = 0; i < n; i++) { cin >> a; arr[i] = a; if (a > v->size()) { v->resize(a); } v->at(a - 1)++; } int nv = v->at(0); for (int i = 1; i < v->size(); i++) { if (v->at(i) > v->at(i - 1) || v->at(i) == 0) { cout << -1; return 0; } } cout << v->at(0) << "\n"; for (int i = 0; i < n; i++) { res[i] = (v->at(arr[i] - 1))--; } for (int i = 0; i < n; i++) { cout << res[i] << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout.setf(ios::fixed); cout.setf(ios::showpoint); { long long int n; cin >> n; long long int a[n], freq[100001] = {0}; long long int max = 0, maxf = 0; for (long long int i = 0; i < n; i++) { cin >> a[i]; freq[a[i]]++; if (freq[a[i]] > maxf) maxf = freq[a[i]]; if (a[i] > max) max = a[i]; } bool ok = true; for (long long int i = max + 1 - 1; i >= 2; i--) { if (freq[i] > freq[i - 1]) { ok = false; break; } } if (!ok) cout << -1 << endl; else { cout << maxf << endl; for (long long int i = 0; i < n; i++) cout << freq[a[i]] << " ", freq[a[i]]--; } } return 0; }
#include <bits/stdc++.h> using namespace std; const long long c = 200000, inf = 2000000000; int n, x[c], a[c]; int main() { cin >> n; int p = 1; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] < 1 || a[i] > n) p = 0; x[a[i]]++; } for (int i = 1; i < n; i++) if (x[i] < x[i + 1]) p = 0; if (p) { cout << x[1] << endl; for (int i = 0; i < n; i++) { cout << x[a[i]] << " "; x[a[i]]--; } } else cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; int s[100010]; int main() { int n; cin >> n; int a[n], mx = 0, ans[n], si = 0; for (int i = 0; i < n; i++) cin >> a[i], s[a[i]]++, mx = max(mx, a[i]), ans[i] = s[a[i]], si = max(si, s[a[i]]); for (int i = 1; i <= mx; i++) if (s[i] < s[i + 1]) { cout << -1; return 0; } cout << si << endl; for (int i = 0; i < n; i++) cout << ans[i] << " "; }
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double eps = 1e-10; bool isdig(char x) { return x >= '0' && x <= '9'; } bool isup(char x) { return x >= 'A' && x <= 'Z'; } bool islow(char x) { return x >= 'a' && x <= 'z'; } bool isalp(char x) { return isup(x) || islow(x); } int a[100005], num[100005]; int main() { int n, i; while (scanf("%d", &n) != EOF) { memset(num, 0, sizeof(num)); for (i = 1; i <= n; i++) { scanf("%d", &a[i]); num[a[i]]++; } int k = 0, sum = 0; for (i = n; i >= 1; i--) { sum += num[i]; if (k <= num[i]) { k = num[i]; } else { k = -1; break; } } if (sum != n) k = -1; if (k == -1) { puts("-1"); } else { printf("%d\n", k); for (i = 1; i <= n; i++) { printf("%d", num[a[i]]--); putchar(i == n ? 10 : ' '); } } } return 0; }
#include <bits/stdc++.h> int n; int a[100005], b[100005], c[100005]; int res[100005]; bool check() { c[0] = 0x3f3f3f3f; for (int i = 0; i < n; i++) { if (a[i] > n || c[i] < c[i + 1]) { return false; } } return true; } int main() { memset(b, 0, sizeof(b)); memset(c, 0, sizeof(c)); scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); c[a[i]]++; } if (!check()) { puts("-1"); return 0; } printf("%d\n", c[1]); for (int i = 0; i < n; i++) { res[i] = ++b[a[i]]; printf("%d%c", res[i], i + 1 == n ? '\n' : ' '); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXn = 1e5 + 10; int ar[MAXn], ar2[MAXn], n, ans[MAXn]; int main() { cin >> n; for (int i = 0; i < n; i++) { int a; cin >> a; ar2[a]++; ans[i] = ++ar[a]; } for (int i = 1; i < MAXn - 1; i++) if (ar2[i] < ar2[i + 1]) return cout << -1, 0; cout << ar2[1] << endl; for (int i = 0; i < n; i++) cout << ans[i] << ' '; }
#include <bits/stdc++.h> using namespace std; const int NMAX = 100000 + 10; int n; pair<int, int> a[NMAX]; int ans[NMAX]; vector<int> p[NMAX]; int cnt = 0; int main() { scanf("%d", &n); for (int i = 0; i < int(n); ++i) { int x; scanf("%d", &x); a[i] = make_pair(x, i); } for (int i = 0; i < int(n); ++i) { p[a[i].first].push_back(a[i].second); } for (int i = 2; i < NMAX; ++i) { if (p[i].size() > p[i - 1].size()) { printf("-1\n"); return 0; } } int cnt = 0; for (int i = 0; i < int(NMAX); ++i) { cnt = max(cnt, int(p[i].size())); for (int j = 0; j < int(p[i].size()); ++j) { ans[p[i][j]] = j + 1; } } printf("%d\n", cnt); for (int i = 0; i < int(n); ++i) { printf("%d ", ans[i]); } printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int INF = 2e9; const int maxn = 1e5 + 5; vector<int> cnt[maxn]; int perm[maxn]; int ans[maxn]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; cnt[x].push_back(i); perm[x]++; } int ans1 = 0; for (int i = 1e5; i >= 1; i--) { if (cnt[i].size() < cnt[i + 1].size()) { cout << -1; return 0; } perm[i] = cnt[i].size() - cnt[i + 1].size(); ans1 += perm[i]; } int p = 0; int u = 0; for (int i = maxn - 1; i > 0; i--) { for (int j = 0; j < perm[i]; j++) { u++; for (int k = 1; k <= i; k++) { ans[cnt[k][p]] = u; } p++; } } cout << ans1 << endl; for (int i = 0; i < n; i++) cout << ans[i] << " "; return 0; }
#include <bits/stdc++.h> using namespace std; signed main() { int n; cin >> n; int a[100005], b[100005] = {}; int ans = 0, ma = 0; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] == 1) ans++; b[a[i]]++; ma = max(ma, a[i]); } for (int i = 1; i < ma; i++) { if (b[i] < b[i + 1]) { cout << -1; return 0; } } cout << ans << endl; map<int, int> mp; for (int i = 0; i < n; i++) { mp[a[i]]++; int an = mp[a[i]]; cout << an << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool uax(T &x, T y) { return (y > x) ? x = y, true : false; } template <typename T> inline bool uin(T &x, T y) { return (y < x) ? x = y, true : false; } template <typename T> void kek(T ans) { cout << ans << endl; exit(0); } const int MOD = (int)1e9 + 7; const long long INF = (long long)1e18 + 42; void solve_case() { int n; cin >> n; const int N = 1e5 + 42; vector<int> cnt(N), a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; cnt[a[i]]++; } for (int i = 1; i < N - 1; ++i) { if (cnt[i] < cnt[i + 1]) kek(-1); } cout << cnt[1] << '\n'; for (int x : a) { cout << cnt[x] << ' '; cnt[x]--; } } int main() { cin.tie(nullptr)->sync_with_stdio(false); int tt = 1; while (tt--) { solve_case(); } }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int a[n], b[100001] = {}; for (int i = 0; i < n; i++) { cin >> a[i]; b[a[i]]++; } int m = b[1]; for (int i = 2; i <= 1e5; i++) { if (m < b[i]) { cout << -1 << endl; return 0; } m = min(b[i], m); } cout << b[1] << endl; for (int i = 0; i < n; i++) { cout << b[a[i]] << " ", b[a[i]]--; } return 0; }
#include <bits/stdc++.h> using namespace std; struct S { int id, num, group; S() {} bool operator<(const S &rhs) const { return num < rhs.num; } }; int main() { int n, tmp[100000], pre, ng; while (scanf("%d", &n) + 1) { S s[100000]; for (int i = 0; i < (int)(n); i++) { scanf("%d", &s[i].num); s[i].id = i; } sort(s, s + n); if (s[0].num > 1) { puts("-1"); goto NEXT; } pre = 1 << 28; for (int i = 1, j = 0; j < n; i++) { int k = j; while (j < n && s[j].num == i) { s[j].group = j - k + 1; j++; } if (i == 1) ng = j - k; if (j - k > pre) { puts("-1"); goto NEXT; } pre = j - k; } for (int i = 0; i < (int)(n); i++) tmp[s[i].id] = s[i].group; printf("%d\n", ng); for (int i = 0; i < (int)(n); i++) printf("%d ", tmp[i]); puts(""); NEXT:; } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxN = 1000 * 100 + 1; int main() { int i, n, a[maxN], b[maxN]; memset(b, 0, sizeof(b)); cin >> n; for (i = 1; i <= n; ++i) { cin >> a[i]; if (a[i] > n) { cout << "-1\n"; return 0; } ++b[a[i]]; } for (i = 2; i <= n; ++i) if (b[i - 1] < b[i]) { cout << "-1\n"; return 0; } cout << b[1] << endl; for (i = 1; i <= n; ++i) { cout << b[a[i]] << " "; --b[a[i]]; } return 0; }
#include <bits/stdc++.h> using namespace std; signed main() { int n; cin >> n; int a[100005], b[100005] = {}; int ans = 0, ma = 0; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] == 1) ans++; b[a[i]]++; ma = max(ma, a[i]); } for (int i = 1; i < ma; i++) { if (b[i] < b[i + 1]) { cout << -1; return 0; } } cout << ans << endl; map<int, int> mp; for (int i = 0; i < n; i++) { mp[a[i]]++; int an = mp[a[i]]; cout << an << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; double esp = 1e-9; const double PI = acos(-1.0); const int INF = 0x3f3f3f3f; const int MOD = 1000000007; int dx[4] = {1, 1, -1, -1}; int dy[4] = {1, -1, 1, -1}; int s[110010]; int q[110010]; int main() { int n, t; memset(s, 0, sizeof(s)); memset(q, 0, sizeof(q)); scanf("%d", &n); for (int x = 1; x <= n; x++) { scanf("%d", &s[x]); q[s[x]]++; } int flag = 0; for (int x = 2; x <= 100000; x++) if (q[x] > q[x - 1]) { flag = 1; break; } int ans = q[1]; memset(q, 0, sizeof(q)); if (flag) printf("%d\n", -1); else { printf("%d\n", ans); for (int x = 1; x <= n; x++) printf("%d ", ++q[s[x]]); } return 0; }
#include <bits/stdc++.h> using namespace std; int N; int A[100001]; int B[100001]; int C[100001]; int main() { int temp, flag; while (cin >> N) { flag = 1; memset(A, -1, sizeof(A)); for (int i = 1; i <= N; ++i) { scanf("%d", &B[i]); if (A[B[i]] == -1) A[B[i]] = 0; C[i] = B[i]; A[B[i]]++; } sort(C + 1, C + 1 + N); for (int i = 1; i <= N - 1; ++i) { if ((C[i + 1] - C[i] != 1 && C[i + 1] != C[i]) || (A[C[i + 1]] > A[C[i]] && C[i + 1] != C[i])) flag = 0; } if (C[1] != 1) flag = 0; if (flag) { cout << A[1] << endl; int sum = 0; int ans = A[1]; for (int i = 1; i <= N; ++i) { printf("%d", ans - A[B[i]] + 1); A[B[i]]--; sum++; if (sum < N) printf(" "); else printf("\n"); } } else cout << "-1" << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int N = 1e5 + 4; long long powmod(long long a, long long b, long long mod) { long long res = 1; a %= mod; for (; b; b >>= 1) { if (b & 1) res = (res * a) % mod; a = (a * a) % mod; } return res; } int main() { int n; cin >> n; int a[n]; int ans[n]; for (long long i = 0; i < n; i++) cin >> a[i]; vector<int> g[N]; int ma = 0; for (long long i = 0; i < n; i++) g[a[i]].push_back(i + 1); for (int i = 2; i < N; i++) { if (g[i].size() > g[i - 1].size()) { return cout << -1, 0; } } for (int i = 1; i <= n; i++) { int cnt = 1; for (int node : g[i]) ans[node - 1] = cnt, cnt++; ma = max(ma, cnt - 1); } cout << ma << "\n"; for (int i = 0; i < n; i++) cout << ans[i] << " "; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = (int)1e5 + 10; vector<int> v[N]; int ans[N]; int main() { int n; scanf("%d", &n); for (int i = 0; i < (int)n; i++) { int a; scanf("%d", &a); v[a].push_back(i); } bool ok = true; for (int i = N - 2; i > 0; i--) ok &= v[i].size() >= v[i + 1].size(); int mx = 0; for (int i = 0; i < (int)N; i++) for (int j = 0; j < (int)v[i].size(); j++) ans[v[i][j]] = j + 1, mx = max(mx, (int)v[i].size()); if (ok) { printf("%d\n", mx); for (int i = 0; i < (int)n; i++) printf("%d ", ans[i]); puts(""); } else puts("-1"); return 0; }
#include <bits/stdc++.h> using namespace std; struct xx { int v; int pla; int res; } a[111111]; int cmp(xx a, xx b) { return a.v < b.v; } int cmp1(xx a, xx b) { return a.pla < b.pla; } int flg[111111] = {0}; int main() { int n; while (scanf("%d", &n) != EOF) { int i, j, t = 0, k; for (i = 0; i < n; i++) { scanf("%d", &a[i].v); a[i].pla = i; a[i].res = 0; } sort(a, a + n, cmp); for (i = 0; i < n; i++) { if (flg[i] == 0) { if (a[i].v == 1) { t++; flg[i] = 1; a[i].res = t; for (k = 2, j = i + 1; j < n; j++) { if (flg[j] == 0) { if (a[j].v == k) flg[j] = 1, a[j].res = t, k++; } } } else break; } } if (i != n) printf("-1\n"); else { sort(a, a + n, cmp1); printf("%d\n", t); printf("%d", a[0].res); for (i = 1; i < n; i++) printf(" %d", a[i].res); printf("\n"); } } return 0; }
#include <bits/stdc++.h> using namespace std; int nums[100005]; int counting[100005]; int ans[100005]; int solve(int n) { for (int a = (1); a < (100005 - 1); a++) { if (counting[a] < counting[a + 1]) return -1; } for (int a = (0); a < (100005); a++) counting[a] = 0; for (int a = (0); a < (n); a++) { counting[nums[a]]++; ans[a] = counting[nums[a]]; } return counting[1]; } int main() { int n; cin >> n; for (int a = (0); a < (n); a++) cin >> nums[a]; for (int a = (0); a < (n); a++) counting[a] = 0; for (int a = (0); a < (n); a++) counting[nums[a]]++; int myon = solve(n); cout << myon << endl; if (myon != -1) { for (int a = (0); a < (n); a++) { cout << ans[a]; if (a != n - 1) cout << " "; else cout << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { ios_base::sync_with_stdio(false); int n; cin >> n; int maxv = 0; vector<int> t(n), c(100000); for (int i = 0; i < n; ++i) { cin >> t[i]; ++c[t[i] - 1]; if (maxv < t[i]) maxv = t[i]; } int maxnum = *max_element(c.begin(), c.begin() + maxv); bool f = true; int temp = c[0]; for (int i = 0; i < maxv; ++i) { if (c[i] > temp) { f = false; break; } temp = c[i]; } if (f) { cout << c[0] << endl; vector<int> cc(maxv); for (int i = 0; i < n; ++i) { cout << ++cc[t[i] - 1] << ' '; } cout << endl; } else { cout << -1 << endl; } return 0; }
#include <bits/stdc++.h> int a[100000]; int c[100001]; void func(int n) { int i, s = 0, t = 0; memset(c, 0, sizeof(c)); for (i = 0; i < n; i++) { c[a[i]]++; s = ((s) > (c[a[i]]) ? (s) : (c[a[i]])); t = ((t) > (a[i]) ? (t) : (a[i])); } for (i = 1; i < t; i++) { if (c[i] < c[i + 1]) { printf("-1\n"); return; } } printf("%d\n", s); for (i = 0; i < n; i++) { c[a[i]]--; a[i] = c[a[i]] + 1; } for (i = 0; i < n - 1; i++) { printf("%d ", a[i]); } printf("%d\n", a[i]); } int main() { int n, i; while (scanf("%d", &n) == 1) { for (i = 0; i < n; i++) { scanf("%d", &a[i]); } func(n); } return 0; }
#include <bits/stdc++.h> using namespace std; const int e9 = 1000 * 1000 * 1000; const long long e18 = (long long)e9 * (long long)e9; const double pi = 3.1415926535; void pV(int num) { cout << num << " "; } int ri() { int x; scanf("%d", &x); return x; } double rd() { double x; scanf("%lf", &x); return x; } long long rll() { long long x; scanf("%lld", &x); return x; } string rs() { string x; cin >> x; return x; } string rsf() { string s; char c; while (c = getchar()) { if (c >= 65 && c <= 90 || c >= 'a' && c <= 'z') s += c; else break; } return s; } void wi(int x) { printf("%d ", x); } void wd(double x) { printf("%lf ", x); } void wll(long long x) { printf("%lld ", x); } void ws(string x) { cout << x << " "; } int mas[105000]; int main() { int n = ri(); map<int, int> gg; for (int i = (int)(1); i <= (int)(n); i++) { mas[i] = ri(); gg[mas[i]]++; } bool ans = true; map<int, int>::iterator it; int last = -1; int ind = 1; for (it = gg.begin(); it != gg.end(); it++) { if (last == -1 && ind == it->first) { ind = 1; last = it->second; } else if (last == -1) { ans = false; break; } else if (it->first - ind == 1) { if (last < it->second) { ans = false; break; } else { last = it->second; ind = it->first; } } else { ans = false; break; } } if (!ans) { wi(-1); return 0; } wi(gg[1]); cout << endl; for (int i = (int)(1); i <= (int)(n); i++) { wi(gg[mas[i]]); gg[mas[i]]--; } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX_N = 100000; int as[MAX_N], cs[MAX_N]; int main() { int n; scanf("%d", &n); int maxa = 0, maxc = 0; for (int i = 0; i < n; i++) { scanf("%d", as + i); as[i]--; cs[as[i]]++; if (maxa < as[i]) maxa = as[i]; if (maxc < cs[as[i]]) maxc = cs[as[i]]; } for (int a = 1; a <= maxa; a++) if (cs[a - 1] < cs[a]) { puts("-1"); return 0; } printf("%d\n", maxc); for (int i = 0; i < n; i++) { if (i) putchar(' '); printf("%d", cs[as[i]]--); } putchar('\n'); return 0; }
#include <bits/stdc++.h> long long int mod = 1e9 + 7; using namespace std; int fre[100005]; int main() { int n; cin >> n; int arr[n]; for (int i = 0; i < n; ++i) { cin >> arr[i]; fre[arr[i]]++; } int temp = 1e6, fl = 0; for (int i = 1; i < 100005; ++i) { if (fre[i] <= temp) temp = fre[i]; else { fl = 1; break; } } if (fl == 1) { cout << -1 << "\n"; } else { sort(fre, fre + 100001, greater<int>()); cout << fre[0] << "\n"; for (int i = 0; i < 100005; ++i) { fre[i] = 0; } for (int i = 0; i < n; ++i) { fre[arr[i]] = 1; } int ans[n]; for (int i = 0; i < n; ++i) { ans[i] = fre[arr[i]]; fre[arr[i]]++; } for (int i = 0; i < n; ++i) { cout << ans[i] << " "; } cout << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int occurrences[100005]; int main() { int n; cin >> n; int numbers[n]; for (int i = 0; i < n; i++) { cin >> numbers[i]; occurrences[numbers[i]]++; } bool flag = true; for (int i = 2; i <= 10002; i++) { if (occurrences[i] > occurrences[i - 1]) { flag = false; } } if (flag) { cout << occurrences[1] << endl; for (int i = 0; i < n; i++) { cout << occurrences[numbers[i]] << " "; occurrences[numbers[i]]--; } cout << endl; } else { cout << -1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5; int mark[maxn + 10], a[maxn + 10]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i], mark[a[i]]++; for (int i = 2; i <= maxn; i++) if (mark[i] > mark[i - 1]) { cout << -1 << endl; return 0; } cout << mark[1] << endl; memset(mark, false, sizeof mark); for (int i = 1; i <= n; i++) { mark[a[i]]++; cout << mark[a[i]] << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { long long int n, i; cin >> n; vector<long long int> a(n), freq(100001, 0), cnt(100001, 0); for (i = 0; i < (n); i++) { cin >> a[i]; freq[a[i]]++; } long long int f = 0; for (i = 1; i < 100000; i++) { if (freq[i] < freq[i + 1]) { f = 1; break; } } if (f == 1) cout << -1 << "\n"; else { cout << freq[1] << "\n"; for (i = 0; i < (n); i++) cout << ++cnt[a[i]] << " "; cout << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> arr; bool cmp(int a, int c) { return arr[a] < arr[c]; } int main() { int n, res = -1; cin >> n; vector<int> b(n), ans(n), list, am; arr.resize(n); for (int k = 0; k < n; k++) { cin >> arr[k]; b[k] = k; if (arr[k] > (int)am.size() - 1) { am.resize(arr[k] + 1); } am[arr[k]]++; res = max(res, am[arr[k]]); } for (int k = 2; k < (int)am.size(); k++) { if (am[k] > am[k - 1]) { cout << -1; return 0; } } sort(b.begin(), b.end(), cmp); int i; for (int k = 0, i = 0; k < n; k++, i++) { if (k == 0 || arr[b[k - 1]] != arr[b[k]]) { i = 0; } ans[b[k]] = i + 1; } cout << res << '\n'; for (int k = 0; k < n; k++) { cout << ans[k] << " "; } cout << '\n'; return 0; }
#include <bits/stdc++.h> int main() { int a[100000], p[100002] = {0}, n, i, max = 0; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); p[a[i]]++; if (a[i] > max) max = a[i]; } for (i = 1; i <= max; i++) if ((p[i] == 0 && p[i + 1] != 0) || p[i] < p[i + 1]) { printf("-1\n"); return 0; } printf("%d\n", p[1]); for (i = 0; i < n; i++) p[i] = 1; for (i = 0; i < n; i++) { printf("%d ", p[a[i]]++); } printf("\n"); }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int a[n], c[n]; int max = 0; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] > n) { cout << "-1"; return 0; } if (a[i] > max) max = a[i]; } int b[max], k[max]; for (int i = 0; i < max; i++) { b[i] = 0; k[i] = 1; } for (int i = 0; i < n; i++) { b[a[i] - 1]++; } for (int i = 1; i < max; i++) { if (b[i] > b[i - 1] || b[i] == 0) { cout << "-1"; return 0; } } for (int j = 0; j < n; j++) { c[j] = k[a[j] - 1]++; } cout << b[0] << endl; for (int i = 0; i < n; i++) cout << c[i] << " "; return 0; }
#include <bits/stdc++.h> using namespace std; signed main() { long long n; cin >> n; long long a[n]; long long max; map<long long, long long> m; long long maxer = -2; for (long long i = 0; i < n; i++) { cin >> a[i]; m[a[i]]++; if (maxer < a[i]) maxer = a[i]; } max = -1; if (n == 1 && a[0] != 1) { cout << -1; return 0; } for (long long i = 0; i < n; i++) if (m[a[i]] > max) max = m[a[i]]; for (auto it = m.begin(); it != m.end(); it++) { auto itr = next(it, 1); if (itr == m.end()) break; else { if (itr->second > it->second) { cout << -1; return 0; } } } for (long long i = 1; i <= maxer; i++) { if (m[i] == 0) { cout << -1; return 0; } } cout << max << endl; for (long long i = 0; i < n; i++) { cout << m[a[i]] << " "; m[a[i]]--; } }
#include <bits/stdc++.h> using namespace std; const int N = 2 * 101 * 1001; vector<int> a[N]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { int x; scanf("%d", &x); a[x].push_back(i); } vector<int> res(n, -1); int m = N; int it = 0; while (m > 0) { while (m > 0 && !a[m].size()) m--; if (m <= 0) break; ++it; for (int i = m; i > 0; --i) { if (!a[i].size()) { return !printf("-1\n"); } res[a[i][a[i].size() - 1]] = it; a[i].pop_back(); } } cout << it << "\n"; for (int i = 0; i < n; ++i) { printf("%d ", res[i]); } return 0; }
#include <bits/stdc++.h> using namespace std; long long int power(long long int a, long long int b) { long long int res = 1; a = a % 1000000007; while (b > 0) { if (b & 1) { res = (res * a) % 1000000007; b--; } a = (a * a) % 1000000007; b >>= 1; } return res; } long long int gcd(long long int a, long long int b) { return (b == 0) ? a : gcd(b, a % b); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n; cin >> n; vector<long long int> v(n); vector<long long int> freq(n + 1, 0); long long int f = 0; for (long long int i = 0; i < n; i++) { cin >> v[i]; if (v[i] > n) { f = 1; break; } freq[v[i]]++; } for (long long int i = 2; i <= n; i++) { if (freq[i] > freq[i - 1]) { f = 1; break; } } if (f) { cout << "-1"; return 0; } cout << freq[1] << "\n"; for (long long int i = 0; i < n; i++) { cout << freq[v[i]] << " "; freq[v[i]]--; } }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int *a = new int[n]; int *count = new int[n + 1]; memset(count, 0, (n + 1) * sizeof(int)); bool yes = true; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] > n) { yes = false; break; } else { count[a[i]]++; } } if (yes) { for (int i = 2; i <= n; i++) { if (count[i] > count[i - 1]) { yes = false; break; } } if (!yes) cout << -1; else { cout << count[1] << '\n'; for (int i = 0; i < n; i++) { cout << count[a[i]] << ' '; count[a[i]]--; } } } else { cout << -1; } return 0; }
#include <bits/stdc++.h> using namespace std; long long power(long long a, long long b) { if (b == 0) return 1; if (b % 2 == 0) return power(a, b / 2); return (a * power(a, b / 2)); } int nums[100002]; int main() { int n; cin >> n; int ar[100001]; vector<int> col[100001]; int lastnum = 0; int last = -1; for (int i = 0; i < n; i++) { cin >> ar[i]; nums[ar[i]]++; last = (last > ar[i]) ? last : ar[i]; } int sum = 0; int cc = 0; while (sum < n) { cc++; bool ff = false; sum += last; for (int i = last; i > 0; i--) { if (nums[i] == 0) { cout << -1 << endl; return 0; } col[i].push_back(cc); nums[i]--; if (!ff && nums[i] > 0) { ff = true; last = i; } } } cout << cc << endl; for (int i = 0; i < n; i++) { if (i > 0) cout << " "; cout << col[ar[i]][col[ar[i]].size() - 1]; col[ar[i]].pop_back(); } cout << endl; return 0; }
#include <bits/stdc++.h> template <typename T> inline void SWAP(T &a, T &b) { T tmp = a; a = b; b = tmp; } template <typename T> inline T ABS(const T &val) { return val < 0 ? -val : val; } template <typename T> inline T MAX(T a, T b) { return a > b ? a : b; } template <typename T> inline T MIN(T a, T b) { return a < b ? a : b; } template <typename T> inline T SQR(const T &a) { return a * a; } const int INTinf = 1000000000; const int nINTinf = 0 - 2147483648; const long long LLinf = 9223372036854775807LL; using namespace std; int n; int a[100005]; int mass[100005]; int mass2[100005]; int main() { cin >> n; int MX = 0; for (int i = 0; i != n; i++) { cin >> a[i]; MX = (MX > a[i] ? MX : a[i]); mass[a[i]]++; } for (int i = 1; i <= MX; i++) { if (mass[i] < mass[i + 1]) { cout << -1 << endl; return 0; } } cout << mass[1] << endl; for (int i = 0; i != n; i++) { cout << ++mass2[a[i]] << ' '; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct node { int id; int belong; int num; } data[100005]; bool cmp(node a, node b) { return a.num < b.num; } bool vis[100005]; int ans[100005]; int main() { int n, cnt; while (~scanf("%d", &n)) { memset(vis, 0, sizeof(vis)); for (int i = 0; i < n; i++) { scanf("%d", &data[i].num); data[i].id = i; data[i].belong = -1; } cnt = 0; sort(data, data + n, cmp); for (int i = 0; i < n && data[i].num == 1; i++) if (!vis[i]) { int pre = 1; vis[i] = 1; data[i].belong = ++cnt; for (int j = i + 1; j < n; j++) if (!vis[j] && data[j].num == pre + 1) { vis[j] = 1; data[j].belong = cnt; pre++; } } bool f = true; for (int i = 0; i < n; i++) if (data[i].belong == -1) { f = false; break; } if (f) { printf("%d\n", cnt); for (int i = 0; i < n; i++) ans[data[i].id] = data[i].belong; for (int i = 0; i < n; i++) { if (i == 0) printf("%d", ans[i]); else printf(" %d", ans[i]); } } else puts("-1"); printf("\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; int n, a[1000060], mx, b[1000000], c, k, mx1; int main() { cin >> n; pair<int, int> p[n + 1]; for (k = 1; k <= n; k++) { cin >> a[k]; mx = max(mx, a[k]); b[a[k]]++; mx1 = max(mx1, b[a[k]]); } for (k = 1; k < mx; k++) if (b[k] < b[k + 1]) { c = 1; cout << -1; return 0; } if (c == 0) { cout << mx1 << endl; for (k = 1; k <= n; k++) { cout << b[a[k]] << " "; b[a[k]]--; } } }
#include <bits/stdc++.h> using namespace std; int n, i; int A[100001], C[100001]; int main() { scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &A[i]); C[A[i]]++; } for (i = 1; i + 1 < 100001 && C[i] >= C[i + 1]; i++) ; if (i + 1 < 100001) printf("-1\n"); else { printf("%d\n", C[1]); memset(C, 0, sizeof C); for (i = 0; i < n; i++) printf("%d ", ++C[A[i]]); } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; int ans[maxn], num[maxn]; pair<int, int> pr[maxn]; int main() { int n, i, j, a, mx; cin >> n; for (i = 0; i < n; i++) { cin >> a; pr[i] = make_pair(a, i + 1); num[a]++; } sort(pr, pr + n); for (i = 2; i <= 100000; i++) if (num[i] > num[i - 1]) { printf("-1"); return 0; } mx = 0; for (i = 0; i < n; i += j) { for (j = 0; i + j < n && pr[i + j].first == pr[i].first; j++) ans[pr[i + j].second] = j + 1; if (j > mx) mx = j; } cout << mx << endl; for (i = 1; i <= n; i++) cout << ans[i] << " "; }
#include <bits/stdc++.h> using namespace std; int a[100005], b[100005]; int main() { int n, i, max = 0; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; if (a[i] > max) max = a[i]; b[a[i]]++; } for (i = 1; i < max; i++) { if (b[i] < b[i + 1]) { cout << -1; return 0; } } cout << b[1] << endl; for (i = 0; i < n; i++) { cout << b[a[i]] << " "; b[a[i]]--; } return 0; }
#include <bits/stdc++.h> using namespace std; int s[200000][2] = {0}, s1[200000] = {0}; int main() { for (int i = 0; i < 200000; i++) s1[i] = 0; int num = 0, n, tr = 1, mid = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> s[i][0]; if (s[i][0] > mid) mid = s[i][0]; s1[s[i][0]]++; s[i][1] = s1[s[i][0]]; } for (int i = 1; i <= mid - 1; i++) if (s1[i] < s1[i + 1]) { tr = 0; break; } if (tr) { cout << s1[1] << endl; for (int i = 0; i < n; i++) { cout << s[i][1] << ' '; } } else cout << "-1"; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; int b[maxn]; struct st { int k, idx; st() {} st(int k1, int idx1) { k = k1; idx = idx1; } } a[maxn]; bool cmp1(const st &a, const st &b) { return a.k < b.k; } vector<st> v[maxn]; int main() { int n, i, j, x, d = 1; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &x); a[i] = st(x, i); } sort(a + 1, a + n + 1, cmp1); v[1].push_back(a[1]); for (i = 2; i <= n; i++) { if (a[i].k > a[i - 1].k) d++; v[d].push_back(a[i]); } for (i = 1; i < d; i++) if (v[i].size() < v[i + 1].size()) { printf("-1\n"); return 0; } for (i = 1; i <= d; i++) if (v[i][0].k != i) { printf("-1\n"); return 0; } i = d; x = 0; while (i > 0) { if (v[i].size() == 0) { i--; continue; } j = i; x++; for (; j > 0; j--) b[v[j].back().idx] = x, v[j].pop_back(); } printf("%d\n", x); for (i = 1; i <= n; i++) printf("%d ", b[i]); printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, temp; cin >> n; map<int, int> mp; int maxu = INT_MIN, maxNo = -1; vector<int> arr(n + 1); for (long long int i = 1; i <= n; ++i) { cin >> temp; mp[temp] += 1; maxu = max(maxu, mp[temp]); maxNo = max(temp, maxNo); arr[i] = temp; } for (long long int i = 1; i <= maxNo - 1; ++i) { if ((mp[i] == 0 || mp[i + 1] == 0) || mp[i] < mp[i + 1]) { cout << -1 << endl; exit(0); } } vector<vector<int> > positions(maxNo + 1); for (long long int i = 1; i <= n; ++i) { positions[arr[i]].push_back(i); } cout << maxu << endl; vector<int> ans(n + 1, 0); for (long long int i = 1; i <= maxNo; ++i) { auto x = positions[i]; temp = mp[i]; for (int p : x) { if (temp > 0) { ans[p] = temp; temp -= 1; } } } for (long long int i = 1; i <= n; ++i) { cout << ans[i] << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; long long power(long long a, long long b) { long long res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } long long int binomial(long long int n, long long int k) { long long int C[k + 1]; memset(C, 0, sizeof(C)); C[0] = 1; for (long long int i = 1; i <= n; i++) { for (int j = min(i, k); j > 0; j--) C[j] = C[j] + C[j - 1]; } return C[k]; } vector<int> pr; bool prime[10000000]; void Sieve(long long int n) { memset(prime, true, sizeof(prime)); prime[1] = false; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { pr.push_back(p); for (int i = p * p; i <= n; i += p) prime[i] = false; } } } int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } int find(int arr[], int n) { int result = arr[0]; for (int i = 1; i < n; i++) { result = gcd(arr[i], result); if (result == 1) { return 1; } } return result; } pair<int, int> fib(int n) { if (n == 0) return {0, 1}; auto p = fib(n >> 1); int c = p.first * (2 * p.second - p.first); int d = p.first * p.first + p.second * p.second; if (n & 1) return {d, c + d}; else return {c, d}; } void reverse(string& str) { int n = str.length(); for (int i = 0; i < n / 2; i++) swap(str[i], str[n - i - 1]); } string fun(string s) { for (int i = 0; i < s.length(); i++) { if (s[i] == 'O' || s[i] == 'o') { s[i] = '0'; } else if (s[i] == 'l' || s[i] == 'I' || s[i] == 'L' || s[i] == 'i') { s[i] = '1'; } else if (isupper(s[i])) { s[i] = tolower(s[i]); } } return s; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long int n; cin >> n; int a[n]; map<int, int> mp; int c[n]; for (int i = 0; i < n; i++) { cin >> a[i]; mp[a[i]]++; c[i] = mp[a[i]]; } sort(a, a + n); for (int i = a[n - 1]; i > 1; i--) { if (mp[i] > mp[i - 1]) { cout << -1 << endl; return 0; } } cout << mp[1] << endl; for (int i = 0; i < n; i++) { cout << c[i] << " "; } }
#include <bits/stdc++.h> using namespace std; const int MN = 1000 * 100 + 100; int ans[MN], cnt[MN]; struct block { int el, k; bool operator<(const block &nw) const { return (el < nw.el); } }; block a[MN]; int main() { int n, k = 0, m = 0; cin >> n; for (int i = 0; i < n; i++) { scanf("%d", &a[i].el); a[i].k = i; cnt[a[i].el]++; k = max(k, a[i].el); m = max(m, cnt[a[i].el]); } for (int i = 2; i <= k; i++) { if (!cnt[1] || !cnt[i] || cnt[i] > cnt[i - 1]) { cout << "-1"; return 0; } } for (int i = 0; i < n; i++) ans[i] = cnt[a[i].el]--; cout << m << endl; for (int i = 0; i < n; i++) printf("%d ", ans[i]); return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 1999999999; const double pi = acos(-1.0); const double eps = 1e-10; char gc() { char c; while (isspace(c = getchar())) ; return c; } int gs(char* s) { gets(s); int l = strlen(s); while (l && isspace(s[l - 1])) s[--l] = 0; return l; } int m[100000]; vector<int> a[100000]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int t; cin >> t; t--; a[t].push_back(i); } int c = 1; for (int i = 99999; i >= 0; i--) { while (((int)(a[i]).size())) { for (int j = i; j >= 0; j--) { if (((int)(a[j]).size())) { m[a[j].back()] = c; a[j].pop_back(); } else { cout << -1; return 0; } } c++; } } cout << c - 1 << ('\n'); for (int i = 0; i < n; i++) cout << m[i] << ' '; }
#include <bits/stdc++.h> using namespace std; using LL = long long; constexpr int N = 1e5 + 5; int ans[N]; vector<int> conn[N]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; conn[x].push_back(i); } for (int i = 2; i <= 100000; i++) if (conn[i].size() > conn[i - 1].size()) return cout << "-1\n", 0; for (int i = 1; i <= 100000; i++) { for (int j = 0; j < conn[i].size(); j++) { ans[conn[i][j]] = j + 1; } } cout << conn[1].size() << "\n"; for (int i = 0; i < n; i++) cout << ans[i] << ' '; }
#include <bits/stdc++.h> using namespace std; const int INF = (1 << 30) - 1; const int DIRX[] = {-1, 0, 0, 1, -1, -1, 1, 1}, DIRY[] = {0, -1, 1, 0, -1, 1, -1, 1}; const double ERR = 1e-9, PI = (2 * acos(0.0)); template <class T> inline int size(T& t) { return (int)t.size(); } template <class T> inline T MIN(T a, T b) { return ((a < b) ? a : b); } template <class T> inline T MAX(T a, T b) { return ((b < a) ? a : b); } template <class T> inline void checkMIN(T& a, T b) { if (b < a) a = b; } template <class T> inline void checkMAX(T& a, T b) { if (a < b) a = b; } template <class T> inline T sqr(T x) { return (x * x); } int compareDouble(double x, double y) { double d = x - y; if (d > ERR) return 1; if (d < -ERR) return -1; return 0; } int compareDouble(double x, double y, double err) { double d = x - y; if (d > err) return 1; if (d < -err) return -1; return 0; } int a[100005], freq[100005]; int main() { int n, m, i, tot; cin >> n; m = 0; for (i = 0; i < (int)(n); i++) { cin >> a[i]; freq[a[i]]++; if (a[i] > m) m = a[i]; } tot = 0; for (i = m; i > 0; i--) { if (freq[i] < freq[i + 1]) { cout << (-1); return 0; } if (freq[i] > freq[i + 1]) { tot += (freq[i] - freq[i + 1]); } } cout << tot << endl; for (i = 0; i < (int)(n); i++) { cout << (freq[a[i]]--) << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; int a[100010], mark[100010], pt[100010]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; mark[a[i]]++; } for (int i = 1; i <= 100000; i++) { if (mark[i] < mark[i + 1]) { cout << "-1"; return 0; } pt[i] = 1; } for (int i = 0; i < n; i++) { a[i] = pt[a[i]]++; } cout << pt[1] - 1 << endl; for (int i = 0; i < n; i++) cout << a[i] << " "; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int *f = new int[200000]; for (int i = 0; i < 200000; i++) f[i] = 0; vector<int> a(n); vector<int> *v = new vector<int>[100001]; for (int i = 0; i < n; i++) { cin >> a[i]; f[a[i]]++; v[a[i]].push_back(i); } vector<int> ans(n, -1); int cur = 1; bool minus = false; for (int i = 100000; i >= 1;) { if (f[i] == 0) { i--; continue; } bool ok = true; for (int j = i; j >= 1; j--) { if (f[j] == 0) { ok = false; break; } f[j]--; for (int q = 0; q < v[j].size(); q++) if (ans[v[j][q]] == -1) { ans[v[j][q]] = cur; break; } } if (!ok) { minus = true; break; } cur++; } if (minus) cout << -1; else { cout << cur - 1 << endl; for (int i = 0; i < n; i++) cout << ans[i] << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, i; cin >> n; int a[n + 1]; for (i = 0; i < n; i++) cin >> a[i]; int m = a[0]; for (i = 1; i < n; i++) m = max(m, a[i]); int cnt[m + 1]; for (i = 1; i <= m; i++) cnt[i] = 0; for (i = 0; i < n; i++) cnt[a[i]]++; for (i = 1; i <= m - 1; i++) { if (cnt[i] < cnt[i + 1]) break; } if (i < m) cout << "-1" << "\n"; else { int cnt1[m + 1]; for (i = 0; i < n; i++) cnt1[a[i]] = 1; int cnt2 = 0; vector<int> v; for (i = 0; i < n; i++) { v.push_back(cnt1[a[i]]); cnt2++; cnt1[a[i]]++; } set<int> s; for (i = 0; i < v.size(); i++) { s.insert(v[i]); } cout << s.size() << "\n"; for (i = 0; i < v.size(); i++) cout << v[i] << " "; } }
#include <bits/stdc++.h> using namespace std; int mod(int n) { if (n < 0) return (-n); else return n; } int min_trees(int n, int m, int a, int b, vector<vector<int>>& garden) { int min_count = a * b; for (int i = 0; i < (n - a + 1); i++) { for (int j = 0; j < (m - b + 1); j++) { int count = 0; for (int i2 = 0; i2 < a; i2++) { for (int j2 = 0; j2 < b; j2++) count += garden[i + i2][j + j2]; } if (count < min_count) min_count = count; } } return min_count; } int main() { int n; vector<int> permutations; vector<int> sequences; int permutations2[100002] = {0}; int sequences2[100002] = {0}; scanf("%d", &n); for (int i = 0; i < n; i++) { int t; scanf("%d", &t); permutations2[t] = permutations2[t] + 1; sequences2[i] = t; } for (int i = 1; i < 100000; i++) { if (permutations2[i] < permutations2[i + 1]) { cout << -1; return 0; } } cout << permutations2[1] << "\n"; for (int i = 1; i <= n; i++) { int t = sequences2[i - 1]; printf("%d ", permutations2[t]); permutations2[t] = permutations2[t] - 1; } return 0; }
#include <bits/stdc++.h> using namespace std; int cnt[100001]; int a[100001]; int n; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); cnt[a[i]]++; } for (int i = 2; i <= 100001; i++) if (cnt[i] > cnt[i - 1]) { printf("-1\n"); return 0; } printf("%d\n", cnt[1]); for (int i = 0; i < n; i++) { if (i) printf(" "); printf("%d", cnt[a[i]]--); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; int cnt[N], a[N], n; int main() { scanf("%d", &n); int mx = -1; for (int i = 0; i < n; ++i) { int x; scanf("%d", &x); a[i] = ++cnt[x]; mx = max(mx, x); } bool bad = 0; for (int i = 1; i <= mx - 1; ++i) { if (cnt[i] < cnt[i + 1]) { bad = 1; break; } } if (bad) { cout << "-1"; } else { cout << cnt[1] << "\n"; for (int i = 0; i < n; ++i) { cout << a[i] << " "; } } return 0; }
#include <bits/stdc++.h> using namespace std; void oj() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } void solve() { long long n; cin >> n; vector<long long> v(n); vector<long long> cnt(100005); vector<long long> ans(100005, 1); for (long long i = 0; i < n; i++) { cin >> v[i]; cnt[v[i]]++; } for (long long i = 2; i <= 100001; i++) if (cnt[i] > cnt[i - 1]) { cout << -1 << endl; return; } cout << *max_element(cnt.begin(), cnt.end()) << " "; for (long long i = 0; i < n; i++) { cout << ans[v[i]] << " "; ans[v[i]]++; } } signed main() { oj(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; bool cmp(const pair<int, vector<int>>& a, const pair<int, vector<int>>& b) { if (a.first == b.first) { return a.second < b.second; } return a.first > b.first; } void solve() { int n; cin >> n; int mark[100005] = {}; vector<int> v[100005]; int Count = n; for (int i = 1; i <= n; i++) { int tem; cin >> tem; v[tem].push_back(i); } bool ok = true; int per = 1; while (v[1].size() > 0) { for (int i = 1; i <= 100000; i++) { if (v[i].size() == 0) { break; } int p = v[i][0]; v[i].erase(v[i].begin()); Count--; mark[p] = per; } per++; } if (Count > 0) { cout << "-1" << endl; return; } cout << per - 1 << endl; for (int i = 1; i <= n; i++) { cout << mark[i] << " "; } cout << endl; } int main() { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; struct node { int val; int index; int stat; }; bool sortbyval(struct node a, struct node b); bool sortbyindex(struct node a, struct node b); int main() { int n; cin >> n; struct node arr[n]; map<int, int> freq; for (int i = 0; i < n; i++) { cin >> arr[i].val; arr[i].index = i; freq[arr[i].val]++; } sort(arr, arr + n, sortbyval); int i = n - 1; freq[0] = INT_MAX; while (i >= 0) { int a = arr[i].val; int f = freq[arr[i].val]; if (f > freq[a - 1]) { cout << "-1"; return 0; } int c = 1; while (i >= 0 && arr[i].val == a) { arr[i].stat = c; c++; i--; } } sort(arr, arr + n, sortbyindex); cout << freq[1] << endl; for (int i = 0; i < n; i++) { cout << arr[i].stat << " "; } } bool sortbyval(struct node a, struct node b) { if (a.val == b.val) { if (a.index < b.index) { return true; } else { return false; } } if (a.val < b.val) { return true; } else { return false; } } bool sortbyindex(struct node a, struct node b) { if (a.index < b.index) { return true; } else { return false; } }
#include <bits/stdc++.h> using namespace std; int mas[100100]; int col[100100]; int main() { int n, mx = 0, t, nmax = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> mas[i]; col[mas[i]]++; if (mas[i] > mx) mx = mas[i]; } for (int i = 1; i <= mx; i++) { if (col[i] > nmax) nmax = col[i]; if (col[i] < col[i + 1]) { cout << "-1"; return 0; } } cout << nmax << endl; for (int i = 0; i < n; i++) { cout << col[mas[i]] << " "; col[mas[i]]--; } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int a[maxn], b[maxn]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> b[i]; a[b[i]]++; } int ans = 0; for (int i = 1; i < maxn - 1; i++) { if (a[i + 1] > a[i]) { puts("-1"); return 0; } } cout << a[1] << endl; for (int i = 1; i <= n; i++) { cout << a[b[i]]-- << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXn = 1000 * 100; vector<int> ind[MAXn + 2]; int ans[MAXn + 10]; int main() { int n, max_el = 0; cin >> n; for (int i = 0; i < n; i++) { int tmp; scanf("%d", &tmp); ind[tmp].push_back(i); max_el = max(max_el, tmp); } bool flag = true; for (int i = 1; i < max_el && flag; i++) if (ind[i + 1].size() > ind[i].size()) flag = false; if (flag) { int cnt = 1; for (int i = max_el; i >= 1; i--) { int size = ind[i].size(); for (int j = 0; j < size; j++) { for (int k = 1; k <= i; k++) { ans[ind[k][ind[k].size() - 1]] = cnt; ind[k].pop_back(); } cnt++; } } cout << cnt - 1 << endl; for (int i = 0; i < n; i++) cout << ans[i] << " "; cout << endl; } else cout << -1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n; int x; int ans[100010]; int main() { cin >> n; vector<vector<pair<int, int>>> b; vector<pair<int, int>> a; for (int i = 0; i < n; ++i) { cin >> x; a.push_back(make_pair(x, i)); } sort(a.begin(), a.end()); if (a[0].first != 1) { cout << -1; return 0; } vector<pair<int, int>> c; c.push_back(a[0]); b.push_back(c); for (int i = 1; i < n; ++i) { if (a[i].first == 1) { vector<pair<int, int>> d; d.push_back(a[i]); b.push_back(d); } else { int f = 0; for (int j = 0; j < b.size(); ++j) { if (b[j][b[j].size() - 1].first + 1 == a[i].first) { b[j].push_back(a[i]); f = 1; break; } } if (f == 0) { cout << -1; return 0; } } } cout << b.size() << endl; for (int i = 0; i < b.size(); ++i) for (int j = 0; j < b[i].size(); ++j) ans[b[i][j].second] = i; for (int i = 0; i < n; ++i) cout << ans[i] + 1 << ' '; return 0; }
#include <bits/stdc++.h> using namespace std; const long long int maxn = 2e6 + 10; const long long int modn = 1e9 + 7; long long int T, len, num, sum, maxi; long long int all[maxn]; long long int dp[maxn]; long long int out[maxn]; int main() { std::ios_base::sync_with_stdio(false); cin >> len; for (int i = 1; i <= len; i++) cin >> all[i]; for (int i = 1; i <= len; i++) { dp[all[i]]++; out[i] = dp[all[i]]; maxi = max(maxi, out[i]); } for (int i = 2; i <= 100005; i++) { if (dp[i] > dp[i - 1]) { cout << -1; return 0; } } cout << maxi << endl; for (int i = 1; i <= len; i++) cout << out[i] << " "; return 0; }
#include <bits/stdc++.h> using namespace std; int a[111111], k[111111]; int main() { int n; cin >> n; int x = 2; int mx = -1; for (int i = 0; i <= 100000; i++) k[i] = 0; for (int i = 0; i < n; i++) cin >> a[i], k[a[i]]++, mx = max(mx, a[i]); for (int i = 2; i <= mx; i++) { if (k[i] > k[1] || k[i] > k[x]) { cout << -1; return 0; } if (k[i] < k[x]) x = i; } cout << k[1] << endl; for (int i = 0; i < n; i++) cout << k[a[i]]-- << " "; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int cnt, rep[N * 2], ans[N * 2], a, n; vector<int> v[N * 2]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n; i++) { cin >> a; rep[a]++; v[a].push_back(i); } cnt = 0; for (int i = N; i >= 1; i--) { if (cnt > rep[i]) return cout << -1, 0; if (rep[i] >= cnt) { int o = 1; for (int j : v[i]) { ans[j] = o; o++; } cnt = max(rep[i], cnt); } } cout << cnt << '\n'; for (int i = 0; i < n; i++) cout << ans[i] << ' '; return 0; }
#include <bits/stdc++.h> using namespace std; int a[1020010]; bool vis[1020010]; int ans[1020010]; int sum[1020010]; int mark[1020010]; int main() { int n; while (scanf("%d", &n) == 1) { memset(sum, 0, sizeof(sum)); memset(ans, 0, sizeof(ans)); memset(mark, 0, sizeof(mark)); int i, j; int big = 0; for (i = 1; i <= n; i++) { scanf("%d", &a[i]); sum[a[i]]++; big = max(big, a[i]); } int cur = sum[1]; bool flag = true; for (i = 1; i <= big; i++) { if (sum[i] > cur) { flag = false; break; } cur = sum[i]; } if (!flag) { cout << "-1" << endl; continue; } for (i = 1; i <= n; i++) { ans[i] = ++mark[a[i]]; } printf("%d\n", sum[1]); for (i = 1; i < n; i++) printf("%d ", ans[i]); printf("%d\n", ans[i]); } return 0; }
#include <bits/stdc++.h> int n, x, N[100005], A[100005]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", N + i); if (N[i] == 1) x++; A[N[i]]++; } for (int i = 2; i <= 100000; i++) if (A[i] > A[i - 1]) { printf("-1"); return 0; } for (int i = 1; i <= 100000; i++) A[i] = 0; printf("%d\n", x); for (int i = 0; i < n; i++) { printf("%d ", ++A[N[i]]); } printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 20; int n, a; int arr[N]; vector<vector<int> > ve(N); int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &a); ve[a].push_back(i); } for (int i = 2; i < N; ++i) if (ve[i].size() > ve[i - 1].size()) return printf("-1"), 0; printf("%d\n", ve[1].size()); for (int i = 1; i < N; ++i) { for (int j = 0; j < ve[i].size(); ++j) { arr[ve[i][j]] = j + 1; } } for (int i = 0; i < n; ++i) printf("%d ", arr[i]); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int no = 1e6; int n; cin >> n; int arr[no]; for (int(i) = 0; (i) < (no); (i)++) { arr[i] = 0; } int array[n + 1]; for (int(i) = 0; (i) < (n); (i)++) { long long int a; cin >> a; array[i] = a; arr[a]++; } long long int temp = -1; for (int(i) = (1); (i) <= (no - 2); (i)++) { if (arr[i] < arr[i + 1]) { temp = 1; cout << -1 << endl; break; } } if (temp == -1) { cout << arr[1] << endl; for (int(i) = 0; (i) < (n); (i)++) { cout << arr[array[i]] << " "; arr[array[i]]--; } cout << endl; } }
#include <bits/stdc++.h> using namespace std; int n, cnt[100005], a[100005], mxid[100005], st = 0; int main() { scanf("%d", &n); memset(cnt, 0, sizeof(cnt)); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); cnt[a[i]]++; st = max(st, a[i]); } memset(mxid, 0, sizeof(mxid)); for (int i = st; i > 0; i--) if (cnt[i] > 0) { int mi = cnt[i]; for (int j = i; j > 0; j--) { cnt[j] -= mi, mxid[j] += mi; if (cnt[j] < 0) { printf("-1\n"); return 0; } } } printf("%d\n", mxid[1]); for (int i = 0; i < n; i++) { if (i == 0) printf("%d", mxid[a[i]]); else printf(" %d", mxid[a[i]]); mxid[a[i]]--; } printf("\n"); }
#include <bits/stdc++.h> using namespace std; void solve() { long long n; cin >> n; long long a[n + 1], i; map<long long, long long> mp; long long maxi = 0, mini = INT_MAX; pair<long long, long long> p[n + 1]; for (i = 0; i < n; i++) { cin >> a[i]; mp[a[i]]++; maxi = max(maxi, a[i]); mini = min(mini, a[i]); p[i].first = a[i]; p[i].second = i; } sort(p, p + n); for (i = 2; i <= maxi; i++) { if (mp[i] > mp[i - 1]) { cout << -1 << endl; return; } } long long k = 1, l = 1; pair<long long, long long> pr[n + 1]; pr[0].first = p[0].second; pr[0].second = l; for (i = 1; i < n; i++) { if (p[i].first == p[i - 1].first) { l++; pr[i].first = p[i].second; pr[i].second = l; } else { l = 1; pr[i].first = p[i].second; pr[i].second = l; } } sort(pr, pr + n); cout << mp[mini] << endl; for (i = 0; i < n; i++) cout << pr[i].second << " "; cout << endl; } int main() { long long t = 1; while (t--) solve(); }