Problem:
デジタルアート
JOI 高校の生徒である葵はデジタルアート制作が趣味であり，今日も新しい画像を作った．
この画像のサイズは，縦
H
ピクセル，横
W
ピクセルであり，
H × W
のマス目のような形で表される．ここで，上から
i
行目 (
1 ≦ i ≦ H
)，左から
j
列目 (
1 ≦ j ≦ W
) のピクセルを
(i, j)
で表す．各ピクセルは
1
つの色で塗られている．各色には
1
から
256
までの番号が付けられており，ピクセル
(i, j)
の色の番号は
A
i, j
である．
葵はこの画像を同級生である凛に見せたが，凛は「画像に使われている色の種類が多すぎる」という理由で気に入らなかった．そこで葵は，以下のように画像内のある領域を隠して，見える色の種類をできるだけ少なくできないかと考えた．
葵は
S
個以下のピクセルを選んで隠す．
ただし，隠すピクセルの領域は
1
つの長方形で表されなければならない．
画像のデータと，隠すピクセルの個数の上限
S
が与えられたとき，画像内のある領域を隠したときに見える色の種類の数としてありうる最小の値を求めるプログラムを作成せよ．
制約
1 ≦ H ≦ 1000
．
1 ≦ W ≦ 1000
．
1 ≦ S ≦ HW
．
1 ≦ A
i, j
≦ 256
(
1 ≦ i ≦ H
，
1 ≦ j ≦ W
)．
入力される値はすべて整数である．
入力
入力は以下の形式で標準入力から与えられる．
H
W
S
A
1, 1
A
1, 2
…
A
1, W
A
2, 1
A
2, 2
…
A
2, W
:
A
H, 1
A
H, 2
…
A
H, W
出力
標準出力に，画像内のある領域を隠したときに見える色の種類の数としてありうる最小の値を
1
行で出力せよ．
特に，見えるピクセルが一つもない状態にできる場合は
0
と出力せよ．
入出力例
入力例 1
1 10 7
5 1 2 5 2 2 5 6 6 5
出力例 1
2
例えば，左から数えて
2
番目から
8
番目までのピクセルを隠すと，番号
5, 6
の色だけが見え，その種類の数は
2
となる．この場合，隠すピクセルは
7
個となり，条件を満たす．また，葵は見える色を
1
種類以下にすることはできない．よって
2
を出力する．
入力例 2
10 10 45
1 1 1 1 1 1 1 1 1 1
1 1 1 2 2 3 3 1 1 1
1 1 2 1 2 3 1 3 1 1
1 2 1 2 6 6 3 1 3 1
1 2 2 6 1 1 6 3 3 1
1 4 4 6 1 1 6 5 5 1
1 4 1 4 6 6 5 1 5 1
1 1 4 1 4 5 1 5 1 1
1 1 1 4 4 5 5 1 1 1
1 1 1 1 1 1 1 1 1 1
出力例 2
4
例えば，左上をピクセル
(2, 1)
，右下をピクセル
(7, 7)
とする長方形領域を隠すと，番号
1, 3, 4, 5
の色だけが見え，その種類の数は
4
となる．この場合，隠すピクセルは
42
個となり，条件を満たす．
この説明を図で表すと，以下のようになる．
入力例 3
5 10 1
2 3 5 7 1 1 1 3 1 7
1 9 2 3 2 9 3 1 3 7
4 1 4 3 4 7 5 3 5 9
6 1 6 7 7 1 7 3 7 9
8 3 8 9 9 7 2 3 5 7
出力例 3
9
隠すピクセルを
S = 1
個以下にするという条件の下では，葵は番号
1, 2, 3, ..., 9
の
9
種類の色が見えるようにしかできない．よって
9
を出力する．
入力例 4
9 6 54
1 1 1 1 1 3
6 14 14 3 3 12
9 13 1 10 3 3
9 13 5 5 3 3
6 13 10 3 7 3
2 5 8 5 3 3
6 5 5 3 15 3
6 5 10 5 3 3
2 2 5 7 3 3
出力例 4
0
54
個すべてのピクセルを隠して，見えるピクセルが一つもないようにできる．よって
0
を出力する．
入力例 5
8 10 59
3 3 3 3 3 3 3 3 2 3
3 1 3 3 3 3 3 2 3 3
3 3 1 4 3 4 2 3 3 3
3 3 3 1 4 2 4 3 3 3
3 3 3 4 1 4 2 3 3 3
3 3 3 1 4 3 4 2 3 3
3 3 1 3 3 3 3 3 2 3
3 1 3 3 3 3 3 3 3 3
出力例 5
2
例えば，左上をピクセル
(3, 1)
，右下をピクセル
(10, 7)
とする長方形領域を隠すと，番号
1, 3
の色だけが見え，その種類の数は
2
となる．この場合，隠すピクセルは
56
個となり，条件を満たす．
この説明を図で表すと，以下のようになる．
情報オリンピック日本委員会
作 『日本情報オリンピック 第1回 女性部門』

Submitted solutions:

code_snippet: #include <iostream>
#include <bitset>
#include <vector>
#include <string.h>
#include <set>
using namespace std;
const int c257=257;
const int c1001=1001;
 
struct E{
	int p1;
	bitset<c257> b1;
	bool operator<(const E e1)const{
		return p1<e1.p1;
	}
};
 
vector<E> f(int *as){
	vector<E> vec1;
	vector<E> vec2;
	for(int i=0;i<c1001;i++){
		E e1;
		e1.p1=i;
		vec1.push_back(e1);
	}
	for(int i=0;i<c257;i++){
		if(as[i]==-1)continue;
		vec1[as[i]].b1.set(i);
	}
	bitset<c257> b2;
	for(int i=0;i<c1001;i++){
		if(vec1[i].b1.count()==0)continue;
		E e1;
		b2|=vec1[i].b1;
		e1.p1=vec1[i].p1;
		e1.b1=b2;
		vec2.push_back(e1);
	}
	return vec2;
}
 
int main() {
	// your code goes here
	int ls[c257];
	int rs[c257];
	int us[c257];
	int ds[c257];
	int cs[c257];
	memset(ls,-1,sizeof(ls));
	memset(rs,-1,sizeof(rs));
	memset(us,-1,sizeof(us));
	memset(ds,-1,sizeof(ds));
	int h,w,s;
	cin>>h>>w>>s;
	for(int i=0;i<c257;i++)cs[i]=1;
	int all2=0;
	for(int y=0;y<h;y++){
		for(int x=0;x<w;x++){
			int t;
			cin>>t;
			all2+=cs[t];
			cs[t]=0;
			if(ls[t]==-1||x<ls[t])ls[t]=x;
			if(rs[t]==-1||rs[t]<x)rs[t]=x;
			if(us[t]==-1||y<us[t])us[t]=y;
			if(ds[t]==-1||ds[t]<y)ds[t]=y;
		}
	}
	int ans=all2;
	vector<E> us2=f(us);
	vector<E> ds2=f(ds);
	vector<E> ls2=f(ls);
	vector<E> rs2=f(rs);
	set<E> ss2;
 
	for(int i=0;i<us2.size();i++){
		ss2.insert(us2[i]);
		//cout<<us2[i].p1<<" "<<us2[i].b1.count()<<" ";
	}
	//for(set<E>::iterator it=ss2.begin();it!=ss2.end();it++){
//	cout<<(*it).p1<<" "<<(*it).b1.count()<<endl;
//	}
	//cout<<endl;
	//for(int i=0;i<ds2.size();i++){
	//	cout<<ds2[i].p1<<" "<<ds2[i].b1.count()<<" ";
	//}
	//cout<<endl;
	//for(int i=0;i<ls2.size();i++){
	//	cout<<ls2[i].p1<<" "<<ls2[i].b1.count()<<" ";
	//}
	//cout<<endl;
	//for(int i=0;i<rs2.size();i++){
	//	cout<<rs2[i].p1<<" "<<rs2[i].b1.count()<<" ";
	//}
 
	//cout<<endl;
	for(int r2=0;r2<rs2.size();r2++){
		E e1=rs2[r2];
		for(int l2=0;l2<ls2.size();l2++){
			E e2;
			e2.p1=ls2[l2].p1;
			if(l2>0)e2.b1=ls2[l2-1].b1;
			if(e1.p1<e2.p1)break;
			int lenx=e1.p1-e2.p1+1;
			if(lenx>s)continue;
			int leny=s/lenx;
 
			bitset<c257> b2=~(e1.b1&e2.b1);
			bitset<c257> b1=e1.b1&b2;
 
			for(int d2=0;d2<ds2.size();d2++){
				E e3=ds2[d2];
				int u0=e3.p1-leny+1;
				//cout<<e3.p1<<" "<<lenx<<" "<<leny<<endl;
				if(u0<0)u0=0;
				E e0;
				e0.p1=u0;
				set<E>::iterator it2=ss2.lower_bound(e0);
				if (it2==ss2.end()) continue;
				E e4;
				e4.p1=(*it2).p1;
				if(it2!=ss2.begin()){
					it2--;
					e4.b1=(*it2).b1;
				}
				//cout<<u0<<" "<<leny<<" r"<<e1.p1<<" l"<<e2.p1<<" d"<<e3.p1<<" u"<<e4.p1<<endl;
				int leny2=e3.p1-e4.p1+1;
				if(lenx*leny2>s)continue;
				bitset<c257> b4=~(e3.b1&e4.b1);
				bitset<c257> b3=e3.b1&b4;
				int ans2=all2-(b1&b3).count();
 
				if(ans2<ans){
					ans=ans2;
				}
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}
accuracy: 1
time_ms: 770
memory_kb: 3252
final_rank: 15

code_snippet: #include <iostream>
#include <bitset>
#include <vector>
#include <string.h>
#include <set>
using namespace std;
const int c257=257;
const int c1001=1001;

struct E{
	int p1;
	bitset<c257> b1;
	bool operator<(const E e1)const{
		return p1<e1.p1;
	}
};

vector<E> f(int *as){
	vector<E> vec1;
	vector<E> vec2;
	for(int i=0;i<c1001;i++){
		E e1;
		e1.p1=i;
		vec1.push_back(e1);
	}
	for(int i=0;i<c257;i++){
		if(as[i]==-1)continue;
		vec1[as[i]].b1.set(i);
	}
	bitset<c257> b2;
	for(int i=0;i<c1001;i++){
		if(vec1[i].b1.count()==0)continue;
		E e1;
		b2|=vec1[i].b1;
		e1.p1=vec1[i].p1;
		e1.b1=b2;
		vec2.push_back(e1);
	}
	return vec2;
}

int main() {
	// your code goes here
	int ls[c257];
	int rs[c257];
	int us[c257];
	int ds[c257];
	int cs[c257];
	memset(ls,-1,sizeof(ls));
	memset(rs,-1,sizeof(rs));
	memset(us,-1,sizeof(us));
	memset(ds,-1,sizeof(ds));
	int h,w,s;
	cin>>h>>w>>s;
	for(int i=0;i<c257;i++)cs[i]=1;
	int all2=0;
	for(int y=0;y<h;y++){
		for(int x=0;x<w;x++){
			int t;
			cin>>t;
			all2+=cs[t];
			cs[t]=0;
			if(ls[t]==-1||x<ls[t])ls[t]=x;
			if(rs[t]==-1||rs[t]<x)rs[t]=x;
			if(us[t]==-1||y<us[t])us[t]=y;
			if(ds[t]==-1||ds[t]<y)ds[t]=y;
		}
	}
	int ans=all2;
	vector<E> us2=f(us);
	vector<E> ds2=f(ds);
	vector<E> ls2=f(ls);
	vector<E> rs2=f(rs);
	set<E> ss2;
	
	for(int i=0;i<us2.size();i++){
		ss2.insert(us2[i]);
		//cout<<us2[i].p1<<" "<<us2[i].b1.count()<<" ";
	}
	//for(set<E>::iterator it=ss2.begin();it!=ss2.end();it++){
//	cout<<(*it).p1<<" "<<(*it).b1.count()<<endl;
//	}
	//cout<<endl;
	//for(int i=0;i<ds2.size();i++){
	//	cout<<ds2[i].p1<<" "<<ds2[i].b1.count()<<" ";
	//}
	//cout<<endl;
	//for(int i=0;i<ls2.size();i++){
	//	cout<<ls2[i].p1<<" "<<ls2[i].b1.count()<<" ";
	//}
	//cout<<endl;
	//for(int i=0;i<rs2.size();i++){
	//	cout<<rs2[i].p1<<" "<<rs2[i].b1.count()<<" ";
	//}
	
	//cout<<endl;
	for(int r2=0;r2<rs2.size();r2++){
		E e1=rs2[r2];
		for(int l2=0;l2<ls2.size();l2++){
			E e2;
			e2.p1=ls2[l2].p1;
			if(l2>0)e2.b1=ls2[l2-1].b1;
			if(e1.p1<e2.p1)break;
			int lenx=e1.p1-e2.p1+1;
			if(lenx>s)continue;
			int leny=s/lenx;
			
			bitset<c257> b2=~(e1.b1&e2.b1);
			bitset<c257> b1=e1.b1&b2;
			
			for(int d2=0;d2<ds2.size();d2++){
				E e3=ds2[d2];
				int u0=e3.p1-leny+1;
				//cout<<e3.p1<<" "<<lenx<<" "<<leny<<endl;
				if(u0<0)u0=0;
				E e0;
				e0.p1=u0;
				set<E>::iterator it2=ss2.lower_bound(e0);
				E e4;
				e4.p1=(*it2).p1;
				if(it2!=ss2.begin()){
					it2--;
					e4.b1=(*it2).b1;
				}
				//cout<<u0<<" "<<leny<<" r"<<e1.p1<<" l"<<e2.p1<<" d"<<e3.p1<<" u"<<e4.p1<<endl;
				int leny2=e3.p1-e4.p1+1;
				if(lenx*leny2>s)continue;
				bitset<c257> b4=~(e3.b1&e4.b1);
				bitset<c257> b3=e3.b1&b4;
				int ans2=all2-(b1&b3).count();
				
				if(ans2<ans){
					ans=ans2;
				}
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}
accuracy: 1
time_ms: 780
memory_kb: 3256
final_rank: 16

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0;i<int(n);i++)
typedef long long ll;
typedef pair<int,int> P;


int main(void){
	int i,j,k;

	cin.tie(0);  ios_base::sync_with_stdio(false);

	int H,W,S;
	cin >> H >> W >> S;
	vector<P> L(257);
	REP(i,257) L[i].first=10000;
	vector<int> U(257,10000),D(257,0),R(257,0);

	for(i=1;i<=H;i++) for(j=1;j<=W;j++){
		int A;
		cin >> A;
		L[A].first=min(L[A].first,j);
		L[A].second=A;
		U[A]=min(U[A],i);
		R[A]=max(R[A],j);
		D[A]=max(D[A],i);
	}

	sort(L.begin()+1,L.end());

	int m=0;
	for(i=1;i<=256;i++) if(L[i].first!=10000) m++;

	int ans=300;
	for(i=1;i<=256;i++){
		if(U[i]==10000) continue;
		for(j=i;j<=256;j++){
			if(U[j]==10000) continue;

			int h1=min(U[i],U[j]),h2=max(D[i],D[j]);

			vector<int> v,u;
			for(k=1;k<=m;k++){
	    	    int x=U[L[k].second],y=D[L[k].second];
	    	    int r=(h2-h1+1)*(R[L[k].second]-L[k].first+1);
	    	    if(h1<=x && h2>=y && r<=S){
	    	    	v.push_back(L[k].first);
	    	    	u.push_back(R[L[k].second]);
	    	    }
	       }

			sort(u.begin(),u.end());
			int n=v.size();
			int mx=0,c,x=S/(h2-h1+1);
			REP(t,n){
				c=upper_bound(u.begin(),u.end(),min(v[t]+x-1,W))-u.begin()-t;
				mx=max(mx,c);
			}

			ans=min(ans,m-mx);
		}
	}

	cout << ans << endl;

	return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 3212
final_rank: 1

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0;i<int(n);i++)
typedef long long ll;
typedef pair<int,int> P;


int main(void){
	int i,j,k;

	cin.tie(0);  ios_base::sync_with_stdio(false);

	int H,W,S;
	cin >> H >> W >> S;
	vector<P> L(257);
	REP(i,257) L[i].first=10000;
	vector<int> U(257,10000),D(257,0),R(257,0);

	for(i=1;i<=H;i++) for(j=1;j<=W;j++){
		int A;
		cin >> A;
		L[A].first=min(L[A].first,j);
		L[A].second=A;
		U[A]=min(U[A],i);
		R[A]=max(R[A],j);
		D[A]=max(D[A],i);
	}

	sort(L.begin()+1,L.end());

	int m=0;
	for(i=1;i<=256;i++) if(L[i].first!=10000) m++;

	int ans=300;
	for(i=1;i<=256;i++){
		if(U[i]==10000) continue;
		for(j=i;j<=256;j++){
			if(U[j]==10000) continue;

			int h1=min(U[i],U[j]),h2=max(D[i],D[j]);

			vector<P> v;
			v.push_back(P(-1,0));
			for(k=1;k<=m;k++){
	    	    int x=U[L[k].second],y=D[L[k].second];
	    	    int r=(h2-h1+1)*(R[L[k].second]-L[k].first+1);
	    	    if(h1<=x && h2>=y && r<=S) v.push_back(P(L[k].first,R[L[k].second]));
	       }

			sort(v.begin(),v.end());

			int n=v.size();
			n--;

			int s=1,t=1,mx=0,c=0;
			while(s<=n){

			    while(t<=n){

			    	 int a=(h2-h1+1)*(v[t].second-v[s].first+1);
		    	     if(a>S) break;

		    	     c++;
		          	 t++;
			    }

			     mx=max(mx,c);

			     c--;
			     s++;
			}

			ans=min(ans,m-mx);

		}
	}

	cout << ans << endl;


	return 0;
}
accuracy: 0.53125
time_ms: 170
memory_kb: 3244
final_rank: 19

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
using namespace std;


const int max_h = 1000;
const int max_w = 1000;

class BinInfo {
public:
	int count;
	int min_x;
	int max_x;
	int min_y;
	int max_y;

public:
	BinInfo() : count(0), min_x(max_w), max_x(0), min_y(max_h), max_y(0) {
		return;
	}

	~BinInfo() {
		return;
	}
};


int main() {
#if READ_FROM_FILE
	ifstream ifs("test0.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	int h = -1;
	int w = -1;
	int s = -1;
	const int max_num_pixel_values = 257;
	int num_pixel_values = 257;
	unordered_set<unsigned char> pixel_value_set;
	BinInfo pixel_hist[max_num_pixel_values];
	vector<const BinInfo*> candidate_bin_vec;

	cin >> h >> w >> s;
	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			int pixel_value = -1;

			cin >> pixel_value;
			BinInfo& c_bin = pixel_hist[pixel_value];
			c_bin.count++;
			c_bin.min_x = min(c_bin.min_x, x);
			c_bin.max_x = max(c_bin.max_x, x);
			c_bin.min_y = min(c_bin.min_y, y);
			c_bin.max_y = max(c_bin.max_y, y);
			pixel_value_set.insert(pixel_value);
		}
	}

	if (s >= h * w) {
		cout << 0 << endl;
	}
	else {
		num_pixel_values = (int)pixel_value_set.size();
		for (const BinInfo& c_bin : pixel_hist) {
			if (0 < c_bin.count && c_bin.count <= s && c_bin.max_x - c_bin.min_x <= s && c_bin.max_y - c_bin.min_y <= s) {
				candidate_bin_vec.emplace_back(&c_bin);
			}
		}

		if (candidate_bin_vec.empty()) {
			cout << num_pixel_values << endl;
		}
		else {
			int max_hh = max(2, min(h + 1, s + 1));
			int min_hh = min(max(1, s / w), max_hh - 1);
			int max_num_removed_pixel_values = 0;

			for (int hh = min_hh; hh < max_hh; hh++) {
				int ww = s / hh;
				vector<const BinInfo*> c_candidate_bin_vec;
				int min_y = h - 1;
				int max_y = 0;
				int c_max_y = -1;
				set<int> by_set;
				set<int>::const_iterator it_end_y;

				for (const BinInfo* c_bin : candidate_bin_vec) {
					if (c_bin->max_x - c_bin->min_x <= ww && c_bin->max_y - c_bin->min_y <= hh) {
						c_candidate_bin_vec.emplace_back(c_bin);
						by_set.insert(c_bin->min_y);
						by_set.insert(c_bin->max_y);
						min_y = min(min_y, c_bin->min_y);
						max_y = max(max_y, c_bin->max_y);
					}
				}

				c_max_y = max(min_y, max_y - hh + 1);
				by_set.insert(c_max_y);
				it_end_y = by_set.find(c_max_y);
				for (set<int>::const_iterator it_y = by_set.begin();; it_y++) {
					int by = *it_y;
					int ey = by + hh - 1;
					vector<int> min_x_vec = vector<int>(w, 0);
					vector<int> max_x_vec = vector<int>(w, 0);
					int min_x = w - 1;
					int max_x = 0;
					int num_removed_pixel_values = 0;

					for (const BinInfo* c_bin : c_candidate_bin_vec) {
						if (by <= c_bin->min_y && c_bin->max_y <= ey) {
							min_x_vec[c_bin->min_x]++;
							max_x_vec[c_bin->max_x]++;
							min_x = min(min_x, c_bin->min_x);
							max_x = max(max_x, c_bin->max_x);
						}
					}

					for (int lx = min_x, rx = min_x; rx <= max_x; rx++) {
						if (rx - lx >= ww) {
							num_removed_pixel_values -= min_x_vec[lx];
							lx++;
						}
						num_removed_pixel_values += max_x_vec[rx];
						max_num_removed_pixel_values = max(max_num_removed_pixel_values, num_removed_pixel_values);
					}

					if (it_y == it_end_y) { break; }
				}
			}
			cout << num_pixel_values - max_num_removed_pixel_values << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 3508
final_rank: 6

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
using namespace std;


const int max_h = 1000;
const int max_w = 1000;

class BinInfo {
public:
	int count;
	int min_x;
	int max_x;
	int min_y;
	int max_y;

public:
	BinInfo() : count(0), min_x(max_w), max_x(0), min_y(max_h), max_y(0) {
		return;
	}

	~BinInfo() {
		return;
	}
};


int main() {
#if READ_FROM_FILE
	ifstream ifs("test0.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	int h = -1;
	int w = -1;
	int s = -1;
	const int max_num_pixel_values = 256;
	int num_pixel_values = 256;
	unordered_set<unsigned char> pixel_value_set;
	BinInfo pixel_hist[max_num_pixel_values];
	vector<const BinInfo*> candidate_bin_vec;

	cin >> h >> w >> s;
	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			int pixel_value = -1;

			cin >> pixel_value;
			pixel_value--;
			BinInfo& c_bin = pixel_hist[pixel_value];
			c_bin.count++;
			c_bin.min_x = min(c_bin.min_x, x);
			c_bin.max_x = max(c_bin.max_x, x);
			c_bin.min_y = min(c_bin.min_y, y);
			c_bin.max_y = max(c_bin.max_y, y);
			pixel_value_set.insert(pixel_value);
		}
	}

	if (s >= h * w) {
		cout << 0 << endl;
	}
	else {
		num_pixel_values = (int)pixel_value_set.size();
		for (const BinInfo& c_bin : pixel_hist) {
			if (0 < c_bin.count && c_bin.count <= s && c_bin.max_x - c_bin.min_x <= s && c_bin.max_y - c_bin.min_y <= s) {
				candidate_bin_vec.emplace_back(&c_bin);
			}
		}

		if (candidate_bin_vec.empty()) {
			cout << num_pixel_values << endl;
		}
		else {
			int max_hh = max(2, min(h + 1, s + 1));
			int min_hh = min(max(1, s / w), max_hh - 1);
			int max_num_removed_pixel_values = 0;

			for (int hh = min_hh; hh < max_hh; hh++) {
				int ww = s / hh;
				vector<const BinInfo*> c_candidate_bin_vec;
				int min_y = h - 1;
				int max_y = 0;
				int c_max_y = -1;
				set<int> by_set;
				set<int>::const_iterator it_end_y;

				for (const BinInfo* c_bin : candidate_bin_vec) {
					if (c_bin->max_x - c_bin->min_x <= ww && c_bin->max_y - c_bin->min_y <= hh) {
						c_candidate_bin_vec.emplace_back(c_bin);
						by_set.insert(c_bin->min_y);
						by_set.insert(c_bin->max_y);
						min_y = min(min_y, c_bin->min_y);
						max_y = max(max_y, c_bin->max_y);
					}
				}

				c_max_y = max(min_y, max_y - hh + 1);
				by_set.insert(c_max_y);
				it_end_y = by_set.find(c_max_y);
				for (set<int>::const_iterator it_y = by_set.begin();; it_y++) {
					int by = *it_y;
					int ey = by + hh - 1;
					vector<int> min_x_vec = vector<int>(w, 0);
					vector<int> max_x_vec = vector<int>(w, 0);
					int min_x = w - 1;
					int max_x = 0;
					int num_removed_pixel_values = 0;

					for (const BinInfo* c_bin : c_candidate_bin_vec) {
						if (by <= c_bin->min_y && c_bin->max_y <= ey) {
							min_x_vec[c_bin->min_x]++;
							max_x_vec[c_bin->max_x]++;
							min_x = min(min_x, c_bin->min_x);
							max_x = max(max_x, c_bin->max_x);
						}
					}

					for (int lx = min_x, rx = min_x; rx <= max_x; rx++) {
						if (rx - lx >= ww) {
							num_removed_pixel_values -= min_x_vec[lx];
							lx++;
						}
						num_removed_pixel_values += max_x_vec[rx];
						max_num_removed_pixel_values = max(max_num_removed_pixel_values, num_removed_pixel_values);
					}

					if (it_y == it_end_y) { break; }
				}
			}
			cout << num_pixel_values - max_num_removed_pixel_values << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 3508
final_rank: 5

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
using namespace std;


const int max_h = 1000;
const int max_w = 1000;

class BinInfo {
public:
	int count;
	int min_x;
	int max_x;
	int min_y;
	int max_y;

public:
	BinInfo() : count(0), min_x(max_w), max_x(0), min_y(max_h), max_y(0) {
		return;
	}

	~BinInfo() {
		return;
	}
};


int main() {
#if READ_FROM_FILE
	ifstream ifs("test0.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	int h = -1;
	int w = -1;
	int s = -1;
	const int max_num_pixel_values = 256;
	int num_pixel_values = 256;
	unordered_set<unsigned char> pixel_value_set;
	BinInfo pixel_hist[max_num_pixel_values];
	vector<const BinInfo*> candidate_bin_vec;

	cin >> h >> w >> s;
	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			int pixel_value = -1;

			cin >> pixel_value;
			pixel_value--;
			BinInfo& c_bin = pixel_hist[pixel_value];
			c_bin.count++;
			c_bin.min_x = min(c_bin.min_x, x);
			c_bin.max_x = max(c_bin.max_x, x);
			c_bin.min_y = min(c_bin.min_y, y);
			c_bin.max_y = max(c_bin.max_y, y);
			pixel_value_set.insert(pixel_value);
		}
	}

	if (s >= h * w) {
		cout << 0 << endl;
	}
	else {
		num_pixel_values = (int)pixel_value_set.size();
		for (const BinInfo& c_bin : pixel_hist) {
			if (0 < c_bin.count && c_bin.count <= s && c_bin.max_x - c_bin.min_x <= s && c_bin.max_y - c_bin.min_y <= s) {
				candidate_bin_vec.emplace_back(&c_bin);
			}
		}

		if (candidate_bin_vec.empty()) {
			cout << num_pixel_values << endl;
		}
		else {
			int max_hh = max(2, min(h + 1, s + 1));
			int min_hh = min(max(1, s / w), max_hh - 1);
			int max_num_removed_pixel_values = 0;

			for (int hh = min_hh; hh < max_hh; hh++) {
				int ww = s / hh;
				vector<const BinInfo*> c_candidate_bin_vec;
				int min_y = h - 1;
				int max_y = 0;
				int c_max_y = -1;
				set<int> by_set;
				set<int>::const_iterator it_end_y;

				for (const BinInfo* c_bin : candidate_bin_vec) {
					if (c_bin->max_x - c_bin->min_x <= ww && c_bin->max_y - c_bin->min_y <= hh) {
						c_candidate_bin_vec.emplace_back(c_bin);
						by_set.insert(c_bin->min_y);
						by_set.insert(c_bin->max_y);
						min_y = min(min_y, c_bin->min_y);
						max_y = max(max_y, c_bin->max_y);
					}
				}

				c_max_y = max(min_y, max_y - hh + 1);
				by_set.insert(c_max_y);
				it_end_y = by_set.find(c_max_y);
				for (set<int>::const_iterator y_it = by_set.begin();; y_it++) {
					int by = *y_it;
					int ey = by + hh - 1;
					vector<int> min_x_vec = vector<int>(w, 0);
					vector<int> max_x_vec = vector<int>(w, 0);
					int min_x = w - 1;
					int max_x = 0;
					int num_removed_pixel_values = 0;

					for (const BinInfo* c_bin : c_candidate_bin_vec) {
						if (by <= c_bin->min_y && c_bin->max_y <= ey) {
							min_x_vec[c_bin->min_x]++;
							max_x_vec[c_bin->max_x]++;
							min_x = min(min_x, c_bin->min_x);
							max_x = max(max_x, c_bin->max_x);
						}
					}

					for (int lx = min_x, rx = min_x; rx <= max_x; rx++) {
						if (rx - lx >= ww) {
							num_removed_pixel_values -= min_x_vec[lx];
							lx++;
						}
						num_removed_pixel_values += max_x_vec[rx];
						max_num_removed_pixel_values = max(max_num_removed_pixel_values, num_removed_pixel_values);
					}

					if (y_it == it_end_y) { break; }
				}
			}
			cout << num_pixel_values - max_num_removed_pixel_values << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 3500
final_rank: 4

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
using namespace std;


const int max_h = 1000;
const int max_w = 1000;

class BinInfo {
public:
	int count;
	int min_x;
	int max_x;
	int min_y;
	int max_y;

public:
	BinInfo() : count(0), min_x(max_w), max_x(0), min_y(max_h), max_y(0) {
		return;
	}

	~BinInfo() {
		return;
	}
};


int main() {
#if READ_FROM_FILE
	ifstream ifs("test0.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	int h = -1;
	int w = -1;
	int s = -1;
	const int max_num_pixel_values = 256;
	int num_pixel_values = 256;
	unordered_set<unsigned char> pixel_value_set;
	BinInfo pixel_hist[max_num_pixel_values];
	vector<const BinInfo*> candidate_bin_vec;

	cin >> h >> w >> s;
	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			int pixel_value = -1;

			cin >> pixel_value;
			pixel_value--;
			BinInfo& c_bin = pixel_hist[pixel_value];

			c_bin.count++;
			c_bin.min_x = min(c_bin.min_x, x);
			c_bin.max_x = max(c_bin.max_x, x);
			c_bin.min_y = min(c_bin.min_y, y);
			c_bin.max_y = max(c_bin.max_y, y);
			pixel_value_set.insert(pixel_value);
		}
	}

	if (s >= h * w) {
		cout << 0 << endl;
	}
	else {
		num_pixel_values = (int)pixel_value_set.size();
		for (const BinInfo& c_bin : pixel_hist) {
			if (0 < c_bin.count && c_bin.count <= s && c_bin.max_x - c_bin.min_x <= s && c_bin.max_y - c_bin.min_y <= s) {
				candidate_bin_vec.emplace_back(&c_bin);
			}
		}
		if (candidate_bin_vec.empty()) {
			cout << num_pixel_values << endl;
		}
		else {
			int max_hh = max(2, min(h + 1, s + 1));
			int min_hh = min(max(1, s / w), max_hh - 1);
			int max_num_removed_pixel_values = 0;

			for (int hh = min_hh; hh < max_hh; hh++) {
				//if (max_num_removed_pixel_values == num_pixel_values - 1) { break; }

				int ww = s / hh;
				vector<const BinInfo*> c_candidate_bin_vec;
				int min_y = h - 1;
				int max_y = 0;
				int c_max_y = -1;
				set<int> by_set;
				set<int>::const_iterator it_end_y;

				for (const BinInfo* c_bin : candidate_bin_vec) {
					if (c_bin->max_x - c_bin->min_x <= ww && c_bin->max_y - c_bin->min_y <= hh) {
						c_candidate_bin_vec.emplace_back(c_bin);
						by_set.insert(c_bin->min_y);
						by_set.insert(c_bin->max_y);
						min_y = min(min_y, c_bin->min_y);
						max_y = max(max_y, c_bin->max_y);
					}
				}
				c_max_y = max(min_y, max_y - hh + 1);
				by_set.insert(c_max_y);
				it_end_y = by_set.find(c_max_y);

				for (set<int>::const_iterator y_it = by_set.begin();; y_it++) {
					//if (max_num_removed_pixel_values == num_pixel_values - 1) { break; }
					int by = *y_it;
					int ey = by + hh - 1;
					vector<int> min_x_vec = vector<int>(w, 0);
					vector<int> max_x_vec = vector<int>(w, 0);
					int min_x = w - 1;
					int max_x = 0;
					int num_removed_pixel_values = 0;

					for (const BinInfo* c_bin : c_candidate_bin_vec) {
						if (by <= c_bin->min_y && c_bin->max_y <= ey) {
							min_x_vec[c_bin->min_x]++;
							max_x_vec[c_bin->max_x]++;
							min_x = min(min_x, c_bin->min_x);
							max_x = max(max_x, c_bin->max_x);
						}
					}
					for (int lx = min_x, rx = min_x; rx <= max_x; rx++) {
						if (rx - lx >= ww) {
							num_removed_pixel_values -= min_x_vec[lx];
							lx++;
						}
						num_removed_pixel_values += max_x_vec[rx];
						max_num_removed_pixel_values = max(max_num_removed_pixel_values, num_removed_pixel_values);
					}
					if (y_it == it_end_y) { break; }
				}
			}
			cout << num_pixel_values - max_num_removed_pixel_values << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 3492
final_rank: 3

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
using namespace std;


const int max_h = 1000;
const int max_w = 1000;

class BinInfo {
public:
	int count;
	int min_x;
	int max_x;
	int min_y;
	int max_y;

public:
	BinInfo() : count(0), min_x(max_w), max_x(0), min_y(max_h), max_y(0) {
		return;
	}

	~BinInfo() {
		return;
	}
};


int main() {
#if READ_FROM_FILE
	ifstream ifs("test0.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	int h = -1;
	int w = -1;
	int s = -1;
	int pixel_value = -1;
	const int max_num_pixel_values = 256;
	int num_pixel_values = 256;
	unordered_set<unsigned char> pixel_value_set;
	BinInfo pixel_hist[max_num_pixel_values];
	vector<const BinInfo*> candidate_bin_vec;

	cin >> h >> w >> s;
	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			cin >> pixel_value;
			pixel_value--;
			BinInfo& c_bin = pixel_hist[pixel_value];

			c_bin.count++;
			c_bin.min_x = min(c_bin.min_x, x);
			c_bin.max_x = max(c_bin.max_x, x);
			c_bin.min_y = min(c_bin.min_y, y);
			c_bin.max_y = max(c_bin.max_y, y);
			pixel_value_set.insert(pixel_value);
		}
	}

	if (s >= h * w) {
		cout << 0 << endl;
	}
	else {
		num_pixel_values = (int)pixel_value_set.size();
		for (const BinInfo& c_bin : pixel_hist) {
			if (0 < c_bin.count && c_bin.count <= s && c_bin.max_x - c_bin.min_x <= s && c_bin.max_y - c_bin.min_y <= s) {
				candidate_bin_vec.emplace_back(&c_bin);
			}
		}
		if (candidate_bin_vec.empty()) {
			cout << num_pixel_values << endl;
		}
		else {
			int max_hh = max(2, min(h + 1, s + 1));
			int min_hh = min(max(1, s / w), max_hh - 1);
			int max_num_removed_pixel_values = 0;

			for (int hh = min_hh; hh < max_hh; hh++) {
				if (max_num_removed_pixel_values == num_pixel_values - 1) { break; }

				int ww = s / hh;
				vector<const BinInfo*> c_candidate_bin_vec;
				int min_y = h - 1;
				int max_y = 0;
				int c_max_y = -1;
				set<int> by_set;
				set<int>::const_iterator it_end_y;

				for (const BinInfo* c_bin : candidate_bin_vec) {
					if (c_bin->max_x - c_bin->min_x <= ww && c_bin->max_y - c_bin->min_y <= hh) {
						c_candidate_bin_vec.emplace_back(c_bin);
						by_set.insert(c_bin->min_y);
						by_set.insert(c_bin->max_y);
						min_y = min(min_y, c_bin->min_y);
						max_y = max(max_y, c_bin->max_y);
					}
				}
				c_max_y = max(min_y, max_y - hh + 1);
				by_set.insert(c_max_y);
				it_end_y = by_set.find(c_max_y);

				for (set<int>::const_iterator y_it = by_set.begin();; y_it++) {
					if (max_num_removed_pixel_values == num_pixel_values - 1) { break; }
					int by = *y_it;
					int ey = by + hh - 1;
					vector<int> min_x_vec = vector<int>(w, 0);
					vector<int> max_x_vec = vector<int>(w, 0);
					int min_x = w - 1;
					int max_x = 0;
					int num_removed_pixel_values = 0;

					for (const BinInfo* c_bin : c_candidate_bin_vec) {
						if (by <= c_bin->min_y && c_bin->max_y <= ey) {
							min_x_vec[c_bin->min_x]++;
							max_x_vec[c_bin->max_x]++;
							min_x = min(min_x, c_bin->min_x);
							max_x = max(max_x, c_bin->max_x);
						}
					}
					for (int lx = min_x, rx = min_x; rx <= max_x; rx++) {
						if (rx - lx >= ww) {
							num_removed_pixel_values -= min_x_vec[lx];
							lx++;
						}
						num_removed_pixel_values += max_x_vec[rx];
						max_num_removed_pixel_values = max(max_num_removed_pixel_values, num_removed_pixel_values);
					}
					if (y_it == it_end_y) { break; }
				}
			}
			cout << num_pixel_values - max_num_removed_pixel_values << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 3476
final_rank: 2

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
using namespace std;


const int max_h = 1000;
const int max_w = 1000;

class BinInfo {
public:
	int count;
	int min_x;
	int max_x;
	int min_y;
	int max_y;

public:
	BinInfo() : count(0), min_x(max_w), max_x(0), min_y(max_h), max_y(0) {
		return;
	}

	~BinInfo() {
		return;
	}
};


int main() {
#if READ_FROM_FILE
	ifstream ifs("test0.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	int h = -1;
	int w = -1;
	int s = -1;
	int pixel_value = -1;
	const int max_num_pixel_values = 256;
	int num_pixel_values = 256;
	unordered_set<unsigned char> pixel_value_set;
	BinInfo pixel_hist[max_num_pixel_values];
	vector<const BinInfo*> candidate_bin_vec;

	cin >> h >> w >> s;
	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			cin >> pixel_value;
			pixel_value--;
			BinInfo& c_bin = pixel_hist[pixel_value];

			c_bin.count++;
			c_bin.min_x = min(c_bin.min_x, x);
			c_bin.max_x = max(c_bin.max_x, x);
			c_bin.min_y = min(c_bin.min_y, y);
			c_bin.max_y = max(c_bin.max_y, y);
			pixel_value_set.insert(pixel_value);
		}
	}

	if (s >= h * w) {
		cout << 0 << endl;
	}
	else {
		num_pixel_values = (int)pixel_value_set.size();
		for (const BinInfo& c_bin : pixel_hist) {
			if (0 < c_bin.count && c_bin.count <= s && c_bin.max_x - c_bin.min_x <= s && c_bin.max_y - c_bin.min_y <= s) {
				candidate_bin_vec.emplace_back(&c_bin);
			}
		}
		if (candidate_bin_vec.empty()) {
			cout << num_pixel_values << endl;
		}
		else {
			int max_hh = max(2, min(h + 1, s + 1));
			int min_hh = min(max(1, s / w), max_hh - 1);
			int max_num_removed_pixel_values = 0;
			vector<const BinInfo*> c_candidate_bin_vec;
			set<int> by_set;
			vector<int> min_x_vec;
			vector<int> max_x_vec;
			int min_x = w - 1;
			int max_x = 0;
			int min_y = h - 1;
			int max_y = 0;
			int c_max_y = -1;
			set<int>::const_iterator it_end_y;
			int by = -1;
			int ey = -1;
			int num_removed_pixel_values = -1;
			int ww = -1;

			for (int hh = min_hh; hh < max_hh; hh++) {
				if (max_num_removed_pixel_values == num_pixel_values - 1) { break; }

				ww = s / hh;
				c_candidate_bin_vec.clear();
				min_y = h - 1;
				max_y = 0;

				for (const BinInfo* c_bin : candidate_bin_vec) {
					if (c_bin->max_x - c_bin->min_x <= ww && c_bin->max_y - c_bin->min_y <= hh) {
						c_candidate_bin_vec.emplace_back(c_bin);
						by_set.insert(c_bin->min_y);
						by_set.insert(c_bin->max_y);
						min_y = min(min_y, c_bin->min_y);
						max_y = max(max_y, c_bin->max_y);
					}
				}
				c_max_y = max(min_y, max_y - hh + 1);
				by_set.insert(c_max_y);
				it_end_y = by_set.find(c_max_y);

				for (set<int>::const_iterator y_it = by_set.begin();; y_it++) {
					if (max_num_removed_pixel_values == num_pixel_values - 1) { break; }
					by = *y_it;
					ey = by + hh - 1;
					min_x_vec = vector<int>(w, 0);
					max_x_vec = vector<int>(w, 0);
					min_x = w - 1;
					max_x = 0;
					for (const BinInfo* c_bin : c_candidate_bin_vec) {
						if (by <= c_bin->min_y && c_bin->max_y <= ey) {
							min_x_vec[c_bin->min_x]++;
							max_x_vec[c_bin->max_x]++;
							min_x = min(min_x, c_bin->min_x);
							max_x = max(max_x, c_bin->max_x);
						}
					}
					num_removed_pixel_values = 0;
					for (int lx = min_x, rx = min_x; rx <= max_x; rx++) {
						if (rx - lx >= ww) {
							num_removed_pixel_values -= min_x_vec[lx];
							lx++;
						}
						num_removed_pixel_values += max_x_vec[rx];
						max_num_removed_pixel_values = max(max_num_removed_pixel_values, num_removed_pixel_values);
					}
					if (y_it == it_end_y) { break; }
				}
			}
			cout << num_pixel_values - max_num_removed_pixel_values << endl;
		}
	}

	return 0;
}
accuracy: 1
time_ms: 240
memory_kb: 3520
final_rank: 7

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
using namespace std;


const int max_h = 1000;
const int max_w = 1000;

class BinInfo {
public:
	int count;
	int min_x;
	int max_x;
	int min_y;
	int max_y;

public:
	BinInfo() : count(0), min_x(max_w), max_x(0), min_y(max_h), max_y(0) {
		return;
	}

	~BinInfo() {
		return;
	}
};


int main() {
#if READ_FROM_FILE
	ifstream ifs("test0.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	int h = -1;
	int w = -1;
	int s = -1;
	int pixel_value = -1;
	const int max_num_pixel_values = 256;
	int num_pixel_values = 256;
	unordered_set<unsigned char> pixel_value_set;
	BinInfo pixel_hist[max_num_pixel_values];
	vector<const BinInfo*> candidate_bin_vec;

	cin >> h >> w >> s;
	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			cin >> pixel_value;
			pixel_value--;
			BinInfo& c_bin = pixel_hist[pixel_value];

			c_bin.count++;
			c_bin.min_x = min(c_bin.min_x, x);
			c_bin.max_x = max(c_bin.max_x, x);
			c_bin.min_y = min(c_bin.min_y, y);
			c_bin.max_y = max(c_bin.max_y, y);
			pixel_value_set.insert(pixel_value);
		}
	}

	if (s >= h * w) {
		cout << 0 << endl;
	}
	else {
		num_pixel_values = (int)pixel_value_set.size();
		for (const BinInfo& c_bin : pixel_hist) {
			if (0 < c_bin.count && c_bin.count <= s && c_bin.max_x - c_bin.min_x <= s && c_bin.max_y - c_bin.min_y <= s) {
				candidate_bin_vec.emplace_back(&c_bin);
			}
		}
		if (candidate_bin_vec.empty()) {
			cout << num_pixel_values << endl;
		}
		else {
			int max_hh = max(2, min(h + 1, s + 1));
			int min_hh = min(max(1, s / w), max_hh - 1);
			int max_num_removed_pixel_values = 0;
			vector<const BinInfo*> c_candidate_bin_vec;
			set<int> by_set;
			vector<int> min_x_vec;
			vector<int> max_x_vec;
			int min_x = w - 1;
			int max_x = 0;
			int min_y = h - 1;
			int max_y = 0;
			int c_max_y = -1;
			set<int>::iterator it_end_y;
			int by = -1;
			int ey = -1;
			int num_removed_pixel_values = -1;
			int ww = -1;

			for (int hh = min_hh; hh < max_hh; hh++) {
				if (max_num_removed_pixel_values == num_pixel_values - 1) { break; }

				ww = s / hh;
				c_candidate_bin_vec.clear();
				min_y = h - 1;
				max_y = 0;

				for (const BinInfo* c_bin : candidate_bin_vec) {
					if (c_bin->max_x - c_bin->min_x <= ww && c_bin->max_y - c_bin->min_y <= hh) {
						c_candidate_bin_vec.emplace_back(c_bin);
						by_set.insert(c_bin->min_y);
						by_set.insert(c_bin->max_y);
						min_y = min(min_y, c_bin->min_y);
						max_y = max(max_y, c_bin->max_y);
					}
				}
				c_max_y = max(min_y, max_y - hh + 1);
				by_set.insert(c_max_y);
				it_end_y = by_set.find(c_max_y);

				for (set<int>::iterator y_it = by_set.begin();; y_it++) {
					if (max_num_removed_pixel_values == num_pixel_values - 1) { break; }
					by = *y_it;
					ey = by + hh - 1;
					min_x_vec = vector<int>(w, 0);
					max_x_vec = vector<int>(w, 0);
					min_x = w - 1;
					max_x = 0;
					for (const BinInfo* c_bin : c_candidate_bin_vec) {
						if (by <= c_bin->min_y && c_bin->max_y <= ey) {
							min_x_vec[c_bin->min_x]++;
							max_x_vec[c_bin->max_x]++;
							min_x = min(min_x, c_bin->min_x);
							max_x = max(max_x, c_bin->max_x);
						}
					}
					num_removed_pixel_values = 0;
					for (int lx = min_x, rx = min_x; rx <= max_x; rx++) {
						if (rx - lx >= ww) {
							num_removed_pixel_values -= min_x_vec[lx];
							lx++;
						}
						num_removed_pixel_values += max_x_vec[rx];
						max_num_removed_pixel_values = max(max_num_removed_pixel_values, num_removed_pixel_values);
					}
					if (y_it == it_end_y) { break; }
				}
			}
			cout << num_pixel_values - max_num_removed_pixel_values << endl;
		}
	}

	return 0;
}
accuracy: 1
time_ms: 250
memory_kb: 3504
final_rank: 8

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
using namespace std;


const int max_h = 1000;
const int max_w = 1000;

class BinInfo {
public:
	int count;
	int min_x;
	int max_x;
	int min_y;
	int max_y;

public:
	BinInfo() : count(0), min_x(max_w), max_x(0), min_y(max_h), max_y(0) {
		return;
	}

	~BinInfo() {
		return;
	}
};


int main() {
#if READ_FROM_FILE
	ifstream ifs("test0.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	int h = -1;
	int w = -1;
	int s = -1;
	int buf = -1;
	unsigned char image[max_h][max_w];

	cin >> h >> w >> s;
	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			cin >> buf;
			image[y][x] = buf - 1;
		}
	}

	if (s >= h * w) {
		cout << 0 << endl;
	}
	else {
		const int max_num_pixel_values = 256;
		int num_pixel_values = 256;
		unordered_set<unsigned char> pixel_value_set;
		BinInfo pixel_hist[max_num_pixel_values];
		vector<const BinInfo*> candidate_bin_vec;

		for (int y = 0; y < h; y++) {
			for (int x = 0; x < w; x++) {
				const unsigned char& pixel_value = image[y][x];
				BinInfo& c_bin = pixel_hist[pixel_value];

				c_bin.count++;
				c_bin.min_x = min(c_bin.min_x, x);
				c_bin.max_x = max(c_bin.max_x, x);
				c_bin.min_y = min(c_bin.min_y, y);
				c_bin.max_y = max(c_bin.max_y, y);
				pixel_value_set.insert(pixel_value);
			}
		}

		num_pixel_values = (int)pixel_value_set.size();
		for (const BinInfo& c_bin : pixel_hist) {
			if (0 < c_bin.count && c_bin.count <= s && c_bin.max_x - c_bin.min_x <= s && c_bin.max_y - c_bin.min_y <= s) {
				candidate_bin_vec.emplace_back(&c_bin);
			}
		}

		if (candidate_bin_vec.empty()) {
			cout << num_pixel_values << endl;
		}
		else {
			int max_hh = max(2, min(h + 1, s + 1));
			int min_hh = min(max(1, s / w), max_hh - 1);
			int max_num_removed_pixel_values = 0;
			vector<const BinInfo*> c_candidate_bin_vec;
			set<int> by_set;
			vector<int> min_x_vec;
			vector<int> max_x_vec;
			int min_x = w - 1;
			int max_x = 0;
			int min_y = h - 1;
			int max_y = 0;
			int c_max_y = -1;
			set<int>::iterator it_end_y;
			int by = -1;
			int ey = -1;
			int num_removed_pixel_values = -1;
			int ww = -1;

			for (int hh = min_hh; hh < max_hh; hh++) {
				if (max_num_removed_pixel_values == num_pixel_values - 1) { break; }

				ww = s / hh;
				c_candidate_bin_vec.clear();
				min_y = h - 1;
				max_y = 0;

				for (const BinInfo* c_bin : candidate_bin_vec) {
					if (c_bin->max_x - c_bin->min_x <= ww && c_bin->max_y - c_bin->min_y <= hh) {
						c_candidate_bin_vec.emplace_back(c_bin);
						by_set.insert(c_bin->min_y);
						by_set.insert(c_bin->max_y);
						min_y = min(min_y, c_bin->min_y);
						max_y = max(max_y, c_bin->max_y);
					}
				}
				c_max_y = max(min_y, max_y - hh + 1);
				by_set.insert(c_max_y);
				it_end_y = by_set.find(c_max_y);

				for (set<int>::iterator y_it = by_set.begin();; y_it++) {
					if (max_num_removed_pixel_values == num_pixel_values - 1) { break; }
					by = *y_it;
					ey = by + hh - 1;
					min_x_vec = vector<int>(w, 0);
					max_x_vec = vector<int>(w, 0);
					min_x = w - 1;
					max_x = 0;
					for (const BinInfo* c_bin : c_candidate_bin_vec) {
						if (by <= c_bin->min_y && c_bin->max_y <= ey) {
							min_x_vec[c_bin->min_x]++;
							max_x_vec[c_bin->max_x]++;
							min_x = min(min_x, c_bin->min_x);
							max_x = max(max_x, c_bin->max_x);
						}
					}
					num_removed_pixel_values = 0;
					for (int lx = min_x, rx = min_x; rx <= max_x; rx++) {
						if (rx - lx >= ww) {
							num_removed_pixel_values -= min_x_vec[lx];
							lx++;
						}
						num_removed_pixel_values += max_x_vec[rx];
						max_num_removed_pixel_values = max(max_num_removed_pixel_values, num_removed_pixel_values);
					}
					if (y_it == it_end_y) { break; }
				}
			}
			cout << num_pixel_values - max_num_removed_pixel_values << endl;
		}
	}

	return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 4492
final_rank: 9

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
using namespace std;


class BinInfo {
public:
	int count;
	int min_x;
	int max_x;
	int min_y;
	int max_y;

public:
	BinInfo(int count, int min_x, int max_x, int min_y, int max_y) : count(count), min_x(min_x), max_x(max_x), min_y(min_y), max_y(max_y) {
		return;
	}

	~BinInfo() {
		return;
	}
};


int main() {
#if READ_FROM_FILE
	ifstream ifs("test0.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	int h = -1;
	int w = -1;
	int s = -1;
	int buf = -1;
	const int max_h = 1000;
	const int max_w = 1000;
	unsigned char image[max_h][max_w];

	cin >> h >> w >> s;
	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			cin >> buf;
			image[y][x] = buf - 1;
		}
	}

	if (s >= h * w) {
		cout << 0 << endl;
	}
	else {
		int num_pixel_values = 256;
		unordered_set<unsigned char> pixel_value_set;
		vector<BinInfo> pixel_hist(num_pixel_values, BinInfo(0, w - 1, 0, h - 1, 0));
		vector<const BinInfo*> candidate_bin_vec;


		for (int y = 0; y < h; y++) {
			for (int x = 0; x < w; x++) {
				const unsigned char& pixel_value = image[y][x];
				BinInfo& c_bin = pixel_hist[pixel_value];

				c_bin.count++;
				c_bin.min_x = min(c_bin.min_x, x);
				c_bin.max_x = max(c_bin.max_x, x);
				c_bin.min_y = min(c_bin.min_y, y);
				c_bin.max_y = max(c_bin.max_y, y);
				pixel_value_set.insert(pixel_value);
			}
		}

		num_pixel_values = (int)pixel_value_set.size();

		for (const BinInfo& c_bin : pixel_hist) {
			if (0 < c_bin.count && c_bin.count <= s && c_bin.max_x - c_bin.min_x <= s && c_bin.max_y - c_bin.min_y <= s) {
				candidate_bin_vec.emplace_back(&c_bin);
			}
		}

		if (candidate_bin_vec.empty()) {
			cout << num_pixel_values << endl;
		}
		else {
			int max_hh = max(2, min(h + 1, s + 1));
			int min_hh = min(max(1, s / w), max_hh - 1);
			int max_num_removed_pixel_values = 0;
			vector<const BinInfo*> c_candidate_bin_vec;
			vector<int> min_x_vec;
			vector<int> max_x_vec;
			set<int> by_set;
			int min_x = w - 1;
			int max_x = 0;
			int min_y = h - 1;
			int max_y = 0;
			int c_max_y = -1;
			set<int>::iterator it_end_y;
			int by = -1;
			int ey = -1;
			int num_removed_pixel_values = -1;
			int ww = -1;

			for (int hh = min_hh; hh < max_hh; hh++) {
				if (max_num_removed_pixel_values == num_pixel_values - 1) { break; }

				ww = s / hh;
				c_candidate_bin_vec.clear();
				min_y = h - 1;
				max_y = 0;

				for (const BinInfo* c_bin : candidate_bin_vec) {
					if (c_bin->max_x - c_bin->min_x <= ww && c_bin->max_y - c_bin->min_y <= hh) {
						c_candidate_bin_vec.emplace_back(c_bin);
						by_set.insert(c_bin->min_y);
						by_set.insert(c_bin->max_y);
						min_y = min(min_y, c_bin->min_y);
						max_y = max(max_y, c_bin->max_y);
					}
				}
				c_max_y = max(min_y, max_y - hh + 1);
				by_set.insert(c_max_y);
				it_end_y = by_set.find(c_max_y);

				for (set<int>::iterator y_it = by_set.begin();; y_it++) {
					if (max_num_removed_pixel_values == num_pixel_values - 1) { break; }
					by = *y_it;
					ey = by + hh - 1;
					min_x_vec = vector<int>(w, 0);
					max_x_vec = vector<int>(w, 0);
					min_x = w - 1;
					max_x = 0;
					for (const BinInfo* c_bin : c_candidate_bin_vec) {
						if (by <= c_bin->min_y && c_bin->max_y <= ey) {
							min_x_vec[c_bin->min_x]++;
							max_x_vec[c_bin->max_x]++;
							min_x = min(min_x, c_bin->min_x);
							max_x = max(max_x, c_bin->max_x);
						}
					}
					num_removed_pixel_values = 0;
					for (int lx = min_x, rx = min_x; rx <= max_x; rx++) {
						if (rx - lx >= ww) {
							num_removed_pixel_values -= min_x_vec[lx];
							lx++;
						}
						num_removed_pixel_values += max_x_vec[rx];
						max_num_removed_pixel_values = max(max_num_removed_pixel_values, num_removed_pixel_values);
					}
					if (y_it == it_end_y) { break; }
				}
			}
			cout << num_pixel_values - max_num_removed_pixel_values << endl;
		}
	}

	return 0;
}
accuracy: 1
time_ms: 230
memory_kb: 4476
final_rank: 11

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
using namespace std;

class BinInfo {
public:
	int count;
	int min_x;
	int max_x;
	int min_y;
	int max_y;

public:
	BinInfo(int count, int min_x, int max_x, int min_y, int max_y) : count(count), min_x(min_x), max_x(max_x), min_y(min_y), max_y(max_y) {
		return;
	}

	~BinInfo() {
		return;
	}
};


int main() {
#if READ_FROM_FILE
	ifstream ifs("test0.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	int h = -1;
	int w = -1;
	int s = -1;
	int buf = -1;
	const int max_h = 1000;
	const int max_w = 1000;
	unsigned char image[max_h][max_w];

	cin >> h >> w >> s;
	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			cin >> buf;
			image[y][x] = buf - 1;
		}
	}

	if (s >= h * w) {
		cout << 0 << endl;
	}
	else {
		int num_pixel_values = 256;
		unordered_set<unsigned char> pixel_value_set;
		vector<BinInfo> pixel_hist(num_pixel_values, BinInfo(0, w - 1, 0, h - 1, 0));
		vector<const BinInfo*> candidate_bin_vec;


		for (int y = 0; y < h; y++) {
			for (int x = 0; x < w; x++) {
				const unsigned char& pixel_value = image[y][x];
				BinInfo& c_bin = pixel_hist[pixel_value];

				c_bin.count++;
				c_bin.min_x = min(c_bin.min_x, x);
				c_bin.max_x = max(c_bin.max_x, x);
				c_bin.min_y = min(c_bin.min_y, y);
				c_bin.max_y = max(c_bin.max_y, y);
				pixel_value_set.insert(pixel_value);
			}
		}

		num_pixel_values = (int)pixel_value_set.size();

		for (const BinInfo& c_bin : pixel_hist) {
			if (0 < c_bin.count && c_bin.count <= s && c_bin.max_x - c_bin.min_x <= s && c_bin.max_y - c_bin.min_y <= s) {
				candidate_bin_vec.emplace_back(&c_bin);
			}
		}

		if (candidate_bin_vec.empty()) {
			cout << num_pixel_values << endl;
		}
		else {
			int max_hh = max(2, min(h + 1, s + 1));
			int min_hh = min(max(1, s / w), max_hh - 1);
			int max_num_removed_pixel_values = 0;
			vector<const BinInfo*> c_candidate_bin_vec;
			vector<int> min_x_vec;
			vector<int> max_x_vec;
			set<int> by_set;
			int min_x = w - 1;
			int max_x = 0;
			int min_y = h - 1;
			int max_y = 0;
			int c_max_y = -1;
			set<int>::iterator it_end_y;
			int by = -1;
			int ey = -1;
			int num_removed_pixel_values = -1;
			int ww = -1;

			for (int hh = min_hh; hh < max_hh; hh++) {
				if (max_num_removed_pixel_values == num_pixel_values - 1) { break; }
				
				ww = s / hh;
				c_candidate_bin_vec.clear();
				min_y = h - 1;
				max_y = 0;

				for (const BinInfo* c_bin : candidate_bin_vec) {
					if (c_bin->max_x - c_bin->min_x <= ww && c_bin->max_y - c_bin->min_y <= hh) {
						c_candidate_bin_vec.emplace_back(c_bin);
						by_set.insert(c_bin->min_y);
						by_set.insert(c_bin->max_y);
						min_y = min(min_y, c_bin->min_y);
						max_y = max(max_y, c_bin->max_y);
					}
				}
				c_max_y = max(min_y, max_y - hh + 1);
				by_set.insert(c_max_y);
				it_end_y = by_set.find(c_max_y);

				for (set<int>::iterator y_it = by_set.begin();; y_it++) {
					if (max_num_removed_pixel_values == num_pixel_values - 1) { break; }
					by = *y_it;
					ey = by + hh - 1;

					min_x_vec = vector<int>(w, 0);
					max_x_vec = vector<int>(w, 0);
					min_x = w - 1;
					max_x = 0;
					for (const BinInfo* c_bin : c_candidate_bin_vec) {
						if (by <= c_bin->min_y && c_bin->max_y <= ey) {
							min_x_vec[c_bin->min_x]++;
							max_x_vec[c_bin->max_x]++;
							min_x = min(min_x, c_bin->min_x);
							max_x = max(max_x, c_bin->max_x);
						}

					}
					num_removed_pixel_values = 0;
					for (int lx = min_x, rx = min_x; rx <= max_x; rx++) {
						if (rx - lx >= ww) {
							num_removed_pixel_values -= min_x_vec[lx];
							lx++;
						}
						num_removed_pixel_values += max_x_vec[rx];
						max_num_removed_pixel_values = max(max_num_removed_pixel_values, num_removed_pixel_values);
					}
					if (y_it == it_end_y) { break; }
				}
			}
			cout << num_pixel_values - max_num_removed_pixel_values << endl;
		}
	}

	return 0;
}
accuracy: 1
time_ms: 230
memory_kb: 4460
final_rank: 10

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
using namespace std;

class BinInfo {
public:
	int count;
	int min_x;
	int max_x;
	int min_y;
	int max_y;

public:
	BinInfo(int count, int min_x, int max_x, int min_y, int max_y) : count(count), min_x(min_x), max_x(max_x), min_y(min_y), max_y(max_y) {
		return;
	}

	~BinInfo() {
		return;
	}
};


int main() {
#if READ_FROM_FILE
	ifstream ifs("test0.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	int h = -1;
	int w = -1;
	int s = -1;
	int buf = -1;
	const int max_h = 1000;
	const int max_w = 1000;
	unsigned char image[max_h][max_w];

	cin >> h >> w >> s;
	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			cin >> buf;
			image[y][x] = buf - 1;
		}
	}

	if (s >= h * w) {
		cout << 0 << endl;
	}
	else {
		int num_pixel_values = 256;
		unordered_set<unsigned char> pixel_value_set;
		vector<BinInfo> pixel_hist(num_pixel_values, BinInfo(0, w - 1, 0, h - 1, 0));
		vector<const BinInfo*> candidate_bin_vec;


		for (int y = 0; y < h; y++) {
			for (int x = 0; x < w; x++) {
				const unsigned char& pixel_value = image[y][x];
				BinInfo& c_bin = pixel_hist[pixel_value];

				c_bin.count++;
				c_bin.min_x = min(c_bin.min_x, x);
				c_bin.max_x = max(c_bin.max_x, x);
				c_bin.min_y = min(c_bin.min_y, y);
				c_bin.max_y = max(c_bin.max_y, y);
				pixel_value_set.insert(pixel_value);
			}
		}

		num_pixel_values = (int)pixel_value_set.size();

		for (const BinInfo& c_bin : pixel_hist) {
			if (0 < c_bin.count && c_bin.count <= s && c_bin.max_x - c_bin.min_x <= s && c_bin.max_y - c_bin.min_y <= s) {
				candidate_bin_vec.emplace_back(&c_bin);
			}
		}

		if (candidate_bin_vec.empty()) {
			cout << num_pixel_values << endl;
		}
		else {
			int max_hh = max(2, min(h + 1, s + 1));
			int min_hh = min(max(1, s / w), max_hh - 1);
			int max_num_removed_pixel_values = 0;
			vector<const BinInfo*> c_candidate_bin_vec;
			vector<const BinInfo*> n_candidate_bin_vec;
			vector<int> min_x_vec;
			vector<int> max_x_vec;
			set<int> by_set;
			int min_x = w - 1;
			int max_x = 0;
			int min_y = h - 1;
			int max_y = 0;
			int c_max_y = -1;
			set<int>::iterator e_idx_y;
			int by = -1;
			int ey = -1;
			int num_removed_pixel_values = -1;
			int ww = -1;

			for (int hh = min_hh; hh < max_hh; hh++) {
				if (max_num_removed_pixel_values == num_pixel_values - 1) { break; }
				
				ww = s / hh;
				c_candidate_bin_vec.clear();
				min_y = h - 1;
				max_y = 0;

				for (const BinInfo* c_bin : candidate_bin_vec) {
					if (c_bin->max_x - c_bin->min_x <= ww && c_bin->max_y - c_bin->min_y <= hh) {
						c_candidate_bin_vec.emplace_back(c_bin);
						by_set.insert(c_bin->min_y);
						by_set.insert(c_bin->max_y);
						min_y = min(min_y, c_bin->min_y);
						max_y = max(max_y, c_bin->max_y);
					}
				}
				c_max_y = max(min_y, max_y - hh + 1);
				by_set.insert(c_max_y);
				e_idx_y = by_set.find(c_max_y);

				for (set<int>::iterator y_it = by_set.begin();; y_it++) {
					if (max_num_removed_pixel_values == num_pixel_values - 1) { break; }
					by = *y_it;
					ey = by + hh - 1;

					n_candidate_bin_vec.clear();
					min_x_vec = vector<int>(w, 0);
					max_x_vec = vector<int>(w, 0);
					min_x = w - 1;
					max_x = 0;
					for (const BinInfo* c_bin : c_candidate_bin_vec) {
						if (by <= c_bin->min_y && c_bin->max_y <= ey) {
							n_candidate_bin_vec.emplace_back(c_bin);
							min_x_vec[c_bin->min_x]++;
							max_x_vec[c_bin->max_x]++;
							min_x = min(min_x, c_bin->min_x);
							max_x = max(max_x, c_bin->max_x);
						}

					}
					num_removed_pixel_values = 0;
					for (int lx = min_x, rx = min_x; rx <= max_x; rx++) {
						if (rx - lx >= ww) {
							num_removed_pixel_values -= min_x_vec[lx];
							lx++;
						}
						num_removed_pixel_values += max_x_vec[rx];
						max_num_removed_pixel_values = max(max_num_removed_pixel_values, num_removed_pixel_values);
					}
					if (y_it == e_idx_y) { break; }
				}
			}
			cout << num_pixel_values - max_num_removed_pixel_values << endl;
		}
	}

	return 0;
}
accuracy: 1
time_ms: 230
memory_kb: 4504
final_rank: 13

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
using namespace std;

class BinInfo {
public:
	int count;
	int min_x;
	int max_x;
	int min_y;
	int max_y;

public:
	BinInfo(int count, int min_x, int max_x, int min_y, int max_y) : count(count), min_x(min_x), max_x(max_x), min_y(min_y), max_y(max_y) {
		return;
	}

	~BinInfo() {
		return;
	}
};


int main() {
#if READ_FROM_FILE
	ifstream ifs("test0.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	int h = -1;
	int w = -1;
	int s = -1;
	int buf = -1;
	const int max_h = 1000;
	const int max_w = 1000;
	unsigned char image[max_h][max_w];

	cin >> h >> w >> s;
	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			cin >> buf;
			image[y][x] = buf - 1;
		}
	}

	if (s >= h * w) {
		cout << 0 << endl;
	}
	else {
		int num_pixel_values = 256;
		unordered_set<unsigned char> pixel_value_set;
		vector<BinInfo> pixel_hist(num_pixel_values, BinInfo(0, w - 1, 0, h - 1, 0));
		vector<const BinInfo*> candidate_bin_vec;


		for (int y = 0; y < h; y++) {
			for (int x = 0; x < w; x++) {
				const unsigned char& pixel_value = image[y][x];
				BinInfo& c_bin = pixel_hist[pixel_value];

				c_bin.count++;
				c_bin.min_x = min(c_bin.min_x, x);
				c_bin.max_x = max(c_bin.max_x, x);
				c_bin.min_y = min(c_bin.min_y, y);
				c_bin.max_y = max(c_bin.max_y, y);
				pixel_value_set.insert(pixel_value);
			}
		}

		num_pixel_values = (int)pixel_value_set.size();

		for (const BinInfo& c_bin : pixel_hist) {
			if (0 < c_bin.count && c_bin.count <= s && c_bin.max_x - c_bin.min_x <= s && c_bin.max_y - c_bin.min_y <= s) {
				candidate_bin_vec.emplace_back(&c_bin);
			}
		}

		if (candidate_bin_vec.empty()) {
			cout << num_pixel_values << endl;
		}
		else {
			int max_hh = max(2, min(h + 1, s + 1));
			int min_hh = min(max(1, s / w), max_hh - 1);
			int max_num_removed_pixel_values = 0;
			vector<const BinInfo*> c_candidate_bin_vec;
			vector<const BinInfo*> n_candidate_bin_vec;
			vector<int> min_x_vec;
			vector<int> max_x_vec;
			set<int> by_set;
			int min_x = w - 1;
			int max_x = 0;
			int min_y = h - 1;
			int max_y = 0;
			int c_max_y = -1;
			int c_max_x = -1;
			set<int>::iterator e_idx_x;
			set<int>::iterator e_idx_y;
			int bx = -1;
			int by = -1;
			int ex = -1;
			int ey = -1;
			int num_removed_pixel_values = -1;
			int ww = -1;

			for (int hh = min_hh; hh < max_hh; hh++) {
				if (max_num_removed_pixel_values == num_pixel_values - 1) { break; }
				
				ww = s / hh;
				c_candidate_bin_vec.clear();
				min_y = h - 1;
				max_y = 0;

				for (const BinInfo* c_bin : candidate_bin_vec) {
					if (c_bin->max_x - c_bin->min_x <= ww && c_bin->max_y - c_bin->min_y <= hh) {
						c_candidate_bin_vec.emplace_back(c_bin);
						by_set.insert(c_bin->min_y);
						by_set.insert(c_bin->max_y);
						min_y = min(min_y, c_bin->min_y);
						max_y = max(max_y, c_bin->max_y);
					}
				}
				c_max_y = max(min_y, max_y - hh + 1);
				by_set.insert(c_max_y);
				e_idx_y = by_set.find(c_max_y);

				for (set<int>::iterator y_it = by_set.begin();; y_it++) {
					if (max_num_removed_pixel_values == num_pixel_values - 1) { break; }
					by = *y_it;
					ey = by + hh - 1;

					n_candidate_bin_vec.clear();
					min_x_vec = vector<int>(w, 0);
					max_x_vec = vector<int>(w, 0);
					min_x = w - 1;
					max_x = 0;
					for (const BinInfo* c_bin : c_candidate_bin_vec) {
						if (by <= c_bin->min_y && c_bin->max_y <= ey) {
							n_candidate_bin_vec.emplace_back(c_bin);
							min_x_vec[c_bin->min_x]++;
							max_x_vec[c_bin->max_x]++;
							min_x = min(min_x, c_bin->min_x);
							max_x = max(max_x, c_bin->max_x);
						}

					}
					c_max_x = max(min_x, max_x - ww + 1);
					num_removed_pixel_values = 0;
					for (int lx = min_x, rx = min_x; rx <= max_x; rx++) {
						if (rx - lx >= ww) {
							num_removed_pixel_values -= min_x_vec[lx];
							lx++;
						}
						num_removed_pixel_values += max_x_vec[rx];
						max_num_removed_pixel_values = max(max_num_removed_pixel_values, num_removed_pixel_values);
					}
					if (y_it == e_idx_y) { break; }
				}
			}
			cout << num_pixel_values - max_num_removed_pixel_values << endl;
		}
	}

	return 0;
}
accuracy: 1
time_ms: 230
memory_kb: 4484
final_rank: 12

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;

class BinInfo {
public:
	int count;
	int min_x;
	int max_x;
	int min_y;
	int max_y;

public:
	BinInfo(int count, int min_x, int max_x, int min_y, int max_y) : count(count), min_x(min_x), max_x(max_x), min_y(min_y), max_y(max_y) {
		return;
	}

	~BinInfo() {
		return;
	}
};


int main() {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	int h = -1;
	int w = -1;
	int s = -1;
	int buf = -1;
	const int max_h = 1000;
	const int max_w = 1000;
	unsigned char image[max_h][max_w];

	cin >> h >> w >> s;
	for (int y = 0; y < h; y++) {
		for (int x = 0; x < w; x++) {
			cin >> buf;
			image[y][x] = buf - 1;
		}
	}

	if (s >= h * w) {
		cout << 0 << endl;
	}
	else {
		int num_pixel_values = 256;
		unordered_set<unsigned char> pixel_value_set;
		vector<BinInfo> pixel_hist(num_pixel_values, BinInfo(0, w - 1, 0, h - 1, 0));
		vector<const BinInfo*> candidate_bin_vec;


		for (int y = 0; y < h; y++) {
			for (int x = 0; x < w; x++) {
				const unsigned char& pixel_value = image[y][x];
				BinInfo& c_bin = pixel_hist[pixel_value];

				c_bin.count++;
				c_bin.min_x = min(c_bin.min_x, x);
				c_bin.max_x = max(c_bin.max_x, x);
				c_bin.min_y = min(c_bin.min_y, y);
				c_bin.max_y = max(c_bin.max_y, y);
				pixel_value_set.insert(pixel_value);
			}
		}

		num_pixel_values = (int)pixel_value_set.size();

		for (const BinInfo& c_bin : pixel_hist) {
			if (0 < c_bin.count && c_bin.count <= s && c_bin.max_x - c_bin.min_x <= s && c_bin.max_y - c_bin.min_y <= s) {
				candidate_bin_vec.emplace_back(&c_bin);
			}
		}

		if (candidate_bin_vec.empty()) {
			cout << num_pixel_values << endl;
		}
		else {
			int min_x = w - 1;
			int max_x = 0;
			int min_y = h - 1;
			int max_y = 0;
			int max_hh = max(2, min(h + 1, s + 1));
			int min_hh = min(max(1, s / w), max_hh - 1);
			int max_ww = min(w, s);
			int max_num_removed_pixel_values = 0;

			for (const BinInfo* c_bin : candidate_bin_vec) {
				min_x = min(min_x, c_bin->min_x);
				max_x = max(max_x, c_bin->max_x);
				min_y = min(min_y, c_bin->min_y);
				max_y = max(max_y, c_bin->max_y);
			}
			
			for (int hh = min_hh; hh < max_hh; hh++) {
				if (max_num_removed_pixel_values == num_pixel_values - 1) { break; }
				int c_max_y = max(min_y + 1, max_y - hh + 1);
				
				for (int ww = max_ww; ww > 0; ww--) {
					if (ww * hh > s) { continue; }
					if (max_num_removed_pixel_values == num_pixel_values - 1) { break; }
					int c_max_x = max(min_x + 1, max_x - ww + 1);
					vector<const BinInfo*> c_candidate_bin_vec;

					for (const BinInfo* c_bin : candidate_bin_vec) {
						if (c_bin->max_x - c_bin->min_x <= ww && c_bin->max_y - c_bin->min_y <= hh) {
							c_candidate_bin_vec.emplace_back(c_bin);
						}
					}

					for (int by = min_y; by < c_max_y; by++) {
						if (max_num_removed_pixel_values == num_pixel_values - 1) { break; }
						int ey = by + hh - 1;
						
						for (int bx = min_x; bx < c_max_x; bx++) {
							int ex = bx + ww - 1;
							int num_removed_pixel_values = 0;
							
							for (const BinInfo* c_bin : c_candidate_bin_vec) {
								if (bx <= c_bin->min_x && c_bin->max_x <= ex && by <= c_bin->min_y && c_bin->max_y <= ey) {
									num_removed_pixel_values++;
								}
							}
							max_num_removed_pixel_values = max(max_num_removed_pixel_values, num_removed_pixel_values);
						}
					}
					break;
				}
			}
			cout << num_pixel_values - max_num_removed_pixel_values << endl;
		}
	}

	return 0;
}
accuracy: 0.4583333333333333
time_ms: 110
memory_kb: 4432
final_rank: 20

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <cstdint>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <functional>
#include <ctime>
#include <fstream>
#include <cmath>
#include <limits>
#include <chrono>
#include <numeric>
#include <type_traits>
#include <iomanip>
#include <float.h>
#include <math.h>
#include <cassert>
#include <random>
#include <complex>
#pragma warning (disable: 4996)
using namespace std;

using ll = long long;
using ld = long double;

long long modpow(long long a, long long n, long long mod) {
    if (n < 0)return 0;
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

ll ll_gcd(ll a, ll b) {
    if (a < b) return ll_gcd(b, a);
    ll r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}

struct UnionFind {
    vector <ll> par;
    vector <ll> siz;
    UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
        for (ll i = 0; i < sz_; ++i) par[i] = i;
    }
    void init(ll sz_) {
        par.resize(sz_);
        siz.assign(sz_, 1LL);
        for (ll i = 0; i < sz_; ++i) par[i] = i;
    }
    ll root(ll x) {
        while (par[x] != x) {
            x = par[x] = par[par[x]];
        }
        return x;
    }
    bool merge(ll x, ll y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        if (siz[x] < siz[y]) swap(x, y);
        siz[x] += siz[y];
        par[y] = x;
        return true;
    }

    bool issame(ll x, ll y) {
        return root(x) == root(y);
    }

    ll size(ll x) {
        return siz[root(x)];
    }
};
template< typename T = int >
struct Edge {
    int from, to;
    T cost;
    int idx;

    Edge() = default;

    Edge(int from, int to, T cost = 1, int idx = -1) : from(from), to(to), cost(cost), idx(idx) {}

    operator int() const { return to; }
};

template< typename T = int >
struct Graph {
    vector< vector< Edge< T > > > g;
    int es;

    Graph() = default;

    explicit Graph(int n) : g(n), es(0) {}

    size_t size() const {
        return g.size();
    }

    void add_directed_edge(int from, int to, T cost = 1) {
        g[from].emplace_back(from, to, cost, es++);
    }

    void add_edge(int from, int to, T cost = 1) {
        g[from].emplace_back(from, to, cost, es);
        g[to].emplace_back(to, from, cost, es++);
    }

    void read(int M, int padding = -1, bool weighted = false, bool directed = false) {
        for (int i = 0; i < M; i++) {
            int a, b;
            cin >> a >> b;
            a += padding;
            b += padding;
            T c = T(1);
            if (weighted) cin >> c;
            if (directed) add_directed_edge(a, b, c);
            else add_edge(a, b, c);
        }
    }

    inline vector< Edge< T > >& operator[](const int& k) {
        return g[k];
    }

    inline const vector< Edge< T > >& operator[](const int& k) const {
        return g[k];
    }
};

template< typename T = int >
using Edges = vector< Edge< T > >;

long long modinv(long long a, long long mod) {
    return modpow(a, mod - 2, mod);
}

template< typename T >
struct ShortestPath {
    vector< T > dist;
    vector< int > from, id;
};

template< typename T >
ShortestPath< T > dijkstra(const Graph< T >& g, int s) {
    const auto INF = numeric_limits< T >::max();
    vector< T > dist(g.size(), INF);
    vector< int > from(g.size(), -1), id(g.size(), -1);
    using Pi = pair< T, int >;
    priority_queue< Pi, vector< Pi >, greater<> > que;
    dist[s] = 0;
    que.emplace(dist[s], s);
    while (!que.empty()) {
        T cost;
        int idx;
        tie(cost, idx) = que.top();
        que.pop();
        if (dist[idx] < cost) continue;
        for (auto& e : g[idx]) {
            auto next_cost = cost + e.cost;
            if (dist[e.to] <= next_cost) continue;
            dist[e.to] = next_cost;
            from[e.to] = idx;
            id[e.to] = e.idx;
            que.emplace(dist[e.to], e.to);
        }
    }
    return { dist, from, id };
}

namespace geometry {
    using Real = double;
    const Real EPS = 1e-8;
    const Real PI = acos(static_cast<Real>(-1));

    enum {
        OUT, ON, IN
    };

    inline int sign(const Real& r) {
        return r <= -EPS ? -1 : r >= EPS ? 1 : 0;
    }

    inline bool equals(const Real& a, const Real& b) {
        return sign(a - b) == 0;
    }
}

namespace geometry {
    using Point = complex< Real >;

    istream& operator>>(istream& is, Point& p) {
        Real a, b;
        is >> a >> b;
        p = Point(a, b);
        return is;
    }

    ostream& operator<<(ostream& os, const Point& p) {
        return os << real(p) << " " << imag(p);
    }

    Point operator*(const Point& p, const Real& d) {
        return Point(real(p) * d, imag(p) * d);
    }

    // rotate point p counterclockwise by theta rad
    Point rotate(Real theta, const Point& p) {
        return Point(cos(theta) * real(p) - sin(theta) * imag(p), sin(theta) * real(p) + cos(theta) * imag(p));
    }

    Real cross(const Point& a, const Point& b) {
        return real(a) * imag(b) - imag(a) * real(b);
    }

    Real dot(const Point& a, const Point& b) {
        return real(a) * real(b) + imag(a) * imag(b);
    }

    bool compare_x(const Point& a, const Point& b) {
        return equals(real(a), real(b)) ? imag(a) < imag(b) : real(a) < real(b);
    }

    bool compare_y(const Point& a, const Point& b) {
        return equals(imag(a), imag(b)) ? real(a) < real(b) : imag(a) < imag(b);
    }

    using Points = vector< Point >;
}

namespace geometry {
    using Polygon = vector< Point >;
    using Polygons = vector< Polygon >;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int h, w, s;
    cin >> h >> w >> s;
    vector<vector<int>> z(h,vector<int>(w));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            cin >> z[i][j];
            z[i][j]--;
        }
    }
    vector<int> a(256,5000);
    vector<int> b(256,-1);
    vector<int> c(256,5000);
    vector<int> d(256,-1);
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            a[z[i][j]] = min(a[z[i][j]], i);
            b[z[i][j]] = max(b[z[i][j]], i);
            c[z[i][j]] = min(c[z[i][j]], j);
            d[z[i][j]] = max(d[z[i][j]], j);
        }
    }
    vector<int> e;
    vector<int> f;
    int now = h;
    int nows = s / h;
    e.push_back(now);
    f.push_back(nows);
    for (int i = h-1; i > 0; i--) {
        now = i;
        int k = s / i;
        if (k != nows&&nows<=w) {
            nows = k;
            e.push_back(now);
            f.push_back(min(nows,w));
        }
    }
    int ans = 256;
    for (int i = 0; i < e.size(); i++) {
        vector<vector<int>> zz(h - e[i] + 2, vector<int>(w - f[i] + 2));
        for (int j = 0; j < 256; j++) {
            if (a[j] == 5000)continue;
            zz[0][0]++;
                zz[min(a[j] + 1, h - e[i] + 1)][min(c[j] + 1, w - f[i] + 1)]--;
                zz[max(0, min(a[j] + 1, b[j] - e[i] + 1))][min(c[j] + 1, w - f[i] + 1)]++;
                zz[min(a[j] + 1, h - e[i] + 1)][max(0, min(c[j] + 1, d[j] - f[i] + 1))]++;
                zz[max(0, min(a[j] + 1, b[j] - e[i] + 1))][max(0, min(c[j] + 1, d[j] - f[i] + 1))] --;
        }
        for (int j = 0; j < h - e[i]; j++) {
            for (int l = 0; l < w - f[i]+1; l++) {
                zz[j + 1][l] += zz[j][l];
            }
        }
        for (int j = 0; j < h - e[i]+1; j++) {
            for (int l = 0; l < w - f[i]; l++) {
                zz[j][l + 1] += zz[j][l];
            }
        }
        for (int j = 0; j < h - e[i]+1; j++) {
            for (int l = 0; l < w - f[i]+1; l++) {
                ans = min(ans, zz[j][l]);
            }
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 450
memory_kb: 10828
final_rank: 18

code_snippet: #include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <vector>
#include <numeric>
#include <algorithm>
#include <queue>
#include <string>
#include <random>
#include <array>
#include <climits>
#include <map>
#include <cassert>
#include <stack>
#include <iomanip>
#include <cfloat>
#include <fstream>

int main() {
	int height, width, s; std::cin >> height >> width >> s;
	std::vector<std::vector<int>> picture(height, std::vector<int>(width));
	for (auto& line : picture) {
		for (auto& pixel : line) {
			std::cin >> pixel; --pixel;
		}
	}
	std::vector<int> top(256, height), bottom(256, -1), left(256, width), right(256, -1);
	std::vector<bool> appear(256);
	for (auto i = 0; i < height; ++i) {
		for (auto j = 0; j < width; ++j) {
			const auto pixel = picture[i][j];
			appear[pixel] = true;
			top[pixel] = std::min(top[pixel], i);
			bottom[pixel] = std::max(bottom[pixel], i);
			left[pixel] = std::min(left[pixel], j);
			right[pixel] = std::max(right[pixel], j);
		}
	}
	int max_remove{ 0 };
	for (auto i = 0; i < 256; ++i) {
		if (!appear[i]) continue;
		for (auto j = i; j < 256; ++j) {
			if (!appear[j]) continue;
			const auto u = std::min(top[i], top[j]);
			const auto d = std::max(bottom[i], bottom[j]);
			const auto h = d - u + 1;
			const auto w = s / h;
			if (w == 0) continue;
			std::priority_queue<int, std::vector<int>, std::greater<>> queue;
			std::vector<std::pair<int, int>> ranges;
			for (auto k = 0; k < 256; ++k) {
				if (!appear[k]) continue;
				if (top[k] < u || d < bottom[k]) continue;
				ranges.emplace_back(left[k], right[k]);
			}
			std::sort(ranges.begin(), ranges.end(), [](const auto a, const auto b) {return a.second < b.second; });
			for (const auto [l, r] : ranges) {
				queue.push(l);
				while (!queue.empty() && queue.top() + w <= r) {
					queue.pop();
				}
				max_remove = std::max<int>(max_remove, queue.size());
			}
		}
	}
	const auto result = std::count(appear.begin(), appear.end(), true) - max_remove;
	std::cout << result << '\n';
}
accuracy: 1
time_ms: 290
memory_kb: 7388
final_rank: 14

code_snippet: #include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)
#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yes(ans) if(ans)printf("yes\n");else printf("no\n")
#define Yes(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YES(ans) if(ans)printf("YES\n");else printf("NO\n")
#define popcount(v) __builtin_popcountll(v)
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
#define vector4d(type,name,h,w,d,...) vector<vector<vector<vector<type>>>>name(h,vector<vector<vector<type>>>(w,vector<vector<type>>(d,vector<type>(__VA_ARGS__))))
using namespace std;
using ll = long long;
template<class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>;
const int MOD=1000000007;
const int MOD2=998244353;
const int INF=1<<30;
const ll INF2=1LL<<60;
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}
template<class T,class U,class V>void scan(tuple<T,U,V>& p){scan(get<0>(p));scan(get<1>(p));scan(get<2>(p));}
template<class T, size_t size> void scan(array<T, size>& a){ for(auto&& i : a) scan(i);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}
ll updivide(ll a,ll b){return (a+b-1)/b;}
int msb(ll v){return 63-__builtin_clzll(v);}
template<class T> void chmax(T &a,const T b){if(b>a)a=b;}
template<class T> void chmin(T &a,const T b){if(b<a)a=b;}

int main(){
    INT(h,w,s);
    vector2d(int,A,h,w);
    in(A);
    vector<array<int,4>> t(256,array<int,4>({INF,INF,-1,-1}));
    rep(y,h){
        rep(x,w){
            A[y][x]--;
            chmin(t[A[y][x]][0],y);
            chmin(t[A[y][x]][1],x);
            chmax(t[A[y][x]][2],y);
            chmax(t[A[y][x]][3],x);
        }
    }
    int ans=INF,tot=0;
    rep(i,256)tot+=(t[i][0]!=INF);
    ans=tot;
    auto f=[&](int ly,int ry){
        if((ry-ly+1)>s)return;
        int cnt=0,len=s/(ry-ly+1);
        vector<vector<int>> c(1000);
        vector<int> c2(256);
        for(int k=0;k<256;k++){
            if(t[k][0]==INF)continue;
            if(ly<=t[k][0]&&t[k][2]<=ry){
                c[t[k][1]].push_back(k);
                c[t[k][3]].push_back(k);
            }
        }
        for(int l=0,r=0;l<1000;l++){
            while(r<1000&&r<l+len){
                for(auto v:c[r]){
                    c2[v]++;
                    if(c2[v]==2)cnt++;
                }
                r++;
            }
            chmin(ans,tot-cnt);
            for(auto v:c[l]){
                if(c2[v]==2)cnt--;
                c2[v]--;
            }
        }
    };
    for(int i=0;i<256;i++){
        if(t[i][0]==INF)continue;
        for(int j=i;j<256;j++){
            if(t[j][0]==INF)continue;
            int ly=min(t[i][0],t[j][0]),ry=max(t[i][2],t[j][2]);
            f(ly,ry);
        }
    }
    out(ans);
}
accuracy: 1
time_ms: 690
memory_kb: 7532
final_rank: 17