#include "wombats.h"
#include <bits/stdc++.h>
#define debug
#define lp(i,a,b) for(int i = a ; i < b ; i++ )
const int DIV = 40 ;
const int inf = 1e9+10 ;
const int MAXN = 5005 ;
const int MAXM = 205 ;
using namespace std ;
int N , M ;
int ini_bucket[MAXN] , end_bucket[MAXN];
int my_block[MAXN ] ;
int resp[MAXM][MAXM] ;
int opt[MAXM][MAXM] ;
int h[MAXN][MAXM] ;
int v[MAXN][MAXM] ;
int tree[130*4][MAXM][MAXM] ;
//Esta funcao recebe um pedaco do meu grid maior
//Com esse pedaco, ela calcula a dp
//Stora o resultado na matriz resp[MAXM][MAXM]
void calc(int n_menor , int n_maior )
{
if( n_menor == n_maior )
{
for(int i = 0 ; i < M ; i++ )
{
for(int j = 0 ; j < M ; j++ ) resp[i][j] = inf ;
resp[i][i] = 0 ;
}
return;
}
int dp[MAXN][MAXM] ;
for(int curr_c = 0; curr_c < M ; curr_c++ )
{
//Calculando a distancia de [n_menor][i] para [n_maior][j] , qualquer j
lp(i,n_menor, n_maior + 1)
lp(j,0,M) dp[i][j] = inf ;
dp[n_menor][ curr_c ] = 0 ;
for(int i = n_menor ; i <= n_maior ; i++ )
{
for(int j = 0 ; j < M ; j++ )
{
if( i > n_menor )
dp[i][j] = min( dp[i][j] , dp[i-1][j] + v[i-1][j] ) ;
if( j >= 1 )
dp[i][j] = min( dp[i][j] , dp[i][j-1] + h[i][j-1] ) ;
}
for(int j = M-2 ; j >= 0 ; j-- )
dp[i][j] = min( dp[i][j] , dp[i][j+1] + h[i][j] ) ;
}
for(int j = 0 ; j < M ; j++ )
resp[ curr_c ][j] = dp[n_maior][j] ;
}
}
int m(int l, int r) { return (l+r)>>1 ; }
void tres_colunas( int adj_left[][MAXM] ,int adj_right[][MAXM] )
{
for(int diff = M-1 ; diff >= 1-M ; diff -- )
{
for(int x = 0 ; x < M ; x++ )
{
int y = x + diff ;
if( y >= M || y < 0 ) continue ;
int opt_min = 0 , opt_max = M-1 ;
if( x-1 >= 0 ) opt_min = opt[x-1][y] ;
if( y+1 < M ) opt_max = opt[x][y+1] ;
int opt_val = inf ;
for(int i = opt_min ; i <= opt_max ; i++ )
if( adj_left[x][i] + adj_right[i][y] < opt_val )
{
opt_val = adj_left[x][i] + adj_right[i][y] ;
resp[x][y] = opt_val ;
opt[x][y] = i ;
}
}
}
}
int adj_aux[MAXM][MAXM] ;
void merge(int pos, int l, int r)
{
int coluna_menor = end_bucket[ m(l,r) ] ;
int coluna_maior = ini_bucket[ m(l,r)+1 ] ;
calc(coluna_menor , coluna_maior ) ;
for(int i = 0 ; i < M ; i++ )
for(int j = 0 ; j < M ; j++ )
adj_aux[i][j] = resp[i][j] ;
tres_colunas( tree[pos*2] , adj_aux ) ;
for(int i = 0 ; i < M ; i++ )
for(int j = 0 ; j < M ; j++ )
adj_aux[i][j] = resp[i][j] ;
tres_colunas( adj_aux, tree[pos*2+1] ) ;
for(int i = 0 ; i < M ; i++ )
for(int j = 0 ; j < M ; j++ )
tree[pos][i][j] = resp[i][j] ;
}
void build_seg(int pos, int l, int r)
{
if( l == r )
{
calc( ini_bucket[l] , end_bucket[l] ) ;
for(int i = 0 ; i < M ; i++ )
for(int j = 0 ; j < M ; j++ )
tree[pos][i][j] = resp[i][j] ;
return ;
}
build_seg( pos*2,l, m(l,r) ) ;
build_seg( pos*2+1, m(l,r)+1 , r ) ;
merge(pos, l, r ) ;
}
void upd(int pos, int l, int r, int idx )
{
if( l == r )
{
calc( ini_bucket[l] , end_bucket[l] ) ;
for(int i = 0 ; i < M ; i++ )
for(int j = 0 ; j < M ; j++ )
tree[pos][i][j] = resp[i][j] ;
return ;
}
if( idx <= m(l,r) ) upd(pos*2,l,m(l,r),idx );
else upd(pos*2+1,m(l,r)+1, r, idx ) ;
merge(pos,l,r) ;
}
void init(int R, int C, int H[5000][200], int V[5000][200])
{
N = R ;
M = C ;
for(int i = 0 ; i < N ; i++ )
for(int j = 0 ; j < M ; j++ )
{
h[i][j] = H[i][j] ;
v[i][j] = V[i][j] ;
}
ini_bucket[1] = 0 ;
for(int i = 0 , j = 0 , k = 1 ; i < N ; i++ , j++ )
{
if( j == DIV )
{
k++ ;
j = 0 ;
ini_bucket[k] = i ;
}
my_block[i] = k ;
end_bucket[k] = i ;
}
build_seg(1,1, my_block[N-1] ) ;
}
void changeH(int P, int Q, int W)
{
h[P][Q] = W ;
upd( 1 , 1 , my_block[ N-1 ] , my_block[P] ) ;
}
void changeV(int P, int Q, int W)
{
v[P][Q] = W ;
upd( 1 , 1, my_block[N-1] , my_block[P] ) ;
}
int escape(int V1, int V2)
{
return tree[1][V1][V2] ;
}
Compilation message
grader.c: In function 'int main()':
grader.c:15:6: warning: variable 'res' set but not used [-Wunused-but-set-variable]
int res;
^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
17400 KB |
Output is correct |
2 |
Correct |
14 ms |
17400 KB |
Output is correct |
3 |
Correct |
102 ms |
19064 KB |
Output is correct |
4 |
Correct |
15 ms |
17400 KB |
Output is correct |
5 |
Correct |
14 ms |
17400 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
6 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
504 KB |
Output is correct |
5 |
Correct |
5 ms |
504 KB |
Output is correct |
6 |
Correct |
5 ms |
504 KB |
Output is correct |
7 |
Correct |
5 ms |
508 KB |
Output is correct |
8 |
Correct |
6 ms |
504 KB |
Output is correct |
9 |
Correct |
5 ms |
504 KB |
Output is correct |
10 |
Correct |
5 ms |
504 KB |
Output is correct |
11 |
Correct |
93 ms |
1528 KB |
Output is correct |
12 |
Correct |
5 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
317 ms |
1476 KB |
Output is correct |
2 |
Correct |
449 ms |
1496 KB |
Output is correct |
3 |
Correct |
425 ms |
1528 KB |
Output is correct |
4 |
Correct |
428 ms |
1400 KB |
Output is correct |
5 |
Correct |
459 ms |
1528 KB |
Output is correct |
6 |
Correct |
5 ms |
380 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
2227 ms |
1400 KB |
Output is correct |
10 |
Correct |
5 ms |
380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
21496 KB |
Output is correct |
2 |
Correct |
20 ms |
21496 KB |
Output is correct |
3 |
Correct |
19 ms |
21496 KB |
Output is correct |
4 |
Correct |
60 ms |
22264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
320 ms |
1476 KB |
Output is correct |
2 |
Correct |
443 ms |
1400 KB |
Output is correct |
3 |
Correct |
432 ms |
1528 KB |
Output is correct |
4 |
Correct |
429 ms |
1480 KB |
Output is correct |
5 |
Correct |
467 ms |
1528 KB |
Output is correct |
6 |
Correct |
19 ms |
21496 KB |
Output is correct |
7 |
Correct |
18 ms |
21496 KB |
Output is correct |
8 |
Correct |
19 ms |
21496 KB |
Output is correct |
9 |
Correct |
62 ms |
22264 KB |
Output is correct |
10 |
Correct |
14 ms |
17400 KB |
Output is correct |
11 |
Correct |
14 ms |
17400 KB |
Output is correct |
12 |
Correct |
101 ms |
19064 KB |
Output is correct |
13 |
Correct |
15 ms |
17400 KB |
Output is correct |
14 |
Correct |
14 ms |
17400 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
504 KB |
Output is correct |
19 |
Correct |
5 ms |
504 KB |
Output is correct |
20 |
Correct |
5 ms |
504 KB |
Output is correct |
21 |
Correct |
5 ms |
504 KB |
Output is correct |
22 |
Correct |
5 ms |
504 KB |
Output is correct |
23 |
Correct |
5 ms |
504 KB |
Output is correct |
24 |
Correct |
5 ms |
504 KB |
Output is correct |
25 |
Correct |
92 ms |
1512 KB |
Output is correct |
26 |
Correct |
5 ms |
504 KB |
Output is correct |
27 |
Correct |
2219 ms |
1476 KB |
Output is correct |
28 |
Correct |
3802 ms |
42132 KB |
Output is correct |
29 |
Correct |
3768 ms |
38288 KB |
Output is correct |
30 |
Correct |
3739 ms |
38392 KB |
Output is correct |
31 |
Correct |
3904 ms |
47276 KB |
Output is correct |
32 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
318 ms |
1400 KB |
Output is correct |
2 |
Correct |
449 ms |
1504 KB |
Output is correct |
3 |
Correct |
426 ms |
1400 KB |
Output is correct |
4 |
Correct |
427 ms |
1400 KB |
Output is correct |
5 |
Correct |
459 ms |
1528 KB |
Output is correct |
6 |
Correct |
19 ms |
21496 KB |
Output is correct |
7 |
Correct |
17 ms |
21496 KB |
Output is correct |
8 |
Correct |
18 ms |
21496 KB |
Output is correct |
9 |
Correct |
61 ms |
22264 KB |
Output is correct |
10 |
Correct |
14 ms |
17400 KB |
Output is correct |
11 |
Correct |
15 ms |
17400 KB |
Output is correct |
12 |
Correct |
107 ms |
19064 KB |
Output is correct |
13 |
Correct |
17 ms |
17400 KB |
Output is correct |
14 |
Correct |
14 ms |
17400 KB |
Output is correct |
15 |
Correct |
2380 ms |
62084 KB |
Output is correct |
16 |
Correct |
15271 ms |
65152 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
504 KB |
Output is correct |
21 |
Correct |
6 ms |
504 KB |
Output is correct |
22 |
Correct |
5 ms |
504 KB |
Output is correct |
23 |
Correct |
5 ms |
504 KB |
Output is correct |
24 |
Correct |
5 ms |
504 KB |
Output is correct |
25 |
Correct |
6 ms |
504 KB |
Output is correct |
26 |
Correct |
5 ms |
504 KB |
Output is correct |
27 |
Correct |
92 ms |
2936 KB |
Output is correct |
28 |
Correct |
5 ms |
504 KB |
Output is correct |
29 |
Correct |
2248 ms |
1560 KB |
Output is correct |
30 |
Correct |
3789 ms |
45840 KB |
Output is correct |
31 |
Correct |
14518 ms |
70612 KB |
Output is correct |
32 |
Correct |
14623 ms |
70584 KB |
Output is correct |
33 |
Correct |
3827 ms |
38444 KB |
Output is correct |
34 |
Correct |
14815 ms |
59384 KB |
Output is correct |
35 |
Correct |
3805 ms |
38432 KB |
Output is correct |
36 |
Correct |
14963 ms |
59160 KB |
Output is correct |
37 |
Correct |
3917 ms |
47544 KB |
Output is correct |
38 |
Correct |
14711 ms |
71196 KB |
Output is correct |
39 |
Correct |
5 ms |
376 KB |
Output is correct |
40 |
Correct |
14896 ms |
59412 KB |
Output is correct |