2026.03.03

· · 个人记录

测试情况

题目 估分 实际
\texttt{A} \texttt{100pts} \texttt{40pts}
\texttt{B} \texttt{100pts} \texttt{100pts}
\texttt{C} \texttt{10pts} \texttt{10pts}
\texttt{D} \texttt{30pts} \texttt{30pts}
总分 \texttt{240pts} \texttt{180pts}

又是挂分的一天。

A

分成 2 个部分,分别统计两边的原子数,最后判断是否相等即可。

时间复杂度:O(nl)。( l 为字符串 s 的长度)

::::error[挂分原因] 处理数字原子时,没有跳过当前的位置,反而重复计算了当前位置。 ::::

::::success[AC code]

/*
writer:akaryan
*/
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define Int __int128
#define db double
#define ldb long double
#define inf (1 << 30)
#define INF (1LL << 60LL)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin() , (x).end()
#define bug(x) cout << #x << " = " << x << '\n'
using namespace std;
const int N = 2e5 + 10 , M = 2e5 + 10;
const int mod = 998244353;
ll T , x , l , pos , flag;
string s;
map<ll , ll> mp , mp2;
inline void solve(){    
    cin >> s , l = s.size() , s = ' ' + s , mp.clear() , mp2.clear() , x = 1 , pos = flag = 0;
    for (int i = 1 ; i <= l ; i ++) if (s[i] == '>') pos = i;
    for (int i = 1 ; i < pos - 1 ; i ++){
        if (s[i] == '+'){
            x = 1;
            continue;
        }
        if (s[i] >= '0' && s[i] <= '9') x = (s[i] - '0');
        else if (s[i] >= 'A' && s[i] <= 'Z'){
            if (i + 1 < pos - 1 && s[i + 1] >= '0' && s[i + 1] <= '9') mp[s[i] - 'A'] += (s[i + 1] - '0') * x , i ++;
            else mp[s[i] - 'A'] += x;
        }
    }
    x = 1;
    for (int i = pos + 1 ; i <= l ; i ++){
        if (s[i] == '+'){
            x = 1;
            continue;
        }
        if (s[i] >= '0' && s[i] <= '9') x = (s[i] - '0');
        else if (s[i] >= 'A' && s[i] <= 'Z'){
            if (i + 1 <= l && s[i + 1] >= '0' && s[i + 1] <= '9') mp2[s[i] - 'A'] += (s[i + 1] - '0') * x , i ++;
            else mp2[s[i] - 'A'] += x;
        }
    }
    for (int i = 0 ; i <= 25 ; i ++) if (mp[i] != mp2[i]) flag = 1;
    cout << (!flag ? "DA" : "NE") << '\n';
}
int main(){
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) , cin >> T;
    while(T --) solve();
    return 0;
}

::::

B

我们想要空杯子的数量最大,应该要尽可能选择当前容积较大的杯子进行填充。所以我们先将杯子的容积从大到小排序,然后依次将液体倒入当前容积较大的杯子中,直到液体被分配完。最后未被分配液体的杯子即为空杯子的数量。

时间复杂度:O(n \log n)。

::::success[AC code]

/*
writer:akaryan
*/
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define Int __int128
#define db double
#define ldb long double
#define inf (1 << 30)
#define INF (1LL << 60LL)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin() , (x).end()
#define bug(x) cout << #x << " = " << x << '\n'
using namespace std;
const int N = 2e5 + 10 , M = 2e5 + 10;
const int mod = 998244353;
ll n , i = 1 , x , ans , pos , lst;
map<ll , ll> mp;
struct Node{
    ll x , val;
}a[N]; 
inline bool cmp(const Node &x , const Node &y){
    return (x.x > y.x) || (x.x == y.x && x.val > y.val);
}
inline void solve(){
    cin >> n , pos = n;
    for (int i = 1 ; i <= n ; i ++) cin >> x >> a[i].x , ans += x , a[i].val = i;
    sort(a + 1 , a + n + 1 , cmp);//从大到小排序
    while(ans) lst = min(a[i].x , ans) , ans -= lst , mp[a[i].val] = lst , pos -- , i ++;
    cout << pos << '\n';
    for (int i = 1 ; i <= n ; i ++) cout << mp[i] << ' ';
}
int main(){
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
    solve();
    return 0;
}

::::

C

这题的思路比较巧妙。

首先,既然我们想要计算最少线段数,那么我们可以反过来想,用总共的线段数 - 可合并的线段数,总共的线段数为 (n+2)+\sum\limits_{i=1}^{n}b_i。

然后,我们令 sum_i 表示相邻柱子可供共享的水平线数,得到公式:

sum_i = \min\left( \frac{b_{i-1}}{\frac{a_i \times b_{i-1}}{\gcd(a_i \times b_{i-1}, a_{i-1} \times b_i)}}, \frac{b_i}{\frac{a_{i-1} \times b_i}{\gcd(a_i \times b_{i-1}, a_{i-1} \times b_i)}} \right)

接着,我们将式子通分一下,得到:

sum_i = \min\left( \frac{\gcd(a_i \times b_{i-1}, a_{i-1} \times b_i)}{a_i}, \frac{\gcd(a_i \times b_{i-1}, a_{i-1} \times b_i)}{a_{i-1}} \right)

也就是:

sum_i = \frac{\gcd(a_i \times b_{i-1}, a_{i-1} \times b_i)}{\max(a_i, a_{i-1})}

所以,最终答案为:

\text{ans} = (n+2)+\sum_{i=1}^n b_i - \sum_{i=2}^{n} sum_i

时间复杂度:O(n \log (\max\limits_{1 \leq i \leq n} \{a_i, b_i\}))。

::::success[AC code]

/*
writer:akaryan
*/
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define Int __int128
#define db double
#define ldb long double
#define inf (1 << 30)
#define INF (1LL << 60LL)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin() , (x).end()
#define bug(x) cout << #x << " = " << x << '\n'
using namespace std;
const int N = 2e5 + 10 , M = 2e5 + 10;
const int mod = 998244353;
ll n , ans , a[N] , b[N] , s[N];
inline ll gcd(ll a , ll b){
    return (!b ? a : gcd(b , a % b));
}
inline ll lcm(ll a , ll b){
    return a / gcd(a , b) * b;
}
inline void solve(){
    cin >> n , ans = n + 2;
    for (int i = 1 ; i <= n ; i ++) cin >> a[i];
    for (int i = 1 ; i <= n ; i ++) cin >> b[i] , ans += b[i];
    for (int i = 2 ; i <= n ; i ++) ans -= (gcd(a[i] * b[i - 1] , a[i - 1] * b[i])) / max(a[i] , a[i - 1]);
    cout << ans << '\n';
}
int main(){
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
    solve();
    return 0;
}

::::

D

首先,题目要求我们求出最大哈明距离,那我们可以将题目转化 m 减最小哈明距离,接着每次翻转 1 位相当于走 1 步,这个问题就被我们迎刃而解啦!最后跑一遍多源多汇 BFS 即可。

时间复杂度:O(m \times 2^m)。

::::success[AC code]

/*
writer:akaryan
*/
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define Int __int128
#define db double
#define ldb long double
#define inf (1 << 30)
#define INF (1LL << 60LL)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin() , (x).end()
#define bug(x) cout << #x << " = " << x << '\n'
using namespace std;
const int N = (1 << 21) , M = 2e5 + 10;
const int mod = 998244353;
ll n , m , a[N] , dis[N];
bool vis[N];
queue<ll> q;
inline void solve(){
    cin >> n >> m;
    for (int i = 0 ; i <= N ; i ++) dis[i] = INF;
    for (int i = 1 ; i <= n ; i ++) cin >> a[i] , q.push(a[i]) , dis[a[i]] = 0 , vis[a[i]] = 1;
    while (!q.empty()){
        auto x = q.front();
        q.pop();
        for (int i = 0 ; i <= m ; i ++) if (!vis[x ^ (1 << i)]) dis[x ^ (1 << i)] = dis[x] + 1 , vis[x ^ (1 << i)] = 1 , q.push((x ^ (1 << i)));
    }
    for (int i = 1 ; i <= n ; i ++) cout << m - dis[((1 << (m + 1)) - 1) ^ a[i]] + 1 << ' ';
}
int main(){
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
    solve();
    return 0;
}

::::

总结

$$ \texttt{Thanks for reading.} $$