选入标准
- 黄 蓝。
- 好用的 trick。
- 其它。
P6067
Subtask#1 做一个 的暴力可以过,对于 考虑优化。
数学表达方式:
拆开里面的求和公式:
里面这个求和用一个前缀和算。
得到答案公式:
注意答案需要乘以 。
单次询问复杂度 ,前缀和跑一次 ,瓶颈在排序。
总时间复杂度为 。
code
#include <bits/stdc++.h>
#define debug(a) cerr << (#a) << " = " << (a) << endl;
#define int long long
#define maxn 100010
#define endl '\n'
using namespace std;
int n, a[maxn], sum[maxn];
int query(int l, int r) {
return sum[r] - sum[l - 1];
}
int solve() {
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++) {
sum[i] = sum[i - 1] + a[i];
}
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += a[i] * (i - 1) - query(1, i - 1);
}
return sum * 2;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t; t = 1;
while (t--) {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
cout << solve() << endl;
}
return 0;
}
B4163
严肃不会简单题。
我咋这么菜/ll 我咋这么菜/ll 我咋这么菜/ll 我咋这么菜/ll 我咋这么菜/ll 我咋这么菜/ll 我咋这么菜/ll
CSP 的题目不给特殊性质可以死吗了。
可以给一个特殊性质例如 的数据保证 ,此时答案为 。
这特殊性质太几把简单了,没啥用。
Those who cannot remember the past are condemned to repeat it.
有无英语大手子能翻译一下。
考虑 dp 正解。
懒得写,先咕这里了。
P15057
前言:搜并查集标签时搜到的,挑战不看题解独立切了。
直接并查集扔图上做就没了。
至于数学方法没够明白,但看到题解区第一个是个屎名的题解彻底怒了。不给证明啥阴。
这里查阅了机房巨佬 @youngwayboy 的题解然后看不懂,反正并查集能过,不学了。
直接输出 YES 有 20pts。
欸欸欸,怎么评了 5 分钟还没好。
code
#include <bits/stdc++.h>
#define debug(a) cerr << (#a) << " = " << (a) << endl;
#define int long long
#define maxn 1000010
#define endl '\n'
using namespace std;
int n;
int fa[maxn];
void init() {
for (int i = 1; i <= n; i++) {
fa[i] = i;
}
}
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
void merge(int a, int b) {
int x = find(a), y = find(b);
if (x != y) {
fa[y] = x;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
init();
for (int i = 1; i <= n; i++) {
fa[find(i)] = find(i + i <= n ? i + i : i + i - n);
}
for (int i = 1; i <= n; i++) {
if (find(i) != find(1)) {
cout << "NO" << endl;
cout << i << " " << 1 << endl;
return 0;
}
}
cout << "YES" << endl;
return 0;
}
=P14359
啊哈,CSP-J 2025 的题。
草草草,当时我甚至连前缀和都不会你信吗?况且是前缀异或,天塌了/ll
本文中的 均指代二进制按位异或。
CSP 的题当然先讲特殊性质骗分,我们挨个查阅特殊性质:
特殊性质 A:对于所有 ,均有 。
特殊性质 B:对于所有 ,均有 。
特殊性质 C:对于所有 ,均有 。
观察 A,我们知道 ,所以当 时,我们的答案应该是 ,10 分到手。
观察 B,,我不会。
观察 C,,我也不会。
所以我考场上只骗到了 10 分。
还有我为什么会想到双指针啊。
@Ethan_sun <= fvv。
再思考部分分做法。
显然我们有一个 的 dp,适用于 的数据,也就是前 12 个数据点。
我们定义 ,并且补充定义 。
转移方程如下:
TLE 60pts code
#include <bits/stdc++.h>
#define debug(a) cerr << (#a) << " = " << (a) << endl;
#define int long long
#define maxn 500010
#define endl '\n'
using namespace std;
int n, k;
int a[maxn], x[maxn], dp[maxn];
void solve() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
x[i] = x[i - 1] ^ a[i];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
dp[i] = max(dp[i], dp[j - 1] + ((x[i] ^ x[j - 1]) == k));
}
}
cout << dp[n] << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t; t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
其实呢你如果前两题都做对得分就是 。
在我们 SH 就有一等奖了。
考虑正解。
我们的时间复杂度应该需要降到 或 ,当然好像没有后者这个方法。哪里来的一只 啊。
其实还是 dp。
枚举左端点,后面比较容易便不过多叙述。
写这么简略是因为我要去打摆了。
有空再补吧。
code
#include <bits/stdc++.h>
#define debug(a) cerr << (#a) << " = " << (a) << endl;
#define int long long
#define maxn 3000010
#define endl '\n'
using namespace std;
int n, k;
int a[maxn], x[maxn], dp[maxn];
void solve() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
x[i] = x[i - 1] ^ a[i];
}
int r = 0, ans = 0;
memset(dp, -1, sizeof(dp));
dp[0] = 0;
for (int i = 1; i <= n; i++) {
int s = x[i] ^ k;
if (dp[s] >= r) {
ans++, r = i;
}
dp[x[i]] = i;
}
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t; t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}P3909
给你一个长度为 的序列 ,对于 ,求:
Subtask 1
考虑一个 的暴力,按理来说能过掉 Subtask 1 的数据。可是为什么我的代码全是 WA + TLE?
WA + TLE code
#include <bits/stdc++.h>
#define debug(a) cerr << (#a) << " = " << (a) << endl;
#define mod (int)(1e9 + 7)
#define int long long
#define maxn 1000010
#define endl '\n'
using namespace std;
int n;
int a[maxn];
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
for (int k = j + 1; k <= n; k++) {
ans += 6 * (a[i] * a[j] * a[k]);
ans = (ans + mod) % mod;
}
}
}
cout << (ans + mod) % mod << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t; t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}让我想想。
