Codeforces-Round-799-Div-4

文章介绍了CodeforcesRound#799(Div.4)中的两道算法题,第一题G.2^Sort要求找到满足特定条件的连续子数组数量,可以通过动态规划求解;第二题H.Gambling涉及到寻找具有最大差值的连续子序列,可以使用最大连续子序列和的思想解决。文章提供了详细的思路和部分代码实现。
摘要由CSDN通过智能技术生成

title: Codeforces Round 799 (Div. 4)
date: 2023-04-07 19:43:45
categories:

  • Algorithm
  • Codeforces
    tags:
  • codeforces
  • 1700
  • 动态规划
  • 贪心
  • 排序

G. 2^Sort

Given an array 𝑎 of length 𝑛 and an integer 𝑘 , find the number of indices 1≤𝑖≤𝑛−𝑘 such that the subarray [𝑎𝑖,…,𝑎𝑖+𝑘] with length 𝑘+1 (not with length 𝑘 ) has the following property:

If you multiply the first element by 20 , the second element by 21 , …, and the (𝑘+1 )-st element by 2𝑘 , then this subarray is sorted in strictly increasing order. More formally, count the number of indices 1≤𝑖≤𝑛−𝑘 such that 20⋅𝑎𝑖<21⋅𝑎𝑖+1<22⋅𝑎𝑖+2<⋯<2𝑘⋅𝑎𝑖+𝑘.

Input

The first line contains an integer 𝑡 (1≤𝑡≤1000 ) — the number of test cases.

The first line of each test case contains two integers 𝑛 , 𝑘 (3≤𝑛≤2⋅105 , 1≤𝑘<𝑛 ) — the length of the array and the number of inequalities.

The second line of each test case contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤109 ) — the elements of the array.

The sum of 𝑛 across all test cases does not exceed 2⋅105 .

Output

For each test case, output a single integer — the number of indices satisfying the condition in the statement.

题目大意

给你一个长度为n的数组和一个整数k,如果满足连续的k个数: 2 0 ∗ a i < 2 1 ∗ a i + 1 . . . < a k ∗ a i + k 2^0*a_i<2^1*a_{i+1}...<a^k*a_{i+k} 20ai<21ai+1...<akai+k,问这些的连续序列有多少个。

思路

动态规划:

  • 状态表示:f[i]表示以a[i]结尾的连续两倍上升序列的最大长度。
  • 状态计算:
    • 初始f[i]=1
    • 如果f[i]*2>f[i-1]f[i]=f[i-1]+1

代码

#include <bits/stdc++.h>

#define int long long
using namespace std;

void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    vector<int> f(n + 1, 1);
    for (int i = 2; i <= n; i++) {
        if (2 * a[i] > a[i - 1]) {
            f[i] = f[i - 1] + 1;
        }
    }

    int res = 0;
    for (int i = k + 1; i <= n; i++) {
        if (f[i] >= (k + 1)) {
            res++;
        }
    }
    cout << res << endl;
}

signed main() {
#ifndef ONLINE_JUDGE
    freopen("../test.in", "r", stdin);
    freopen("../test.out", "w", stdout);
#endif
    int _;
    cin >> _;
    while (_--) solve();

    return 0;
}

H. Gambling

image-20230410192059433

题目大意

给你一个长度为n的数组,需要你找一个连续的子序列,这个子序列中比如4 3 4,要求这段里面出现次数最多的数字减去其他数字的个数,这个值最大是多少,区间的左右断点是多少。

思路

把当前数字对答案的权重看成1,其他数字看成0,就是求子序列和的最大值。

可以参考最大连续子序列

代码

#include <bits/stdc++.h>

#define int long long
using namespace std;

void solve() {
    int n;
    cin >> n;
    map<int, vector<int>> m;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        m[a[i]].push_back(i);
    }
    int val = a[1], L = 1, R = 1;
    int res = 1;
    for (const auto &[value, v]: m) {
        int cnt = 1;//当前这个数字value有多少个
        int l = v[0];
        for (int i = 1; i < v.size(); i++) {
            int num = v[i] - v[i - 1] - 1;//除了当前数其他的数有多少个
            cnt = cnt - num + 1; //+1是加的当前第i个
            if (cnt < 1) {
                cnt = 1;
                l = v[i];
            }
            if (cnt > res) {
                res = cnt;
                L = l, R = v[i];
                val = value;
            }
        }
    }
    cout << val << " " << L  << " " << R  << endl;
}

signed main() {
#ifndef ONLINE_JUDGE
    freopen("../test.in", "r", stdin);
    freopen("../test.out", "w", stdout);
#endif
    int _;
    cin >> _;
    while (_--) solve();

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

重生之我是cxk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值