Submission #485091

#TimeUsernameProblemLanguageResultExecution timeMemory
485091nhanbin03Watermelon (INOI20_watermelon)C++14
49 / 100
121 ms19292 KiB
#include <bits/stdc++.h> #define task "INOI20_watermelon" #define X first #define Y second #define left ___left #define right ___right using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int N = 1e5 + 10; int n, k; int a[N], inDeg[N], ans[N], rev[N]; vector<int> adj[N], pos[N]; bool buildDAG(int maxVal) { for (int i = 1; i <= n; i++) { pos[a[i]].push_back(i); } set<int> posSet; for (int i = maxVal; i >= 1; i--) { if (pos[i].empty()) return 0; for (int j = pos[i].size() - 1; j >= 0; j--) { int x = pos[i][j]; auto it = posSet.lower_bound(x); if (it != posSet.end()) { adj[x].push_back(*it); inDeg[*it]++; } if (it != posSet.begin()) { it--; if (j == 0 || pos[i][j - 1] < *it) { adj[x].push_back(*it); inDeg[*it]++; } } posSet.insert(x); } } return 1; } void solve() { priority_queue<int, vector<int>, greater<int>> pq; for (int i = 1; i <= n; i++) { if (inDeg[i] == 0) { pq.push(i); } } for (int i = 1; i <= n; i++) { int u = pq.top(); pq.pop(); ans[u] = i; for (int v : adj[u]) { inDeg[v]--; if (inDeg[v] == 0) pq.push(v); } } for (int i = 1; i <= n; i++) { assert(inDeg[i] == 0); rev[ans[i]] = i; } } bool check() { vector<int> left(n + 1), right(n + 1), tmp(n + 1, -1); set<int> cur; for (int i = 1; i < n; i++) { left[i + 1] = i; right[i] = i + 1; if (ans[i] < ans[i + 1]) cur.insert(i); } int cnt = 0; while (cur.size()) { cnt++; set<int> nxt; for (auto it = cur.begin(); it != cur.end(); it++) { int x = *it; // cout << x << endl; tmp[x] = cnt; right[left[x]] = right[x]; left[right[x]] = left[x]; if (left[x] != 0 && ans[left[x]] < ans[right[x]]) nxt.insert(left[x]); } // cout << endl; cur = nxt; } // for (int i = 1; i <= n; i++) { // cout << tmp[i] << " " << a[i] << endl; // }cout << endl; for (int i = 1; i <= n; i++) { if (tmp[i] != -1 && a[i] != tmp[i]) return 0; } return 1; } bool findNext() { set<int> s; for (int i = n; i >= 1; i--) { int u = rev[i]; // cout << i << " " << u << endl; for (int v : adj[u]) { inDeg[v]++; s.erase(v); } // for (auto it = s.begin(); it != s.end(); it++) cout << *it << " "; cout << endl; auto it = s.lower_bound(u); if (it != s.end()) { priority_queue<int, vector<int>, greater<int>> pq; int u = *it; pq.push(u); for (int j = i; j <= n; j++) { int u = pq.top(); pq.pop(); if (j == i) { pq.push(rev[i]); // cout << rev[i] << endl; for (auto it = s.begin(); it != s.end(); it++) { // cout << *it << " "; if (*it != u) pq.push(*it); } // cout << endl; } ans[u] = j; for (int v : adj[u]) { inDeg[v]--; // cout << u << " " << v << " " << inDeg[v] << endl; if (inDeg[v] == 0) pq.push(v); } } return 1; } s.insert(u); } return 0; } int main() { cin.tie(0)->sync_with_stdio(0); cout.tie(0); if (fopen(task ".inp","r")) { freopen(task ".inp","r",stdin); freopen(task ".out","w",stdout); } cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; } assert(n != 10 || (a[1] <= 4 && a[2] <= 3)); if (a[n] != -1) { cout << -1; return 0; } int maxVal = 0; for (int i = 1; i <= n; i++) { maxVal = max(maxVal, a[i]); } for (int i = n; i >= 1; i--) { if (a[i] == -1) a[i] = ++maxVal; } // for (int i = 1; i <= n; i++) { // cout << a[i] << " "; // } // cout << "\n"; if (!buildDAG(maxVal)) { cout << -1; return 0; } // for (int i = 1; i <= n; i++) { // cout << i << ": "; // for (int j : adj[i]) { // cout << j << " "; // } // cout << "\n"; // } solve(); if (!check()) { cout << -1; return 0; } for (int i = 1; i < k; i++) { if (!findNext()) { cout << -1; return 0; } for (int j = 1; j <= n; j++) { // cout << ans[j] << " "; assert(inDeg[j] == 0); rev[ans[j]] = j; } // cout << endl; } for (int i = 1; i <= n; i++) { cout << ans[i] << " "; } }

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:144:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  144 |         freopen(task ".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:145:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  145 |         freopen(task ".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...