Codeforces Round #719 (Div. 3) ABCD

https://codeforces.com/contest/1520

A. Do Not Be Distracted!

题意:给出一个长度为n的字符串,问相同字母之间是否存在其他字母,有的话输出NO,没有输出YES。
思路:无脑暴力模拟啦。

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

char a[66];

int main()
{
    int t, n;
    cin >> t;
    while(t--)
    {
        cin >> n >> a + 1;
        int flag = 0;
        for(int i = 1; i <= n;i++)
        {
            flag = 0;
            for(int j = n;j > i; j--)
            {
                if(a[j] == a[i])
                {
                    for(int k = i;k < j; k++)
                    {
                        if(a[k] != a[i])
                        {
                            flag = 1;
                            break;
                        }
                    }
                    if(flag) break;
                }
            }
            if(flag) break;
        }
        if(flag) puts("NO");
        else puts("YES");
    }
    return 0;
}

B. Ordinary Numbers

题意:给出n,问[1, n]中每一位都相同的数字有多少个。

思路:很容易发现,假设n有k位,那么ans = (k - 1) * 9 + n / k个1。

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

int a[13] = {0, 1, 11, 111, 1111, 11111, 111111, 1111111,
			 11111111,111111111,1111111111};

int main()
{
    int t, n;
    cin >> t;
    while(t--)
    {
        cin >> n;
        int ans = 0;
        int cnt = 0;
        int m = n;
        while(m)
        {
            cnt++;
            m /= 10;
        }
        ans = (cnt - 1) * 9 + n / a[cnt];
        cout << ans << endl;
    }
    return 0;
}

C. Not Adjacent Matrix

题意:给出一个n,构造一个n * n的矩阵,使得相邻的两个数相差不为1,且[1, n * n]只在矩阵中出现一次,不存在这样的矩阵输出-1.

思路:将矩阵压成一维,跳着构造就行,只有2 * 2无解。

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

const int maxn = 111;

int a[maxn * maxn];

int main()
{
    int t, n;
    cin >> t;
    while(t--)
    {
        cin >> n;
        if(n == 2)
        {
            cout << -1 << endl;
            continue;
        }
        int cnt = 1;
        for(int i = 1;i <= n * n; i += 2) a[i] = cnt++;
        for(int i = 2;i <= n * n; i += 2) a[i] = cnt++;
        for(int i = 1;i <= n * n ;i++)
        {
            cout << a[i] << ' ';
            if(i % n == 0) cout << endl;
        }
    }
    return 0;
}

D. Same Differences

题意:给出一个长度为n的序列a,问有多少对a[j] - a[i] = j - i。

思路:移项得 a[j] - j = a[i] - i, 统计一下再计数就行,用map会很方便,不用处理差为负数的情况。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>

using namespace std;

typedef long long ll;

int main()
{
    int t, n;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        map<int, int> m;
        int a;
        for(int i = 1;i <= n; i++)
        {
            scanf("%d", &a);
            m[a - i]++;
        }
        ll ans = 0;
        for(auto i:m) ans += (ll)i.second * (i.second - 1) / 2;
        printf("%lld\n", ans);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值