답안 #1011517

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1011517 2024-06-30T14:42:36 Z SonicML 곤돌라 (IOI14_gondola) C++14
100 / 100
47 ms 6692 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include "gondola.h"
#include <map>

using namespace std;

int const NMAX = 1e5;
int const MMAX = 250000; 
map <int, int> freq;

int valid(int n, int inputSeq[]) {
  int minpos = 0;
  bool isUnbroken = false;
  for(int i = 0;i < n;i++) {
    freq[inputSeq[i]]++;
    if(freq[inputSeq[i]] > 1) {
      return 0;
    }
    if(inputSeq[i] <= n) {
      isUnbroken = true;
      if(inputSeq[minpos] > inputSeq[i]) {
        minpos = i;
      }
    }
  }
  if(isUnbroken) {
    for(int i = 0;i < n;i++) {
      if(inputSeq[i] <= n) {
        int distPos = (n + i - minpos) % n;
        int distVal = inputSeq[i] - inputSeq[minpos];
        if(distPos != distVal) {
          return 0;
        }
      }
    }
  }
  return 1;
}

int arr[1 + NMAX];

void printArr(int n) {
  for(int i = 0;i < n;i++) {
    cout << arr[i] << ' ';
  }
  cout << '\n';
  return;
}

int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
 bool unmarked[1 + NMAX];
 int pos[1 + MMAX];
 int maxx = 0, minpos = 0;
 bool isUnbroken = false;
 for(int i = 1;i <= MMAX;i++) {
   pos[i] = -1;
 }
 for(int i = 0;i < n;i++) {
   if(gondolaSeq[i] <= n) {
     unmarked[i] = false;
     isUnbroken = true;
     if(gondolaSeq[minpos] > gondolaSeq[i]) {
       minpos = i;
     }
   } else {
     unmarked[i] = true;
   }
   pos[gondolaSeq[i]] = i;
   maxx = max(maxx, gondolaSeq[i]);
 }
 if(isUnbroken) {
   for(int i = 0;i < n;i++) {
     int dist = (n + i - minpos);
     int val = (gondolaSeq[minpos] + dist) % n;
     if(val == 0) {
       val = n;
     }
     arr[i] = val;
   }
 }else {
   for(int i = 0;i < n;i++) {
     arr[i] = i + 1;
   }
 }
 //printArr(n);
 int dump = 0, index = 0;
 for(int i = n+1;i <= maxx;i++) {
   if(pos[i] == -1) {
     while(!unmarked[dump]) {
       dump++;
     }
     replacementSeq[index] = arr[dump];
     arr[dump] = i;
     index++;
   }else {
     replacementSeq[index] = arr[pos[i]];
     unmarked[pos[i]] = false;
     arr[pos[i]] = i;
     index++;
   }
 }
 return index;
}

int const MODULO = 1e9+9;

int logPow(int a, int b) {
  if(b == 0) {
    return 1;
  } else if(b == 1) {
    return a;
  } else {
    int mult = logPow(a, b/2);
    mult = (1LL * mult * mult) % MODULO;
    if(b % 2 == 1) {
      return (1LL * mult * a) % MODULO;
    } else {
      return mult;
    }
  }
}

int countReplacement(int n, int inputSeq[]) {
  if(!valid(n, inputSeq)) {
    //cerr << "SUDOKU\n";
    return 0;
  }
  vector <int> toMark;
  int unmarked = n;
  bool isUnbroken = false;
  for(int i = 0;i < n;i++) {
    if(inputSeq[i] <= n) {
      unmarked--;  
      isUnbroken = true;
    } else {
      toMark.push_back(inputSeq[i]);
    }
  }
  sort(toMark.begin(), toMark.end());
  int prev = n, ans = 1;

  for(int i = 0;i < toMark.size();i++) {
    int dist = (toMark[i] - prev - 1);
    //cerr << unmarked << ' ' << logPow(unmarked, dist) << '\n';
    ans = (1LL * ans * logPow(unmarked, dist)) % MODULO;
    unmarked--;
    prev = toMark[i];
  }
  if(!isUnbroken) {
    ans = (1LL * ans * n) % MODULO;
  }
  return ans;
}

Compilation message

gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:144:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  144 |   for(int i = 0;i < toMark.size();i++) {
      |                 ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 9 ms 2364 KB Output is correct
7 Correct 8 ms 1144 KB Output is correct
8 Correct 17 ms 4444 KB Output is correct
9 Correct 5 ms 1624 KB Output is correct
10 Correct 21 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 9 ms 2532 KB Output is correct
7 Correct 6 ms 1164 KB Output is correct
8 Correct 17 ms 4440 KB Output is correct
9 Correct 5 ms 1628 KB Output is correct
10 Correct 26 ms 4956 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 13 ms 2356 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 31 ms 5136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1372 KB Output is correct
2 Correct 1 ms 1372 KB Output is correct
3 Correct 1 ms 1372 KB Output is correct
4 Correct 1 ms 1372 KB Output is correct
5 Correct 1 ms 1372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1368 KB Output is correct
2 Correct 1 ms 1372 KB Output is correct
3 Correct 1 ms 1372 KB Output is correct
4 Correct 1 ms 1372 KB Output is correct
5 Correct 1 ms 1368 KB Output is correct
6 Correct 1 ms 1372 KB Output is correct
7 Correct 1 ms 1372 KB Output is correct
8 Correct 1 ms 1372 KB Output is correct
9 Correct 1 ms 1372 KB Output is correct
10 Correct 1 ms 1372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1372 KB Output is correct
2 Correct 1 ms 1372 KB Output is correct
3 Correct 1 ms 1372 KB Output is correct
4 Correct 1 ms 1372 KB Output is correct
5 Correct 1 ms 1372 KB Output is correct
6 Correct 1 ms 1372 KB Output is correct
7 Correct 1 ms 1372 KB Output is correct
8 Correct 1 ms 1372 KB Output is correct
9 Correct 1 ms 1476 KB Output is correct
10 Correct 1 ms 1368 KB Output is correct
11 Correct 6 ms 2396 KB Output is correct
12 Correct 7 ms 2652 KB Output is correct
13 Correct 8 ms 2652 KB Output is correct
14 Correct 6 ms 2392 KB Output is correct
15 Correct 12 ms 3420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 448 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 444 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 32 ms 4892 KB Output is correct
10 Correct 24 ms 4180 KB Output is correct
11 Correct 9 ms 1884 KB Output is correct
12 Correct 11 ms 2140 KB Output is correct
13 Correct 2 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 448 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 600 KB Output is correct
9 Correct 33 ms 4948 KB Output is correct
10 Correct 24 ms 4172 KB Output is correct
11 Correct 9 ms 1884 KB Output is correct
12 Correct 11 ms 2244 KB Output is correct
13 Correct 3 ms 860 KB Output is correct
14 Correct 40 ms 6096 KB Output is correct
15 Correct 47 ms 6692 KB Output is correct
16 Correct 8 ms 1880 KB Output is correct
17 Correct 29 ms 4564 KB Output is correct
18 Correct 15 ms 2864 KB Output is correct