Submission #949600

# Submission time Handle Problem Language Result Execution time Memory
949600 2024-03-19T11:52:40 Z KiaRez Watermelon (INOI20_watermelon) C++17
0 / 100
39 ms 24156 KB
/*
    IN THE NAME OF GOD
*/
#include <bits/stdc++.h>

// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;

#define F                                      first
#define S                                      second
#define Mp                                     make_pair
#define pb                                     push_back
#define pf                                     push_front
#define size(x)                                ((ll)x.size())
#define all(x)                                 (x).begin(),(x).end()
#define kill(x)		                           cout << x << '\n', exit(0);
#define fuck(x)                                cout << "(" << #x << " , " << x << ")" << endl
#define endl                                   '\n'

const int N = 3e5+23, lg = 18;
ll Mod = 1e9+7; //998244353;

inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
    ll ans = 1;
    a=MOD(a, mod);
    while (b) {
        if (b & 1) ans = MOD(ans*a, mod);
        b >>= 1;
        a = MOD(a*a, mod);
    }
    return ans;
}

int n, k, b[N], deg[N], p[N], seg[2*N], r[N];
vector<int> neg, adj[N];
set <int> pq;

void upd(int ind, int val) {
	while(ind > 0) {
		seg[ind] = max(seg[ind], val);
		ind/=2;
	}
}

int qry(int l, int r, int ind=1, int lc=1, int rc=(1<<lg)+1) {
	if(l>=rc || lc>=r) return 0;
	if(lc>=l && rc<=r) return seg[ind];
	int mid = (lc+rc)/2;
	return max(qry(l, r, 2*ind, lc, mid), qry(l, r, 2*ind+1, mid, rc));
}

int cnt = 0;
void solve(int now) {
	if(now <= n && size(pq) == 0) kill(-1);
	if(now == n+1) {
		if(k == 1) {
			for(int i=1; i<=n; i++) cout<<p[i]<<' ';
			cout<<endl;
			exit(0);
		}
		k--;
		return;
	}
	vector<int> tmp;
	for(auto it : pq) {
		if(size(tmp)==10) break;
		tmp.pb(it);
	}
	for(auto it : tmp) {
		for(int u : adj[it]) {
			deg[u] --;
			if(deg[u] == 0) pq.insert(u);
		}
		pq.erase(it);
		p[it] = now;

		solve(now+1);
		
		for(int u : adj[it]) {
			deg[u] ++;
			if(deg[u] == 1) pq.erase(u);
		}
		pq.insert(it);
	}
}

int main () {
	ios_base::sync_with_stdio(false), cin.tie(0);

	cin>>n>>k;
	for(int i=1; i<=n; i++) {
		cin>>b[i];
		upd(i+(1<<lg)-1, b[i]);
		if(b[i] == -1) neg.pb(i);
	}
	if(size(neg) == 0 || neg.back() < n) kill(-1);

	for(int i=0; i<size(neg); i++) {
		int ptr = (i==0 ? 1 : neg[i-1]+1);
		if(i > 0) adj[neg[i]].pb(neg[i-1]);
		
		for(int j=ptr; j<neg[i]; j++) adj[j].pb(neg[i]);

		for(int j=neg[i]-1; j>=ptr; j--) {
			r[j] = j+1;
			while(r[j] < neg[i] && b[r[j]] < b[j]) {
				adj[r[j]].pb(j);
				r[j] = r[r[j]];
			}
			adj[j].pb(r[j]);
			if(qry(j+1, r[j]) + 1 != b[j]) kill(-1);
		}
	}

	for(int i=1; i<=n; i++) for(auto u : adj[i]) deg[u] ++;
	for(int i=1; i<=n; i++) if(deg[i] == 0) pq.insert(i);

	solve(1);

	cout<<-1<<endl;

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Incorrect 2 ms 10892 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Incorrect 2 ms 10892 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 39 ms 24156 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 39 ms 24156 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Incorrect 2 ms 10892 KB Output isn't correct
4 Halted 0 ms 0 KB -