文章目录
- C++
- 总结
本题链接:CSP 202112-2 序列查询新解
本博客给出本题截图:
C++
#include <iostream> #include <algorithm> #include <cstring> #include <cmath> using namespace std; typedef long long LL; const int N = 100010; int a[N], b[2 * N]; LL res, cnt; int main() { int n, m; cin >> n >> m; a[n + 1] = m; for (int i = 1; i <= n; i ++ ) cin >> a[i]; int t = m / (n + 1); for (cnt = 1; cnt * t < m; cnt ++ ) b[cnt] = cnt * t; b[cnt ++ ] = m; int i = 0, j = 0; while (i < n + 1 || j < cnt - 1) { if (a[i] < b[j]) { res += (min(a[i + 1], b[j]) - a[i]) * abs(j - i - 1); i ++; } else if (a[i] == b[j]) { res += (min(a[i + 1], b[j + 1]) - a[i]) * abs(i - j); i ++; j ++; } else { res += (min(b[j + 1], a[i]) - b[j]) * abs(i - j - 1); j ++; } } cout << res << endl; return 0; }
总结
先放代码,后续补题解,期末有点忙…