Submission #406754

#TimeUsernameProblemLanguageResultExecution timeMemory
406754ngraceRectangles (IOI19_rect)C++14
0 / 100
15 ms460 KiB
#include "rect.h"
#include <vector>
#include <iostream>
#include <set>
using namespace std;
#define v vector
#define pii pair<int,int>
#define fi first
#define se second

long long count_rectangles(std::vector<std::vector<int> > a) {
	long long out=0;
	int R=a.size();
	int C=a[0].size();

	bool subSix=true;
	for(int i=0;i<R;i++){//check for sub six
		for(int j=0;j<C;j++){
			if(a[i][j]>1){
				subSix=false;
				break;
			}
		}
		if(!subSix){
			break;
		}
	}

	if(subSix){
		//find sub rectangles with dp[i][j] = {min(dp[i-1][j-1].first, dp[i][j-1].first),
		//										min(dp[i-1][j-1].second, dp[i-1][j].second)}
		//then for each rectangle check the border.
		//can speed up by making so only consider if dp[i-1][j-1].first and dp[i][j-1].first are equal
		v<v<pii>> dp(R,v<pii>(C,{-1,-1}));
		for(int r=1;r<R-1;r++){
			for(int c=1;c<C-1;c++){
				if(a[r][c]==0){
					int f=r;
					int s=c;

					int r1=dp[r-1][c-1].fi;
					int r2=dp[r-1][c].fi;
					int c1=dp[r-1][c-1].se;
					int c2=dp[r][c-1].se;

					if(c1==c2 && c1!=-1){
						s=max(c1,1);
					}
					if(r1==r2 && r1!=-1){
						f=max(r1,1);
					}
					if(c1==-1 && c2!=-1){
						s=c2;
					}
					else if(r1==-1 && r2!=-1){
						f=r2;
					}

					dp[r][c]={f,s};
				}
			}
		}

		//check dp table
		for(int r=0;r<R;r++){
			for(int c=0;c<C;c++){
				cout<<"("<<dp[r][c].fi<<" "<<dp[r][c].se<<") ";
			}
			cout<<endl;
		}

		//dp table is now made
		//iterate from bottom right to top left
		//for every distinct dp[i][j] value check borders of that rect then mark as covered
		set<pii> checked;
		checked.insert({-1,-1});
		for(int r=R-1;r>0;r--){
			for(int c=C-1;c>0;c--){
				if(a[r][c]==0 && checked.find(dp[r][c])==checked.end()){
					checked.insert(dp[r][c]);
					int r1=dp[r][c].fi;
					int c1=dp[r][c].se;
					bool works=true;
					for(int i=r1;i<=r;i++){
						if(a[i][c1-1]==0 || a[i][c+1]==0){
							works=false;
							break;
						}
					}
					for(int j=c1;j<=c;j++){
						if(a[r1-1][j]==0 || a[r+1][j]==0){
							works=false;
							break;
						}
					}
					if(works){
						out++;
					}
				}
			}
		}
	}
	else if((R<=200 && C<=200) || R<=3){//sub one, two, three and five
		for(int r1=1;r1<R-1;r1++){
			for(int c1=1;c1<C-1;c1++){
				for(int r2=r1;r2<R-1;r2++){
					for(int c2=c1;c2<C-1;c2++){
						//have rect with top left r1, c1, bottom right r2, c2
						//check every value of the rect
						bool works=true;
						for(int r=r1;r<=r2;r++){
							for(int c=c1;c<=c2;c++){
								if(a[r1-1][c]<=a[r][c]){
									works=false;
									break;
								}
								if(a[r2+1][c]<=a[r][c]){
									works=false;
									break;
								}
								if(a[r][c1-1]<=a[r][c]){
									works=false;
									break;
								}
								if(a[r][c2+1]<=a[r][c]){
									works=false;
									break;
								}
							}
							if(!works){
								break;
							}
						}
						if(works){
							out++;
						}
					}
				}
			}
		}
	}
	else{
		//full
	}

	return out;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...