首页 > 其他分享 >Educational Codeforces Round 159 (Rated for Div. 2)

Educational Codeforces Round 159 (Rated for Div. 2)

时间:2023-12-17 22:56:35浏览次数:28  
标签:Educational Rated return 159 ll typedef long int solve

Educational Codeforces Round 159 (Rated for Div. 2)

A - Binary Imbalance

解题思路:

有一对\((0,1)\),那么\(0\)就能无限增长。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
typedef pair<ll, ll> pii;
const ll mod = 998244353;
ll n, m;
ll a[N];

void solve()
{
    int n;
    cin >> n;
    string s;
    cin >> s;
    int a = 0;
    int b = 0;
    for (int i = 0; i < n - 1; i++)
    {
        if (s[i] != s[i + 1])
        {
            puts("YES");
            return;
        }
    }
    for (auto c : s)
    {
        if (c == '1')
        {
            a++;
        }
        else
        {
            b++;
        }
    }
    if (a >= b)
    {
        puts("NO");
    }
    else
    {
        puts("YES");
    }
}

int main()
{
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }

    return 0;
}

B - Getting Points

解题思路:

全部留到最后几天做。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
typedef pair<ll, ll> pii;
const ll mod = 998244353;
ll n, m;
ll a[N];

void solve()
{
    ll n, p, l, t;
    cin >> n >> p >> l >> t;
    ll a = 1 + (n - 1) / 7;
    ll ans = 0;
    ll b = a / 2;
    ll base = (l + 2 * t);
    ll res = 0;
    ll cnt = 0;
    if (a & 1)
    {
        res = b * (base) + t + l;
        cnt = b + 1;
    }
    else
    {
        res = b * base;
        cnt = b;
    }
    // cout << res << endl;
    if (res >= p)
    {
        ans = ((p + base - 1) / base);
    }
    else
    {
        ans = cnt + ((p - res) + l - 1) / l;
    }
    cout << n - ans << endl;
}

int main()
{
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }

    return 0;
}

C - Insert and Equalize

解题思路:

对\(a\)排个序,设\(b[i] = a[i + 1] - a[i]\),我们得到数组\(b[1\sim (n - 1)]\)。

\(x = gcd(b)\),此时根据此\(x\)计算数组\(a\)中的最小运算次数一定数最少的。

如果我们可以加入\(a[n + 1] = a[n] - x\),那么最小运算次数\(+ 1\);同理,若\(a[n + 1] = a[n] - 2 * x\),那么$ + 2$。

如果直到\(a[n + 1] = a[n] - (n - 1) * x\)都已经存在,那么\(a[n + 1] = a[n] + x\)。

因为\(a[n + 1] = a[n] + x\),最小运算次数$ + n$,且一定合法。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
typedef pair<ll, ll> pii;

int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}

void solve()
{
    int n;
    cin >> n;
    vector<int> a(n + 1);
    set<int> s;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        s.insert(a[i]);
    }
    sort(a.begin() + 1, a.end());
    int g = 0;
    for (int i = 2; i <= n; i++)
    {
        g = gcd(g, a[i] - a[i - 1]);
    }
    // cout << g << endl;
    if (g == 0)
    {
        puts("1");
        return;
    }
    ll t = 0;
    bool f = false;
    for (int i = 1; i < n; i++)
    {
        if (!s.count(a[n] - i * g))
        {
            f = true;
            t = a[n] - i * g;
            break;
        }
    }
    ll ans = 0;
    for (int i = 1; i <= n; i++)
    {
        ans += (a[n] - a[i]) / g;
    }
    if (f)
    {
        ans += (a[n] - t) / g;
    }
    else
    {
        ans += n;
    }
    cout << ans << endl;
}

int main()
{
    int t = 1;
    cin >> t;
    while (t--)
    {

        solve();
    }

    return 0;
}

D - Robot Queries

解题思路:

对于每一次查询,我们分三段判断\((1,l -1),(l ,r),(r+1,n)\)。

我们记录每个出现过的坐标是在第几步出现的\(pos[{x,y}] = i\)。

对于\((1,l-1)和(r + 1,n)\)我们可直接二分判断是否存在\((x,y)\)。

对于\((l,r)\),由于题目要求,我们要进行一些变换。

设\((l-1)步到达的坐标为(a_1,b_1),第r步到达的坐标为(a_2,b_2)\)。

一个区间段中无论正序倒序走,\(x\)轴和\(y\)轴最后的偏移量都是不变的。

目标位置到第\((l - 1)\)步位置的偏移量为\((x - a_1,y - b_1)\)。

第\(r\)步道减去这些偏移量为\((a_2 - (x - a_1),b_2 - (y - b_1))\)

\(x_1 = a_2 - (x - a_1),y_1 = b_2 - (y - b_1)\)。

\((x_1,y_1)\),就是转换后我们正序走该走到的位置。

我们二分判断该位置是否存在即可。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
typedef pair<ll, ll> pii;
#define fi first
#define se second

void solve()
{
    int n, m;
    cin >> n >> m;
    string s;
    cin >> s;
    map<pii, vector<int>> v;
    s = ' ' + s;
    v[{0, 0}].push_back(0);
    map<int, pii> pos;
    int x = 0;
    int y = 0;
    for (int i = 1; i <= n; i++)
    {

        if (s[i] == 'U')
        {
            y++;
        }
        else if (s[i] == 'D')
        {
            y--;
        }
        else if (s[i] == 'L')
        {
            x--;
        }
        else
        {
            x++;
        }
        v[{x, y}].push_back(i);
        pos[i] = {x, y};
        // cout << i << ' ' << x << ' ' << y << endl;
    }
    for (int i = 1; i <= m; i++)
    {
        int x, y, l, r;
        cin >> x >> y >> l >> r;
        if (v[{x, y}].size())
        {
            auto idx = *(v[{x, y}].begin());
            if (idx < l)
            {
                puts("YES");
                continue;
            }
            idx = *v[{x, y}].rbegin();
            if (idx >= r)
            {
                puts("YES");
                continue;
            }
        }
        int a1 = pos[l - 1].fi;
        int b1 = pos[l - 1].se;
        int a2 = pos[r].fi;
        int b2 = pos[r].se;
        int x1 = a2 + a1 - x;
        int y1 = b2 + b1 - y;
        // cout << a1 << b1 << endl;
        // cout << a2 << ' ' << b2 << endl;
        // cout << x1 << ' ' << y1 << endl;
        if (v[{x1, y1}].size())
        {
            auto it = lower_bound(v[{x1, y1}].begin(), v[{x1, y1}].end(), l);
            if (it != v[{x1, y1}].end())
            {
                int idx = *it;
                if (idx >= l && idx <= r)
                {
                    puts("YES");
                    continue;
                }
            }
        }
        puts("NO");
    }
}

int main()
{
    int t = 1;
    // cin >> t;
    while (t--)
    {

        solve();
    }

    return 0;
}

E - Collapsing Strings

解题思路:

\(s1 = aabba,s2 = abbcc\),我们发现剩下\(aacc\)。

观察得到规律:\(C(a,b) = len(s1) + len(s2) - 2 *len(a_{pre} == b_{suf})\)。就是减去二者前后缀公共部分。

先将所有字符长度累加起来。

\(trie\)树插入所有字符串,记录每个前缀出现的次数。

将每个字符逆序后一一查询,减去对应前后缀相同长度的代价。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
typedef pair<ll, ll> pii;
#define fi first
#define se second
int tr[N][26];
ll cnt[N];
int idx;

void insert(string s)
{
    int p = 0;
    for (auto c : s)
    {
        int u = c - 'a';
        if (!tr[p][u])
        {
            tr[p][u] = ++idx;
        }
        p = tr[p][u];
        cnt[p]++;
    }
}

ll query(string s)
{
    ll res = 0;
    int p = 0;
    for (auto c : s)
    {
        int u = c - 'a';
        if (!tr[p][u])
        {
            return res;
        }
        p = tr[p][u];
        res += cnt[p] * 2;
    }
    return res;
}

void solve()
{
    int n;
    cin >> n;
    vector<string> v;
    ll ans = 0;
    for (int i = 1; i <= n; i++)
    {
        string s;
        cin >> s;
        v.push_back(s);
        ans += 2 * (ll)s.size() * n;
        insert(s);
    }
    for (auto &s : v)
    {
        reverse(s.begin(), s.end());
        ans -= query(s);
    }
    cout << ans << endl;
}

int main()
{
    int t = 1;
    // cin >> t;
    while (t--)
    {

        solve();
    }

    return 0;
}

标签:Educational,Rated,return,159,ll,typedef,long,int,solve
From: https://www.cnblogs.com/value0/p/17910052.html

相关文章

  • Educational Codeforces Round 134 (Rated for Div. 2)
    基本情况AB秒了。C搞了一个错的二分答案,虽然过样例了。C.Min-MaxArrayTransformation错误分析没有进一步推导性质,而是觉得数据单调递增估计是二分,然后就无脑写,实际上check的正确性没有保证。boolcheck(intind,intnow){ if(ind==now)returntrue; if(b[ind]......
  • Educational Codeforces Round 159 (Rated for Div. 2) C. Insert and Equalize (贪心+
    EducationalCodeforcesRound159(RatedforDiv.2)C.InsertandEqualize思路:首先对\(a\)进行排序,然后对所有差值取gcd,获得可用的最大因子\(gc\),答案有两种情况:一种是\(a_{n+1}\)在$a_1\(~\)a_n$范围内,这时要获得最大的位置一种情况是$a_1\(~\)a_n$......
  • openGauss学习笔记-159 openGauss 数据库运维-备份与恢复-导出数据-使用gs_dump和gs_d
    openGauss学习笔记-159openGauss数据库运维-备份与恢复-导出数据-使用gs_dump和gs_dumpall命令导出数据-导出所有数据库-导出所有数据库159.1导出所有数据库openGauss支持使用gs_dumpall工具导出所有数据库的全量信息,包含openGauss中每个数据库信息和公共的全局对象信息。可根......
  • CF1592F2 Alice and Recoloring 2
    题意给定一个矩阵,有两种颜色\(W\)和\(B\)。你可以花\(1\)的代价反转一个包含\((1,1)\)的矩阵。你可以花\(3\)的代价反转一个包含\((n,1)\)的矩阵。你可以花\(4\)的代价反转一个包含\((1,m)\)的矩阵。你可以花\(2\)的代价反转一个包含\((n,m)\)的矩阵......
  • [CF1592F2] Alice and Recoloring 2
    题目链接操作2和3可以用另两个代替,没有任何用。设W表示\(t_{i,j}=0\),B表示\(t_{i,j}=1\)考虑差分。设\(t_{i,j}=s_{i,j}\opluss_{i+1,j}\opluss_{i,j+1}\opluss_{i+1,j+1}\),那么目标变为把$t4数组清0那么操作1是把单点翻转,操作4是对于一个\(x,y(x<n,y<m)......
  • Educational Codeforces Round 158 (Rated for Div. 2)
    Preface补题,妈的现在EduE都做不来这搞毛啊A.LineTrip签到#include<cstdio>#include<iostream>#include<utility>#include<vector>#include<cstring>#include<cmath>#include<cstdlib>#include<algorithm>#include<queue&......
  • CodeTON Round 7 (Div. 1 + Div. 2, Rated, Prizes!)
    Preface补题,经典不会F,看了会题解发现看不懂,索性直接开摆A.JaggedSwaps判断\(a_1\)是否为\(1\)即可#include<cstdio>#include<iostream>#include<utility>#include<vector>#include<cstring>#include<cmath>#include<cstdlib>#include<al......
  • Educational Codeforces Round 159 总结
    最失败的一集。C开题顺序搞错,不小心先开了C,以为是A。还好C不难。题意大概是在给定的数组最后添一个数(所有数两两不同),再自定义一个数\(k\),数组中每个数可以加上若干个\(k\),最后使得所有数字相等。要求加\(k\)的次数最少。如果不加最后一个数,那么显然把所有的数加到与最大......
  • [CF1902] Educational Codeforces Round 159 A~E 题解
    [CF1902]EducationalCodeforcesRound159A~E题解A.BinaryImbalance很快观察到如果有不同的相邻元素,那么一定有解,意味着如果全是1无解,其他有解B.GettingPoints题面很长,可以发现,最好的偷懒方式一定是把所有的课都拖到最后几天上(真实),可以简单调整证明这样是不劣的,最后......
  • [Educational Codeforces Round 159 (Rated for Div. 2)](https://codeforces.com/con
    EducationalCodeforcesRound159(RatedforDiv.2)好困,差点没打A-BinaryImbalance#include<bits/stdc++.h>#defineintlonglong#defineendl'\n'usingnamespacestd;voidsolve(){ strings; intn; cin>>n; cin>>s; if(n==......