codeforces 997F Consecutive Subsequence (dp)

本文介绍了一种使用哈希映射寻找数组中最长公差为1的等差数列的方法,并通过递推公式更新数列长度,最终输出等差数列的元素下标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述

题意就是让你找一个最长的公差为1的等差数列,然后输出他们的下标。

思路:用一个map表示到当前这个数的最大长度为多少,则有如下递推公式

mp[a[i]]=mp[a[i]1]+1mp[a[i]]=mp[a[i]−1]+1

然后用一个pre记录一下前序就行了。
代码如下:

#include<iostream>
#include<cstring>
#include<cmath>
#include<string>
#include<set>
#include<map>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<vector>
using namespace std;
const int MAX = 200010;
int a[MAX],N,pre[MAX];
map<int, int> mp;
map<int, int> pos;//记录每个数在哪个位置
int main(void) {
    memset(pre, -1, sizeof(pre));
    scanf("%d", &N);
    for (int i = 1; i <= N; ++i) {
        scanf("%d", &a[i]);
        pos[a[i]] = i;
        if (mp.count(a[i] - 1)) {//如果a[i]-1存在
            mp[a[i]] = mp[a[i] - 1] + 1;
            pre[i] = pos[a[i] - 1];
        }
        else {//不存在相当于0
            mp[a[i]] = 1;
        }
    }
    int res = 0;
    int p = 0;
    for (map<int, int>::iterator it = mp.begin(); it != mp.end(); ++it) {
        if (it->second > res) {//寻找最大长度
            res = it->second;
            p = pos[it->first];
        }
    }
    vector<int> vec;
    while (p != -1) {
        vec.push_back(p);
        p = pre[p];
    }
    reverse(vec.begin(), vec.end());
    printf("%d\n", (int)vec.size());
    for (int i = 0; i < vec.size(); ++i) {
        printf("%d", vec[i]);
        if (i != vec.size() - 1)
            printf(" ");
        else
            printf("\n");
    }
    return 0;
}
### Codeforces 平台上的编号为 997 的题目或比赛信息 对于编号为 997 的题目或比赛,在Codeforces平台上有特定的比赛记录和问题描述。然而,提供的参考资料并未直接提及编号为 997 的具体详情。 通常情况下,Codeforces 上的每场比赛或问题都有详细的页面介绍其规则、输入输出格式以及样例测试数据等内容。为了获取关于编号为 997 的确切资料,建议访问Codeforces官方网站并搜索对应编号的比赛或问题[^1]。 如果目标是了解某场具体的竞赛或者解决某个特定的问题,则可以按照如下方式查询: - 访问 [Codeforces](https://codeforces.com/) 官方网站。 - 使用站内搜索功能键入 "contest 997" 或者 "problem 997" 来定位相关内容。 - 查看官方给出的具体说明文档来获得最权威的信息源。 此外,也可以通过API接口调用来批量检索比赛列表或是单个比赛/问题的数据。下面是一个简单的Python脚本例子用于展示如何利用Codeforces API获取指定ID的比赛信息: ```python import requests def get_contest_info(contest_id): url = f"https://codeforces.com/api/contest.standings?contestId={contest_id}&from=1&count=1" response = requests.get(url).json() if 'result' in response: contest = response['result']['contests'][0] print(f"Name: {contest['name']}") print(f"Start Time: {contest['startTimeSeconds']}") print(f"Duration: {contest['durationSeconds']} seconds") else: print("Contest not found.") get_contest_info(997) ``` 此代码片段展示了如何向Codeforces API发送请求以获取有关给定ID的比赛基本信息。请注意实际应用时可能需要处理更多异常情况,并遵循API使用指南中的速率限制和其他规定[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值