답안 #782276

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
782276 2023-07-13T17:43:21 Z APROHACK 고대 책들 (IOI17_books) C++14
0 / 100
1 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 ++){
      |                  ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB 3rd lines differ - on the 1st token, expected: '3304', found: '158960'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
2 Halted 0 ms 0 KB -