// Отче наш, сущий на небесах! Да святится имя Твое; да приидет Царствие Твое; да будет воля Твоя и на земле, как на небе; хлеб наш насущный подавай нам на каждый день; и прости нам грехи наши, ибо и мы прощаем всякому должнику нашему; и не введи нас в искушение, но избавь нас от лукавого ML, TL, WA
#include <bits/stdc++.h>
#pragma optimize("Ofast")
#pragma target("avx2")
#define ll long long
#define ld long double
#define pii pair<int,int>
#define F first
#define S second
#define in insert
#define all(v) v.begin(),v.end()
#define pb push_back
#define sz(s) (int)s.size()
#define ppb pop_back
#define mem(a,i) memset(a,i,sizeof(a))
#define lb lower_bound
#define ub upper_bound
#define int ll
using namespace std;
const int MAX=21;
const int N=1000+10;
const int inf=1e18;
const int mod=1e9+9;
const int B=2003;
const ld eps=1e-9;
const int dx[4]={0,0,1,-1};
const int dy[4]={-1,1,0,0};
int binpow(int a,int n){
if(n==0)return 1;
if(n%2==1)return a*binpow(a,n-1)%mod;
int k=binpow(a,n/2);
return (k*k)%mod;
}
int n,m;
int ans=-inf;
int a[MAX][MAX];
int d[MAX][MAX];
int use[MAX][MAX];
int mark[MAX][MAX];
int bit(int i,int j){
return (i>>j)&1;
}
int good(int i,int j){
return (1<=i&&i<=n&&1<=j&&j<=m);
}
void dfs(int i,int j){
use[i][j]=1;
for(int k=0;k<4;k++){
int ni=i+dx[k];
int nj=j+dy[k];
if(good(ni,nj)&&!use[ni][nj]&&mark[ni][nj]){
dfs(ni,nj);
}
}
}
void solve(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
}
}
for(int mask=1;mask<(1<<(n*m));mask++){
mem(use,0);
mem(mark,0);
vector<pii> v;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(bit(mask,(i-1)*m+j-1)){
v.pb({i,j});
mark[i][j]=1;
}
}
}
assert(!v.empty());
// cout<<sz(v)<<"\n";
dfs(v[0].F,v[0].S);
int mn=inf,mx=-inf,ok=1;
for(pii x:v){
if(!use[x.F][x.S])ok=0;
mn=min(mn,a[x.F][x.S]);
mx=max(mx,a[x.F][x.S]);
}
if(ok){
ans=max(ans,mx-mn-sz(v));
}
}
cout<<ans<<"\n";
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
// cin>>t;
while(t--){
solve();
}
}
Compilation message
maxcomp.cpp:4: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
4 | #pragma optimize("Ofast")
|
maxcomp.cpp:5: warning: ignoring '#pragma target ' [-Wunknown-pragmas]
5 | #pragma target("avx2")
|
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
366 ms |
344 KB |
Output is correct |
2 |
Correct |
420 ms |
440 KB |
Output is correct |
3 |
Correct |
416 ms |
448 KB |
Output is correct |
4 |
Correct |
334 ms |
600 KB |
Output is correct |
5 |
Correct |
413 ms |
344 KB |
Output is correct |
6 |
Correct |
451 ms |
444 KB |
Output is correct |
7 |
Correct |
443 ms |
448 KB |
Output is correct |
8 |
Correct |
419 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
366 ms |
344 KB |
Output is correct |
2 |
Correct |
420 ms |
440 KB |
Output is correct |
3 |
Correct |
416 ms |
448 KB |
Output is correct |
4 |
Correct |
334 ms |
600 KB |
Output is correct |
5 |
Correct |
413 ms |
344 KB |
Output is correct |
6 |
Correct |
451 ms |
444 KB |
Output is correct |
7 |
Correct |
443 ms |
448 KB |
Output is correct |
8 |
Correct |
419 ms |
600 KB |
Output is correct |
9 |
Execution timed out |
1046 ms |
348 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
366 ms |
344 KB |
Output is correct |
2 |
Correct |
420 ms |
440 KB |
Output is correct |
3 |
Correct |
416 ms |
448 KB |
Output is correct |
4 |
Correct |
334 ms |
600 KB |
Output is correct |
5 |
Correct |
413 ms |
344 KB |
Output is correct |
6 |
Correct |
451 ms |
444 KB |
Output is correct |
7 |
Correct |
443 ms |
448 KB |
Output is correct |
8 |
Correct |
419 ms |
600 KB |
Output is correct |
9 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 11 |
10 |
Halted |
0 ms |
0 KB |
- |