T1 没清空,变成第 $8$ 名,好耶。
期望分数 $100$,实际分数 $60$。树状数组没清空!
首先要排序,肯定是跟元素关系有关,容易发现与逆序对有关。先打个暴力求一下逆序对数 $s$,然后等大你直径 $2^{128}$ 厘米的眼睛,发现 $s \bmod 2 = 0$ 时答案为 Yes,否则答案为 No 。然后大胆猜测这是正确结论,结果是:满分。
当然这么好的题肯定需要证明:通过推导发现,一次操作显然不会改变逆序对数量的奇偶性,而排好之后逆序对数量就是 $0$,所以一个排列可以被排序当且仅当其逆序对数量为偶数。
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int kMaxN = 1e6 + 10;
ll n, a[kMaxN], tr[kMaxN], ans;
ll lowbit(ll x) {
return x & (-x);
}
void Update(ll x, ll k) {
for (; x < kMaxN; x += lowbit(x)) {
tr[x] += k;
}
}
ll Query(ll x) {
ll ans = 0;
for (; x; x -= lowbit(x)) {
ans += tr[x];
}
return ans;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
freopen("chick.in", "r", stdin);
freopen("chick.out", "w", stdout);
int t;
for (cin >> t; t; -- t) {
cin >> n, ans = 0;
for (int i = 1; i <= n; ++ i) {
cin >> a[i];
tr[i] = 0;
}
for (int i = n; i; -- i) {
ans += Query(a[i]);
Update(a[i], 1);
}
cout << (ans % 2? "No" : "Yes") << '\n';
}
return 0;
}
期望分数 $[40, 50]$,实际分数 $40$。
赛时思路:先把题目转换成从 $n$ 到 $1$,然后把加变减,乘变整除。当 $n \le 10^4$ 时用记忆化搜索,每一次枚举加和整除(当前位置为 $x_2$ 的倍数)的情况,加一点优化。当 $n > 10^4$ 时就要发挥自己的智慧了。由于数据在完全随机的情况下小 L 能到达 $n$ 的概率是极小的,所以直接赌在 $n > 10^4$ 时小 L 最多只有一条移动路径可以到 $n$,直接枚举路径即可。最终大样例过了四个,最后一个 TLE 了。
正确思路:设乘了 $k$ 次,第 $i$ 次乘之后用了 $a_i$ 次加,那么最后的位置就为 $\displaystyle x_1 \sum_{i = 0}^k a_i x_2^i + x_2^m$,花费为 $\displaystyle c_1 \sum_{i = 0}^k a_i + mc_2$,只需最小化 $\displaystyle \sum_{i = 0}^k a_i$ 即可。然后贪心解决即可。
赛时代码:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int kMaxN = 1e5 + 10;
ll n, x1, c1, x2, c2, ans;
map<ll, ll> f;
void Bfs() {
queue<pair<ll, ll>> q;
q.push({n, 0});
for (; !q.empty(); ) {
auto x = q.front();
q.pop();
if (x.second >= ans) {
continue;
}
if (f[x.first]) {
q.push({1, x.second + f[x.first]});
continue;
}
if (x.first == 1) {
ans = min(ans, x.second);
continue;
}
if (x.first - x1 >= 1) {
q.push({x.first - x1, x.second + c1});
}
if (!(x.first % x2) && x2 != 1) {
q.push({x.first / x2, x.second + c2});
}
}
}
int main() {
freopen("river.in", "r", stdin);
freopen("river.out", "w", stdout);
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t;
for (cin >> t >> t; t; -- t) {
cin >> n >> x1 >> c1 >> x2 >> c2, ans = 9e18;
if (x2 == 1 && n >= 1e4) {
cout << (!((n - 1) % x1)? (n - 1) / x1 * c1 : -1) << '\n';
continue;
}
if (n >= 1e4) {
ans = 0;
for (; ; ) {
if (!(n % x2) && x2 != 1) {
n /= x2;
ans += c2;
} else {
n -= x1;
ans += c1;
}
if (n == 1) {
break;
}
if (n < 1) {
ans = -1;
break;
}
}
cout << ans << '\n';
continue;
}
f.clear();
Bfs();
cout << (ans == 9e18? -1 : ans) << '\n';
}
return 0;
}
正确代码:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int kMaxN = 1e5 + 10;
ll n, x1, c1, x2, c2, mn, ans, s;
int main() {
freopen("river.in", "r", stdin);
freopen("river.out", "w", stdout);
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t;
for (cin >> t >> t; t; -- t) {
cin >> n >> x1 >> c1 >> x2 >> c2, mn = 9e18, ans = 0, s = 1;
if (x2 == 1) {
cout << ((n - 1) % x1? -1 : (n - 1) / x1 * c1) << '\n';
continue;
}
for (ll i = 0, x; ; ++ i, s *= x2) {
if ((n - s) % x1) {
if (s > n / x2) {
break;
}
continue;
}
x = (n - s) / x1, ans = i * c2;
for (ll j = 1; j <= i; ++ j) {
ans += (x % x2) * c1;
x /= x2;
}
ans += x * c1;
mn = min(mn, ans);
if (s > n / x2) {
break;
}
}
cout << (mn == 9e18? -1 : mn) << '\n';
}
return 0;
}
期望分数 $[24, 32]$,实际分数 $24$。
赛时直接分段暴力。当 $n \le 5000$ 时,直接 dp。设 $f_i$ 表示处理到第 $i$ 个数的最大贡献,枚举分割序列的位置 $j \in [0,\ i - 1]$,显然转移方程为 $f_i = \max(f_i,\ f_j + (\max_{k = j + 1}^i a_k - \min_{k = j + 1}^i a_k)^2)$。当 $n > 5000$ 时,写一个伪贪心,把一个上升/下降子序列分一组。
赛后我们充分发挥人类智慧,我们赌一组的长度不会超过 $200$,所以我们只需枚举 $j \in [\max(0,\ i - 200),\ i - 1]$ 即可。结果发现 $96$ 分,特判全部上升即可获得满分。其实这很容易卡的,长度为 $10^6$ 的序列分两半,一半上升一半下降。
赛时代码:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int kMaxN = 1e6 + 10;
ll n, a[kMaxN], f[kMaxN], ans, mx, mn;
bool msk;
int main() {
freopen("sequence.in", "r", stdin);
freopen("sequence.out", "w", stdout);
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++ i) {
cin >> a[i];
}
if (n >= 5000) {
msk = (a[2] >= a[1]);
mx = mn = a[1];
for (int i = 2; i <= n; ++ i) {
if (msk) {
if (a[i] < a[i - 1] && a[i] <= mx && a[i] >= mn) {
ans += (mx - mn) * (mx - mn);
msk ^= 1;
mx = 0, mn = 9e18;
}
mx = max(mx, a[i]);
mn = min(mn, a[i]);
} else {
if (a[i] >= a[i - 1] && a[i] <= mx && a[i] >= mn) {
ans += (mx - mn) * (mx - mn);
msk ^= 1;
mx = 0, mn = 9e18;
}
mx = max(mx, a[i]);
mn = min(mn, a[i]);
}
}
return cout << ans + (mx - mn) * (mx - mn) << '\n', 0;
}
for (int i = 1; i <= n; ++ i) {
ll mx = 0, mn = 9e18;
for (int j = i - 1; j >= 0; -- j) {
mx = max(mx, a[j + 1]);
mn = min(mn, a[j + 1]);
f[i] = max(f[i], f[j] + (mx - mn) * (mx - mn));
}
}
cout << f[n] << '\n';
return 0;
}