QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#577230 | #9310. Permutation Counting 4 | wanghuimin | WA | 0ms | 3536kb | C++17 | 1.4kb | 2024-09-20 09:28:00 | 2024-09-20 09:28:01 |
Judging History
answer
#include<bits/stdc++.h>
#define mp make_pair
#define pii pair<int,int>
#define ll long long
#define pc putchar
inline int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48),ch=getchar();}
return x*f;
}
using namespace std;
const int maxn=2e5+5;
int n;
int main() {
int T=read();
while(T--){
n=read();
vector<int>a(n+5);
for(int i=1;i<=n;i++) a[i]=read();
//现在我们要建笛卡尔树。
vector<int>stk(n+4);int top=0;
vector<int>ls(n+4),rs(n+4);
vector<int>ans(n+4);
for(int i=1;i<=n;i++){
int pre=0;
while(top&&stk[a[top]]<a[i]) pre=stk[top],stk[top--]=0;
if(top){
rs[stk[top]]=i;
}
if(pre){
ls[i]=pre;
}
stk[++top]=i;
// for(int j=1;j<=top;j++) cout<<stk[j]<<" ";pc('\n');
}
int ret=0;
auto dfs =[&](auto &&dfs,int x)->void{
// cout<<"x:"<<x<<" a[x]:"<<a[x]<<" ls[x]:"<<ls[x]<<" rs[x]:"<<rs[x]<<endl;
if(ls[x]) ans[ls[x]]=ans[x]+(a[x]!=a[ls[x]]),dfs(dfs,ls[x]);
if(rs[x]) ans[rs[x]]=ans[x]+(a[x]!=a[rs[x]]),dfs(dfs,rs[x]);
ret+=ans[x];
};
dfs(dfs,stk[1]);
cout<<ret<<endl;
}
// system("pause");
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3536kb
input:
4 5 1 2 1 5 1 2 1 2 2 2 5 1 1 2 4 2 3 5 5 3 4 5 3 5 1 2 3 4 3 5 3 3 5 1 5 1 4 4 5 5 5 1 2
output:
6 1 1 0
result:
wrong answer 1st words differ - expected: '0', found: '6'