前言
这次没挂分,发挥还是可以的。
得分:
题目
公平合影(fairphoto)
这题直接按位置排序,然后把G看成1、H看成-1做前缀和,答案既是区间和为 的段。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
struct node{
char c;
int x;
}a[N];
int n, sum[N];
map<int, int> mp;
signed main(){
freopen("fairphoto.in", "r", stdin);
freopen("fairphoto.out", "w", stdout);
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i].x >> a[i].c;
}
sort(a + 1, a + 1 + n, [](const node &a, const node &b){
return a.x < b.x;
});
for(int i = 1; i <= n; i++){
sum[i] = sum[i - 1] + (a[i].c == 'W' ? 1 : -1);
}
int ans = 0;
for(int i = 1; i <= n; i++){
int j = i + 1;
while(j <= n && a[i].c == a[j].c){
j++;
}
j--;
ans = max(ans, a[j].x - a[i].x), i = j;
}
mp[0] = 0;
for(int i = 1; i <= n; i++){
if(mp.count(sum[i])){
ans = max(ans, a[i].x - a[mp[sum[i]] + 1].x);
}else{
mp[sum[i]] = i;
}
}
cout << ans;
return 0;
}暗号解码(cipher)
容易发现一个性质:设 为询问字符串,从 中任选两个字符,若过滤完都合法,那么 也合法。
那么我们就可以预处理所有的字符对是否合法,然后暴力判断即可,时间 (跑不满),也可以预处理优化成 。
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, m, q, vis[26][26];
string s, t;
signed main(){
freopen("cipher.in", "r", stdin);
freopen("cipher.out", "w", stdout);
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> s >> t >> q;
n = s.size(), s = ' ' + s;
m = t.size(), t = ' ' + t;
for(int i = 0; i < 26; i++){
for(int j = 0; j < 26; j++){
string ss, tt;
for(int k = 1; k <= n; k++){
if(s[k] - 'a' == i || s[k] - 'a' == j) {
ss += s[k];
}
}
for(int k = 1; k <= m; k++){
if(t[k] - 'a' == i || t[k] - 'a' == j) {
tt += t[k];
}
}
vis[i][j] = ss == tt;
}
}
for(string r; q--;){
cin >> r;
int fl = 1;
for(int i = 0; i < r.size() && fl; i++){
for(int j = i; j < r.size() && fl; j++){
fl &= vis[r[i] - 'a'][r[j] - 'a'];
}
}
cout << (fl ? 'Y' : 'N');
}
return 0;
}分组(grouping)
这题的套路很经典,把 的 设为 ,其余设为 ,那么题目就变成了找有多少个总和大于 的字段,用树状数组计算即可。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
int n, x, a[N], sum[N], tr[N * 2];
void add(int x, int k){
for(; x < N * 2; x += (x & -x)){
tr[x] += k;
}
}
int query(int x){
int res = 0;
for(; x > 0; x -= (x & -x)){
res += tr[x];
}
return res;
}
signed main(){
freopen("grouping.in", "r", stdin);
freopen("grouping.out", "w", stdout);
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> x;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
for(int i = 1; i <= n; i++){
sum[i] = sum[i - 1] + (a[i] >= x ? 1 : -1);
}
int ans = 0;
add(N, 1);
for(int i = 1; i <= n; i++){
ans += query(N + sum[i]);
add(N + sum[i], 1);
}
cout << ans;
return 0;
}平衡小径(balance)
注: 边权都替换为 。
首先, 分暴力是直接送的, 暴力即可。
再来思考 分,我们可以枚举以哪个点为起点然后去dfs,容易发现若一个点到枚举的那个点的路径中的树上前缀和为 的点大于 个则是一条合法路径。
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, L = 31;
struct node{
int x, w;
};
int n, sum[N], f[N], dep[N], ans;
vector<node> v[N];
void dfs(int x, int fa, int s){
ans += (s > 1) * (sum[x] == 0);
for(node v : v[x]){
int i = v.x, w = v.w;
if(i != fa){
sum[i] = sum[x] + w;
dfs(i, x, s + (sum[x] == 0));
}
}
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1, x, y, w; i < n; i++){
cin >> x >> y >> w;
v[x].push_back({y, (!w ? -1 : 1)});
v[y].push_back({x, (!w ? -1 : 1)});
}
for(int i = 1; i <= n; i++){
fill(sum + 1, sum + 1 + n, 0);
dfs(i, 0, 0);
}
cout << ans / 2;
return 0;
}分就很显然是点分治了,计数和 分的差不多。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
struct node{
int x, w;
}p[N];
int n, sz[N], del[N], sum[N * 2], cnt[N * 2][2], ans, id, top;
vector<node> v[N];
void tr_wht_pt(int x, int fa, int s, int &rt){
sz[x] = 1;
int wt = 0;
for(node l : v[x]){
int i = l.x, w = l.w;
if(i != fa && !del[i]){
tr_wht_pt(i, x, s, rt);
if(rt){
return ;
}
sz[x] += sz[i], wt = max(wt, sz[i]);
}
}
wt = max(wt, s - sz[x]);
if(wt <= s / 2){
rt = x, sz[fa] = s - sz[x];
}
}
void update(int x, int fa, int s){
ans += (s == N && sum[N]);
ans += cnt[N * 2 - s][1];
bool fl = sum[s];
if(s == N || fl){
ans += cnt[N * 2 - s][0];
}
sum[s]++, p[++top] = {s, fl};
for(node l : v[x]){
int i = l.x, w = l.w;
if(!del[i] && i != fa){
update(i, x, s + w);
}
}
sum[s]--;
}
void dfs(int x){
for(node l : v[x]){
int i = l.x, w = l.w;
if(del[i]){
continue;
}
int t = top;
update(i, x, N + w);
for(int j = t + 1; j <= top; j++){
cnt[p[j].x][p[j].w]++;
}
}
for(int j = 1; j <= top; j++){
cnt[p[j].x][p[j].w]--;
}
top = 0, del[x] = 1;
for(node l : v[x]){
int i = l.x, w = l.w;
if(del[i]){
continue;
}
id = 0, tr_wht_pt(i, x, sz[i], id);
dfs(id);
}
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1, x, y, w; i < n; i++){
cin >> x >> y >> w;
v[x].push_back({y, (!w ? -1 : 1)});
v[y].push_back({x, (!w ? -1 : 1)});
}
tr_wht_pt(1, 0, n, id);
dfs(id);
cout << ans;
return 0;
}后记
666前三题为什么是蓝