CF #297 div2

A:

题意:

找钥匙匹配,遍历一下字符串就好了。

注意数组要开2倍速。,。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1

using namespace std;
const int maxn = 100000 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);

char str[2 * maxn];
int key[30];

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif // LOCAL
    int n;
    scanf("%d", &n);
    scanf("%s", str);
    int len = strlen(str);
    memset(key, 0, sizeof(key));
    int ans = 0;
    for (int i = 0; i < len; i++)
    {
        if (i % 2 == 0)
        {
            key[str[i] - 'a']++;
        }
        else
        {
            if (key[str[i] - 'A'] == 0)
            {
                ans++;
            }
            else
            {
                key[str[i] - 'A']--;
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}

B:

题意:

给一个字符串,然后给m个数。

这个数代表着将m到其在字符串中的对称点t翻转。

求翻转完了以后的字符串。

解析:

暴力肯定要超时的。

我把所有的翻转的位置的次数用pos数组保存下来,然后遍历一遍用前缀和,翻转次数为奇数次的就不改变,为偶数次的将其与对称点交换。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1

using namespace std;
const int maxn = 200000 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);

char str[maxn];
int pos[maxn];

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif // LOCAL
    scanf("%s", str);
    int len = strlen(str);
    int n;
    scanf("%d", &n);
    memset(pos, 0, sizeof(pos));
    for (int i = 0; i < n; i++)
    {
        int x;
        scanf("%d", &x);
        x--;
        x = min(x, len - x - 1);
        pos[x]++;
    }
    //cout << pos[0] << endl;
    for (int i = 1; i < len / 2; i++)
    {
        pos[i] = pos[i] + pos[i - 1];
        //cout << pos[i] << endl;
    }
    for (int i = 0; i < len / 2; i++)
    {
        if (pos[i] % 2)
        {
            char t = str[i];
            str[i] = str[len - i - 1];
            str[len - i - 1] = t;
        }
    }
    printf("%s", str);
    return 0;
}

C:

题意:

给n只木棍,木棍的长度为li,若四根木棍a1, a2, a3, a4 满足以下条件:

  • a1 ≤ a2 ≤ a3 ≤ a4
  • a1 = a2
  • a3 = a4
则可以拼成一个矩形。

每只木根都可以选择减小1个长度或者不减小。

问拼成的矩形,最大的面积。

解析:

排个序,然后从大往小了取。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1

using namespace std;
const int maxn = 100000 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);

LL sticks[maxn];
LL ans[maxn];

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif // LOCAL
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        scanf("%I64d", &sticks[i]);
    }
    sort(sticks, sticks + n);
    int cnt = 0;
    int pos = n - 1;
    while (pos >= 1)
    {
        if (sticks[pos] == sticks[pos - 1])
        {
            ans[cnt++] = sticks[pos];
            pos -= 2;
        }
        else if (sticks[pos] == sticks[pos - 1] + 1)
        {
            ans[cnt++] = sticks[pos - 1];
            pos -= 2;
        }
        else
        {
            pos--;
        }
    }
    if (cnt == 1)
        printf("0\n");
    else
    {
        LL area = 0;
        for (int i = 0; i < cnt - 1; i += 2)
        {
            area += ans[i] * ans[i + 1];
        }
        printf("%I64d\n", area);
    }
    return 0;
}

D:

题意:

给一个n * m 的只含有" . " " * "矩阵,现在可以把矩阵中的” * " 替换成" . "。

要求替换最少的星,使得点的连通块是一个矩形。

解析:

贪心,若一个2 * 2的矩形块里面出现了一个星,则把这个星变成点,然后向四周扩展。

注意的是。。八个方向扩展。。。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1

using namespace std;
const int maxn = 2000 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);

int dir[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1}};
char g[maxn][maxn];
int n, m;

void dfs(int x, int y)
{
    if (x == -1 || n - 1 == x || y == -1 || m - 1 == y)
        return;
    int cnt = 0;
    for (int i = 0; i <= 1; i++)
    {
        for (int j = 0; j <= 1; j++)
        {
            if (g[x + i][y + j] == '*')
                cnt++;
        }
    }
    if (cnt == 1)
    {
        for (int i = 0; i <= 1; i++)
        {
            for (int j = 0; j <= 1; j++)
            {
                g[x + i][y + j] = '.';
            }
        }
        for (int i = 0; i < 8; i++)
        {
            dfs(x + dir[i][0], y + dir[i][1]);
        }
    }
}

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif // LOCAL
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++)
        scanf("%s", g[i]);
    for (int i = 0; i < n - 1; i++)
    {
        for (int j = 0; j < m - 1; j++)
        {
            dfs(i, j);
        }
    }
    for (int i = 0; i < n; i++)
    {
        printf("%s\n", g[i]);
    }
    printf("\n");
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值