首页 > 其他分享 >Educational Codeforces Round 161 (Rated for Div

Educational Codeforces Round 161 (Rated for Div

时间:2024-01-26 19:13:36浏览次数:20  
标签:std boy Educational Rated int Codeforces long Lazy mp

A Tricky Template

题目描述

给一个数字n和三个长度为n的字符串 a,b,c。找一个模板使得字符串a,b匹配,而c不匹配,是否存在这样一个模板.

匹配的定义是:当模板字母为小写时,两个字符串字符相同,为大写时,两个字符不同,这样的两个字符串则匹配

解题思路

我们可以直接得出当a字符串和b字符串都不等于c字符串时,就存在这样一个模板使得满足题意

参考代码

#include <bits/stdc++.h>

#define int long long
#define endl '\n'
[[maybe_unused]]const int INF = 1e18 + 50, pi = 3;
[[maybe_unused]]typedef std::pair<int, int> pii;

void solve() {
    int n;
    std::cin >> n;
    std::string a, b, c;
    std::cin >> a >> b >> c;
    for (int i = 0; i < n; i++)
        if (a[i] != c[i] && b[i] != c[i]) {
            std::cout << "YES" << endl;
            return;
        }
    std::cout << "NO" << endl;
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr), std::cout.tie(nullptr);
    int Lazy_boy_ = 1;
    std::cin >> Lazy_boy_;
    while (Lazy_boy_--) solve();
    return 0;
}

B Forming Triangles

题目描述

给定一个长度为n的一个数组a,对于a[i]来说,就是一个长度为2^a[i]的木棒,现在问:用这些木棒能组成多少个三角形.

解题思路

对于一个三角形来说,两边之和大于第三边,两边之差小于第三边,不可能存在两边之和等于第三边,那么我们就可以得到要组成一个三角形就至少需要两条边相等,第三边小于等于这两个边,那么这个问题就成了一个组合数问题

组合数用C(cnt, m)来表示,其中cnt为这个数的总数,m为我们需要选的个数;
cnt可以用map来写,对数组去重,遍历一次数组,s表示比遍历到当前数字小的个数
即写成ans += C(mp[a[i]], 3) + C(mp[a[i]], 2) * s

参考代码

#include <bits/stdc++.h>

#define int long long
#define endl '\n'
[[maybe_unused]]const int INF = 1e18 + 50, pi = 3;

int C(int n, int m) {
    if (n < m) return 0;
    int ans = 1;
    for (int i = 1; i <= m; i++)
        ans = ans * (n - m + i) / i;
    return ans;
}

void solve() {
    int n;
    std::cin >> n;
    std::vector<int> a(n, 0ll);
    std::map<int, int> mp;
    for (int i = 0; i < n; i++) std::cin >> a[i], mp[a[i]]++;
    std::sort(a.begin(), a.end());
    n = std::unique(a.begin(), a.end()) - a.begin();
    int ans = C(mp[a[0]], 3), s = 0;
    for (int i = 1; i < n; i++) {
        int t = mp[a[i]];
        s += mp[a[i - 1]];
        ans += C(t, 3) + C(t, 2) * s;
    }
    std::cout << ans << endl;
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr), std::cout.tie(nullptr);
    int Lazy_boy_ = 1;
    std::cin >> Lazy_boy_;
    while (Lazy_boy_--)
        solve();
    return 0;
}

C Closest Cities

题目描述

x轴上有n个城市,其位置为a[i],并且序列a按升序排列,保证每个城市只有一个最近的城市
i的最近城市是i+1则花费1,否则花费|a[i]-a[i+1]|,现在有q次询问
问:每次询问城市x到城市y的最小花费.

解题思路

先分析一下x<y,我们只有一直向右走才会有最小花费,那就需要判断当前城市的最近城市是否为下一个城市.
由于是一直向右走,我么就可以用前缀和求解这个问题.
同理,x>y时,我们是从右向左走,可以用后缀和求解.

参考代码

#include <bits/stdc++.h>

#define int long long
#define endl '\n'
[[maybe_unused]]const int INF = 1e18 + 50, pi = 3;

void solve() {
    int n, q;
    std::cin >> n;
    std::vector<int> a(n + 1, 0ll);
    auto s1 = a, s2 = a;
    for (int i = 1; i <= n; i++) std::cin >> a[i];
    s1[2] = 1;
    for (int i = 2; i < n; i++) {
        if (a[i] - a[i - 1] > a[i + 1] - a[i]) s1[i + 1] = s1[i] + 1;
        else s1[i + 1] = s1[i] + abs(a[i + 1] - a[i]);
    }
    s2[n - 1] = 1;
    for (int i = n - 1; i > 1; i--) {
        if (a[i] - a[i - 1] > a[i + 1] - a[i]) s2[i - 1] = s2[i] + abs(a[i] - a[i - 1]);
        else s2[i - 1] = s2[i] + 1;
    }
//    for (int i = 1; i <= n; i++)
//        std::cout << s1[i] << ' ';
//    std::cout << endl;
//    for (int i = 1; i <= n; i++)
//        std::cout << s2[i] << ' ';
    std::cout << endl;
    std::cin >> q;
    while (q--) {
        int x, y;
        std::cin >> x >> y;
        if (x < y)
            std::cout << s1[y] - s1[x] << endl;
        else
            std::cout << s2[y] - s2[x] << endl;
    }
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr), std::cout.tie(nullptr);
    int Lazy_boy_ = 1;
    std::cin >> Lazy_boy_;
    while (Lazy_boy_--)
        solve();
    return 0;
}

标签:std,boy,Educational,Rated,int,Codeforces,long,Lazy,mp
From: https://www.cnblogs.com/Lazyboyjdj/p/17990497

相关文章

  • hey_left 17 Codeforces Round 817 (Div. 4)
    题目链接A.把标准字符串和输入字符串排序,看是否相等#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongconstintN=1e5+10;voidsolve(){intn;cin>>n;stringt;cin>>t;strings="Timur";sort(s.begin(),s.end());......
  • hey_left 16 Codeforces Round 827 (Div. 4)
    题目链接A.判最大的数是不是另外两个数的和#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongconstintN=1e5+10;voidsolve(){inta,b,c;cin>>a>>b>>c;cout<<(max({a,b,c})==a+b+c-max({a,b,c})?"YES":"......
  • Codeforces 1667E Centroid Probabilities
    这个连边方式就可以理解为\(1\)为根,点\(u\)的父亲\(fa_u\)满足\(fa_u<u\)。重心有不止一种表示法,考虑用“子树\(siz\ge\lceil\frac{n}{2}\rceil\)最深的点”来表示重心。令\(m=\lceil\frac{n}{2}\rceil\),\(f_i\)为节点\(i\)的\(siz\gem\)的方案数。考虑枚......
  • Codeforces Round 920 (Div. 3)
    赛时过了A~E,表现分1738。感觉D~G都挺有意义拿出来说的。[D]VeryDifferentArray首先一个贪心的猜想是:如果A和B长度相同,那一个顺序一个逆序应该是最优的情况。思考下如何证明这个猜想。假如A和B的长度均为2,易证\(A_1\)<\(A_2\),\(B_1\)>\(B_2\)时最优......
  • CodeForces 1667E Centroid Probabilities
    洛谷传送门CF传送门首先需要了解重心的三种定义:删掉一个点后剩下子树大小\(\le\frac{n}{2}\)的点\(\sum\limits_{i=1}^n\text{dis}(u,i)\)最小的点最深的\(sz_u\ge\left\lceil\frac{n}{2}\right\rceil\)的点这道题我们使用第三种定义,也就是要统计\(i\)为最......
  • Codeforces round 919 (div2)
    Problem-A-Codeforces暴力枚举就可以;#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;vector<int>a;intn;signedmain(){int_;cin>>_;while(_--){a.clear();cin>>n;......
  • hey_left 15 Codeforces Round 835 (Div. 4)
    题目链接A.总和-最小值-最大值即为中间数#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongvoidsolve(){inta,b,c;cin>>a>>b>>c;cout<<a+b+c-min({a,b,c})-max({a,b,c})<<'\n';}signedmain(){io......
  • Codeforces Round 170 (Div. 1)A. Learning Languages并查集
    如果两个人会的语言中有共同语言那么他们之间就可以交流,并且如果a和b可以交流,b和c可以交流,那么a和c也可以交流,具有传递性,就容易联想到并查集,我们将人和语言看成元素,一个人会几种语言的话,就将这些语言和这个人所在的集合合并,最后求一下人一共在几个连通块中,连通块的个数-1就是答案,......
  • CodeForces 1010F Tree
    洛谷传送门CF传送门educational的。另一道类似的题是[ABC269Ex]Antichain(但是我还没写)。考虑令\(b_u=a_u-\sum\limits_{v\inson_u}a_v\)。那么\(\sum\limits_{i=1}^nb_i=a_1=x\),且\(\foralli\in[1,n],b_i\ge0\)。所以最后连通块内有\(y\)个点,那......
  • hey_left 14 Codeforces Round 849 (Div. 4) 续
    题目链接F.区间修改,单点查询,考虑用树状数组可以维护每个点需要操作的次数然后直接对询问的点进行操作#include<bits/stdc++.h>usingnamespacestd;constintN=2e5+10;#defineintlonglongstructTreeArray{vector<int>tree;TreeArray(intn){......