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;
}

發佈了84 篇原創文章 · 獲贊 11 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章