# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
419772 |
2021-06-07T12:43:07 Z |
Kerim |
Group Photo (JOI21_ho_t3) |
C++17 |
|
43 ms |
98116 KB |
#include "bits/stdc++.h"
#define MAXN 100009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x) cerr<< #x <<" = "<< x<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int N=5002;
int n,arr[N],inv[N][N],dp[N][N],odp[N][N],suf[N][N];
int F[N][N],AB[N][N],BA[N][N],par[N][N],G[N][N];
int g(int x,int y){
if(x>=y)return 0;
if(~odp[x][y])return odp[x][y];
return odp[x][y]=g(x,y-1)+par[y][y]-par[x-1][y];
}
int cost(int a,int b){
return g(a+1,b-1)+F[a][b-1]+(BA[b][b-1]-BA[b][a]);
}
int main(){
freopen("file.in", "r", stdin);
memset(odp,-1,sizeof odp);
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",arr+i);
for(int i=2;i<=n;i++)
for(int j=1;j<i;j++)
inv[arr[i]][arr[j]]++;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
par[i][j]=par[i-1][j]+inv[j][i];
for(int z=1;z<=n;z++)
for(int x=1;x<=n;x++)
G[x][z]=G[x-1][z]+inv[x][z];
for(int y=1;y<=n;y++)
for(int z=y+1;z<=n;z++)
F[y][z]=F[y][z-1]+G[y][z];
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
BA[i][j]=BA[i][j-1]+inv[i][j];
AB[i][j]=AB[i][j-1]+inv[j][i];
}
for(int a=n;a>=0;a--){
for(int b=a+1;b<=n;b++){
if(b==n)
dp[a][b]=cost(a,b);
else
dp[a][b]=suf[b][b+1]+cost(a,b);
}
suf[a][n+1]=INF;
for(int i=n;i>a;i--)
suf[a][i]=min(suf[a][i+1],dp[a][i]+AB[i][a]);
}
int ans=INF;
for(int i=1;i<=n;i++)
umin(ans,dp[0][i]);
printf("%d\n",ans);
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:32:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen("file.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:34:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
Main.cpp:35:28: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | for(int i=1;i<=n;i++)scanf("%d",arr+i);
| ~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
43 ms |
98116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
43 ms |
98116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
43 ms |
98116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
43 ms |
98116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
43 ms |
98116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |