답안 #341000

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
341000 2020-12-28T21:03:51 Z pggp 고대 책들 (IOI17_books) C++14
0 / 100
4 ms 5228 KB
#include <bits/stdc++.h>

using namespace std;

bool used[1000000];
vector < int > P;
int N;
int cur_cycle;
int cycle[1000000];
vector < vector < int > > cycs;
vector < int > l_cyc, r_cyc;

long long ans = 0;

void dfs(int vertex){
	cycle[vertex] = cur_cycle;
	cycs[cur_cycle].push_back(vertex);
	used[vertex] = true;
	ans += abs(vertex - P[vertex]);
	if(!used[P[vertex]]){
		dfs(P[vertex]);
	}
}

long long minimum_walk(vector < int > p, int s){
	cur_cycle = 0;
	for (int i = 0; i < 1000000; ++i)
	{
		used[i] = false;
		cycle[i] = 0;
	}
	cycs.clear();
	l_cyc.clear();
	r_cyc.clear();

	N = p.size();
	P = p;
	cur_cycle = 0;
	for (int i = 0; i < N; ++i)
	{
		if(!used[i]){
			vector < int > v;
			cycs.push_back(v);
			dfs(i);
			cur_cycle++;
		}
	}

	vector < pair < int, int > > f;	


	for (int i = 0; i < cycs.size(); ++i)
	{
		//if(cycs[i].size() == 1)	continue;
		int l_cyc = 1000000;
		int r_cyc = 0;
		for(int v : cycs[i]){
			l_cyc = min(l_cyc, v);
			r_cyc = max(r_cyc, v);
		}
		f.push_back(make_pair(l_cyc, r_cyc));
		
	}


	sort(f.begin(), f.end());

	int components = 1;
	int max_r = f[0].second;
	int last_comp = 0;
	for (int i = 1; i < f.size(); ++i)
	{
		if(f[i].first > max_r){
			components++;
		}
		max_r = max(max_r, f[i].second);
		if(f[i].first == f[i].second){
			last_comp++;
		}
		else{
			last_comp = 0;
		}
	}
	components -= last_comp;
	ans += 2 * components - 2;
	//cout << components << endl;
	return ans;
}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:52:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |  for (int i = 0; i < cycs.size(); ++i)
      |                  ~~^~~~~~~~~~~~~
books.cpp:71:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |  for (int i = 1; i < f.size(); ++i)
      |                  ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5228 KB Output is correct
2 Incorrect 4 ms 5228 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5228 KB Output is correct
2 Incorrect 4 ms 5228 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5228 KB Output is correct
2 Incorrect 4 ms 5228 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 5228 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2740'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5228 KB Output is correct
2 Incorrect 4 ms 5228 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
3 Halted 0 ms 0 KB -