結果
| 問題 |
No.2858 Make a Palindrome
|
| コンテスト | |
| ユーザー |
tottoripaper
|
| 提出日時 | 2024-08-27 23:02:38 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 436 ms / 3,000 ms |
| コード長 | 2,988 bytes |
| コンパイル時間 | 1,985 ms |
| コンパイル使用メモリ | 197,840 KB |
| 最終ジャッジ日時 | 2025-02-24 02:34:44 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 40 |
ソースコード
#include <bits/stdc++.h>
using ll = std::int64_t;
using T = std::tuple<int, int, int>;
template <typename C>
std::vector<int> manacher(C seq){
auto n = seq.size();
std::vector<int> res(n, 0);
int c = 0;
for(int i = 0; i < n; i += 1){
int ri = c - (i - c);
if(ri >= 0 && i + res[ri] < c + res[c]){
res[i] = res[ri];
}else{
int j = c + res[c] - i;
while(i - j >= 0 && i + j < n && seq[i - j] == seq[i + j]){
j += 1;
}
res[i] = j;
c = i;
}
}
return res;
}
int main(){
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
int T;
std::cin >> T;
for(int _=0;_<T;_++){
int N, M;
std::cin >> N >> M;
std::string S1;
std::cin >> S1;
S1 = S1 + S1 + S1;
std::string S2(S1.size() * 2, '$');
for(int i=0;i<S1.size();i++){
S2[i * 2] = S1[i];
}
auto L1 = manacher(S1);
auto L2 = manacher(S2);
auto check = [&](int x){
{
int last = std::min(x, 2) * N;
for(int i=0;i<last;i++){
int r1 = std::min(L1[i], last - i);
int r2 = std::min(L2[i * 2 + 1] / 2, last - (i + 1));
int c = std::max(r1 * 2 - 1, r2 * 2);
if(c >= M){return true;}
}
}
if(x <= 2){return false;}
for(int i=N;i<2*N;i++){
if(L1[i] >= std::max(i - (N - 1), 2 * N - i)){
int r = std::min(2 * N - i, i - (N - 1));
ll c = 1ll * (1 + (x - 1) / 2 * 2) * (2 * r - 1) + 1ll * (x / 2 * 2) * (N - (2 * r - 1));
if(c >= M){
return true;
}
}
}
for(int i=N;i<2*N;i++){
int r = L2[i * 2 + 1] / 2;
if(r >= std::max(i - (N - 1), 2 * N - (i + 1))){
int r2 = std::min(2 * N - (i + 1), i - (N - 1));
ll c = 1ll * (1 + (x - 1) / 2 * 2) * (2 * r2) + 1ll * (x / 2 * 2) * (N - (2 * r2));
if(c >= M){
return true;
}
}
}
int r = L2[(2 * N - 1) * 2 + 1] / 2;
if(r >= N){
ll c = 1ll * (x / 2) * (2 * N);
if(c >= M){
return true;
}
}
return false;
};
if(check(2'000'000'000)){
int ok = 2'000'000'000, ng = 0;
while(ok - ng >= 2){
int m = ng + (ok - ng) / 2;
if(check(m)){
ok = m;
}else{
ng = m;
}
}
std::cout << ok << std::endl;
}else{
std::cout << -1 << std::endl;
}
}
}
tottoripaper