Codeforces 987C 题解

题意:

给定两个长度为 n 的序列,要求在第一个序列 a 里,找到一个严格递增的子序列,要求 i < j < k 且 a[i] < a[j] < a[k],问你找到的这三个位置i , j , k,在第二个序列 b 里所对应的 b[i] + b[j] + b[k],的最小值是多少。

思路:

先n^2处理出来每个元素 a[i] 后面比 a[i] 大的 a[j] 所对应的 b[j] 的最小值,记为mint[i]。这样做的目的是降维,显然n^3枚举是不可能的了,只要先按上述预处理出来,就可以进行如下操作:

for(int i = 1; i <= n; i++) {
      for(int j = i + 1; j <= n; j++) {
          if(a[j] > a[i]) minT = min(minT, b[j] + b[i] + mint[j]);
      }
 }

即直接n^2暴力维护前两个值就可以了。

本人AC代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <string>
#include <ctime>
#include <cctype>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 3050;
const int Inf = 3e8 + 7;
int n;
int a[maxn], b[maxn];
int mint[maxn];

int main() {
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= n; i++) cin >> b[i];
    int minT = Inf;
    for(int i = 1; i <= n; i++) {
        int min0 = Inf;
        for(int j = i + 1; j <= n; j++) {
            if(a[j] > a[i]) min0 = min(min0, b[j]);
        }
        mint[i] = min0;
    }
    for(int i = 1; i <= n; i++) {
        for(int j = i + 1; j <= n; j++) {
            if(a[j] > a[i]) minT = min(minT, b[j] + b[i] + mint[j]);
        }
    }
    if(minT == Inf) puts("-1");
    else cout << minT << endl;
}

猜你喜欢

转载自blog.csdn.net/ericgipsy/article/details/80621495
今日推荐