QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#793565 | #9584. 顾影自怜 | jiangzhihui# | WA | 164ms | 46428kb | C++14 | 1.4kb | 2024-11-29 21:08:00 | 2024-11-29 21:08:03 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
int a[maxn],l[maxn],r[maxn];
vector<int> pos[maxn];
void solve(){
int ans=0,n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
pos[i].clear();
for(int i=1;i<=n;i++){
cin>>a[i];
pos[a[i]].push_back(i);
l[i]=0;
r[i]=n+1;
}
stack<int> stkr,stkl;
for(int i=1;i<=n;i++){
while(!stkr.empty()&&a[i]>=a[stkr.top()]){
r[stkr.top()]=i;
stkr.pop();
}
stkr.push(i);
}
for(int i=n;i>=1;i--){
while(!stkl.empty()&&a[i]>a[stkl.top()]){
l[stkl.top()]=i;
stkl.pop();
}
stkl.push(i);
}
for(int i=1;i<=n;i++){
//以a[i]为最大值
//l[i],r[i]
int ll=0,rr=pos[a[i]].size()-1,res=0;//查找i是第几个a[i]的下标
while(ll<=rr){
int mid=(ll+rr)/2;
if(pos[a[i]][mid]>=i){
res=mid;
rr=mid-1;
}else{
ll=mid+1;
}
}
if(res+1<k)continue;
int p=pos[a[i]][res+1-k];//从这往前数第k个a[i]的下标
if(p<l[i])continue;
ans+=(p-l[i])*(r[i]-i);
}
cout<<ans<<endl;
}
int main(){
int q;
cin>>q;
while(q--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 32088kb
input:
2 5 2 1 3 3 2 2 4 3 1 4 2 1
output:
7 0
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 164ms
memory: 46428kb
input:
1 1000000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
output:
1784293664
result:
wrong answer 1st lines differ - expected: '500000500000', found: '1784293664'