This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// seq ai com n numeros
// a dist de 2 intervalos e a qtd de caras diferentes ai+x e ai+y
// dois intervalos são k similares se a dist entre eles é no max k
// p cada inicio diga qts intervalos ele tem tal q a dist eh menor que k
// n pequeno acha a dist de cada um pros outros n^2
// k == 0 qts intervalos iguais? -> freq dos valores
// p cada cara qual o igual a ele mais prox dele
// consigo descobrir em o(n) qts caras sao iguais a mim?
// dado um i e um l vc sabe dizer qual a pos ele vai estar num vetor j?
#include<bits/stdc++.h>
#define ll long long
using namespace std ;
const int prime = 37 ;
const int mod = 1e9 + 7 ;
const int maxn = 1e4 + 5 ;
int n, l, v[maxn], dist[maxn][maxn], k, q ;
ll pot[maxn], inv[maxn], pref[maxn], val[maxn] ;
void solve(){
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= n ; j++){ // dist de i p j
if(i == j || i + l - 1 > n || j + l - 1 > n) continue ;
int ctr = 0 ;
for(int k = 0 ; k < l ; k++) if(v[i+k] != v[j+k]) ctr++ ;
dist[i][j] = dist[j][i] = ctr ;
}
}
}
ll exp(ll a, ll b){
ll ans = 1 ;
while(b){
if(b&1) ans = (ans*a)%mod ;
a = (a*a)%mod ;
b >>= 1 ;
}
return ans ;
}
void solve_hash(){
pot[0] = 1LL ;
for(int i = 1 ; i < maxn ; i++) pot[i] = (pot[i-1]*prime)%mod ;
inv[0] = 1LL ; inv[1] = exp(prime, mod-2) ;
for(int i = 2 ; i < maxn ; i++) inv[i] = (inv[i-1]*inv[1])%mod ;
for(int i = 1 ; i <= n ; i++) pref[i] = (pref[i-1] + (v[i]*pot[i]*1LL)%mod)%mod ;
vector<ll> vals ;
for(int i = 1 ; i + l - 1 <= n ; i++){
ll v1 = pref[i+l-1] ;
if(i - 1 >= 1) v1 = (v1 - pref[i-1] + mod)%mod ;
v1 = (v1*inv[i])%mod ;
vals.push_back(v1) ; val[i] = v1 ;
}
sort(vals.begin(), vals.end()) ;
for(int i = 1 ; i + l - 1 <= n ; i++){
int l = lower_bound(vals.begin(), vals.end(), val[i]) - vals.begin() ;
int r = upper_bound(vals.begin(), vals.end(), val[i]) - vals.begin() ;
cout << (r - l - 1) << " " ;
}
cout << "\n" ;
}
int main(){
ios_base::sync_with_stdio(false) ; cin.tie(NULL) ;
cin >> n >> l ;
for(int i = 1 ; i <= n ; i++) cin >> v[i] ;
// for(int i = 1 ; i <= n ; i++){
// for(int j = 1 ; j <= n ; j++){
// cout << dist[i][j] <<" " ;
// }
// cout << "\n" ;
// }
cin >> q ;
cin >> k ;
if(q == 1 && !k){
solve_hash() ;
exit(0) ;
}
solve() ;
for(int i = 1 ; i + l - 1 <= n ; i++){
int ct = 0 ;
for(int j = 1 ; j + l - 1 <= n ; j++) if(i != j && dist[i][j] <= k) ct++ ;
cout << ct << " " ;
}
cout << "\n" ;
for(int i = 1 ; i < q ; i++){
cin >> k ;
for(int i = 1 ; i + l - 1 <= n ; i++){
int ct = 0 ;
for(int j = 1 ; j + l - 1 <= n ; j++) if(i != j && dist[i][j] <= k) ct++ ;
cout << ct << " " ;
}
cout << "\n" ;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |