Submission #295352

#TimeUsernameProblemLanguageResultExecution timeMemory
295352tzeroArranging Shoes (IOI19_shoes)C++14
Compilation error
0 ms0 KiB
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
using ll = long long;

ll count_swaps(vector<int> s) {
  int n = (int)s.size();

  bool same_size = true;
  for(int i = 0; i < n; i++) {
    if(i > 0 && abs(s[i]) != abs(s[i - 1])) same_size = false;
  }

  if(n == 2) {
    if(s[0] < 0) {
      printf("0\n");
    } else {
      printf("1\n");
    }
  } else if(same_size) {
    ll move_left = 0, move_right = 0;
    int found_already = 0;
    for(int i = 0; i < n; i++) if(s[i] > 0) { 
      int pos = found_already++ * 2;
      move_left += abs(i - pos);
    }
    found_already = 0;
    for(int i = 0; i < n; i++) if(s[i] > 0) { 
      int pos = found_already++ * 2 + 1;
      move_right += abs(i - pos);
    }
    return min(move_left, move_right);
  } else {
    return -1;
  }
}

int main() {
  int n;
  scanf("%d", &n);

  bool same_size = true;
  vector<int> s(n);
  for(int i = 0; i < 2 * n; i++) {
    scanf("%d", &s[i]);
    if(i > 0 && abs(s[i]) != abs(s[i - 1])) same_size = false;
  }

  if(n == 1) {
    if(s[0] < 0) {
      printf("0\n");
    } else {
      printf("1\n");
    }
  } else if(same_size) {
    ll move_left = 0, move_right = 0;
    int found_already = 0;
    for(int i = 0; i < 2 * n; i++) if(s[i] > 0) { 
      int pos = found_already++ * 2;
      move_left += abs(i - pos);
    }
    found_already = 0;
    for(int i = 0; i < 2 * n; i++) if(s[i] > 0) { 
      int pos = found_already++ * 2 + 1;
      move_right += abs(i - pos);
    }
    printf("%lld\n", min(move_left, move_right));
  } else {
    printf("%d\n", -1);
  }

  return 0;
}

Compilation message (stderr)

shoes.cpp: In function 'll count_swaps(std::vector<int>)':
shoes.cpp:37:1: warning: control reaches end of non-void function [-Wreturn-type]
   37 | }
      | ^
shoes.cpp: In function 'int main()':
shoes.cpp:41:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   41 |   scanf("%d", &n);
      |   ~~~~~^~~~~~~~~~
shoes.cpp:46:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   46 |     scanf("%d", &s[i]);
      |     ~~~~~^~~~~~~~~~~~~
/tmp/cc4mgXrl.o: In function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'
/tmp/ccBL0g8q.o:shoes.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status