提出 #49162531
ソースコード 拡げる
/*
Author: cnyz
世界があたしを拒んでも今 愛の唄 歌わせてくれないかな
*/
#include<bits/stdc++.h>
using namespace std;
using db=double;
using ll=long long;
using vi=vector<int>;
using pii=pair<int,int>;
using ull=unsigned long long;
#define fi first
#define se second
#define gc getchar
#define pb emplace_back
#define mkp make_pair
#define push emplace
#define sz(a) (int)a.size()
#define all(p) p.begin(),p.end()
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define ppc(x) __builtin_popcount(x)
#define clz(x) __builtin_clz(x)
bool Mbe;
void chkmax(int &x,int y) {if(x<y) x=y;}
void chkmin(int &x,int y) {if(x>y) x=y;}
void chkmax(ll &x,ll y) {if(x<y) x=y;}
void chkmin(ll &x,ll y) {if(x>y) x=y;}
int read() { int x; cin>>x; return x; }
const int N=2e5+5;
int n,p,q,a[N],b[N],c[N];
struct Hash {
int pw[N],s[N],p;
void init(int base,int mod,int *a,int n) {
p=mod,pw[0]=1;
FOR(i,1,n) s[i]=(1ll*s[i-1]*base%p+a[i]%p),pw[i]=1ll*pw[i-1]*base%p;
}
int get(int l,int r) {
return (s[r]-1ll*s[l-1]*pw[r-l+1]%p+p)%p;
}
} p1,p2,q2,q1;
map<pii,int> mp;
bool chk(int x) {
mp.clear();
FOR(i,1,p-x+1) mp[mkp(p1.get(i,i+x-1),p2.get(i,i+x-1))]=1;
FOR(i,1,q-x+1) if(mp.find(mkp(q1.get(i,i+x-1),q2.get(i,i+x-1)))!=mp.end()) return 1;
return 0;
}
vector<pii> G[N];
int dis[N];
int bfs(int u) {
FOR(i,1,n+2) dis[i]=n*10;
dis[u]=0; deque<int> q; q.push_back(u);
while(sz(q)) {
int u=q.front(); q.pop_front();
for(auto [v,d]:G[u]) if(dis[v]>dis[u]+d) {
dis[v]=dis[u]+d;
if(d==0) q.push_front(v);
else q.push_back(v);
}
}
return dis[n+2];
}
int calc() {
int S=n+1,T=n+2;
FOR(i,1,p) G[S].pb(mkp(b[i],0));
FOR(i,1,q) G[c[i]].pb(mkp(T,0));
FOR(i,1,n-1) G[a[i]].pb(mkp(a[i+1],1)),G[a[i+1]].pb(mkp(a[i],1));
int d=bfs(S);
return p+q+d*2-2;
}
void solve() {
n=read(); FOR(i,1,n) a[i]=read();
p=read(); FOR(i,1,p) b[i]=read();
q=read(); FOR(i,1,q) c[i]=read();
p1.init(19491001,1e9+7,b,p);
p2.init(19491001,1e9+9,b,p);
q1.init(19491001,1e9+7,c,q);
q2.init(19491001,1e9+9,c,q);
int l=1,r=min(p,q),s=0;
while(l<=r) {
int mid=(l+r)>>1;
if(chk(mid)) s=mid,l=mid+1;
else r=mid-1;
}
if(s) cout<<p+q-2*s<<"\n";
else cout<<calc()<<"\n";
}
bool Med;
int main() {
ios::sync_with_stdio(false),cin.tie(0);
// fprintf(stderr, "%.3lf MB\n", (&Med - &Mbe) / 1048576.0);
int T=1;
while(T--) solve();
// fprintf(stderr, "%d ms\n", int(1e3 * clock() / CLOCKS_PER_SEC));
}
提出情報
| 提出日時 |
|
| 問題 |
E - Keep Being Substring |
| ユーザ |
cnyz |
| 言語 |
C++ 20 (gcc 12.2) |
| 得点 |
700 |
| コード長 |
2748 Byte |
| 結果 |
AC |
| 実行時間 |
781 ms |
| メモリ |
18668 KiB |
ジャッジ結果
| セット名 |
Sample |
All |
| 得点 / 配点 |
0 / 0 |
700 / 700 |
| 結果 |
|
|
| セット名 |
テストケース |
| Sample |
example0.txt, example1.txt |
| All |
000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, 043.txt, 044.txt, 045.txt, 046.txt, 047.txt, 048.txt, 049.txt, 050.txt, 051.txt, 052.txt, 053.txt, 054.txt, 055.txt, 056.txt, 057.txt, 058.txt, 059.txt, example0.txt, example1.txt |
| ケース名 |
結果 |
実行時間 |
メモリ |
| 000.txt |
AC |
4 ms |
3560 KiB |
| 001.txt |
AC |
84 ms |
18388 KiB |
| 002.txt |
AC |
13 ms |
4900 KiB |
| 003.txt |
AC |
620 ms |
18668 KiB |
| 004.txt |
AC |
18 ms |
5804 KiB |
| 005.txt |
AC |
16 ms |
8248 KiB |
| 006.txt |
AC |
769 ms |
14436 KiB |
| 007.txt |
AC |
770 ms |
14416 KiB |
| 008.txt |
AC |
781 ms |
14440 KiB |
| 009.txt |
AC |
769 ms |
14324 KiB |
| 010.txt |
AC |
772 ms |
14472 KiB |
| 011.txt |
AC |
426 ms |
12872 KiB |
| 012.txt |
AC |
617 ms |
13624 KiB |
| 013.txt |
AC |
740 ms |
14180 KiB |
| 014.txt |
AC |
754 ms |
14360 KiB |
| 015.txt |
AC |
709 ms |
14356 KiB |
| 016.txt |
AC |
728 ms |
14492 KiB |
| 017.txt |
AC |
701 ms |
14464 KiB |
| 018.txt |
AC |
736 ms |
14484 KiB |
| 019.txt |
AC |
39 ms |
15996 KiB |
| 020.txt |
AC |
36 ms |
15940 KiB |
| 021.txt |
AC |
36 ms |
15964 KiB |
| 022.txt |
AC |
34 ms |
15976 KiB |
| 023.txt |
AC |
35 ms |
16048 KiB |
| 024.txt |
AC |
36 ms |
15964 KiB |
| 025.txt |
AC |
34 ms |
15980 KiB |
| 026.txt |
AC |
13 ms |
10500 KiB |
| 027.txt |
AC |
13 ms |
10612 KiB |
| 028.txt |
AC |
13 ms |
10428 KiB |
| 029.txt |
AC |
16 ms |
12336 KiB |
| 030.txt |
AC |
17 ms |
12272 KiB |
| 031.txt |
AC |
16 ms |
12200 KiB |
| 032.txt |
AC |
11 ms |
8184 KiB |
| 033.txt |
AC |
2 ms |
3512 KiB |
| 034.txt |
AC |
108 ms |
7452 KiB |
| 035.txt |
AC |
4 ms |
3740 KiB |
| 036.txt |
AC |
88 ms |
7784 KiB |
| 037.txt |
AC |
150 ms |
8168 KiB |
| 038.txt |
AC |
360 ms |
9148 KiB |
| 039.txt |
AC |
64 ms |
5544 KiB |
| 040.txt |
AC |
182 ms |
7608 KiB |
| 041.txt |
AC |
117 ms |
6712 KiB |
| 042.txt |
AC |
142 ms |
7128 KiB |
| 043.txt |
AC |
35 ms |
15332 KiB |
| 044.txt |
AC |
26 ms |
11564 KiB |
| 045.txt |
AC |
21 ms |
10920 KiB |
| 046.txt |
AC |
19 ms |
10760 KiB |
| 047.txt |
AC |
16 ms |
10040 KiB |
| 048.txt |
AC |
16 ms |
10540 KiB |
| 049.txt |
AC |
15 ms |
9576 KiB |
| 050.txt |
AC |
15 ms |
10288 KiB |
| 051.txt |
AC |
15 ms |
10324 KiB |
| 052.txt |
AC |
14 ms |
9412 KiB |
| 053.txt |
AC |
14 ms |
9728 KiB |
| 054.txt |
AC |
14 ms |
10796 KiB |
| 055.txt |
AC |
13 ms |
10116 KiB |
| 056.txt |
AC |
13 ms |
9440 KiB |
| 057.txt |
AC |
12 ms |
8996 KiB |
| 058.txt |
AC |
48 ms |
13224 KiB |
| 059.txt |
AC |
21 ms |
7448 KiB |
| example0.txt |
AC |
2 ms |
3496 KiB |
| example1.txt |
AC |
2 ms |
3508 KiB |