Submission #376448

# Submission time Handle Problem Language Result Execution time Memory
376448 2021-03-11T13:34:43 Z CaroLinda Cultivation (JOI17_cultivation) C++14
30 / 100
2000 ms 620 KB
#include <bits/stdc++.h>

#define ll long long
#define sz(x) (int)(x.size() )
#define all(x) x.begin(),x.end()
#define lp(i,a,b) for(int i= a ; i < b ; i++ )
#define pb push_back

const int MAXN = 310 ;

using namespace std ;

/*
	O que acontece quando tem numa linha mais de uma coluna inserida?
	Se eu fizer set vai dar zika

*/

int N , R , C , ans = INT_MAX ;
int mnNorth , mnSouth ;
pair<int,int> seeds[MAXN] ;
set< pair<int,int> > s ;
multiset<int> values ;

void add(int col, int id)
{
	s.insert(make_pair(col,id) ) ;	
	
	auto it = s.find( make_pair(col,id) ) ;
	auto it_less = it ; it_less-- ;
	auto it_more = it ; it_more++ ;

	if( it != s.begin() )
	{
		if( it_more != s.end() )
			values.erase( values.find(it_more->first - it_less->first) ) ;

		values.insert( col - it_less->first ) ;
	}
	if( it_more != s.end() )
		values.insert( it_more->first - col ) ;

}

void rem(int col, int id )
{
	auto it = s.find( make_pair(col, id) ) ;	
	auto it_less = it ; it_less-- ;
	auto it_more = it ; it_more++ ;

	if( it != s.begin() )
	{
		values.erase( values.find( col - it_less->first ) );

		if( it_more != s.end() ) values.insert( it_more->first - it_less->first ) ;
	}
	if( it_more != s.end() ) values.erase( values.find(it_more->first - col) ) ;

	s.erase( it ) ;
}

struct Event
{
	int row , col , id , isClosing ;
	
	Event(int row = 0 , int col = 0 , int id = 0 , int isClosing = 0 ) :
		row(row), col(col) , id(id) , isClosing(isClosing) {}

	bool operator < ( Event other ) const { return make_pair(row, isClosing) < make_pair(other.row, other.isClosing) ; } 

} ;

void test(int north,int south)
{
	vector< Event > sweep ;

	for(int i= 1 ; i <= N ; i++ )
	{
		sweep.push_back( Event( max(1, seeds[i].first-north) , seeds[i].second , i , 0 ) ) ;
		sweep.push_back( Event( min(seeds[i].first + south,R) , seeds[i].second , i , 1 ) ) ;

		if( seeds[i].first + south+1 <= R )
			sweep.push_back( Event( seeds[i].first +south+1 , seeds[i].second , i , -1 ) ) ;
	}

	sort(all(sweep) ) ;
	int mx = 0 ;
	int west_min = 0 , east_min = 0 ;

	for(int i = 0 , ptr = 0 ; i < sz(sweep) ; i = ptr )
	{
		int R = sweep[i].row ;

		while( ptr < sz(sweep) && sweep[ptr].row == R && sweep[ptr].isClosing != 1 )
		{
			if( sweep[ptr].isClosing == 0 )
				add( sweep[ptr].col , sweep[ptr].id ) ;
			ptr++ ;
		}

		west_min = max(west_min , s.begin()->first - 1 ) ;
		east_min = max(east_min, C - prev(s.end() )->first ) ;
		int k = ( values.empty() ) ? 0 : *prev(values.end() ) ;

		mx = max(mx, k-1) ;

		while( ptr < sz(sweep) && sweep[ptr].row == R  )
		{
			if( sweep[ptr].isClosing == 1 )
				rem(sweep[ptr].col , sweep[ptr].id ) ;
			ptr++ ;
		}

	}

	mx = max(mx, west_min + east_min ) ;

	ans = min( ans , north+south + mx) ;
}

int main()
{
	scanf("%d %d %d", &R, &C, &N ) ;
	for(int i = 1 ; i <= N ; i++ ) scanf("%d %d", &seeds[i].first, &seeds[i].second ) ;

	sort(seeds+1, seeds+1+N) ;

	int mx = 0 ;
	for(int i= 1 ; i < N ; i++ ) mx = max(mx, seeds[i+1].first-seeds[i].first-1 ) ;

	mnNorth = seeds[1].first-1 ;
	mnSouth = R-seeds[N].first ;

	for(int i = mnNorth ; i <= R ; i++ )
		for(int j = mnSouth ; j <= R ; j++ ) 
			if( i+j >= mx )
				test(i,j) ;

/*	for(int i = 1 ; i <= N ; i++ )
	{
		for(int j = 1 ; j <= N ; j++ ) 
		{
			int delta_row = seeds[i].first - seeds[j].first ;
			if( delta_row >= max( mnNorth + mnSouth , mx) ) test(delta_row) ;
		}
		if( seeds[i].first-1 >= max(mnNorth+mnSouth, mx) ) test(seeds[i].first-1) ;
		if( R-seeds[i].first >= max(mnNorth+mnSouth, mx) ) test(R-seeds[i].first) ;
	} */
	

	printf("%d\n", ans ) ;

}

Compilation message

cultivation.cpp: In function 'int main()':
cultivation.cpp:123:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  123 |  scanf("%d %d %d", &R, &C, &N ) ;
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
cultivation.cpp:124:38: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  124 |  for(int i = 1 ; i <= N ; i++ ) scanf("%d %d", &seeds[i].first, &seeds[i].second ) ;
      |                                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 236 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 236 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 4 ms 376 KB Output is correct
18 Correct 40 ms 364 KB Output is correct
19 Correct 25 ms 364 KB Output is correct
20 Correct 2 ms 364 KB Output is correct
21 Correct 64 ms 364 KB Output is correct
22 Correct 190 ms 364 KB Output is correct
23 Correct 14 ms 364 KB Output is correct
24 Correct 320 ms 364 KB Output is correct
25 Correct 243 ms 364 KB Output is correct
26 Correct 490 ms 492 KB Output is correct
27 Correct 521 ms 492 KB Output is correct
28 Correct 309 ms 504 KB Output is correct
29 Correct 451 ms 492 KB Output is correct
30 Correct 482 ms 436 KB Output is correct
31 Correct 512 ms 620 KB Output is correct
32 Correct 490 ms 560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 236 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 4 ms 376 KB Output is correct
18 Correct 40 ms 364 KB Output is correct
19 Correct 25 ms 364 KB Output is correct
20 Correct 2 ms 364 KB Output is correct
21 Correct 64 ms 364 KB Output is correct
22 Correct 190 ms 364 KB Output is correct
23 Correct 14 ms 364 KB Output is correct
24 Correct 320 ms 364 KB Output is correct
25 Correct 243 ms 364 KB Output is correct
26 Correct 490 ms 492 KB Output is correct
27 Correct 521 ms 492 KB Output is correct
28 Correct 309 ms 504 KB Output is correct
29 Correct 451 ms 492 KB Output is correct
30 Correct 482 ms 436 KB Output is correct
31 Correct 512 ms 620 KB Output is correct
32 Correct 490 ms 560 KB Output is correct
33 Correct 4 ms 364 KB Output is correct
34 Correct 436 ms 556 KB Output is correct
35 Correct 547 ms 556 KB Output is correct
36 Correct 595 ms 492 KB Output is correct
37 Correct 429 ms 364 KB Output is correct
38 Correct 590 ms 492 KB Output is correct
39 Correct 590 ms 620 KB Output is correct
40 Correct 561 ms 492 KB Output is correct
41 Correct 478 ms 492 KB Output is correct
42 Correct 511 ms 500 KB Output is correct
43 Correct 555 ms 432 KB Output is correct
44 Correct 546 ms 436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2081 ms 364 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2081 ms 364 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 236 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 4 ms 376 KB Output is correct
18 Correct 40 ms 364 KB Output is correct
19 Correct 25 ms 364 KB Output is correct
20 Correct 2 ms 364 KB Output is correct
21 Correct 64 ms 364 KB Output is correct
22 Correct 190 ms 364 KB Output is correct
23 Correct 14 ms 364 KB Output is correct
24 Correct 320 ms 364 KB Output is correct
25 Correct 243 ms 364 KB Output is correct
26 Correct 490 ms 492 KB Output is correct
27 Correct 521 ms 492 KB Output is correct
28 Correct 309 ms 504 KB Output is correct
29 Correct 451 ms 492 KB Output is correct
30 Correct 482 ms 436 KB Output is correct
31 Correct 512 ms 620 KB Output is correct
32 Correct 490 ms 560 KB Output is correct
33 Correct 4 ms 364 KB Output is correct
34 Correct 436 ms 556 KB Output is correct
35 Correct 547 ms 556 KB Output is correct
36 Correct 595 ms 492 KB Output is correct
37 Correct 429 ms 364 KB Output is correct
38 Correct 590 ms 492 KB Output is correct
39 Correct 590 ms 620 KB Output is correct
40 Correct 561 ms 492 KB Output is correct
41 Correct 478 ms 492 KB Output is correct
42 Correct 511 ms 500 KB Output is correct
43 Correct 555 ms 432 KB Output is correct
44 Correct 546 ms 436 KB Output is correct
45 Execution timed out 2081 ms 364 KB Time limit exceeded
46 Halted 0 ms 0 KB -