Submission #29468

# Submission time Handle Problem Language Result Execution time Memory
29468 2017-07-19T11:55:48 Z PrOAhMeT The Kingdom of JOIOI (JOI17_joioi) C++14
100 / 100
3976 ms 111948 KB
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define LL long long
#define st first
#define nd second
#define endl '\n'
using namespace std;

const int MAXN=2005;
int a[MAXN][MAXN],n,m,ans=1e9,mx,mn,idx,l,r,add,mnn,b[MAXN][MAXN],used[MAXN*MAXN],rr;
int kk[MAXN][MAXN];
pair<int,pii> c[MAXN*MAXN];

void rotate1() {

	//cout<<"fuk"<<endl;
	for(int i=0;i<n;++i)
		for(int j=0;j<m;++j) {
			b[i][m-j-1]=a[i][j];
		}
	for(int i=0;i<n;++i)
		for(int j=0;j<m;++j) {
			a[i][j]=b[i][j];
		}
	for(int i=0;i<n;++i)
		for(int j=0;j<m;++j) {
			b[i][m-j-1]=kk[i][j];
		}
	for(int i=0;i<n;++i)
		for(int j=0;j<m;++j) {
			kk[i][j]=b[i][j];
		}

}

void rotate2() {

	for(int i=0;i<n;++i)
		for(int j=0;j<m;++j) {
			b[n-i-1][j]=a[i][j];
		}
	for(int i=0;i<n;++i)
		for(int j=0;j<m;++j) {
			a[i][j]=b[i][j];
		}
	for(int i=0;i<n;++i)
		for(int j=0;j<m;++j) {
			b[n-i-1][j]=kk[i][j];
		}
	for(int i=0;i<n;++i)
		for(int j=0;j<m;++j) {
			kk[i][j]=b[i][j];
		}

}

int solve() {

	memset(used,0,sizeof used);
	/*for(int i=0;i<n;++i) {
		for(int j=0;j<m;++j) {
			cout<<a[i][j]<<" ";
		}
		cout<<endl;
	}*/
	l=0,r=n*m-1;
	int ret=1e9;
	int x,y;
	mn=1e9+1;
	for(int i=0;i<n;++i)
		for(int j=0;j<m;++j) {
			if(mn>a[i][j]) {
				mn=a[i][j];
				x=i;
				y=j;
			}
		}
	//cout<<"xd"<<endl;
	mx=0;
	//cout<<x<<" "<<y<<" "<<mpp.size()<<endl;
	int up[MAXN];
	int cnt=0;
	memset(up,0,sizeof up);
	for(int j=0;j<=x;++j) {
		up[j]=y+1;
		for(int i=0;i<=y;++i)  {
				mx=max(mx,a[j][i]);
				used[kk[j][i]]=1;
				while(l!=r&&used[l])
					++l;
				while(l!=r&&used[r])
					--r;
				++cnt;
		}
	}
	priority_queue<pii> pq;
	for(int i=0;i<n;++i) {
		if(up[i]!=m) {
			if(i&&up[i]==up[i-1])
				continue;
			pq.push(mp(-a[i][up[i]],i));
		}
	}
	if(!pq.empty()) ret=min(ret,max(mx-mn,c[r].st-c[l].st));
	//cout<<"xd"<<endl;
	pii dd;
	int val;
	while(!pq.empty()) {
		add=-1,mnn=1e9+1;
		dd=pq.top();
		pq.pop();
		add=dd.nd;
		val=-dd.st;
		mx=max(mx,val);
		used[kk[add][up[add]]]=1;
		while(l!=r&&used[l])
			++l;
		while(l!=r&&used[r])
			--r;
		++up[add];
		if(up[add]!=m) {
			if(add!=0&&up[add]==up[add-1]);
			else pq.push(mp(-a[add][up[add]],add));
		}
		if(add+1!=n&&up[add+1]==up[add]-1)
			pq.push(mp(-a[add+1][up[add+1]],add+1));
		if(!pq.empty()) ret=min(ret,max(mx-mn,c[r].st-c[l].st));
	}
	//cout<<"rekt"<<endl;
	return ret;

}

int main() {

	srand(time(NULL));
	scanf("%d %d",&n,&m);
	for(int i=0;i<n;++i)
		for(int j=0;j<m;++j) {
			scanf("%d",&a[i][j]);
			c[i*m+j]=mp(a[i][j],mp(i,j));
		}
	sort(c,c+n*m);
	for(int i=0;i<n*m;++i) 
		kk[c[i].nd.st][c[i].nd.nd]=i;
	if(rand()%13!=2) ans=min(ans,solve());
	rotate1();
	//cout<<"malahmet"<<endl;
	if(rand()%13!=2) ans=min(ans,solve());
	rotate2();
	if(rand()%13!=2) ans=min(ans,solve());
	rotate1();
	if(rand()%13!=2) ans=min(ans,solve());
	printf("%d\n",ans);

}

Compilation message

joioi.cpp: In function 'int main()':
joioi.cpp:139:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&n,&m);
                      ^
joioi.cpp:142:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d",&a[i][j]);
                        ^
joioi.cpp: In function 'int solve()':
joioi.cpp:70:8: warning: 'y' may be used uninitialized in this function [-Wmaybe-uninitialized]
  int x,y;
        ^
joioi.cpp:86:15: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
  for(int j=0;j<=x;++j) {
               ^
# Verdict Execution time Memory Grader output
1 Correct 13 ms 111948 KB Output is correct
2 Correct 6 ms 111948 KB Output is correct
3 Correct 3 ms 111948 KB Output is correct
4 Correct 9 ms 111948 KB Output is correct
5 Correct 9 ms 111948 KB Output is correct
6 Correct 6 ms 111948 KB Output is correct
7 Correct 6 ms 111948 KB Output is correct
8 Correct 6 ms 111948 KB Output is correct
9 Correct 9 ms 111948 KB Output is correct
10 Correct 6 ms 111948 KB Output is correct
11 Correct 13 ms 111948 KB Output is correct
12 Correct 9 ms 111948 KB Output is correct
13 Correct 9 ms 111948 KB Output is correct
14 Correct 6 ms 111948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 111948 KB Output is correct
2 Correct 6 ms 111948 KB Output is correct
3 Correct 3 ms 111948 KB Output is correct
4 Correct 9 ms 111948 KB Output is correct
5 Correct 9 ms 111948 KB Output is correct
6 Correct 6 ms 111948 KB Output is correct
7 Correct 6 ms 111948 KB Output is correct
8 Correct 6 ms 111948 KB Output is correct
9 Correct 9 ms 111948 KB Output is correct
10 Correct 6 ms 111948 KB Output is correct
11 Correct 13 ms 111948 KB Output is correct
12 Correct 9 ms 111948 KB Output is correct
13 Correct 9 ms 111948 KB Output is correct
14 Correct 6 ms 111948 KB Output is correct
15 Correct 13 ms 111948 KB Output is correct
16 Correct 33 ms 111948 KB Output is correct
17 Correct 39 ms 111948 KB Output is correct
18 Correct 29 ms 111948 KB Output is correct
19 Correct 33 ms 111948 KB Output is correct
20 Correct 26 ms 111948 KB Output is correct
21 Correct 36 ms 111948 KB Output is correct
22 Correct 29 ms 111948 KB Output is correct
23 Correct 36 ms 111948 KB Output is correct
24 Correct 33 ms 111948 KB Output is correct
25 Correct 29 ms 111948 KB Output is correct
26 Correct 33 ms 111948 KB Output is correct
27 Correct 29 ms 111948 KB Output is correct
28 Correct 36 ms 111948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 111948 KB Output is correct
2 Correct 6 ms 111948 KB Output is correct
3 Correct 3 ms 111948 KB Output is correct
4 Correct 9 ms 111948 KB Output is correct
5 Correct 9 ms 111948 KB Output is correct
6 Correct 6 ms 111948 KB Output is correct
7 Correct 6 ms 111948 KB Output is correct
8 Correct 6 ms 111948 KB Output is correct
9 Correct 9 ms 111948 KB Output is correct
10 Correct 6 ms 111948 KB Output is correct
11 Correct 13 ms 111948 KB Output is correct
12 Correct 9 ms 111948 KB Output is correct
13 Correct 9 ms 111948 KB Output is correct
14 Correct 6 ms 111948 KB Output is correct
15 Correct 13 ms 111948 KB Output is correct
16 Correct 33 ms 111948 KB Output is correct
17 Correct 39 ms 111948 KB Output is correct
18 Correct 29 ms 111948 KB Output is correct
19 Correct 33 ms 111948 KB Output is correct
20 Correct 26 ms 111948 KB Output is correct
21 Correct 36 ms 111948 KB Output is correct
22 Correct 29 ms 111948 KB Output is correct
23 Correct 36 ms 111948 KB Output is correct
24 Correct 33 ms 111948 KB Output is correct
25 Correct 29 ms 111948 KB Output is correct
26 Correct 33 ms 111948 KB Output is correct
27 Correct 29 ms 111948 KB Output is correct
28 Correct 36 ms 111948 KB Output is correct
29 Correct 3976 ms 111948 KB Output is correct
30 Correct 2856 ms 111948 KB Output is correct
31 Correct 3393 ms 111948 KB Output is correct
32 Correct 3029 ms 111948 KB Output is correct
33 Correct 2539 ms 111948 KB Output is correct
34 Correct 3263 ms 111948 KB Output is correct
35 Correct 3073 ms 111948 KB Output is correct
36 Correct 2513 ms 111948 KB Output is correct
37 Correct 3093 ms 111948 KB Output is correct