結果
| 問題 |
No.2210 equence Squence Seuence
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-02-11 18:41:57 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,591 ms / 2,000 ms |
| コード長 | 4,733 bytes |
| コンパイル時間 | 2,084 ms |
| コンパイル使用メモリ | 204,268 KB |
| 最終ジャッジ日時 | 2025-02-10 14:30:36 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 25 |
ソースコード
// 提出時にassertはオフ
#ifndef DEBUG
#ifndef NDEBUG
#define NDEBUG
#endif
#endif
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) (x).begin(), (x).end()
template <class T>
using vec = vector<T>;
struct rolhash_mod {
public:
// [2, MOD - 2]のbaseを乱数として生成
static ll genBase() {
random_device seed; // 非決定的な乱数
mt19937_64 mt(seed()); // メルセンヌ・ツイスタ 64bit
uniform_int_distribution<ll> randbase(2LL, MOD - 2);
return randbase(mt);
}
// べき乗のmod(tableを用意せずその場でやってるのでlog(beki)かかる)
constexpr static ll power(ll base, ll beki) {
rolhash_mod curbekiMod(base);
rolhash_mod ret(1);
while(beki > 0) {
if(beki & 1) ret *= curbekiMod;
curbekiMod *= curbekiMod;
beki >>= 1;
}
return ret.val();
}
constexpr rolhash_mod(ll x = 0) : value(calcMod(x)) {}
constexpr ll val() const { return value; }
constexpr rolhash_mod &operator+=(const rolhash_mod &a) {
if((value += a.value) >= MOD) value -= MOD;
return *this;
}
constexpr rolhash_mod &operator-=(const rolhash_mod &a) {
*this += (MOD - a.value);
return *this;
}
constexpr rolhash_mod &operator*=(const rolhash_mod &a) {
__int128_t product = (__int128_t)value * a.value;
product = (product >> 61) + (product & MOD);
if(product >= MOD) product -= MOD;
value = product;
return *this;
}
constexpr rolhash_mod operator+(const rolhash_mod &a) const {
rolhash_mod cpy(*this);
return cpy += a;
}
constexpr rolhash_mod operator-(const rolhash_mod &a) const {
rolhash_mod cpy(*this);
return cpy -= a;
}
constexpr rolhash_mod operator*(const rolhash_mod &a) const {
rolhash_mod cpy(*this);
return cpy *= a;
}
constexpr bool operator==(const rolhash_mod &a) { return value == a.value; }
private:
ll value; // 中で保持しておくmod
constexpr static ll MOD = (1LL << 61) - 1;
constexpr static ll calcMod(const ll &x) {
ll cur = x >> 61;
if((cur += (x & MOD)) >= MOD) cur -= MOD;
return cur;
}
};
// 部分文字列[l,r)のhash値を返すqueryを定数時間で
struct rolhash {
public:
template <class T>
rolhash(const vector<T> &input) : base(rolhash_mod::genBase()), N(input.size()) {
basebeki_table = vector<rolhash_mod>(N + 1);
basebeki_table[0] = 1;
for(int i = 1; i <= N; ++i) {
basebeki_table[i] = basebeki_table[i - 1] * base;
}
front_table = vector<rolhash_mod>(N + 1);
for(int i = 1; i <= N; ++i) {
front_table[i] = front_table[i - 1] * base + input[i - 1];
}
}
// 部分列[l,r)のhash値を返す
rolhash_mod query(int l, int r) const { return (front_table[r] - front_table[l] * basebeki_table[r - l]); }
// 部分列[0,i)のhash値を返す
rolhash_mod get_basebeki(int beki) const { return basebeki_table[beki]; }
// baseのbeki乗を返す
rolhash_mod get_front(int i) const { return front_table[i]; }
private:
constexpr static ll MOD = (1LL << 61) - 1;
const ll base;
const int N;
// baseの0乗からN乗を記録
vector<rolhash_mod> basebeki_table;
// [0,i)の部分列のhash値を記録
vector<rolhash_mod> front_table;
};
int N, K;
vec<int> A;
// X_iの最初j数字のhash値を返す
rolhash_mod calc_hash(int i, int j, const rolhash &rol) {
if(j <= i) return rol.get_front(j); // [0,j)
// [0,i) + [i+1,j+1)
return rol.get_front(i) * rol.get_basebeki(j - i) + rol.query(i + 1, j + 1);
}
// X_iとX_jの大小比較
bool cmphash(int i, int j, const rolhash &rol) {
if(calc_hash(i, N - 1, rol) == calc_hash(j, N - 1, rol)) return false;
int l = 0, r = N - 1;
while(r - l > 1) {
int m = (l + r) / 2;
if(calc_hash(i, m, rol) == calc_hash(j, m, rol)) l = m;
else r = m;
}
int X_i_num = A[l], X_j_num = A[l];
if(i <= l) X_i_num = A[l + 1];
if(j <= l) X_j_num = A[l + 1];
return X_i_num < X_j_num;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> K;
A = vec<int>(N);
for(int &a : A) cin >> a;
rolhash rol(A);
vec<int> perm(N);
for(int i = 0; i < N; ++i) perm[i] = i;
sort(all(perm), [&rol](int i, int j) -> bool {
return cmphash(i, j, rol);
});
int ansIndex = perm[K - 1];
for(int i = 0; i < N; ++i){
if(i != ansIndex) cout << A[i] << " ";
}
cout << "\n";
}