結果
| 問題 |
No.1867 Partitions and Inversions
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-12-26 20:22:15 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,749 bytes |
| コンパイル時間 | 1,795 ms |
| コンパイル使用メモリ | 173,756 KB |
| 実行使用メモリ | 76,280 KB |
| 最終ジャッジ日時 | 2024-09-27 15:13:36 |
| 合計ジャッジ時間 | 8,588 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 5 WA * 60 |
ソースコード
#include<bits/stdc++.h>
#define int long long
#define uint unsigned long long
#define il inline
#define ct const
#define dl double
#define pk push_back
#define fi first
#define N 3010
#define se second
#define pii pair<int,int>
#define inf (int)(1000000000000000000)
using namespace std;
#define lowbit(x) (x&-x)
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
il int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);ch=getchar();
}
return x*f;
}
char f__[40];
il void write(int x){
int cnt=0;
if(x<0){
putchar('-');x=-x;
}
if(!x) putchar('0');
while(x){
f__[cnt++]=x%10+'0';x/=10;
}
while(cnt) putchar(f__[--cnt]);
}
int n,p[N],sum[N][N];
struct node{
int l,r,mid,w;
il bool operator<(ct node &x)ct{
return w<x.w;
}
};
priority_queue<node> P;
il pii solve(int l,int r){
int res=0,mn=inf,q=l;
for(int i=l;i<r;++i){
res+=sum[r][p[i]]-sum[i][p[i]]-(sum[i-1][n]-sum[i-1][p[i]]-(sum[l-1][n]-sum[l-1][p[i]]));
if(res<mn){
q=i;mn=res;
}
}
return pii(q,mn);
}
signed main(){
// freopen("1.in","r",stdin);
// freopen("1.out","w",stdout);
n=read();
for(int i=1;i<=n;++i) p[i]=read();
for(int i=1;i<=n;++i){
for(int j=1;j<=i;++j) ++sum[i][p[j]];
for(int j=1;j<=n;++j) sum[i][j]+=sum[i][j-1];
}
pii y=solve(1,n);
P.push((node){1,n,y.fi,y.se});
int res=0;
for(int i=1;i<=n;++i){
node x=P.top();P.pop();
write(res);putchar('\n');
res+=x.w;
if(x.l<x.mid){
y=solve(x.l,x.mid);
P.push((node){x.l,x.mid,y.fi,y.se});
}
if(x.mid+1<x.r){
y=solve(x.mid+1,x.r);
P.push((node){x.mid+1,x.r,y.fi,y.se});
}
}
return 0;
}