Codeforces Round #799(Div.4)

A.Marathon (800)

题 意 : 题意:

比Timur跑得远的有多少人

思 路 : 思路: :

枚举

C o d e Code Code

#include <bits/stdc++.h>
 
using namespace std;
 
const int N = 2e5 + 10, M = N << 1, INF = 0x3f3f3f3f;
 
typedef long long LL;
 
 
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
 
    int _;
    
    for (cin >> _; _ ; _ -- ) {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        
        int num = 0;
        if (b > a)
            num ++ ;
        if (c > a)
            num ++ ;
        if (d > a)
            num ++ ;
        cout << num << "\n";
    }
    
    return 0;
}

B.All Distinct (800)

题 意 : 题意:

每次移除两个元素,最终所含不重复元素的数组最长值

思 路 : 思路:

将出现的元素计数,根据元素的数量分为奇偶,若出现次数偶数的元素有偶数个,那么答案为奇数数量 + 偶数数量,否则为奇数数量 + 偶数数量 - 1

C o d e Code Code

#include <bits/stdc++.h>

using namespace std;

const int N = 1e4 + 10, M = N << 1, INF = 0x3f3f3f3f;

typedef long long LL;


int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int _;
    
    for (cin >> _; _ ; _ -- ) {
        int n;
        cin >> n;
        
        vector<int> a(N, 0);
        
        for (int i = 0; i < n; i ++ ) {
            int x;
            cin >> x;
            a[x] ++ ;
        }
        
        int odd = 0, even = 0;
        for (int i = 1; i < N; i ++ )
            if (a[i] && a[i] & 1)
                odd ++ ;
            else if (a[i])
                even ++ ;
        
        cout << odd + (even & 1 ? even - 1 : even) << "\n";
    }
    
    return 0;
}

C.Where’s the Bishop? (800)

题 意 : 题意:

找到对角线的中心点位置

思 路 : 思路:

暴力枚举,找到左上、右上、左下、右下都有#的元素位置

C o d e Code Code

#include <bits/stdc++.h>

using namespace std;

const int N = 1e4 + 10, M = N << 1, INF = 0x3f3f3f3f;

typedef long long LL;

char g[10][10];
int dx[4] = {-1, -1, 1, 1}, dy[4] = {-1, 1, 1, -1};

bool check(int x, int y) {
    for (int i = 0; i < 4; i ++ ) {
        if (g[x + dx[i]][y + dy[i]] != '#')
            return false;
    }
    return true;
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int _;
    
    for (cin >> _; _ ; _ -- ) {
        for (int i = 0; i < 8; i ++ )
            cin >> g[i];
        
        for (int i = 1; i <= 6; i ++ )
            for (int j = 1; j <= 6; j ++ )
                if (check(i, j)) {
                    cout << i + 1 << " " << j + 1 << "\n";
                    break;
                }
    }
    
    return 0;
}

D.The Clock (1100)

题 意 : 题意:

在一个循环内,是回文子串形式的时间点出现次数

思 路 : 思路:

先找到x与1440的最大公因数t,然后将初始时间%t,在1440内每次时间+t,并计数

C o d e Code Code

#include <bits/stdc++.h>

using namespace std;

const int N = 1e4 + 10, M = N << 1, INF = 0x3f3f3f3f;

typedef long long LL;


int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int _;

    for (cin >> _; _ ; _ -- ) {
        string s;
        cin >> s;

        int x;
        cin >> x;

        x = __gcd(x, 1440);

        int t = stoi(s.substr(0, 2)) * 60 + stoi(s.substr(3));
        t %= x;

        int ans = 0;
        for (int i = t; i < 1440; i += x ) {
            int a = i / 600;
            int b = i / 60 % 10;
            int c = i / 10 % 6;
            int d = i % 10;
            if (a == d && b == c)
                ans ++ ;
        }

        cout << ans << "\n";
    }
    
    return 0;
}

E.Binary Deque (1200)

题 意 : 题意:

每次可以移除左端点或右端点,花费1,求使得数组内元素的和为s的最小花费

思 路 : 思路:

从头遍历数组,直到累积和为s,然后更新花费,并更新满足累计和为s的左右端点,更新花费

C o d e Code Code

#include <bits/stdc++.h>

using namespace std;

const int N = 1e4 + 10, M = N << 1, INF = 0x3f3f3f3f;

typedef long long LL;



int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int _;

    for (cin >> _; _ ; _ -- ) {
        int n, s, sum = 0;
        cin >> n >> s;
        
        vector<int> a(n);
        for (int &ai : a)
            cin >> ai;

        int ans = n + 1, cur = 0;

        for (int i = 0, j = 0; i < n; i ++ ) {
            while (j < n && cur + a[j] <= s)
                cur += a[j], j ++ ;
            if (cur == s)
                ans = min(ans, n - (j - i));
            cur -= a[i];
        }

        if (ans > n)
            ans = -1;

        cout << ans << "\n";
    }
    
    return 0;
}

F.3SUM (1300)

题 意 : 题意:

从数组选三个数,累计和最后一位为3

思 路 : 思路:

计算0~9每个数出现的次数,暴力枚举累计和出现的可能

C o d e Code Code

#include <bits/stdc++.h>

using namespace std;

const int N = 1e4 + 10, M = N << 1, INF = 0x3f3f3f3f;

typedef long long LL;

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int _;

    for (cin >> _; _ ; _ -- ) {
        int n;
        cin >> n;
        
        vector<int> a(10, 0);
        for (int i = 0; i < n; i ++ ) {
            int x;
            cin >> x;
            a[x % 10] ++ ;
        }

        bool f = false;
        for (int i = 0; i < 10; i ++ )
            if (a[i])
                for (int j = 0; j < 10; j ++ )
                    if (a[j])
                        for (int k = 0; k < 10; k ++ )
                            if (a[k])
                                if ((i + j + k) % 10 == 3) {
                                    a[i] -- , a[j] -- , a[k] -- ;
                                    if (a[i] >= 0 && a[j] >= 0 && a[k] >= 0)
                                        f = true;
                                    a[i] ++ , a[j] ++, a[k] ++ ;
                                }

        cout << (f ? "YES" : "NO") << "\n";
    }
    
    return 0;
}

G.2^Sort (1400)

题 意 : 题意:

从下标为 i i i开始,满足 2 0 2^0 20 a i a_{i} ai < 2 1 2^1 21 a i + 1 a_{i+1} ai+1<…< 2 k 2^k 2k* a i + k a_{i+k} ai+k (i = 1, 2, …)格式的数量

思 路 : 思路:

经过观察,每次只需要维护相邻两个元素的大小关系,双指针,当长度满足的情况下就一直计数,否则从头开始计算长度

C o d e Code Code

#include <bits/stdc++.h>

using namespace std;

const int N = 1e4 + 10, M = N << 1, INF = 0x3f3f3f3f;

typedef long long LL;

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int _;

    for (cin >> _; _ ; _ -- ) {
        int n, k;
        cin >> n >> k;
        vector<int> a(n);

        for (int &ai : a)
            cin >> ai;

        int ans = 0;

        for (int i = 0; i < n - k; i ++) {
            int t = a[i], tem = k, j = i + 1;
            while (t < 2 * a[j] && j < n) {
                t = a[j], j ++ ;
                tem -- ;
                ans += (tem <= 0);
            }
            i = j - 1;
        }

        cout << ans << "\n";
    }
    
    return 0;
}

H.Gambling (1700)

题 意 : 题意:

找出一个区间内的众数

思 路 : 思路:

使用map记录每个出现元素的次数和位置,然后假设区间左端点下标为i,右端点下标为j,那么 n u m = j − i + 1 + ( a j − a i + 1 − ( j − i − 1 ) ) num = j - i + 1 + (a_{j} - a_{i} + 1 - (j - i - 1)) num=ji+1+(ajai+1(ji1)),归纳后为 n u m = 2 ∗ j + a j − 2 ∗ i − a i + 1 num = 2 * j + a_{j} - 2 * i - a_{i} + 1 num=2j+aj2iai+1,由于左端点固定,所以只考虑j,即只考虑 2 ∗ j + a j 2*j+a_{j} 2j+aj,使它变大

C o d e Code Code

#include <bits/stdc++.h>

using namespace std;

const int N = 1e4 + 10, M = N << 1, INF = 0x3f3f3f3f;

typedef long long LL;


int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int _;

    for (cin >> _; _ ; _ -- ) {
        int n;
        cin >> n;

        map<int, vector<int>> p;
        
        vector<int> x(n);

        for (int i = 0; i < n; i ++ ) {
            cin >> x[i];
            p[x[i]].push_back(i);
        }

        int a = x[0], l = 0, r = 1, ans = 1;

        for (auto pi : p) {
            int b = pi.first;
            vector<int> q = pi.second;
            
            int min = 0, k = q[0];
            for (int j = 0; j < int(q.size()); j ++ ) {
                int i = q[j];
                int cur = j - (i - j);
                if (cur < min) { // num变负数,不符最优,更换左端点
                    min = cur;
                    k = i;
                }
                int res = cur + 1 - min;
                if (res > ans) {
                    ans = res;
                    a = b;
                    l = k;
                    r = i + 1;
                }
            }
        }

        cout << a << " " << l + 1 << " " << r << "\n";
    }
    
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值