zoukankan      html  css  js  c++  java
  • Codeforces Round #246 (Div. 2)

    题目链接:Codeforces Round #246 (Div. 2)

    A:直接找满足的人数,然后整除3就是答案

    B:开一个vis数组记录每一个衣服的主场和客场出现次数。然后输出的时候主场数量加上反复的,客场数量减掉反复的

    C:原来是YY乱搞的。原来是哥德巴赫猜想,一个合数能够表示为3个质数相加,然后就先打个素数表,然后从最小的数字一个个模拟往前放就可以。放的时候走的步数直接拆成都是质数就可以

    D:KMP算法,利用next数组性质求前缀和后缀匹配,然后在利用累加求和求出每一个串相应的出现次数

    代码:

    A:

    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    
    int n, k, num, i;
    
    int main() {
        scanf("%d%d", &n, &k);
        int ans = 0;
        for (i = 0; i < n; i++) {
            scanf("%d", &num);
            if (5 - num >= k) ans++;
        }
        printf("%d
    ", ans / 3);
        return 0;
    }

    B:

    #include <stdio.h>
    #include <string.h>
    
    const int N = 100005;
    int vis[N][2];
    int n, x[N], y[N], i;
    
    int main() {
        scanf("%d", &n);
        for (i = 0; i < n; i++) {
            scanf("%d%d", &x[i], &y[i]);
            vis[x[i]][0]++;
            vis[y[i]][1]++;
        }
        for (i = 0; i < n; i++) {
            printf("%d %d
    ", (n - 1) + vis[y[i]][0], (n - 1) - vis[y[i]][0]);
        }
        return 0;
    }

    C:

    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    const int N = 100005;
    
    int pri[N], ans[5 * N][2], ansn = 0;
    
    void init() {
        int vis[N];
        memset(vis, 0, sizeof(vis));
        for (int i = 2; i < N; i++) {
            if (vis[i]) continue;
            pri[i] = 1;
            for (int j = i; j < N; j += i)
                vis[j] = 1;
        }
    }
    
    int n, num[N], v[N], i, snum[N];
    
    void swap(int &a, int &b) {
        a ^= b;
        b ^= a;
        a ^= b;
    }
    
    int main() {
        init();
        scanf("%d", &n);
        for (i = 0; i < n; i++) {
            scanf("%d", &num[i]);
            snum[i] = num[i];
            v[num[i]] = i;
        }
        sort(snum, snum + n);
        i = 0;
        while (i < n) {
            while (v[snum[i]] != i) {
                for (int j = i; ;j++) {
                    if (pri[v[snum[i]] - j + 1]) {
                        ans[ansn][0] = j + 1;
                        ans[ansn][1] = v[snum[i]] + 1;
                        ansn++;
                        int t = v[snum[i]];
                        v[snum[i]] = j;
                        v[num[j]] = t;
                        swap(num[j], num[t]);
                        break;
                    }
                }
            }
            i++;
        }
        printf("%d
    ", ansn);
        for (i = 0; i < ansn; i++)
            printf("%d %d
    ", ans[i][0], ans[i][1]);
        return 0;
    }

    D:

    #include <stdio.h>
    #include <string.h>
    #define INF 0x3f3f3f3f
    const int N = 100005;
    
    char s[N];
    int next[N], n, ans[N], ansn = 0;
    
    
    void get_next(char *seq, int m) {
        next[1] = 0;
        int j = next[1];
        for (int i = 2; i <= m; i++) {
            while (j && seq[i] != seq[j + 1]) j = next[j];
            if (seq[i] == seq[j + 1]) j++;
            next[i] = j;
        }
    }
    
    int vis[N];
    
    int main() {
            int i = 0;
            scanf("%s", s + 1);
            n = strlen(s + 1);
            get_next(s, n);
            int t = next[n];
            while (t) {
                ans[ansn++] = t;
                t = next[t];
            }
            for (i = n; i > 0; i--)
                vis[next[i]]++;
            for (i = n; i > 0; i--)
                vis[next[i]] += vis[i];
            printf("%d
    ", ansn + 1);
            for (i = ansn - 1; i >= 0; i--)
                printf("%d %d
    ", ans[i], vis[ans[i]] + 1);
            printf("%d %d
    ", n, vis[n] + 1);
        return 0;
    }


  • 相关阅读:
    调用Mapreduce,org.apache.hadoop.hbase.mapreduce处理hbase问题
    mysql用视图和事件实现数据间隔插入数据到表
    mysql自定义函数实现数据正则处理
    ambari安装Zeppelin Notebook提示ES源找不到问题解决
    IDEA 进行编译代码的时候,报错误Error:scalac: IO error while decoding .* with GBK
    Linux最大线程数限制及当前线程数查询
    rhel log 日志时间戳时间不对更改方法
    usb禁用验证
    CentOS 7 下挂载NTFS文件系统并实行开机自动挂载
    linux 判定那块网卡为eth0 eth0对应的物理网卡闪灯30秒
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5121898.html
Copyright © 2011-2022 走看看