Submission #782273

# Submission time Handle Problem Language Result Execution time Memory
782273 2023-07-13T17:34:20 Z APROHACK Ancient Books (IOI17_books) C++14
0 / 100
0 ms 340 KB
#include "books.h"
#include <bits/stdc++.h>
#define ll long long 
#define ff first
#define ss second
#define pb push_back
using namespace std;
int nn;
ll bit[1000006]; // prefix sum hacia adelante.
void update(int pos, int val){
	for(int i = pos ; i <= nn ; i += i&(-i)){
		bit[i] += val;
	}
}
ll query(int donde){
	ll ans = 0;
	for(int i = donde ; i > 0 ; i -= i&(-i)){
		ans += bit[i];
	}
	return ans;
}
bool vis[1000006];
vector<int>perm;
ll minimo = 0;
void go(int i, int j){
	if(i < j){
		update(i, 1);
		update(j, -1);
	}else if( j < i ){
		update(j, 1);
		update(i, -1);
	}
	minimo += abs(i - j);
}

void run(int u){
	if(vis[u])return ;
	vis[u] = true;
	go(u, perm[u]);
	run(perm[u]);
}


long long minimum_walk(std::vector<int> p, int s) {
	nn = p.size();
	perm.pb(0);
	for(int i = 0 ; i < p.size() ; i ++){
		//cout << s << endl;
		perm.pb(p[i]+1);
		//cout << perm.back() << " ";
	}
	//cout << endl;
	
	for(int i = 0 ; i <= nn ; i ++){
		vis[i] = false;
		bit[i] = 0;
	}
	for(int i = 1 ; i <= nn ; i ++){
		if(!vis[i])run(i);
	}
	ll mas = 0, cur = 0;
	
	for(int i = 1 ; i <= nn ; i ++){
		if(query(i) - query(i+1) > 0){
			cur += 2;
		}else{
			mas += cur;
		}
	}
	//cout << minimo << " " << mas << endl;
	return minimo + mas;
	
	
	
}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:47:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |  for(int i = 0 ; i < p.size() ; i ++){
      |                  ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 0 ms 212 KB 3rd lines differ - on the 1st token, expected: '8', found: '12'
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 0 ms 212 KB 3rd lines differ - on the 1st token, expected: '8', found: '12'
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 0 ms 212 KB 3rd lines differ - on the 1st token, expected: '8', found: '12'
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 340 KB 3rd lines differ - on the 1st token, expected: '3304', found: '159344'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 0 ms 212 KB 3rd lines differ - on the 1st token, expected: '8', found: '12'
4 Halted 0 ms 0 KB -