Submission #830525

# Submission time Handle Problem Language Result Execution time Memory
830525 2023-08-19T07:42:26 Z josanneo22 Rarest Insects (IOI22_insects) C++17
Compilation error
0 ms 0 KB
#include <cassert>
#include <cstdio>

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

const 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 inline 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());
}
#include <bits/stdc++.h>
using namespace std;

#define ld long double
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second

/*草,以后一定要拿金牌啊!!! 注意数值范围拿分数哦*/
#include "insects.h"
const int S=1e5+5;
mt19937 rand1(time(0));
int l,r,cnt,cnt1,a[S],n;
bool vs[S],vs1[S];

void ins(int x) {++cnt1;move_inside(x-1);}
void dlt(int x) {--cnt1;move_outside(x-1);}
int qry() {return press_button();}

int min_cardinality(int N) {
	n=N;
	for(int i=1;i<=n;++i) {
		ins(i);
		if(qry()>1) dlt(i);
		else vs[i]=1,++cnt;
	}
	if(cnt==1) return n;
	l=2;r=n/cnt;
	while(l<=r)
	{
		int mid=(l+r)>>1;
		for(int i=1;i<=n;++i) {vs1[i]=0,a[i]=i;}
		shuffle(a+1,a+n+1,rand1);
		for(int i=1;i<=n;++i){
			if(!vs[a[i]])
			{
				if(cnt*mid<=cnt1) break;
				ins(a[i]);
				if(qry()>mid) dlt(a[i]);
				else vs1[a[i]]=1;
			}
		}
		if(cnt*mid<=cnt1) {
			l=mid+1;
			for(int i=1;i<=n;++i) if(vs1[i]) vs[i]=1;
		}
		else {
			r=cnt1/cnt;
			for(int i=1;i<=n;++i) {
				if(vs1[i]) dlt(i);
				else vs[i]=1;
			}
		}
	}
	return r;
}
int main() {
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
  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;
}

Compilation message

insects.cpp: In function 'int main()':
insects.cpp:126:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  126 |  freopen("input.txt","r",stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
insects.cpp:127:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  127 |  freopen("output.txt","w",stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/ld: /tmp/ccOyEBwH.o: in function `move_inside(int)':
stub.cpp:(.text+0x0): multiple definition of `move_inside(int)'; /tmp/ccvTRRgG.o:insects.cpp:(.text+0x320): first defined here
/usr/bin/ld: /tmp/ccOyEBwH.o: in function `move_outside(int)':
stub.cpp:(.text+0xe0): multiple definition of `move_outside(int)'; /tmp/ccvTRRgG.o:insects.cpp:(.text+0x510): first defined here
/usr/bin/ld: /tmp/ccOyEBwH.o: in function `press_button()':
stub.cpp:(.text+0x1c0): multiple definition of `press_button()'; /tmp/ccvTRRgG.o:insects.cpp:(.text+0x200): first defined here
/usr/bin/ld: /tmp/ccOyEBwH.o: in function `main':
stub.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccvTRRgG.o:insects.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status