首页 > 其他分享 >Codeforces Round 886 (Div. 4)

Codeforces Round 886 (Div. 4)

时间:2023-07-22 11:00:11浏览次数:32  
标签:10 886 int dsu Codeforces mp ans Div dis

F. We Were Both Children

image-20230722103513062

题解:约数

  • 我们先利用\(map\)记录\(a_i\)的出现次数
  • 然后对\(map\)中的每一个元素的在其所有不超过\(n\)的倍数的位置上都加上对应的贡献
  • 时间复杂度:调和级数\(O(nlogn)\)
const int N = 2e5 + 10, M = 4e5 + 10;

int n;
int a[N];
map<int, int> mp;

void solve()
{
    cin >> n;
    mp.clear();
    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i];
        mp[a[i]]++;
    }
    vector<int> ans(n + 10);
    for (auto [x, y] : mp)
        for (int j = x; j <= n; j += x)
            ans[j] += y;

    int res = 0;
    for (int i = 1; i <= n; ++i)
        res = max(res, ans[i]);
    cout << res << endl;
}

G. The Morning Star

image-20230722104226994

题解:\(map\) + 组合计数

  • 我们开\(4\)个\(map\)记录四个方向上点的数量
  • 假设每一个方向上的点数为\(x\),显然每一个方向上的方案数为\(x \times (x - 1)\)
const int N = 2e5 + 10, M = 4e5 + 10;

int n;
map<int, int> mp[4];

void solve()
{
    cin >> n;
    mp[0].clear();
    mp[1].clear();
    mp[2].clear();
    mp[3].clear();
    for (int i = 1; i <= n; ++i)
    {
        int x, y;
        cin >> x >> y;
        mp[0][x]++;
        mp[1][y]++;
        mp[2][y - x + n]++;
        mp[3][y + x]++;
    }
    int ans = 0;
    for (auto [x, y] : mp[0])
        if (y)
            ans += y * (y - 1);
    for (auto [x, y] : mp[1])
        if (y)
            ans += y * (y - 1);
    for (auto [x, y] : mp[2])
        if (y)
            ans += y * (y - 1);
    for (auto [x, y] : mp[3])
        if (y)
            ans += y * (y - 1);
    cout << ans << endl;
}

H. The Third Letter

image-20230722104617379

题解:带权并查集

  • 我们考虑带权并查集做法

  • 并查集中维护每个点到根节点的距离为\(dis_u\)

  • 显然如果两个士兵\(u,v\)有关系的话,那么根据简单容斥得到,两者之间的距离为\(dis_u - dis_v\)

  • 对于两个士兵\(u,v\)来说,如果当前没有关系,那么合并两个点,即将\(u\)的根节点\(fu\)合并到\(v\)的根节点\(fv\)上去,我们考虑如何构造\(dis_{fu}\)

\[dis_u + dis_{fu} - dis_v = w\\ dis_{fu} = dis_v - dis_u + w \]

  • 那么如果两个士兵有关系的话,判断两者之间的距离是否合法即可
const int N = 2e5 + 10, M = 4e5 + 10;

int n, m;

// 带权并查集
struct DSU
{
    int fa[N], dis[N];
    void init()
    {
        for (int i = 1; i <= n; ++i)
        {
            fa[i] = i;
            dis[i] = 0;
        }
    }
    int find(int x)
    {
        if (x == fa[x])
            return x;
        int rt = find(fa[x]);
        dis[x] += dis[fa[x]];
        fa[x] = rt;
        return fa[x];
    }
    void merge(int u, int v, int w)
    {
        int fu = find(u);
        int fv = find(v);
        if (fu != fv)
        {
            fa[fu] = fv;
            // dis[u] + dis[fu] - dis[v] = w;
            dis[fu] = dis[v] - dis[u] + w;
        }
    }
} dsu;

void solve()
{
    cin >> n >> m;
    dsu.init();
    bool flag = true;
    for (int i = 1; i <= m; ++i)
    {
        int u, v, w;
        cin >> u >> v >> w;
        if (dsu.find(u) != dsu.find(v))
            dsu.merge(v, u, w);
        else
        {
            if (dsu.dis[v] - dsu.dis[u] != w)
                flag = false;
        }
    }
    if (flag)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
}

标签:10,886,int,dsu,Codeforces,mp,ans,Div,dis
From: https://www.cnblogs.com/Zeoy-kkk/p/17572996.html

相关文章

  • Codeforces Round 886 (Div. 4)
    CodeforcesRound886(Div.4)A-ToMyCritics思路:最大的两个数的和大于等于10则YES#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongtypedefpair<int,int>PII;typedefpair<string,int>PSI;typedefpair<string,string>PSS;cons......
  • 「解题报告」Codeforces Round 886 (Div. 4)
    比赛地址:Dashboard-CodeforcesRound886(Div.4)-Codeforces由于时间太晚了,因此并没有参加比赛,题目都是后来补做的。A.ToMyCriticsProblem-A-Codeforces\(T\)组数据,有\(a,b,c\)三个数,判断是否存在两个数的和\(sum\ge10\)。/*Thecodewaswrittenby......
  • Codeforces 1456E - XOR-ranges
    考虑一个\(L\lex\leR\)的数\(x\),必然是一段前缀贴着\(L\)或者\(R\),然后下一位脱离了\(L\)和\(R\)的限制,后面随便乱填。注意到一个性质,对于某一位\(d\),考虑这一位上没有限制的那些位置,最优方案肯定是令其等于其左边(或者右边)第一个有限制的数的第\(d\)位上的值。这......
  • Codeforces Round 886 (Div. 4)
    A.ToMyCritics#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongvoidsolve(){vector<int>a(3);for(auto&i:a)cin>>i;sort(a.begin(),a.end());if(a[2]+a[1]>=10)cout......
  • Codeforces 1830E - Bully Sort
    这种题肯定首先要寻找不变量。显然后面排好序的后缀不会被改变。因此从整体上来看我们的流程肯定是,如果当前\(p_n=n\),就令\(n\)减一,否则你一步换的\(i\)肯定满足\(p_i=n\)。而显然\(\min\limits_{j=i}^np_j\lei\),因此我们考察\(\sum|i-p_i|\)和\(\sum\limits_{i<j}[p_......
  • Codeforces 794G - Replace All
    一个比较垃圾的做法,卡着时限过了这道题。首先大胆猜个结论:要么\(|s|=|t|\),此时\(A,B\)任取,要么存在字符串\(c\)和整数\(x,y\)使得\(A=c^x,B=c^y\),其中\(c^x\)表示\(x\)个\(c\)拼接得到的结果。证明的话感觉还挺复杂的,可能要border引理之类的东西,不过我是先写了个......
  • 洛谷 P8861 - 线段
    牛逼题。先考虑\(l\le10^5,10^5+1\ler\)的部分分:一种方法是线段树,即因为左右端点是独立的,因此对左右端点各维护一个权值线段树表示有多少个区间以这个值为左/右端点,这样对于修改,左端点的部分相当于先查询\(\lel\)的数的个数,然后将它们都挂到\(l\)上,最后把\(<l\)的部......
  • Vue3 响应式全局对象json 动态绑定界面三 (Div块样式 字符串叠加)
    效果 man.js  定义响应式全局对象 globalData//全局对象constglobalData=reactive({missedCallData:"",currentUserTel:"",})app.provide('globalData',globalData);在main.js的函数中改变missedCallData 的值从而改变界面列表//改变全局变量gl......
  • Vue3 响应式全局对象json 动态绑定界面四 (Div块样式 Json数据绑定)
    效果man.js  定义响应式全局对象 globalData//全局对象constglobalData=reactive({extTelTalkData:[{userExten:"1000",userName:"刘亦菲",callStatus:"通话"},{......
  • Codeforces 856F - To Play or not to Play
    首先,DP肯定是逃不掉的,因为直接贪心其实不好判断在两个人都可以上线的时间段究竟是哪个人上线,需要通过后面的情况来做出判断,但是这题值域比较大直接维护DP值肯定不行,因此考虑先设计一个与值域有关的DP然后优化。将时间区间离散化,然后依次考虑每个时间区间。一个很自然的想法......