周报(2025.1.20 ~ 2025.1.26)

 一、

 Codeforces Round 998 (Div. 3)  E题

用到并查集

并查集模板

#include 
using namespace std;
#define ll long long
#define PII pair
#define endl '\n'
struct DSU{
    vector f,siz;
 
    DSU() {}
    DSU(ll n){
        init(n);
    }
 
    void init(ll n){
        f.resize(n);
        iota(f.begin() , f.end() , 0);
        siz.assign(n , 1);
    }
 
    ll find(ll x){
        while(x != f[x]){
            x = f[x] = f[f[x]];
        }
        return x;
    }
 
    bool same(ll x , ll y){
        return find(x) == find(y);
    }
 
    bool merge(ll x , ll y){
        x = find(x);
        y = find(y);
        if(x == y){
            return 0;
        }
        siz[x] += siz[y];
        f[y] = x;
        return 1;
    }
 
    ll size(ll x){
        return siz[find(x)];
    }
};
 
void solve(){
    ll n,m1,m2;
    cin >> n >> m1 >> m2;
    DSU g(n+1);
    vector ef(m1+1); // edge F
    for(ll i = 1 ; i <= m1 ; ++i){
        ll u,v;
        cin >> u >> v;
        ef[i] = {u,v};
    }
 
    ll cg = n; // cnt G
    for(ll i = 1 ; i <= m2 ; ++i){
        ll u,v;
        cin >> u >> v;
        cg -= g.merge(u,v);
    }
 
    DSU f(n+1);
    ll cf = n; // cnt F
    ll ans = 0;
    for(auto [u,v] : ef){
        if(g.same(u,v)){
            cf -= f.merge(u,v);
        }
        else{
            ans++;
        }
    }
 
    ans += cf - cg;
    cout << ans << endl;
}
 
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
 
    ll t;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

二、

 设置小数几位时,不能在setprecison()后面加 endl,会WA!!!

周报(2025.1.20 ~ 2025.1.26)_第1张图片

三、

 周报(2025.1.20 ~ 2025.1.26)_第2张图片

 没有公元0年,所以当公元为负值时,需要再减1。

#include 
using namespace  std;
#define ll long long
#define PII pair
#define endl '\n'

void solve(){
    ll n,m;
    cin >> n >> m;
    ll res = n + m;
    if(res <= 0){
        res = abs(n + m - 1); // !!!
    }
    if(res % 4 == 0 and res % 100 != 0) cout << 29 << endl;
    else if(res % 400 == 0) cout << 29 << endl;
    else cout << 28 << endl;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);

    ll t;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

四、

周报(2025.1.20 ~ 2025.1.26)_第3张图片 注意范围词汇,所有,不能当成每一场的;

认真读题!!!

五、

周报(2025.1.20 ~ 2025.1.26)_第4张图片

 可以直接计算相邻两个元素直接的差值,若符合答案,一定只有两种值,一种是1 或 -1 , 另一种是 n-1 或 1 - n;

#include 
using namespace  std;
#define ll long long
#define PII pair
#define endl '\n'

void solve(){
    ll n;
    cin >> n;
    vector a(n+1);
    for(ll i = 1 ; i <= n ; ++i) cin >> a[i];
    if(n == 1){
        if(a[1] != 1) cout << "NO" << endl;
        else cout << "YES" << endl;
        return;
    }
    map mp;
    for(ll i = 2 ; i <= n ; ++i){
        mp[a[i] - a[i-1]]++;
    }
    mp[a[1] - a[n]]++;
    multiset s;
    for(auto [x,y] : mp){
        s.insert(x);
    }
    if(s.size() != 2) {
        cout << "NO" << endl;
        return;
    }
    if(s.contains(-1) || s.contains(1)){
        cout << "YES" << endl;
    }
    else cout << "NO" << endl;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);

    int t;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

六、 

 

周报(2025.1.20 ~ 2025.1.26)_第5张图片

可以预处理出从第i个元素起往后的最大值,然后遍历,计算最大值;

 

你可能感兴趣的:(cocoa,macos,objective-c)