CF452F Permutation 与 P2757 [国家集训队] 等差子序列 题解
两道基本一样的题:
题目链接:
Permutation 链接:CF 或者 洛谷
等差子序列那题其实就是长度不小于 的等差数列是否存在,我们考虑等于 的是否存在就行,因为等于 长度的都不存在,更长的就不可能了,然后多了一个多测前提,就没啥了。两题其实基本一致。
考虑一下等差数列的三项,我们考虑中间那项设做
-
注意到原序列为排列,所以里面的数不重不漏且唯一。
-
基于第一点,那么对于
或者 要么在 之前要么在它之后,我们如果从左往右更新到了 发现没有找到 或者 ,那么它们二者不存在于 前面的一定在它后面的。
以这张图为例,比如从左往依次加入值,这里我们可以考虑把所有值放在一个轴上,类似权值线段树,在这个轴处记录加入这个值,考虑比如
在权值轴上,这些数不同的
具体的,我们假如出现过的数标
例如这样,就表示:所有的对应
例如:
线段树维护哈希挺简单的,首先确定模数,然后确定下底数,这里就写单哈希了。具体的,再预处理个底数的幂次方数组,当合并线段树节点信息
显然从左往右合并的答案为:
CF题目参考代码
#include <bits/stdc++.h> // #pragma GCC optimize("Ofast,unroll-loops") // #pragma GCC optimize(2) // #define isPbdsFile #ifdef isPbdsFile #include <bits/extc++.h> #else #include <ext/pb_ds/priority_queue.hpp> #include <ext/pb_ds/hash_policy.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/trie_policy.hpp> #include <ext/pb_ds/tag_and_trait.hpp> #include <ext/pb_ds/hash_policy.hpp> #include <ext/pb_ds/list_update_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/exception.hpp> #include <ext/rope> #endif using namespace std; using namespace __gnu_cxx; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef tuple<int, int, int> tii; typedef tuple<ll, ll, ll> tll; typedef unsigned int ui; typedef unsigned long long ull; typedef __int128 i128; #define hash1 unordered_map #define hash2 gp_hash_table #define hash3 cc_hash_table #define stdHeap std::priority_queue #define pbdsHeap __gnu_pbds::priority_queue #define sortArr(a, n) sort(a+1,a+n+1) #define all(v) v.begin(),v.end() #define yes cout<<"YES" #define no cout<<"NO" #define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr); #define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout); #define forn(i, a, b) for(int i = a; i <= b; i++) #define forv(i, a, b) for(int i=a;i>=b;i--) #define ls(x) (x<<1) #define rs(x) (x<<1|1) #define endl '\n' //用于Miller-Rabin [[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}; template <typename T> int disc(T* a, int n) { return unique(a + 1, a + n + 1) - (a + 1); } template <typename T> T lowBit(T x) { return x & -x; } template <typename T> T Rand(T l, T r) { static mt19937 Rand(time(nullptr)); uniform_int_distribution<T> dis(l, r); return dis(Rand); } template <typename T1, typename T2> T1 modt(T1 a, T2 b) { return (a % b + b) % b; } template <typename T1, typename T2, typename T3> T1 qPow(T1 a, T2 b, T3 c) { a %= c; T1 ans = 1; for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c; return modt(ans, c); } template <typename T> void read(T& x) { x = 0; T sign = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-')sign = -1; ch = getchar(); } while (isdigit(ch)) { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); } x *= sign; } template <typename T, typename... U> void read(T& x, U&... y) { read(x); read(y...); } template <typename T> void write(T x) { if (typeid(x) == typeid(char))return; if (x < 0)x = -x, putchar('-'); if (x > 9)write(x / 10); putchar(x % 10 ^ 48); } template <typename C, typename T, typename... U> void write(C c, T x, U... y) { write(x), putchar(c); write(c, y...); } template <typename T11, typename T22, typename T33> struct T3 { T11 one; T22 tow; T33 three; bool operator<(const T3 other) const { if (one == other.one) { if (tow == other.tow)return three < other.three; return tow < other.tow; } return one < other.one; } T3() { one = tow = three = 0; } T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three) { } }; template <typename T1, typename T2> void uMax(T1& x, T2 y) { if (x < y)x = y; } template <typename T1, typename T2> void uMin(T1& x, T2 y) { if (x > y)x = y; } constexpr int N = 3e5 + 10; constexpr int MX = 3e5; constexpr int MOD = 1e9 + 7; constexpr int BASE = 33; ll powBase[N]; struct Node { ll val, revVal; //正反哈希 int len; //区间长度 } node[N << 2]; int n; #define val(x) node[x].val #define revVal(x) node[x].revVal #define len(x) node[x].len inline void push_up(const int curr) { val(curr) = (val(ls(curr)) * powBase[len(rs(curr))] + val(rs(curr))) % MOD; revVal(curr) = (revVal(ls(curr)) + revVal(rs(curr)) * powBase[len(ls(curr))]) % MOD; } inline void add(const int curr, const int pos, const int l = 1, const int r = n) { const int mid = l + r >> 1; if (l == r) { val(curr) = revVal(curr) = 1; return; } if (pos <= mid)add(ls(curr), pos, l, mid); else add(rs(curr), pos, mid + 1, r); push_up(curr); } inline pll query(const int curr, const int l, const int r, const int s = 1, const int e = n) { if (l <= s and e <= r)return pll(val(curr),revVal(curr)); const int mid = s + e >> 1; if (r <= mid)return query(ls(curr), l, r, s, mid); if (l > mid)return query(rs(curr), l, r, mid + 1, e); auto [leftVal,leftRevVal] = query(ls(curr), l, mid, s, mid); auto [rightVal,rightRevVal] = query(rs(curr), mid + 1, r, mid + 1, e); auto ansVal = (leftVal * powBase[r - mid] + rightVal) % MOD; auto ansRevVal = (leftRevVal + rightRevVal * powBase[mid - l + 1]) % MOD; return pll(ansVal, ansRevVal); } inline void build(const int curr = 1, const int l = 1, const int r = n) { len(curr) = r - l + 1; val(curr) = revVal(curr) = 0; const int mid = l + r >> 1; if (l == r)return; build(ls(curr), l, mid); build(rs(curr), mid + 1, r); } int x; inline void solve() { cin >> n; build(); bool check = false; forn(i, 1, n) { cin >> x; auto minLen = min(x - 1, n - x); auto [val1,val2] = query(1, x - minLen, x + minLen); if (val1 != val2)check = true; add(1, x); } cout << (check ? "YES" : "NO") << endl; } signed int main() { // MyFile Spider //------------------------------------------------------ // clock_t start = clock(); powBase[0] = 1; forn(i, 1, MX)powBase[i] = powBase[i - 1] * BASE % MOD; int test = 1; // read(test); // cin >> test; forn(i, 1, test)solve(); // while (cin >> n, n)solve(); // while (cin >> test)solve(); // clock_t end = clock(); // cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl; }
洛谷题目参考代码
#include <bits/stdc++.h> // #pragma GCC optimize("Ofast,unroll-loops") // #pragma GCC optimize(2) // #define isPbdsFile #ifdef isPbdsFile #include <bits/extc++.h> #else #include <ext/pb_ds/priority_queue.hpp> #include <ext/pb_ds/hash_policy.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/trie_policy.hpp> #include <ext/pb_ds/tag_and_trait.hpp> #include <ext/pb_ds/hash_policy.hpp> #include <ext/pb_ds/list_update_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/exception.hpp> #include <ext/rope> #endif using namespace std; using namespace __gnu_cxx; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef tuple<int, int, int> tii; typedef tuple<ll, ll, ll> tll; typedef unsigned int ui; typedef unsigned long long ull; typedef __int128 i128; #define hash1 unordered_map #define hash2 gp_hash_table #define hash3 cc_hash_table #define stdHeap std::priority_queue #define pbdsHeap __gnu_pbds::priority_queue #define sortArr(a, n) sort(a+1,a+n+1) #define all(v) v.begin(),v.end() #define yes cout<<"YES" #define no cout<<"NO" #define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr); #define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout); #define forn(i, a, b) for(int i = a; i <= b; i++) #define forv(i, a, b) for(int i=a;i>=b;i--) #define ls(x) (x<<1) #define rs(x) (x<<1|1) #define endl '\n' //用于Miller-Rabin [[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}; template <typename T> int disc(T* a, int n) { return unique(a + 1, a + n + 1) - (a + 1); } template <typename T> T lowBit(T x) { return x & -x; } template <typename T> T Rand(T l, T r) { static mt19937 Rand(time(nullptr)); uniform_int_distribution<T> dis(l, r); return dis(Rand); } template <typename T1, typename T2> T1 modt(T1 a, T2 b) { return (a % b + b) % b; } template <typename T1, typename T2, typename T3> T1 qPow(T1 a, T2 b, T3 c) { a %= c; T1 ans = 1; for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c; return modt(ans, c); } template <typename T> void read(T& x) { x = 0; T sign = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-')sign = -1; ch = getchar(); } while (isdigit(ch)) { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); } x *= sign; } template <typename T, typename... U> void read(T& x, U&... y) { read(x); read(y...); } template <typename T> void write(T x) { if (typeid(x) == typeid(char))return; if (x < 0)x = -x, putchar('-'); if (x > 9)write(x / 10); putchar(x % 10 ^ 48); } template <typename C, typename T, typename... U> void write(C c, T x, U... y) { write(x), putchar(c); write(c, y...); } template <typename T11, typename T22, typename T33> struct T3 { T11 one; T22 tow; T33 three; bool operator<(const T3 other) const { if (one == other.one) { if (tow == other.tow)return three < other.three; return tow < other.tow; } return one < other.one; } T3() { one = tow = three = 0; } T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three) { } }; template <typename T1, typename T2> void uMax(T1& x, T2 y) { if (x < y)x = y; } template <typename T1, typename T2> void uMin(T1& x, T2 y) { if (x > y)x = y; } constexpr int N = 5e5 + 10; constexpr int MX = 5e5; constexpr int MOD = 1e9 + 7; constexpr int BASE = 33; ll powBase[N]; struct Node { ll val, revVal; //正反哈希 int len; //区间长度 } node[N << 2]; int n; #define val(x) node[x].val #define revVal(x) node[x].revVal #define len(x) node[x].len inline void push_up(const int curr) { val(curr) = (val(ls(curr)) * powBase[len(rs(curr))] + val(rs(curr))) % MOD; revVal(curr) = (revVal(ls(curr)) + revVal(rs(curr)) * powBase[len(ls(curr))]) % MOD; } inline void add(const int curr, const int pos, const int l = 1, const int r = n) { const int mid = l + r >> 1; if (l == r) { val(curr) = revVal(curr) = 1; return; } if (pos <= mid)add(ls(curr), pos, l, mid); else add(rs(curr), pos, mid + 1, r); push_up(curr); } inline pll query(const int curr, const int l, const int r, const int s = 1, const int e = n) { if (l <= s and e <= r)return pll(val(curr),revVal(curr)); const int mid = s + e >> 1; if (r <= mid)return query(ls(curr), l, r, s, mid); if (l > mid)return query(rs(curr), l, r, mid + 1, e); auto [leftVal,leftRevVal] = query(ls(curr), l, mid, s, mid); auto [rightVal,rightRevVal] = query(rs(curr), mid + 1, r, mid + 1, e); auto ansVal = (leftVal * powBase[r - mid] + rightVal) % MOD; auto ansRevVal = (leftRevVal + rightRevVal * powBase[mid - l + 1]) % MOD; return pll(ansVal, ansRevVal); } inline void build(const int curr = 1, const int l = 1, const int r = n) { len(curr) = r - l + 1; val(curr) = revVal(curr) = 0; const int mid = l + r >> 1; if (l == r)return; build(ls(curr), l, mid); build(rs(curr), mid + 1, r); } int x; inline void solve() { cin >> n; build(); bool check = false; forn(i, 1, n) { cin >> x; auto minLen = min(x - 1, n - x); auto [val1,val2] = query(1, x - minLen, x + minLen); if (val1 != val2)check = true; add(1, x); } cout << (check ? "Y" : "N") << endl; } signed int main() { // MyFile Spider //------------------------------------------------------ // clock_t start = clock(); powBase[0] = 1; forn(i, 1, MX)powBase[i] = powBase[i - 1] * BASE % MOD; int test = 1; // read(test); cin >> test; forn(i, 1, test)solve(); // while (cin >> n, n)solve(); // while (cin >> test)solve(); // clock_t end = clock(); // cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统