Submission #1029044

# Submission time Handle Problem Language Result Execution time Memory
1029044 2024-07-20T11:39:02 Z AmirAli_H1 Ancient Books (IOI17_books) C++17
0 / 100
2000 ms 348 KB
// In the name of Allah
 
#include <bits/stdc++.h>
#include "books.h"
using namespace std;
 
typedef		long long int			ll;
typedef		long double				ld;
typedef		pair<int, int>			pii;
typedef		pair<ll, ll>			pll;
typedef		complex<ld>				cld;
 
#define		all(x)					(x).begin(),(x).end()
#define		len(x)					((ll) (x).size())
#define		F						first
#define		S						second
#define		pb						push_back
#define		sep						' '
#define		endl					'\n'
#define		Mp						make_pair
#define		kill(x)					cout << x << '\n', exit(0)
#define		set_dec(x)				cout << fixed << setprecision(x);
#define		file_io(x,y)			freopen(x, "r", stdin); freopen(y, "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
const int maxn = 1e6 + 4;
const int oo = 1e9 + 7;
 
int n; ll res;
int A[maxn];

int get_min(int l, int r) {
	int mn = oo;
	for (int i = l; i < r; i++) mn = min(mn, A[i]);
	return mn;
}

int get_max(int l, int r) {
	int mx = -oo;
	for (int i = l; i < r; i++) mx = max(mx, A[i]);
	return mx;
}

ll minimum_walk(vector<int> p, int s) {
	n = len(p); res = 0;
	for (int i = 0; i < n; i++) {
		A[i] = p[i];
		res += abs(i - A[i]);
	}
	
	int m1 = s, m2 = s;
	for (int i = 0; i < n; i++) {
		if (A[i] != i) {
			m1 = min(m1, i); m2 = max(m2, i);
		}
	}
	
	int l = s, r = s;
	while (l > m1 || r < m2) {
		int lx = get_min(l, r + 1), rx = get_max(l, r + 1);
		if (l == lx && r == rx) {
			res += 2;
			if (r < m2) r++;
			else l--;
		}
		else {
			l = lx; r = rx;
		}
	}
	return res;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Execution timed out 2095 ms 348 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Execution timed out 2095 ms 348 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Execution timed out 2095 ms 348 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2028 ms 344 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Execution timed out 2095 ms 348 KB Time limit exceeded
4 Halted 0 ms 0 KB -