QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#44227#4565. Rarest InsectsGracey#Compile Error//C++173.2kb2022-08-13 22:16:522024-05-26 01:05:40

Judging History

你现在查看的是最新测评结果

  • [2024-05-26 01:05:40]
  • 评测
  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-08-13 22:16:52]
  • 提交

answer

#include "insects.h"

#include <cassert>
#include <cstdio>

#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <vector>

static constexpr int kMaxQueries = 40000;

static int N;
// Insect types are compressed to colors in the range [0, N).
static std::vector<int> color;
static std::vector<bool> in_box;

static std::vector<int> color_occurrences;
static std::multiset<int> max_occurrences;

static std::vector<int> op_counter(3, 0);

static void protocol_violation(std::string message) {
  printf("Protocol Violation: %s\n", message.c_str());
  exit(0);
}

void move_inside(int i) {
  if (i < 0 || i >= N) {
    protocol_violation("invalid parameter");
  }
  ++op_counter[0];
  if (op_counter[0] > kMaxQueries) {
    protocol_violation("too many calls");
  }
  if (!in_box[i]) {
    in_box[i] = true;
    max_occurrences.erase(max_occurrences.find(color_occurrences[color[i]]));
    ++color_occurrences[color[i]];
    max_occurrences.insert(color_occurrences[color[i]]);
  }
}

void move_outside(int i) {
  if (i < 0 || i >= N) {
    protocol_violation("invalid parameter");
  }
  ++op_counter[1];
  if (op_counter[1] > kMaxQueries) {
    protocol_violation("too many calls");
  }
  if (in_box[i]) {
    in_box[i] = false;
    max_occurrences.erase(max_occurrences.find(color_occurrences[color[i]]));
    --color_occurrences[color[i]];
    max_occurrences.insert(color_occurrences[color[i]]);
  }
}

int press_button() {
  ++op_counter[2];
  if (op_counter[2] > kMaxQueries) {
    protocol_violation("too many calls");
  }
  return *(max_occurrences.rbegin());
}

int main() {
  freopen("sample-1.in", "r", stdin);
  assert(1 == scanf("%d", &N));
  color.resize(N);
  in_box.assign(N, false);

  std::map<int, int> type_to_color;
  for (int i = 0; i < N; ++i) {
    int Ti;
    assert(1 == scanf("%d", &Ti));
    if (type_to_color.find(Ti) == type_to_color.end()) {
      int new_color = type_to_color.size();
      type_to_color[Ti] = new_color;
      max_occurrences.insert(0);
    }
    color[i] = type_to_color[Ti];
  }

  color_occurrences.assign(type_to_color.size(), 0);

  int answer = min_cardinality(N);
  int Q = *std::max_element(op_counter.begin(), op_counter.end());
  printf("%d\n", answer);
  printf("%d\n", Q);
  return 0;
}

#include "insects.h"
#include <bits/stdc++.h>
using namespace std;

int n, cnt;

int solve(vector<int> a, int l, int len) { // l + [0, len]
  if (!len) {
    return l;
  }
  int pos = (len > 2) ? (len / 2) : ((len + 1) / 2);
  vector<int> b, c;
  for (auto i : a) {
    move_inside(i);
    if (press_button() <= l + pos) {
      b.emplace_back(i);
    } else {
      move_outside(i);
      c.emplace_back(i);
    }
  }
  if ((int) b.size() == pos * cnt) {
    return solve(c, l + pos, len - pos);
  } else {
    for (auto i : b) {
      move_outside(i);
    }
    return solve(b, l, pos - 1);
  }
}

int min_cardinality(int N) {
  n = N;
  vector<int> a;
  for (int i = 0; i < n; ++i) {
    move_inside(i);
    if (press_button() == 1) {
      ++cnt;
    } else {
      move_outside(i);
      a.emplace_back(i);  
    }
  }
  int ans = solve(a, 1, n / cnt - 1);
  return ans;
}

Details

answer.code: In function ‘int main()’:
answer.code:70:10: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   70 |   freopen("sample-1.in", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/ld: /tmp/ccw2VYzm.o: in function `press_button()':
answer.code:(.text+0x470): multiple definition of `press_button()'; /tmp/ccwCxL2i.o:implementer.cpp:(.text+0x270): first defined here
/usr/bin/ld: /tmp/ccw2VYzm.o: in function `move_inside(int)':
answer.code:(.text+0x510): multiple definition of `move_inside(int)'; /tmp/ccwCxL2i.o:implementer.cpp:(.text+0xb0): first defined here
/usr/bin/ld: /tmp/ccw2VYzm.o: in function `move_outside(int)':
answer.code:(.text+0x700): multiple definition of `move_outside(int)'; /tmp/ccwCxL2i.o:implementer.cpp:(.text+0x190): first defined here
/usr/bin/ld: /tmp/ccw2VYzm.o: in function `main':
answer.code:(.text.startup+0x0): multiple definition of `main'; /tmp/ccwCxL2i.o:implementer.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status