首页 > 其他分享 >Educational Codeforces Round 25

Educational Codeforces Round 25

时间:2023-02-27 20:47:06浏览次数:57  
标签:25 Educational idx int Codeforces ++ cnt using include

Educational Codeforces Round 25

https://codeforces.com/contest/825
ABCD没有什么意思,阅读理解题比较无聊(((
EFG不难写,但是思维比较巧,知道就是知道,不知道就写不出emm

A. Binary Protocol

#include <bits/stdc++.h>

using namespace std;

int main () {
    int n;
    string s;
    cin >> n >> s;
    int len = 0;
    for (auto i : s) {
        if (i == '1')   len ++;
        else {
            cout << len;
            len = 0;
        }
    }
    cout << len;
}

B. Five-In-a-Row

#include <bits/stdc++.h>

using namespace std;
const int N = 15;
char a[N][N];
int n = 10;

bool check(int i, int j) {
    int cnt = 1;
    //横向
    cnt = 1;
    for (int k = j + 1; k <= n; k++) {
        if (a[i][k] == 'X')    cnt++;
        else    break;
    }
    for (int k = j - 1; k >= 1; k--) {
        if (a[i][k] == 'X')    cnt++;
        else    break;
    }
    if (cnt >= 5)   return true;

    //纵向
    cnt = 1;
    for (int k = i + 1; k <= n; k++) {
        if (a[k][j] == 'X')    cnt++;
        else    break;
    }
    for (int k = i - 1; k >= 1; k--) {
        if (a[k][j] == 'X')    cnt++;
        else    break;
    }
    if (cnt >= 5)   return true;

    //主对角线
    cnt = 1;
    for (int k = 1; k + i <= n; k++) {
        if (a[i + k][j + k] == 'X')    cnt++;
        else    break;
    }
    for (int k = 1; i - k >= 1; k++) {
        if (a[i - k][j - k] == 'X')    cnt++;
        else    break;
    }
    if (cnt >= 5)   return true;

    //副对角线
    cnt = 1;
    for (int k = 1; k + i <= n && j - k >= 1; k++) {
        if (a[i + k][j - k] == 'X')    cnt++;
        else    break;
    }
    for (int k = 1; i - k >= 1 && j + k <= n; k++) {
        if (a[i - k][j + k] == 'X')    cnt++;
        else    break;
    }
    if (cnt >= 5)   return true;

    return false;
}

int main() {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> a[i][j];
        }
    }


    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (a[i][j] == 'O')    continue;
            if (check (i, j)) {
                cout << "YES\n";
                return 0;
            }
        }
    }

    cout << "NO\n";
}

//若是放下之前就已经有能够连成的了

C. Multi-judge Solving

#include <bits/stdc++.h>
#define ll long long

using namespace std;
const int N = 1e3 + 5;
ll a[N], n, k, ans;

int main() {
    cin >> n >> k;
    for (int i = 1; i <= n; i++)    cin >> a[i];
    sort (a + 1, a + n + 1);
    for (int i = 1; i <= n; k = max (k, a[i++])) {
        if (a[i] <= 2 * k)      continue;
        while (k * 2 < a[i])    k *= 2, ans ++;
        //cout << k << ' ';
    }
    cout << ans << endl;
}

//自身是可以当作跳板的

D. Suitable Replacement

#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main() {
    string s, t;
    cin >> s >> t;
    map<char, int> mps, mpt;
    for (auto i : s)    mps[i]++;
    for (auto i : t)    mpt[i]++;
    mps['?'] = 0;
    for (auto &i : s) {
        if (i != '?')   continue;
        int minn = 1e9;
        char ch;
        for (char a = 'a'; a <= 'z'; a++) {
            if (1ll * minn * mpt[a] > mps[a]) {
                minn = mps[a] / mpt[a];
                ch = a;
            }
        }
        i = ch, mps[i] ++;
    }
    cout << s;
}

E. Minimal Labels

以为是差分约束,怎么是字典序最大的逆拓扑序??

// LUOGU_RID: 103196572
#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5, M = 3e5 + 5;//不是很懂为什么是这么大
int n, m, d[N], ans[N];
int h[N], e[M], ne[M], idx;

void add (int a, int b) {
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}

void topsort(){
    priority_queue<int> q; //为什么是优先队列: 字典序最大的top序
    for(int i = 1;i <= n; ++i)    if(d[i] == 0) q.push(i);
    int tot = n;
    while(q.size()){
        int t = q.top();
        ans[t] = tot--;
        q.pop();
        for(int i = h[t];i != -1; i = ne[i]){
            int j = e[i];
            d[j] --;// j 的入度 --
            if(d[j] == 0) q.push(j); 
        }
    }
}

int main () {
    memset (h, -1, sizeof h);
    cin >> n >> m;
    while (m --) {
        int a, b; //a < b
        cin >> a >> b;
        add (b, a);
        d[a] ++;
    }
    topsort ();
    for (int i = 1; i <= n; i++)    cout << ans[i] << ' ';
}

//逆拓扑序从大到小排

F. String Compression

dp + kmp蛮不错的小题
涉及到对kmp的理解,求循环节那块
(可能比较板?!)
对后缀进行next数组求解

// LUOGU_RID: 103202632
#include  <bits/stdc++.h>

using namespace std;
const int N = 8005;
int f[N], cnt[N], nxt[N]; //f[i]: 压缩到i时的答案, cnt[i]: i的位数
char s[N];

void kmp(char *s, int len, int nxt[]) {
    //cout << s << endl;
    nxt[0] = nxt[1] = 0;
    for(int i = 1; i < len; ++i) {
        int j = nxt[i];
        while(j && s[i]!=s[j])  j=nxt[j];
        if (s[j] == s[i])   nxt[i+1] = j + 1;
        else    nxt[i+1] = 0;
    }
}

int main () {
    cin >> s;
    int n = strlen (s);
    for (int i = 1; i <= n; i++) {
        cnt[i] = cnt[i/10] + 1, f[i] = i + 1; //字符+个数1
    }
    for (int i = 0; i < n; i++) {
        kmp (s + i, n - i, nxt);  //截取i为起点的后缀[i,n)
        for (int j = 1; j + i <= n; j++) { //区间[i,i+j)
            int len = j - nxt[j]; //最长循环节
            if (j % len)    f[i+j] = min (f[i+j], f[i] + j + 1);
            else    f[i+j] = min (f[i+j], f[i] + len + cnt[j/len]);
        }
    }
    cout << f[n] << endl;
}


//dp + kmp(nxt数组)
//nxt[i]: 后缀i可以匹配的最长前缀
//[nxt[i],i]: 以i结尾的循环串

G. Tree Queries

取巧(思维!?)
以第一个黑点为根,\(a_i\) 记录 \(i\) 到根上最小点的编号,则所有点的求解都可转化为 \(a_i\)。分为在/不在同一根上,都是直接记录最小值即可。根作为中介已经能转化了。

// LUOGU_RID: 103207434
#include <bits/stdc++.h>

using namespace std;
const int N = 1e6 + 5, M = N * 2;
int a[N], n, q; //a[i]: i到根上最小的点的编号
int h[N], e[M], ne[M], idx;

void add (int a, int b) {
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

void dfs (int u, int fa) {
    for (int i = h[u]; ~i; i = ne[i]) {
        int j = e[i];
        if (j == fa)    continue;
        a[j] = min (j, a[u]);
        dfs (j, u);
    }
}

int main () {
    memset (h, -1, sizeof h);
     scanf ("%d%d", &n, &q);
    for (int i = 1; i < n; i++) {
        int a, b;
        scanf ("%d%d", &a, &b);
        add (a, b), add (b, a);
    }
    int lst = 0, ans = 1e9, fst = 0;
    while (q--) {
        int op, x;
        scanf ("%d%d", &op, &x);
        x = (x + lst) % n + 1;
        if (op == 1) {
            if (fst == 0)   a[x] = x, fst = 1, dfs (x, -1); //以第一个黑为根
            ans = min (ans, a[x]);
        }
        else {
            lst = min (ans, a[x]);
            printf ("%d\n", lst);
        }
    }
}

//在不在一条路上的都可以转化

标签:25,Educational,idx,int,Codeforces,++,cnt,using,include
From: https://www.cnblogs.com/CTing/p/17161776.html

相关文章

  • 【比赛记录】 Codeforces Round #682 Div.2
    Problems:#NameSubmitASpecificTastesofAndreBValeriiAgainstEveryoneCEngineerArtemDPowerfulKseniaEYuriiCanDoEverything......
  • Codeforces Round #111 (Div. 2) E. Buses and People 线段树|多维限制|离散化
    一看发现要求满足3个条件,有点头大可以先把所有的bus和people拎出来,用bus的s和people的l去排序,这样能保证对于当前的people,si都合法。然后考虑如何满足ti最小的情况下,使得......
  • Shiro 身份认证绕过漏洞 CVE-2022-32532
    前言ApacheShiro是一个强大且易用的Java安全框架,通过它可以执行身份验证、授权、密码和会话管理。使用Shiro的易用API,您可以快速、轻松地保护任何应用程序——从最......
  • 25、完整模型训练套路-----torch.train()-----torch.eval()
    1、在训练、测试步骤开始的时候   并不是一定要设置训练或者测试模式才能开始训练,如果网络模型中有特殊的层可以调用,但是如果没有,调用也不会出错的。   ......
  • 【2023-02-25】连岳摘抄
    23:59我不忧伤,也不泄气。生活终究是生活,生活存在于我们自身之中,而不在于外界。以后我身边会有许多人,在他们中间做一个人并永远如此:不管有多么不幸,永不灰心和泄气,这就是生......
  • Codeforces Global Round 15 CF1552 A~G 题解
    点我看题对两三年前的cf进行考古的时候偶然做到这场,像这种全是构造题和思维题的比赛还是比较少见的。题目本身很有意思,但是对于现场打比赛想上分的人来说体验就比较差了。......
  • 2月25至2月26日——关于个人制作原创模型的尝试
      上述代码在我经过多方查询和对代码和报错的仔细观察后,发现我之前安装Python的方式一直有问题,导致后面AI所用的虚拟环境对应的Python版本一直不对。但重新安装真的是......
  • 25.高级子查询
    1.多列子查询--主查询中每条记录都会与多条记录和多字段子查询得结果进行比较--列对比匹配原则----多字段子查询得字段比较有两种------成对比较hr@ORCLPDB012023-02......
  • (AtCoder Beginner Contest 289)And Codeforces Round #851 (Div. 2)
     <C-Coverage Editorial>       这道题可以用dfs进行爆搜,但是在爆搜的时候要注意:是否同一个状态重复计数了比如dfs(i......
  • 统计难题 HDU - 1251
     给一些字符串,问以某个串为前缀的串有几个 #include<iostream>#include<algorithm>#include<cstring>usingnamespacestd;constintN=5e5+4;charnum[80]......