Codeforces Round #627 (Div. 3)

比赛链接

A题 解题思路:

A题还是很水的,只要看他们的奇偶性是否相同即可。

因为如果一个奇数一个偶数,无论如何他们都不会消除的。


代码:
#include <iostream>
#include <cstdio>
 
using namespace std;
 
const int N = 110;
 
int a[N];
 
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n;
        scanf("%d",&n);
//        int res = -1;
        int k = 0;
        scanf("%d",&a[0]);
        int res = a[0] % 2;
        for (int i = 1; i < n; i++){
            scanf("%d",&a[i]);
            int result = a[i] % 2;
            if (result != res) k = 1;
        }
        if (n == 1) {
            puts("YES");
            continue;
        }
        if (!k) puts("YES");
        else puts("NO");
    }
    return 0;
}
B题 解题思路:


从头到尾遍历一遍即可,记录下当前点第一次出现的位置坐标,当下次再出现时,判断下他们的距离是否大于 1 即可。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 5010;

int a[N],b[N];

int main(){
    int t;
    cin>>t;
    while(t--){
        memset(b,0,sizeof b);
        int n;
        int k = 1;
        scanf("%d",&n);
        for (int i = 1; i <= n; i++){
            scanf("%d",&a[i]);
        }
        for (int i = 1; i <= n; i++){
            if(b[a[i]] !=0 ){
                if (i - b[a[i]] > 1){
                    puts("Yes");
                    k = 0;
                    break;
                }
            }
            else{
                b[a[i]] = i;
            }
        }
        if (k){
            puts("NO");
        }
    }
    return 0;
}

C题 解题思路:


也挺水,贪心问题。

只需求从 0 位置开始,与 k 的最大间隔,最后还要判断第 n (字符串从0开始的)位置与前面符合条件位置的距离,取最大值。



代码:
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 5010;

int a[N],b[N];

int main(){
    int n;
    scanf("%d",&n);
    for (int i = 0; i < n; i++){
        string st;
        cin>>st;
        int res = -1;
        int x = -1; // 为初始点,原本应该为0,因为我们字符串是从0开始,所以设置成-1 
        int y = st.length(); // 注意长度不是 n
        for (int i = 0; i < y; i++){
            if (st[i] == 'R'){ // 如果是R,那我们判断他与前一个点的距离,前一个点可能是R也可以是初始值-1
                res = max(res,i - x); 
                x = i;
            }
        }
        res = max(res,y - x); //最后要判断终点
        printf("%d\n",res);
    }
    return 0;
}

D题 解题思路:


这题自己的思路不太好,查了网上代码,很思维(自己脑子不转)

就是将a[i] - b[i] , 然后进行排序,对于 n 个点,我们从前到最后,首先选最右边的点,a[r],l 从最左边开始,如果 a[r] + a[l] <= 0 那么我们将 l 向右移动,因此符合的为 r - l个,然后我们将 r 向左移,循环操作即可。


代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 200010;

int a[N],b[N];

int main(){
    int n, k;
    scanf("%d",&n);
    for (int i = 0; i < n; i++){
        scanf("%d",&a[i]);
    }
    for (int i = 0; i < n; i++){
        scanf("%d",&k);
        a[i] -= k;
    }
    sort(a,a + n);
    int l = 0, r = n - 1;
    long long res = 0; // 注意开ll
    while(l < r && a[r] > 0){
        while((a[l] + a[r]) <= 0){ // 这里的while需要注意,很灵魂
            l ++;
        }
        if (l >= r) break;
        res = res + r - l;
        r--;
    }
    printf("%lld\n",res);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值