当前位置:网站首页>2021 CCPC Harbin B. magical subsequence (thinking question)

2021 CCPC Harbin B. magical subsequence (thinking question)

2022-07-04 21:21:00 GHOSTANDBREAD

Problem - B - Codeforces

Ideas :

From front to back, find the maximum length of the sum of two . It can be discontinuous , But it must be in the order from front to back . It can be found that the range of each number is [1,100], Not much , Is a breakthrough , The sum of two numbers sum The range is [2,200], The traverse sum In every case . When the number being traversed and the number appearing before are added together, it is equal to sum when ,res+=2, Then set the previous number to be unusable . If the sum of the numbers being traversed sum The difference has not appeared before , Then set the number to be usable , Continue traversing backwards .

Code :

#include<iostream>
#include<cstring>
#include<vector>
#include<string>
#include<algorithm>

using namespace std;

int vis[205];
int n, maxn;

int main() {
    
    scanf("%d", &n);
    vector<int> a(n);
    

    for(int i = 0; i < n; i ++)   scanf("%d", &a[i]);

    int maxn = -1;
    for(int sum = 2; sum <= 200; sum ++) {
         int res = 0;
         memset(vis, 0, sizeof vis);
        for(int i = 0; i < n; i ++) {
            if(a[i] < sum) {
                if(vis[sum - a[i]]) {
                    res += 2;
                    memset(vis, 0, sizeof vis);
                } else {
                    vis[a[i]] = 1;
                }
            }
        }
        maxn = max(maxn, res);
    }
    
    printf("%d", maxn);

    return 0;
}

原网站

版权声明
本文为[GHOSTANDBREAD]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207042013095043.html