This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
#define OPEN freopen(".in", "r", stdin); \
freopen(".out", "w", stdout);
#else
#define OPEN void(23);
#endif
void solve1(int n, string str)
{
str = '$' + str;
double ans = 2;
pair <int, int> cev;
int l = 1, r = 1;
while(r <= n && l <= n)
{
if(r + 1 <= n && str[r +1] == str[l]) r++;
else
{
l = r +1;
continue;
}
double calc = double(1.0f) / (r - l +1);
if(calc < ans)
{
ans = calc;
cev = {l, r};
}
}
bool ok = true;
for(int i = 1; i +1 <= n; i++) ok &= str[i] == str[i +1];
if(!ok)
{
double calc = double(2.0f) / n;
if(calc < ans)
{
ans = calc;
cev = {1, n};
}
}
cerr << 'T' << ans;
cout << cev.first << " " << cev.second;
return;
}
void solve2(int n, string str)
{
str = '$' + str;
vector <array <int, 26>> pref(n +1);
for(int i = 0; i < 26; i++) pref[0][i] = 0;
for(int i = 1; i <= n; i++)
{
pref[i] = pref[i -1];
pref[i][str[i] - 'a']++;
}
long double ans = 2;
pair <int, int> inds;
for(int l = 1; l <= n; l++)
{
for(int r = l; r <= n; r++)
{
array <int, 26> c;
int tot = 0;
for(int i = 0; i < 26; i++)
c[i] = pref[r][i] - pref[l -1][i], tot += (c[i] != 0);
long double cost = tot;
cost /= (r - l +1);
//cerr << l << " " << r << " " << cost << "\n";
if(cost < ans)
{
ans = cost;
inds = {l, r};
}
}
}
cout << inds.first << " " << inds.second << "\n";
return;
}
int arr[256];
int find(int x)
{
int res = 0;
while(x) res += (x & 1), x /= 2;
return res;
}
int check(int a, int b)
{
bool ok = true;
for(int i = 5; i >= 1; i--)
{
if(b & 1)
{
ok &= (a & 1);
}
b >>= 1;
a >>= 1;
}
return ok;
}
void solve3(int n, string str)
{
str = '$' + str;
arr['a'] = 1;
arr['b'] = 2;
arr['c'] = 4;
arr['d'] = 8;
arr['e'] = 16;
double ans = 1;
pair <int, int> cev = {1, 1};
for(int i = 0; i < (1 << 5); i++)
{
//cerr << i << " ";
int l = 1, r = 1;
while(r <= n && l <= n)
{
//cerr << l << " " << r << "\n";
if(!check(i, arr[str[l]]))
{
l++;
r++;
continue;
}
if(r + 1 <= n && check(i, arr[str[r +1]])) r++;
else
{
l = r +1;
r++;
continue;
}
double calc = double(find(i)) / (r - l +1);
if(l == 2 && r == 19) cerr << calc;
if(calc < ans)
{
ans = calc;
cev = {l, r};
}
}
}
cerr << ans;
cout << cev.first << " " << cev.second << "\n";
}
int32_t main()
{
OPEN;
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n; string str; cin >> n >> str;
int cnta = count(str.begin(), str.end(), 'a');
int cntb = count(str.begin(), str.end(), 'b');
int cntc = count(str.begin(), str.end(), 'c');
int cntd = count(str.begin(), str.end(), 'd');
int cnte = count(str.begin(), str.end(), 'e');
if(cnta + cntb == n) solve1(n, str);
else if(cnta + cntb + cntc + cntd + cnte == n) solve3(n, str);
else solve2(n, str);
}
Compilation message (stderr)
nivelle.cpp: In function 'void solve3(long long int, std::string)':
nivelle.cpp:144:36: warning: array subscript has type 'char' [-Wchar-subscripts]
144 | if(!check(i, arr[str[l]]))
| ^
nivelle.cpp:150:52: warning: array subscript has type 'char' [-Wchar-subscripts]
150 | if(r + 1 <= n && check(i, arr[str[r +1]])) r++;
| ^
# | 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... |