Submission #109518

# Submission time Handle Problem Language Result Execution time Memory
109518 2019-05-06T19:08:06 Z updown1 Ancient Books (IOI17_books) C++17
12 / 100
3 ms 384 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 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, cost[MAXN];
pair<int, int> rng[MAXN];
bool vis[MAXN], del[MAXN];

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;
	cost[co] += 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++;
	}
	//For (i, 0, at) w<< cost[i]<<e;
	sort(rng, rng+at);
	int far = -1;
	For (i, 0, at) {
		if (rng[i].b < far) {
			//w<<"deleting"<<e;
			out += cost[comp[rng[i].a]]; del[comp[rng[i].a]] = true;}
		far = max(far, rng[i].b);
	}
	int far_rig = s, far_lef = s;
	ffi if (loc[i] == i || del[comp[i]]) vis[i] = true;
	//ffi w<< vis[i]<<e;
	For (i, s, N) if (!vis[i]) {go(i); far_rig = i; out += cost[comp[i]];}
	for (int i=s-1; i>=0; i--) if (!vis[i]) {far_lef = i; out += cost[comp[i]];}
	return out+2*((s-far_lef)+(far_rig-s));
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 3 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 3 ms 384 KB Output is correct
18 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 3 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 3 ms 384 KB Output is correct
18 Correct 3 ms 384 KB Output is correct
19 Incorrect 3 ms 384 KB 3rd lines differ - on the 1st token, expected: '338572', found: '338578'
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 3 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 3 ms 384 KB Output is correct
18 Correct 3 ms 384 KB Output is correct
19 Incorrect 3 ms 384 KB 3rd lines differ - on the 1st token, expected: '338572', found: '338578'
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 384 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3888'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 3 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 3 ms 384 KB Output is correct
18 Correct 3 ms 384 KB Output is correct
19 Incorrect 3 ms 384 KB 3rd lines differ - on the 1st token, expected: '338572', found: '338578'
20 Halted 0 ms 0 KB -