QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#412449 | #8228. 排序 | do_while_true | 100 ✓ | 4651ms | 460300kb | C++20 | 5.1kb | 2024-05-16 14:33:58 | 2024-05-16 14:33:58 |
Judging History
answer
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<ctime>
#include<random>
#include<array>
#include<assert.h>
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define dbg(x) cerr<<"In Line "<< __LINE__<<" the "<<#x<<" = "<<x<<'\n'
#define dpi(x,y) cerr<<"In Line "<<__LINE__<<" the "<<#x<<" = "<<x<<" ; "<<"the "<<#y<<" = "<<y<<'\n'
#define DE(fmt,...) fprintf(stderr, "Line %d : " fmt "\n",__LINE__,##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>pii;
typedef pair<ll,int>pli;
typedef pair<ll,ll>pll;
typedef pair<int,ll>pil;
typedef vector<int>vi;
typedef vector<ll>vll;
typedef vector<pii>vpii;
typedef vector<pll>vpll;
template<typename T>T cmax(T &x, T y){return x=x>y?x:y;}
template<typename T>T cmin(T &x, T y){return x=x<y?x:y;}
template<typename T>
T &read(T &r){
r=0;bool w=0;char ch=getchar();
while(ch<'0'||ch>'9')w=ch=='-'?1:0,ch=getchar();
while(ch>='0'&&ch<='9')r=r*10+(ch^48),ch=getchar();
return r=w?-r:r;
}
template<typename T1,typename... T2>
void read(T1 &x,T2& ...y){read(x);read(y...);}
const int mod=1000000007;
inline void cadd(int &x,int y){x=(x+y>=mod)?(x+y-mod):(x+y);}
inline void cdel(int &x,int y){x=(x-y<0)?(x-y+mod):(x-y);}
inline int add(int x,int y){return (x+y>=mod)?(x+y-mod):(x+y);}
inline int del(int x,int y){return (x-y<0)?(x-y+mod):(x-y);}
int qpow(int x,int y){
int s=1;
while(y){
if(y&1)s=1ll*s*x%mod;
x=1ll*x*x%mod;
y>>=1;
}
return s;
}
const int N=31;
const int M=11;
const int T=1100010;
const int inf=0x3f3f3f3f;
inline int bit(int x){return 1<<x;}
inline int all(int x){return (1<<x)-1;}
int n,m;
struct ele{
int f,g;
ele(){f=-inf;g=0;}
}dp[T];
int ok[T];
void cadd(ele &x,ele y){
if(y.f>x.f)x=y;
else if(y.f==x.f)cadd(x.g,y.g);
}
int f[T][N],g[T][N],mx[N];
int cf[T][N];
int nowd,lad,b[N],c[N];
int d[M];
pii enc(int *a,int d){
int now=0;
for(int w=1;w<=d;w++){
int s=0,c=0;
for(int i=w;i<=n;i+=d){
++s;c+=(a[i]==-1);
}
now=now*(s+1)+c;
}
int p=0;
for(int i=1;i<=n;i++)if(a[i]==0)p=i;
return mp(now,p);
}
void dec(int *a,int S,int o,int d){
for(int w=d;w>=1;w--){
int s=0,c=0;
for(int i=w;i<=n;i+=d)++s;
c=S%(s+1);
S=S/(s+1);
for(int i=w;i<=n;i+=d){
if(c)a[i]=-1,--c;
else a[i]=1;
}
}
a[o]=0;
}
void print(int S,int d){
static int a[N];
dec(a,S,0,d);
for(int i=1;i<=n;i++)cerr<<a[i]<<' ';
}
int qwq;
void dfs(int x,int o){
if(x==n+1){
if(!o)return ;
++qwq;
int T=enc(b,nowd).fi,ct=0;
cmax(mx[nowd],T);
for(int i=1;i<=n;i++)c[i]=b[i];
// if(qwq==5){
// dbg(lad);
// for(int i=1;i<=n;i++)cerr<<b[i]<<' ';
// cerr<<'\n';
// }
for(int w=1;w<=lad;w++){
int cf=0,c0=0,cz=0;
for(int i=w;i<=n;i+=lad){
if(c[i]==-1)++cf;
else if(c[i]==0)++c0,ct=cz;
else ++cz;
}
for(int i=w;i<=n;i+=lad){
if(cf)c[i]=-1,--cf;
else if(c0)c[i]=0,--c0;
else c[i]=1,--cz;
}
}
auto [S,p]=enc(c,lad);
g[T][o]=f[S][p]+ct;
if(nowd==d[1]){
dec(c,T,o,d[1]);
int w=o%d[1];
w=(w==0?d[1]:w);
int cz=0;
for(int i=w;i<=n;i+=d[1])
if(c[i]==1)
++cz;
c[o]=-1;
int A=enc(c,d[1]).fi;
cf[A][o]=g[T][o]+cz;
// print(A,d[1]);
// cerr<<'\n'<<o<<" : ";
// cerr<<cf[A][o]<<'\n';
// dpi(ct,qwq);
}
return ;
}
if(x<=nowd||b[x-nowd]<=-1)
b[x]=-1,dfs(x+1,o);
if(x<=nowd||b[x-nowd]<=1)
b[x]=1,dfs(x+1,o);
if((x<=nowd||b[x-nowd]<=0)&&!o)
b[x]=0,dfs(x+1,x);
}
vi pre[T];
vi nowvec;
void dbgdfs(int x){
nowvec.pb(x);
if(pre[x].empty()){
vi tmp=nowvec;
reverse(tmp.begin(),tmp.end());
for(auto i:tmp){
print(i,d[1]);
cerr<<'\n';
}
cerr<<'\n';
}
else{
if(pre[x].size()>=2){
cerr<<"!!! : ";
print(x,d[1]);
cerr<<'\n';
}
for(auto v:pre[x])dbgdfs(v);
}
nowvec.pop_back();
}
signed main(){
#ifdef do_while_true
assert(freopen("data.in","r",stdin));
// assert(freopen("data.out","w",stdout));
#endif
read(n,m);
for(int i=1;i<=m;i++)read(d[i]);
if(m==1){
cout<<n*(n-1)/2<<' '<<1<<'\n';
return 0;
}
for(int i=m-1;i>=1;i--){
nowd=d[i];lad=d[i+1];
dfs(1,0);
for(int j=0;j<=mx[lad];j++)for(int k=1;k<=n;k++)f[j][k]=-inf;
for(int j=0;j<=mx[nowd];j++)for(int k=1;k<=n;k++)f[j][k]=g[j][k],g[j][k]=-inf;
}
for(int i=1;i<=n;i++)b[i]=-1;
int sta=enc(b,d[1]).fi;
dp[sta].f=0;dp[sta].g=1;
ok[sta]=1;
queue<int>q;
q.push(sta);
while(!q.empty()){
int S=q.front();q.pop();
dec(b,S,0,d[1]);
for(int i=1;i<=n;i++){
if(b[i]==-1&&(i>=n-d[1]+1||b[i+d[1]]==1)){
b[i]=1;
int T=enc(b,d[1]).fi;
b[i]=-1;
if(!ok[T]){
q.push(T);
ok[T]=1;
}
auto z=dp[S];
z.f+=cf[S][i];
if(z.f>dp[T].f){
pre[T].clear();
}
cadd(dp[T],z);
if(z.f==dp[T].f){
pre[T].pb(S);
}
}
}
}
for(int i=1;i<=n;i++)b[i]=1;
sta=enc(b,d[1]).fi;
cout<<dp[sta].f<<' '<<dp[sta].g<<'\n';
// dbgdfs(sta);
#ifdef do_while_true
// cerr<<'\n'<<"Time:"<<1.0*clock()/CLOCKS_PER_SEC*1000<<" ms"<<'\n';
#endif
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 18
Accepted
Test #1:
score: 18
Accepted
time: 2ms
memory: 16388kb
input:
2 2 2 1
output:
1 1
result:
ok 2 number(s): "1 1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 17996kb
input:
5 4 5 4 2 1
output:
6 4
result:
ok 2 number(s): "6 4"
Test #3:
score: 0
Accepted
time: 4ms
memory: 18408kb
input:
8 4 6 3 2 1
output:
15 4
result:
ok 2 number(s): "15 4"
Test #4:
score: 0
Accepted
time: 0ms
memory: 18044kb
input:
8 6 8 7 5 4 2 1
output:
14 2
result:
ok 2 number(s): "14 2"
Test #5:
score: 0
Accepted
time: 2ms
memory: 18388kb
input:
8 3 8 7 1
output:
22 7
result:
ok 2 number(s): "22 7"
Test #6:
score: 0
Accepted
time: 2ms
memory: 16276kb
input:
8 1 1
output:
28 1
result:
ok 2 number(s): "28 1"
Subtask #2:
score: 27
Accepted
Dependency #1:
100%
Accepted
Test #7:
score: 27
Accepted
time: 8ms
memory: 22916kb
input:
16 2 6 1
output:
77 15
result:
ok 2 number(s): "77 15"
Test #8:
score: 0
Accepted
time: 36ms
memory: 26472kb
input:
16 8 10 9 8 7 6 5 4 1
output:
57 5
result:
ok 2 number(s): "57 5"
Test #9:
score: 0
Accepted
time: 44ms
memory: 26612kb
input:
16 10 10 9 8 7 6 5 4 3 2 1
output:
57 3
result:
ok 2 number(s): "57 3"
Test #10:
score: 0
Accepted
time: 37ms
memory: 26508kb
input:
16 7 10 9 8 6 5 4 1
output:
49 1
result:
ok 2 number(s): "49 1"
Test #11:
score: 0
Accepted
time: 4ms
memory: 18140kb
input:
16 4 7 6 2 1
output:
52 9
result:
ok 2 number(s): "52 9"
Subtask #3:
score: 21
Accepted
Dependency #2:
100%
Accepted
Test #12:
score: 21
Accepted
time: 8ms
memory: 18532kb
input:
22 3 5 3 1
output:
100 1
result:
ok 2 number(s): "100 1"
Test #13:
score: 0
Accepted
time: 4ms
memory: 15036kb
input:
22 1 1
output:
231 1
result:
ok 2 number(s): "231 1"
Test #14:
score: 0
Accepted
time: 307ms
memory: 65408kb
input:
22 4 10 8 3 1
output:
97 4
result:
ok 2 number(s): "97 4"
Test #15:
score: 0
Accepted
time: 282ms
memory: 62540kb
input:
22 5 10 7 6 3 1
output:
92 70
result:
ok 2 number(s): "92 70"
Test #16:
score: 0
Accepted
time: 390ms
memory: 64128kb
input:
22 6 10 9 8 7 3 1
output:
97 1
result:
ok 2 number(s): "97 1"
Test #17:
score: 0
Accepted
time: 406ms
memory: 65636kb
input:
22 10 10 9 8 7 6 5 4 3 2 1
output:
109 1
result:
ok 2 number(s): "109 1"
Subtask #4:
score: 10
Accepted
Test #18:
score: 10
Accepted
time: 4ms
memory: 18556kb
input:
14 2 10 1
output:
61 210
result:
ok 2 number(s): "61 210"
Test #19:
score: 0
Accepted
time: 0ms
memory: 18572kb
input:
18 2 2 1
output:
117 1
result:
ok 2 number(s): "117 1"
Test #20:
score: 0
Accepted
time: 1547ms
memory: 232224kb
input:
30 2 9 1
output:
264 84
result:
ok 2 number(s): "264 84"
Test #21:
score: 0
Accepted
time: 1220ms
memory: 193048kb
input:
29 2 9 1
output:
253 36
result:
ok 2 number(s): "253 36"
Test #22:
score: 0
Accepted
time: 184ms
memory: 54616kb
input:
25 2 8 1
output:
195 8
result:
ok 2 number(s): "195 8"
Subtask #5:
score: 24
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Test #23:
score: 24
Accepted
time: 4116ms
memory: 456992kb
input:
30 4 10 9 5 1
output:
188 40
result:
ok 2 number(s): "188 40"
Test #24:
score: 0
Accepted
time: 4623ms
memory: 458336kb
input:
30 9 10 9 8 7 6 5 4 3 1
output:
184 6
result:
ok 2 number(s): "184 6"
Test #25:
score: 0
Accepted
time: 4585ms
memory: 460300kb
input:
30 8 10 9 8 7 4 3 2 1
output:
154 1
result:
ok 2 number(s): "154 1"
Test #26:
score: 0
Accepted
time: 3871ms
memory: 458136kb
input:
30 8 10 8 7 6 5 4 3 1
output:
155 1
result:
ok 2 number(s): "155 1"
Test #27:
score: 0
Accepted
time: 3770ms
memory: 457312kb
input:
30 6 10 8 6 4 3 1
output:
150 4
result:
ok 2 number(s): "150 4"
Test #28:
score: 0
Accepted
time: 4651ms
memory: 459776kb
input:
30 10 10 9 8 7 6 5 4 3 2 1
output:
184 6
result:
ok 2 number(s): "184 6"
Test #29:
score: 0
Accepted
time: 3229ms
memory: 350348kb
input:
29 6 10 9 7 5 3 1
output:
129 200
result:
ok 2 number(s): "129 200"
Extra Test:
score: 0
Extra Test Passed