雅礼cch杯 round 1总结
T1
记录
为什么是蓝题为什么是蓝题为什么是蓝题为什么是蓝题
思路
首先考虑一半对一半的情况,考虑把 G 组的权值设为 H 组的权值设为 G,一半 H,所以在这种情况下的“公平区间”的权值和即为
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 10;
struct Node {
int a, tp;
bool operator < (const Node & x) const {
return a < x.a;
}
}a[N];
int n, sum[N], ans;
map<int, int> vis;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
freopen("fairphoto.in", "r", stdin);
freopen("fairphoto.out", "w", stdout);
cin >> n;
for(int i = 1; i <= n; i++) {
char tp;
cin >> a[i].a >> tp;
if(tp == 'G') {
a[i].tp = 1;
}
else {
a[i].tp = -1;
}
}
sort(a + 1, a + n + 1);
vis[0] = 0; // 对合法前缀的处理
for(int i = 1; i <= n; i++) {
sum[i] = sum[i - 1] + a[i].tp;
if(!vis.count(sum[i])) {
vis[sum[i]] = i;
}
else {
ans = max(ans, a[i].a - a[vis[sum[i]] + 1].a);
}
}
for(int i = 1, j = 1; i <= n; i++) { // 剩下来的两种情况可以合并在一起写
j = i;
while(j <= n && a[j].tp == a[i].tp) {
j++;
}
j--;
ans = max(ans, a[j].a - a[i].a);
i = j;
}
cout << ans << "\n";
}
T2
记录
lemon测评机还是泰剧了\bx\bx
思路
首先很容易发现,对于一种有
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int m, vis[30][30];
string s, t;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
freopen("cipher.in", "r", stdin);
freopen("cipher.out", "w", stdout);
cin >> s >> t >> m;
int n1 = s.size(), n2 = t.size();
s = " " + s;
t = " " + t;
for(int i = 0; i < 26; i++) {
for(int j = i; j < 26; j++) {
string ns, nt;
for(int k = 0; k < max(n1, n2); k++) {
if(k < n1 && s[k] - 'a' == i || s[k] - 'a' == j) {
ns += s[k];
}
if(k < n2 && t[k] - 'a' == i || t[k] - 'a' == j) {
nt += t[k];
}
}
if(ns == nt) {
vis[i][j] = vis[j][i] = 1;
}
}
}
while(m--) {
string q;
cin >> q;
bool flag = true;
for(int i = 0; i < q.size(); i++) {
for(int j = i; j < q.size(); j++) {
if(!vis[q[i] - 'a'][q[j] - 'a']) {
flag = false;
break;
}
}
}
cout << (flag ? "Y" : "N");
}
return 0;
}
T3
记录
也是相当的套路。
思路
看到中位数,自然就想到了把 所以这题他妈不就是T1加一个树状数组
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 10;
int n, x, a[N], t[N], ans;
int lowbit(int x) {
return x & -x;
}
void add(int x, int w) {
while(x <= n * 2 + 1) {
t[x] += w;
x += lowbit(x);
}
}
int query(int x) {
int ans = 0;
while(x) {
ans += t[x];
x -= lowbit(x);
}
return ans;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
freopen("grouping.in", "r", stdin);
freopen("grouping.out", "w", stdout);
cin >> n >> x;
for(int i = 1, sum = 0; i <= n; i++) {
cin >> a[i];
sum += (a[i] >= x ? 1 : -1);
add(sum + n + 1, 1);
ans += query(sum + n + 1) - 1 + (sum >= 0);
}
cout << ans << "\n";
return 0;
}
T4
记录
拼尽全力无法战胜淀粉质是什么东西
思路
首先,看到书上计数问题,就可以想到点分治作为一个没学过点分治的人,只能在考试后听别人讲的了QWQ。
先讲点分治。首先,树有一(多)个点,叫重心。重心的性质是其最大的子树大小不超过
首先,依然和一三题一样,先修改权值,把
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 50;
int n, sz[N * 2], vis[N * 2], del[N * 2], maxx[N * 2], cnt[N * 2][2], ctr, ans;
vector<pair<int, int> > E[N], id;
void get_ctr(int x, int fa, int n) {
sz[x] = 1;
maxx[x] = 0;
for(int i = 0; i < E[x].size(); i++) {
int y = E[x][i].first;
if(y == fa || del[y]) {
continue;
}
get_ctr(y, x, n);
if(ctr) {
return;
}
sz[x] += sz[y];
maxx[x] = max(maxx[x], sz[y]);
}
maxx[x] = max(maxx[x], n - sz[x]);
if(maxx[x] * 2 <= n) {
sz[fa] = n - sz[x];
ctr = x;
}
}
void get_ans(int x, int fa, int sum) {
if(sum == N && vis[N]) {
ans++;
}
ans += cnt[N * 2 - sum][1];
if(sum == N || vis[sum]) {
ans += cnt[N * 2 - sum][0];
}
id.push_back({sum, vis[sum] != 0});
vis[sum]++;
for(int i = 0; i < E[x].size(); i++) {
int y = E[x][i].first, w = E[x][i].second;
if(del[y] || y == fa) {
continue;
}
get_ans(y, x, sum + w);
}
vis[sum]--;
}
void solve(int rt) {
// cout << rt << "\n";
for(int i = 0; i < E[rt].size(); i++) {
int y = E[rt][i].first, w = E[rt][i].second;
if(del[y]) {
continue;
}
int s = id.size();
get_ans(y, rt, N + w);
for(int i = s; i < id.size(); i++) {
cnt[id[i].first][id[i].second]++;
}
}
for(int i = 0; i < id.size(); i++) {
cnt[id[i].first][id[i].second]--;
}
id.clear();
del[rt] = 1;
for(int i = 0; i < E[rt].size(); i++) {
int y = E[rt][i].first, w = E[rt][i].second;
if(del[y]) {
continue;
}
ctr = 0;
get_ctr(y, rt, sz[y]);
solve(ctr);
}
}
int main() {
cin >> n;
for(int i = 1; i < n; i++) {
int u, v, w;
cin >> u >> v >> w;
w = (w ? w : -1);
E[u].push_back({v, w});
E[v].push_back({u, w});
}
get_ctr(1, 0, n);
solve(ctr);
cout << ans << "\n";
return 0;
}