题目描述
n个显示器排成一排,第i个显示器能显示(s[i])个字,价格为(c[i]),选出三个编号为(i<j<k)的显示器,并且满足(s[i]<s[j]<s[k]),求满足条件的最小花费
思路
枚举中间显示器的编号(j),向左找满足(s[i]<s[j])的花费最小的,向右找满足(s[j]<s[k])的花费最小的
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3005;
const long long inf = 0x3f3f3f3f3f3f3f3f;
typedef long long ll;
ll s[maxn];
ll c[maxn];
ll n;
int main(){
ll c1,c2,c3;
ll ans;
scanf("%lld", &n);
for (int i = 0; i < n; ++i){
scanf("%lld", s+i);
}
for (int i = 0; i < n; ++i){
scanf("%lld", c+i);
}
ans = inf;
// 枚举中间显示器的编号j
for (int j = 1; j < n - 1; ++j){
c2 = c[j];
c1 = inf;
c3 = inf;
// 向左找满足s[i]<s[j]的花费最小的
for (int i = 0; i < j; ++i){
if (s[i] < s[j] && c[i] < c1){
c1 = c[i];
}
}
// 向右找满足s[j]<s[k]的花费最小的
for (int k = j + 1; k < n; ++k){
if (s[k] > s[j] && c[k] < c3){
c3 = c[k];
}
}
if (c1 == inf || c3 == inf) continue;
ans = min(ans, c1 + c2 + c3);
}
if (ans == inf){
puts("-1");
}
else{
printf("%lld
", ans);
}
return 0;
}