제출 #1026727

#제출 시각아이디문제언어결과실행 시간메모리
1026727doducanhMatching (CEOI11_mat)C++14
100 / 100
168 ms51968 KiB
#include <bits/stdc++.h> using namespace std; #define ii pair<int,int> #define fi first #define se second const int maxn=1e6+7; int p[maxn]; int a[maxn]; ii pre[maxn];int h[maxn]; ii nex[maxn];int g[maxn]; int kmp[maxn]; int n,m; void init(){ vector<int>pos(n+2,0); for(int i=1;i<=n;i++)pos[p[i]]=i; for(int i=1;i<=n;i++)swap(pos[i],p[i]); for(int i=1;i<=n;i++){ nex[i]={i+1,pos[i+1]}; pre[i]={i-1,pos[i-1]}; } for(int i=n;i>=1;i--){ int k=p[i]; h[i]=pre[k].se; g[i]=nex[k].se; pre[nex[k].fi]=pre[k]; nex[pre[k].fi]=nex[k]; } } bool can(int i,int j,int u,int v,bool type){ if(i>n)return 0; if(type==0){ if(u&&p[j-i+u]>p[j])return 0; if(v&&p[j-i+v]<p[j])return 0; } else{ if(u&&a[j-i+u]>a[j])return 0; if(v&&a[j-i+v]<a[j])return 0; } return 1; } main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>n>>m; for(int i=1;i<=n;i++)cin>>p[i]; init(); for(int i=1;i<=m;i++)cin>>a[i]; // for(int i=1;i<=n;i++){ // cout<<h[i]<<" "<<g[i]<<"\n"; // } kmp[1]=0; for(int i=2,j=0;i<=n;i++){ while(j&&!can(j+1,i,h[j+1],g[j+1],0))j=kmp[j]; if(can(j+1,i,h[j+1],g[j+1],0))j++; kmp[i]=j; } // for(int i=1;i<=n;i++)cout<<kmp[i]<<"\n"; // cout<<8<<" "<<can(2,7,h[2],g[2],1)<<"\n"; vector<int>ans; for(int i=1,j=0;i<=m;i++){ while(j&&!can(j+1,i,h[j+1],g[j+1],1)){ j=kmp[j]; } // if(i==7)cout<<j<<"\n"; if(can(j+1,i,h[j+1],g[j+1],1))j++; // if(i==7)cout<<j<<"\n"; if(j==n)ans.push_back(i-n+1); } cout<<ans.size()<<"\n"; for(int x:ans)cout<<x<<" "; return 0; }

컴파일 시 표준 에러 (stderr) 메시지

mat.cpp:42:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   42 | main()
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...