Submission #49265

# Submission time Handle Problem Language Result Execution time Memory
49265 2018-05-24T16:26:26 Z MatheusLealV Ancient Books (IOI17_books) C++17
0 / 100
3 ms 644 KB
#include "books.h"
#include <bits/stdc++.h>
#define N 1000005
using namespace std;
typedef long long ll;
 
int G[N], ok[N], cor, sz[N], C[N], qtd_trivial = 0;

bool trivial[N];

int pref[N];
 
void dfs(int x)
{
  if(ok[G[x]]) return;
 
  ok[G[x]] = 1;
 
  dfs(G[x]);
}
 
void co(int x)
{
  if(C[G[x]]) return;
 
  C[G[x]] = cor, sz[cor] ++;
 
  co(G[x]);
}
 
ll minimum_walk(vector<int> p, int s)
{
  int n = p.size();

  ll ans = 0;
 
  for(int i = 0; i < n; i++)
  {
  	G[i] = p[i];

  	ans += abs(i - p[i]);

  if(i != p[i])	pref[min(i, p[i])] ++, pref[max(i, p[i]) + 1] --;
  }

  for(int i = 0; i < n; i++) pref[i] += pref[i - 1];
 
  for(int i = 0; i < n; i++)
  {
    if(C[i]) continue;

    ++cor;
 
    C[i] = cor;
 
    sz[cor] = 1;
 
    co(i);
  }

  for(int i = 0; i < n; i++)
  	if(sz[C[i]] == 1) trivial[i] = true;

  for(int i = 0; i < n; i++)
  {
  	if(trivial[i] && pref[i] > 0) qtd_trivial ++;
  }


  for(int i = n - 1; i >= 0; i--)
  {
  	if(!trivial[i]) break;

  	qtd_trivial ++;
  }

  return ans + 2*(cor - qtd_trivial - 1);
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 400 KB Output is correct
4 Correct 3 ms 480 KB Output is correct
5 Correct 2 ms 484 KB Output is correct
6 Incorrect 2 ms 564 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 400 KB Output is correct
4 Correct 3 ms 480 KB Output is correct
5 Correct 2 ms 484 KB Output is correct
6 Incorrect 2 ms 564 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 400 KB Output is correct
4 Correct 3 ms 480 KB Output is correct
5 Correct 2 ms 484 KB Output is correct
6 Incorrect 2 ms 564 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 644 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3106'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 400 KB Output is correct
4 Correct 3 ms 480 KB Output is correct
5 Correct 2 ms 484 KB Output is correct
6 Incorrect 2 ms 564 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -