8月22日考试总结
T1
题目大意
有
思路
其实这道题是一个诈骗题。
这道题如果去模拟样例就会发现,这道题和顺序没啥关系,无论怎么改变顺序,最终的热度值是一样的。
然后组合计算的话是考试中没想出来的,我们可以按照石子合并的方式,每一次混合其实就是从
我们只需要将这些值乘起来即可,所以选择的方案就有:
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;
const int N = 2e4 + 10;
int T, n, op, a[N];
void solve() {
cin >> n >> op;
for (int i = 1; i <= n; i++)
cin >> a[i];
int sum = 0, num = a[1], ans = 1;
for (int i = 2; i <= n; i++) {
ans = (ans * i * (i - 1) / 2) % mod;
sum = (sum + num * a[i] % mod) % mod;
num = (num + a[i]) % mod;
}
cout << sum << ' ' << op * ans << "\n";
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
cin >> T;
while (T--) {
solve();
}
return 0;
}
T2
题目大意
有
将所有亲密关系按以下规则排序:
- 第一关键字:亲密度(即
|i-j| )从小到大 - 第二关键字:编号较小的同学的编号从小到大
- 第三关键字:编号较大的同学的编号从小到大
求第
思路
这道题以亲密度为第一关键字,于是我们可以先二分亲密度,找到
我们二分出一个亲密值的上届
现在的问题就是二分中的 check 函数如何写,如何计算
我们发现如果正着写的话非常不好写,好像没有办法快速的去枚举出所有的合法亲密度,
正难则反,我们可以先计算出不考虑颜色的方案数,然后减去那些不合法的方案。
统计不合法的方案数可以用一个类似于滑动窗口的东西来统计有多少个相同颜色的方案数,然后减去即可。
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
int n, k, a[N];
vector<int> num[N];
int check(int x) {
int sum = (n - x) * x + x * (x - 1) / 2;
for (int i = 1; i <= n; i++) {
int l = 0;
for (int j = 0; j < num[i].size(); j++) {
while (l < num[i].size() && num[i][j] - num[i][l] > x)
l++;
sum -= j - l;
}
}
return sum;
}
signed main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
num[a[i]].push_back(i);
}
if (check(n) < k) {
cout << -1;
return 0;
}
int l = -1, r = n + 1, ans = n;
while (l <= r) {
int mid = l + r >> 1;
if (check(mid) < k) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
int d = ans + 1;
int cnt = check(ans);
for (int i = 1; i <= n - d; i++) {
if (a[i] != a[i + d]) {
cnt++;
}
if (cnt == k) {
cout << i << ' ' << i + d;
return 0;
}
}
return 0;
}
T3
题目大意
给定
对于每次询问:
- 第一行输出满足条件的
x 的个数m - 第二行按升序输出所有满足条件的
x (如果m>0 )
思路
我们可以将
正约数和定理:假设
根据这上面的两个公式,我们就可以利用 DFS 来求解即可。
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 10;
int T, n;
int vis[N], cnt[N];
vector<int> v, ans, prime;
void init() {
vis[0] = vis[1] = 1;
for (int i = 2; i < 1e6 + 7; i++) {
if (!vis[i]) {
prime.push_back(i);
for (int j = i + i; j < 1e6 + 7; j += i) {
// cout << j << ' ';
vis[j] = 1;
}
}
}
}
bool check(int x) {
if (x <= 1)
return 0;
for (int i = 2; i * i <= x; i++) {
if (x % i == 0)
return 0;
}
return 1;
}
void dfs(int now, int x, int sum) {
if (now == 1) {
ans.push_back(sum);
return;
}
if (check(now - 1) && now > prime[x]) {
ans.push_back(sum * (now - 1));
}
for (int i = x; prime[i] * prime[i] <= now; i++) {
int tmp = prime[i];
for (int j = prime[i] + 1; j <= now; tmp *= prime[i], j += tmp) {
if (now % j == 0) {
dfs(now / j, i + 1, sum * tmp);
}
}
}
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
init();
cin >> T;
while (T--) {
cin >> n;
ans.clear();
dfs(n, 0, 1);
cout << ans.size() << '\n';
sort(ans.begin(), ans.end());
if (ans.size()) {
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << ' ';
}
cout << '\n';
}
}
return 0;
}
T4
题目大意
给定
思路
我们考虑用平常求解最长上升子序列的方式
很明显,这个状态在这道题用不了,因为
我们设
可以得到状态转移方程:
但是如果直接进行转移的话,所需要的时间是
我们发现我们好像无法去优化状态和转移方程了,所以我们只能去优化转移的效率。
发现
题目中添加一个
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 10;
struct node {
int l, r, val, key, lazy;
} tree[N];
int n, root, l, r, p, cnt, ans;
void push_down(int p) {
if (tree[p].lazy) {
tree[p].val += tree[p].lazy;
tree[tree[p].l].lazy += tree[p].lazy;
tree[tree[p].r].lazy += tree[p].lazy;
tree[p].lazy = 0;
}
}
void split(int u, int x, int &l, int &r) {
if (!u) {
l = r = 0;
return;
}
push_down(u);
if (tree[u].val < x) {
l = u;
split(tree[u].r, x, tree[u].r, r);
} else {
r = u;
split(tree[u].l, x, l, tree[u].l);
}
}
int add(int x) {
cnt++;
tree[cnt].val = x;
tree[cnt].key = rand();
return cnt;
}
int merge(int l, int r) {
if (!l || !r)
return l + r;
if (tree[l].key < tree[r].key) {
push_down(l);
tree[l].r = merge(tree[l].r, r);
return l;
} else {
push_down(r);
tree[r].l = merge(l, tree[r].l);
return r;
}
}
void del(int &x) {
if (tree[x].l)
del(tree[x].l);
else
x = tree[x].r;
}
void insert(int x, int y) {
split(root, x, l, p);
split(p, y, p, r);
tree[p].lazy++;
if (r)
del(r);
else
ans++;
root = merge(l, merge(add(x), merge(p, r)));
}
signed main() {
srand(time(0));
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1, l, r; i <= n; i++) {
cin >> l >> r;
insert(l, r);
}
cout << ans;
return 0;
}