Codeforces Round #533 (Div. 2)题解

link

orz olinr AK Codeforces Round #533 (Div. 2)

中文水平和英文水平都太渣..翻译不准确见谅

T1.给定n<=1000个整数,你需要钦定一个值t,使得每个整数到线段[t-1,t+1]距离和最小,求最小距离,每个数<=100

枚举t

#include <cstdio>
using namespace std;

int n, a[1010];

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    int minans = 0x3f3f3f3f, fuck = -233;
    for (int ans = 1; ans <= 100; ans++)
    {
        int cost = 0;
        for (int i = 1; i <= n; i++)
        {
            if (a[i] > ans)
                cost += a[i] - ans - 1;
            else if (a[i] < ans)
                cost += ans - a[i] - 1;
        }
        if (cost < minans)
        {
            minans = cost;
            fuck = ans;
        }
    }
    printf("%d %d\n", fuck, minans);
    return 0;
}

T2.给定长度为n<=1e5的字符串s和一个数k<=1e5,要求求出一个最大的x,使得字符串中出现x个相同的不重叠的子串,每个子串内只含有一种字符

枚举每段连续区间,开桶记录个数 注意循环完毕字符最后还要算下答案(代码里直接多循环了一次处理)

#include <cstdio>
using namespace std;

int bucket[30];
char s[200010];
int n, k;

int main()
{
    scanf("%d%d", &n, &k);
    scanf("%s", s);
    char lastch = 0;
    int len = 0;
    for (int i = 0; i == 0 || s[i - 1] != 0; i++)
    {
        if (s[i] == lastch)
            len++;
        else
        {
            if (lastch != 0)
            bucket[lastch - 96] += len / k;
            len = 1;
            lastch = s[i];
        }
    }
    int ans = 0;
    for (int i = 1; i <= 26; i++)
        if (ans < bucket[i])
            ans = bucket[i];
    printf("%d\n", ans);
    return 0;
}

T3.求长度为n,数组里每个元素在[l,r]之间并且元素和%3=0的数组数量%1e9+7 n<=2e5,l<=r<=1e9

处理出[l,r]内%3=0,1,2的数字数量,然后无脑dp

#include <cstdio>
#include <iostream>
using namespace std;

int n, l, r, p = 1000000007;
int cnt[3];
long long f[200010][3];

int main()
{
    scanf("%d%d%d", &n, &l, &r), l--;
    cnt[0] = (r + 3) / 3 - (l + 3) / 3;
    cnt[1] = (r + 2) / 3 - (l + 2) / 3;
    cnt[2] = (r + 1) / 3 - (l + 1) / 3;
    f[0][0] = 1;
    for (int i = 1; i <= n; i++)
    {
        for (int now = 0; now <= 2; now++)
        {
            for (int last = 0; last <= 2; last++)
            {
                int dis = (now - last) % 3;
                if (dis < 0) dis += 3;
                f[i][now] += f[i - 1][last] * cnt[dis] % p;
                f[i][now] %= p;
            }
        }
    }
    cout << f[n][0] << endl;
    return 0;
}

T4.N*M(N<=1000,M<=1000)的棋盘上Van一个游戏,一共p<=9个人,每个人有个速度si,每次每个人将他棋盘上所有棋子能走<=si步的所有位置都放上棋子,要求不能经过墙和别的玩家的棋子

bfs套bfs?第一层bfs维护每个玩家的等待扩展的棋子队列,第二层bfs在每个玩家扩展棋子时候维护,对第二层bfs扩展到距离为si的位置加入第一层bfs的队列即可(因为只有他们才能继续扩展)

代码略长

#include <bits/stdc++.h>
using namespace std;

int n, m, p, s[15];
int mp[1010][1010];
int dis[1010][1010];

int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

int main()
{
    scanf("%d%d%d", &n, &m, &p);
    for (int i = 1; i <= p; i++) scanf("%d", &s[i]);
    for (int i = 1; i <= n; i++)
        mp[i][0] = mp[i][m + 1] = -1;
    for (int j = 1; j <= m; j++)
        mp[0][j] = mp[n + 1][j] = -1;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            char s;
            scanf(" %c", &s);
            if (s == '#')
                mp[i][j] = -1;
            if (s >= '1' && s <= '9')
                mp[i][j] = s - 48;
        }
    }
    queue<int> qx, qy;
    for (int pl = 1; pl <= p; pl++)
    {
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                if (mp[i][j] == pl)
                {
                    qx.push(i), qy.push(j);
                }
            }
        }
    }
    while (!qx.empty())
    {
        int x = qx.front(), y = qy.front();
        qx.pop(), qy.pop();
        int col = mp[x][y];
        queue<int> qx1, qy1;
        dis[x][y] = 0;
        qx1.push(x), qy1.push(y);
        while (!qx1.empty())
        {
            int x1 = qx1.front(), y1 = qy1.front();
            qx1.pop(), qy1.pop();
            for (int d = 0; d < 4; d++)
            {
                int nx = x1 + dx[d], ny = y1 + dy[d];
                if (mp[nx][ny] != 0) continue;
                mp[nx][ny] = col;
                dis[nx][ny] = dis[x1][y1] + 1;
                if (dis[nx][ny] == s[col])
                {
                    qx.push(nx), qy.push(ny);
                }
                else
                    qx1.push(nx), qy1.push(ny);
            }
        }
    }
    int ans[13];
    memset(ans, 0, sizeof(ans));
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if (mp[i][j] > 0)
                ans[mp[i][j]]++;
    for (int i = 1; i <= p; i++)
        printf("%d ", ans[i]);
    return 0;
}

做题时候变量名写错了WA了一发。。。

T5.你有一个长度为n(n<=1e5)的事件序列,包含两种事件:1.你可以修改你的社交账户名(修改为你的m个朋友之一 m<=40)2.朋友s访问你的社交账户。如果一个朋友是高兴的,当且仅当每次他访问你时候你的社交用户名都是他的名字。最大化高兴的朋友的数量

将两个事件1之间的所有事件2定义为一个区间,那么这个区间内所有朋友是互斥的,即这些朋友最多有一个是高兴的,那么我们可以构建无向图,每个区间内所有朋友两两连边,最后求最大独立集,可以转化为最大团(其实他俩差不多)

由于m<=40,所以不能枚举集合,官方题解给出了meet in the middle做法,不过这种最大团都可以被随机序列怒艹。。。

#include <bits/stdc++.h>
using namespace std;

bool vis[50][50];
int n, tot;
map<string, int> id;

bool in[50];
int seq[50];

bool valid(int x)
{
    for (int j = 1; j <= n; j++)
        if (in[j] == true && vis[j][x] == true) return false;
    return true;
}

int work()
{
    int cnt = 0;
    memset(in, 0, sizeof(in));
    for (int i = 1; i <= n; i++)
        if (valid(seq[i])) in[seq[i]] = true, cnt++;
    return cnt;
}

void clean()
{
    for (int i = 1; i <= n; i++)
        if (in[i] == true)
            for (int j = 1; j <= n; j++)
                if (in[j] == true)
                    vis[i][j] = true;
    memset(in, 0, sizeof(in));
}

int main()
{
    int cnt;
    srand(time(0));
    scanf("%d%d", &cnt, &n);
    for (int i = 1; i <= cnt; i++)
    {
        int opd;
        scanf("%d", &opd);
        if (opd == 1) clean();
        else
        {
            string name;
            cin >> name;
            if (id.count(name) == false) id[name] = ++tot;
            in[id[name]] = true;
        }
    }
    clean();
    for (int i = 1; i <= n; i++) seq[i] = i;
    int ans = 0, t = 100000;
    while (clock() / (double)CLOCKS_PER_SEC <= 1.95)
    {
        random_shuffle(seq + 1, seq + 1 + n);
        ans = max(ans, work());
    }
    printf("%d\n", ans);
    return 0;
}

学校太操蛋,9点55放学,10点10分睡觉,没时间打CF

这次终于有个时间合适的比赛了

第一次打CF,网太卡没hack,最后rating就涨了201,感觉一般般,还是因为自己太菜

前40分钟之内切掉了前4题,然后第五题题意没读懂,问出题人,出题人给回了句原题中的话,给一个词加了粗,然后懂了,懂了后转化出最大团的模型,就是不会做

忘了有个东西叫meet in the middle,也忘了有个东西叫随机化,这就非常cd了

由于学校太操蛋,以后在线打CF的机会肯定不多

打CF主要是练手速和一遍A,毕竟一遍不A扣50分也很恶心的。。。比如说这次B和D都没一遍A

转载于:https://www.cnblogs.com/oier/p/10300593.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值