Submission #828730

# Submission time Handle Problem Language Result Execution time Memory
828730 2023-08-17T14:25:10 Z Abrar_Al_Samit Counting Mushrooms (IOI20_mushrooms) C++17
90.7631 / 100
7 ms 400 KB
#include <bits/stdc++.h>
#include "mushrooms.h"
using namespace std;

const int bl = 230;

int count_mushrooms(int n) {
	vector<int>mac;

	int c1 = use_machine(mac = {0, 1});
	if(n==2) return 1 + 1-c1;

	int c2 = use_machine(mac = {0, 2});

	int tp[n];
	tp[0] = 1;
	tp[1] = 1-c1;
	tp[2] = 1-c2;

	int s1, s2;
	if(c1 && c2) s1 = 1, s2 = 2;
	else {
		s1 = 0;
		if(c1) s2 = 2;
		else s2 = 1;
	}

	if(n==3) {
		return tp[0] + tp[1] + tp[2];
	}

	int cnt[2] = {0};

	int upto;
	for(int i=3; i<min(bl, n); i+=2) {
		if(i==n-1) {
			tp[i] = 1 - use_machine(mac = {0, i});
			upto = i;
		} else {
			int c = use_machine(mac = {i, s1, i+1, s2});
			if(c==0) {
				tp[i] = tp[i+1] = tp[s1];
			} else if(c==1) {
				tp[i] = 1-tp[s1];
				tp[i+1] = tp[s1];
			} else if(c==2) {
				tp[i+1] = 1-tp[s1];
				tp[i] = tp[s1];
			} else {
				tp[i] = tp[i+1] = 1-tp[s1];
			}

			++cnt[tp[i]], ++cnt[tp[i+1]];

			upto = i+1;
			if(max(cnt[tp[i]], cnt[tp[i+1]])>=bl/2) {
				break;
			}
		}
	}

	int ans = 0;
	vector<int>cl[2];
	for(int i=0; i<=upto; ++i) {
		cl[tp[i]].push_back(i);

		ans += tp[i];
	}

	if(upto == n-1) return ans;

	int cr = upto + 1;

	while(cr < n) {
		mac.clear();

		bool mxw = 0;
		if(cl[1].size()>=cl[0].size()) mxw = 1;

		mac = {cr}; int f = cr; ++cr;

		int ctr = 0;
		for(int j=0; j<cl[mxw].size(); ++j) {
			mac.push_back(cl[mxw][j]);
			if(cr==n) {
				while(cl[mxw].size()!=j+1) cl[mxw].pop_back();
				break;
			}

			if(j!=cl[mxw].size()-1) mac.push_back(cr), ++cr, ++ctr;
		}

		int get = use_machine(mac);

		if(get & 1) {
			cl[1-mxw].push_back(f);
			--get;
			ans += 1-mxw;
		} else {
			cl[mxw].push_back(f);
			ans += mxw;
		}

		if(!mxw) ans += get / 2;
		else ans += (ctr * 2 - get) / 2;
	}
	return ans;
}

Compilation message

mushrooms.cpp: In function 'int count_mushrooms(int)':
mushrooms.cpp:83:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |   for(int j=0; j<cl[mxw].size(); ++j) {
      |                ~^~~~~~~~~~~~~~~
mushrooms.cpp:86:25: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   86 |     while(cl[mxw].size()!=j+1) cl[mxw].pop_back();
      |           ~~~~~~~~~~~~~~^~~~~
mushrooms.cpp:90:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |    if(j!=cl[mxw].size()-1) mac.push_back(cr), ++cr, ++ctr;
      |       ~^~~~~~~~~~~~~~~~~~
mushrooms.cpp:70:2: warning: 'upto' may be used uninitialized in this function [-Wmaybe-uninitialized]
   70 |  if(upto == n-1) return ans;
      |  ^~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 208 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 2 ms 208 KB Output is correct
6 Correct 2 ms 208 KB Output is correct
7 Correct 6 ms 336 KB Output is correct
8 Correct 4 ms 336 KB Output is correct
9 Correct 4 ms 336 KB Output is correct
10 Correct 6 ms 336 KB Output is correct
11 Partially correct 5 ms 384 KB Output is partially correct
12 Correct 6 ms 384 KB Output is correct
13 Correct 5 ms 336 KB Output is correct
14 Correct 3 ms 336 KB Output is correct
15 Partially correct 5 ms 336 KB Output is partially correct
16 Partially correct 6 ms 336 KB Output is partially correct
17 Correct 3 ms 336 KB Output is correct
18 Correct 4 ms 336 KB Output is correct
19 Partially correct 5 ms 336 KB Output is partially correct
20 Correct 4 ms 336 KB Output is correct
21 Correct 4 ms 392 KB Output is correct
22 Partially correct 5 ms 336 KB Output is partially correct
23 Correct 4 ms 336 KB Output is correct
24 Correct 4 ms 352 KB Output is correct
25 Partially correct 4 ms 336 KB Output is partially correct
26 Partially correct 6 ms 336 KB Output is partially correct
27 Partially correct 5 ms 304 KB Output is partially correct
28 Partially correct 7 ms 336 KB Output is partially correct
29 Partially correct 7 ms 336 KB Output is partially correct
30 Partially correct 5 ms 396 KB Output is partially correct
31 Partially correct 6 ms 336 KB Output is partially correct
32 Partially correct 5 ms 336 KB Output is partially correct
33 Partially correct 5 ms 336 KB Output is partially correct
34 Partially correct 5 ms 336 KB Output is partially correct
35 Partially correct 6 ms 392 KB Output is partially correct
36 Partially correct 6 ms 336 KB Output is partially correct
37 Partially correct 5 ms 388 KB Output is partially correct
38 Partially correct 5 ms 308 KB Output is partially correct
39 Partially correct 5 ms 308 KB Output is partially correct
40 Partially correct 5 ms 392 KB Output is partially correct
41 Partially correct 6 ms 384 KB Output is partially correct
42 Partially correct 5 ms 400 KB Output is partially correct
43 Partially correct 7 ms 336 KB Output is partially correct
44 Partially correct 5 ms 400 KB Output is partially correct
45 Partially correct 5 ms 336 KB Output is partially correct
46 Partially correct 5 ms 336 KB Output is partially correct
47 Partially correct 5 ms 336 KB Output is partially correct
48 Partially correct 7 ms 388 KB Output is partially correct
49 Partially correct 4 ms 400 KB Output is partially correct
50 Partially correct 5 ms 336 KB Output is partially correct
51 Partially correct 5 ms 396 KB Output is partially correct
52 Partially correct 5 ms 336 KB Output is partially correct
53 Partially correct 6 ms 336 KB Output is partially correct
54 Partially correct 5 ms 336 KB Output is partially correct
55 Partially correct 5 ms 392 KB Output is partially correct
56 Partially correct 5 ms 396 KB Output is partially correct
57 Partially correct 6 ms 336 KB Output is partially correct
58 Partially correct 5 ms 336 KB Output is partially correct
59 Partially correct 6 ms 336 KB Output is partially correct
60 Partially correct 6 ms 336 KB Output is partially correct
61 Partially correct 6 ms 336 KB Output is partially correct
62 Correct 1 ms 208 KB Output is correct
63 Correct 0 ms 208 KB Output is correct
64 Correct 0 ms 208 KB Output is correct
65 Correct 0 ms 208 KB Output is correct
66 Correct 0 ms 208 KB Output is correct
67 Correct 0 ms 208 KB Output is correct
68 Correct 0 ms 208 KB Output is correct
69 Correct 0 ms 208 KB Output is correct
70 Correct 0 ms 208 KB Output is correct
71 Correct 0 ms 208 KB Output is correct
72 Correct 0 ms 208 KB Output is correct
73 Correct 0 ms 208 KB Output is correct
74 Correct 0 ms 208 KB Output is correct
75 Correct 0 ms 208 KB Output is correct
76 Correct 0 ms 208 KB Output is correct
77 Correct 0 ms 208 KB Output is correct
78 Correct 0 ms 208 KB Output is correct
79 Correct 0 ms 208 KB Output is correct
80 Correct 0 ms 208 KB Output is correct
81 Correct 0 ms 208 KB Output is correct
82 Correct 0 ms 208 KB Output is correct
83 Correct 0 ms 208 KB Output is correct
84 Correct 0 ms 208 KB Output is correct
85 Correct 0 ms 208 KB Output is correct
86 Correct 0 ms 208 KB Output is correct
87 Correct 0 ms 208 KB Output is correct
88 Correct 0 ms 208 KB Output is correct
89 Correct 0 ms 208 KB Output is correct
90 Correct 0 ms 208 KB Output is correct
91 Correct 0 ms 208 KB Output is correct