20250915总结
前言
这次没挂分,发挥还是可以的。
得分:
题目
公平合影(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)
注:
首先,
再来思考
#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;
}