Submission #112342

# Submission time Handle Problem Language Result Execution time Memory
112342 2019-05-18T21:55:32 Z updown1 Ancient Books (IOI17_books) C++17
50 / 100
297 ms 32632 KB
#include "books.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define For(i, a, b) for(int i=a; i<b; i++)
#define ffi For(i, 0, N)
#define ffj For(j, 0, M)
#define ffa ffi ffj
//#define s <<" "<<
#define c <<" : "<<
#define w cout
#define e "\n"
#define pb push_back
#define mp make_pair
#define a first
#define b second
//#define int ll
const int MAXN = 1000000;
//Global Variables
ll N, out = 0, loc[MAXN], comp[MAXN], at = 0;
///             element ,   element
pair<int, int> rng[MAXN];
///                comp
bool vis[MAXN], del[MAXN];
///   element ,     comp
 
void go1(int at, int co) {
	if (comp[at] != -1) return;
	rng[co].a = min(rng[co].a, at);
	rng[co].b = max(rng[co].b, at);
	comp[at] = co;
	out += abs(loc[at]-at);
	go1(loc[at], co);
}
void go(int at) {
	if (vis[at]) return;
	vis[at] = true;
	go(loc[at]);
}
 
ll minimum_walk(vector<int> p, int s) {
	N = p.size();
	ffi {rng[i] = mp(N, -1); comp[i] = -1; loc[i] = p[i];}
	ffi if (comp[i] == -1) {
		go1(i, at);
		at++;
	}
	//w<< "from components: " << out<<e;
	sort(rng, rng+at);
	int far = -1;
	For (i, 0, at) {
		if (rng[i].b < far) {
			//w<<"deleting" c rng[i].a << " " << rng[i].b <<e;
            del[comp[rng[i].a]] = true;
        }
		far = max(far, rng[i].b);
	}
    far = -1;
	For (i, 0, at) {
	    if (rng[i].a >= s) {
            if (rng[i].a < far) {
                //w<<"deleting"<<e;
                del[comp[rng[i].a]] = true;
            }
	    }
        if (rng[i].b >= s) far = max(far, rng[i].b);
	}
	//ffi w<< i c del[comp[i]]<<e;
	far = N+1;
	for (int i=at-1; i>=0; i--) {
        if (rng[i].b <= s) {
            if (rng[i].b > far) {
                //w<<"deleting"<<e;
                del[comp[rng[i].a]] = true;
            }
        }
		if (rng[i].a <= s) far = min(far, rng[i].a);
	}
	//ffi w<< i c del[comp[i]]<<e;
	ffi if (loc[i] == i) del[comp[i]] = true;
	bool moved = false;
	far = rng[comp[s]].b;
	For (i, 0, at) if (rng[i].a > s) {
	    if (!del[comp[rng[i].a]]) {
            out += 2*(rng[i].a - far);
            moved = true;
	    }
		if (loc[rng[i].b] != rng[i].b) far = max(far, rng[i].b);
		//w<< out<<e;
	}
	far = rng[comp[s]].a;
	for (int i=at-1; i>=0; i--) if (rng[i].b < s) {
	    if (!del[comp[rng[i].a]]) {
            out += 2*(far - rng[i].b);
            moved = true;
	    }
		if (loc[rng[i].b] != rng[i].b) far = min(far, rng[i].a);
		//w<< out<<e;
	}
	//w<< "moved: " << moved c out<<e;
	if (moved) return out;
	/// find closest location to s that isn't deleted
	int dist = N+1;
	ffi if (!del[comp[i]]) {
        dist = min(dist, abs(s-i));
	}
	//w<< "dist: " << dist<<e;
	if (dist == N+1) dist = 0;
	//w<< "dist: " << dist<<e;
	return out + 2*dist;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 2 ms 384 KB Output is correct
29 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 2 ms 384 KB Output is correct
29 Correct 2 ms 384 KB Output is correct
30 Correct 282 ms 31688 KB Output is correct
31 Correct 297 ms 31736 KB Output is correct
32 Correct 156 ms 32632 KB Output is correct
33 Correct 163 ms 32320 KB Output is correct
34 Correct 159 ms 32376 KB Output is correct
35 Correct 164 ms 32196 KB Output is correct
36 Correct 146 ms 31936 KB Output is correct
37 Correct 140 ms 31932 KB Output is correct
38 Correct 148 ms 31608 KB Output is correct
39 Correct 147 ms 31808 KB Output is correct
40 Correct 162 ms 31688 KB Output is correct
41 Correct 214 ms 31680 KB Output is correct
42 Correct 185 ms 31688 KB Output is correct
43 Correct 171 ms 32444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 384 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3590'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 2 ms 384 KB Output is correct
29 Correct 2 ms 384 KB Output is correct
30 Correct 282 ms 31688 KB Output is correct
31 Correct 297 ms 31736 KB Output is correct
32 Correct 156 ms 32632 KB Output is correct
33 Correct 163 ms 32320 KB Output is correct
34 Correct 159 ms 32376 KB Output is correct
35 Correct 164 ms 32196 KB Output is correct
36 Correct 146 ms 31936 KB Output is correct
37 Correct 140 ms 31932 KB Output is correct
38 Correct 148 ms 31608 KB Output is correct
39 Correct 147 ms 31808 KB Output is correct
40 Correct 162 ms 31688 KB Output is correct
41 Correct 214 ms 31680 KB Output is correct
42 Correct 185 ms 31688 KB Output is correct
43 Correct 171 ms 32444 KB Output is correct
44 Incorrect 2 ms 384 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3590'
45 Halted 0 ms 0 KB -