22052023-01-02 22:05:16kohumarkJegesmedve (50)cpp11Wrong answer 0/50400ms4432 KiB
#include <iostream>
#include <set>
#define speed ios::sync_with_stdio(false);cin.tie(NULL)
#define mp make_pair
using namespace std;

int tomb[10000][4];
int adat[100][100][2];

int main(){
	int n, m, x, y;
	speed;
	cin >> n >> m;
	int tlen=(n-2)*(m-2)*4+(n+m-4)*6+4*2;
	for(int k=0; k<2; k++){
		for(int i=0; i<n; i++) for(int j=0; j<m; j++){
			cin >> adat[i][j][k];
			if(adat[i][j][0]==-1){x=i; y=j;}
		}
	}
	int v=0;
	for(int i=0; i<n; i++) for(int j=0; j<m; j++){
		if(i>0){tomb[v][0]=i; tomb[v][1]=j; tomb[v][2]=i-1; tomb[v][3]=j; v++;}
		if(i<n-1){tomb[v][0]=i; tomb[v][1]=j; tomb[v][2]=i+1; tomb[v][3]=j; v++;}
		if(j>0){tomb[v][0]=i; tomb[v][1]=j; tomb[v][2]=i; tomb[v][3]=j-1; v++;}
		if(j<m-1){tomb[v][0]=i; tomb[v][1]=j; tomb[v][2]=i; tomb[v][3]=j+1; v++;}
	}
	int temp=0; int halak=0; int sol=0; int rt = 0; int rh = 0;
	while(true){
		set<pair<int,int>> places;
		set<pair<int,int>> last;
		places.insert(mp(x,y));
		v=0; int sx = x; int sy = y;
		while(!places.empty()){
			auto it=places.begin();
			sx=(*it).first; sy=(*it).second;
			v=0;
			for(int i=0; i<tlen; i++){
				if(tomb[i][0]==sx&&tomb[i][1]==sy){
					if(adat[tomb[i][2]][tomb[i][3]][0]>=temp&&!last.count(mp(tomb[i][2],tomb[i][3]))){places.insert(mp(tomb[i][2],tomb[i][3])); v++;}
				}
				if(v==4){v=0; break;}
			}
			last.insert(*it); places.erase(it);
		}
		halak=0; int ntemp=1000000001;
		for(auto it=last.begin(); it!=last.end(); it++){
			pair<int,int> pos = *it;
			if(adat[pos.first][pos.second][0]>temp) ntemp=min(ntemp,adat[pos.first][pos.second][0]);
			halak+=adat[pos.first][pos.second][1];
		}
		places.clear(); last.clear();
		//cout << rt << ' ' << temp << ' ' << rh << ' ' << halak << '\n';
		if(temp!=0) sol+=min(temp-rt,rh-halak);
		if(temp==1000000001) break;
		rt = temp;
		rh = halak;
		temp = ntemp;
	}
	cout << sol;
}
SubtaskSumTestVerdictTimeMemory
base0/50
1Accepted0/03ms1844 KiB
2Wrong answer0/013ms2196 KiB
3Wrong answer0/314ms2448 KiB
4Wrong answer0/329ms2932 KiB
5Runtime error0/33ms3168 KiB
6Runtime error0/44ms3132 KiB
7Wrong answer0/32ms2792 KiB
8Wrong answer0/32ms2936 KiB
9Wrong answer0/33ms3140 KiB
10Wrong answer0/43ms3352 KiB
11Time limit exceeded0/4400ms2776 KiB
12Time limit exceeded0/4374ms2832 KiB
13Time limit exceeded0/4356ms3076 KiB
14Runtime error0/44ms4272 KiB
15Runtime error0/44ms4416 KiB
16Runtime error0/44ms4432 KiB