0
点赞
收藏
分享

微信扫一扫

C. Three displays(动态规划)Codeforces Round #485 (Div. 2)

原题链接: ​​https://codeforces.com/problemset/problem/987/C​​

C. Three displays(动态规划)Codeforces Round #485 (Div. 2)_最小值
测试样例:

样例输入1
5
2 4 5 4 10
40 30 20 10 40
样例输出1
90
样例输入2
3
100 101 100
2 4 5
样例输出2
-1
样例输入3
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
样例输出3
33

提示:

不要暴力哦。

解题思路: 学长都提醒不要暴力了,还是暴力尝试了两发。赛后发现简直了,dp解决就好了。好了,来说一下思路,这道题我们是要求解能让C. Three displays(动态规划)Codeforces Round #485 (Div. 2)_#define_02的最小值C. Three displays(动态规划)Codeforces Round #485 (Div. 2)_最小值_03,其中C. Three displays(动态规划)Codeforces Round #485 (Div. 2)_ios_04,那么我们可以先探讨其中一部分也就是C. Three displays(动态规划)Codeforces Round #485 (Div. 2)_最小值_05,这里我们就要用到dp来解决了,用C. Three displays(动态规划)Codeforces Round #485 (Div. 2)_#define_06表示满足C. Three displays(动态规划)Codeforces Round #485 (Div. 2)_ios_07C. Three displays(动态规划)Codeforces Round #485 (Div. 2)_ios_08的最小值,我们不用在意C. Three displays(动态规划)Codeforces Round #485 (Div. 2)_dp_09的值,我们只要求得最小值,同时C. Three displays(动态规划)Codeforces Round #485 (Div. 2)_dp_09满足C. Three displays(动态规划)Codeforces Round #485 (Div. 2)_ios_11就行。那么我们求出这个最小值之后我们不就可以再求一个最小值吗?我们将这个比较式拆成两个,那么我们原来的这三层暴力for循环经过这样拆成了两个,OK,我们具体看代码。

AC代码:

/*

*
*/
#include<bits/stdc++.h> //POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int n;
int a[maxn],b[maxn],dp[maxn];//dp[i]表示的是a[i]>a[j]使得b[i]+b[j]最小的值,其中1<=j<i。
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
while(cin>>n){
rep(i,1,n)cin>>a[i];
rep(i,1,n)cin>>b[i];
memset(dp,inf,sizeof(dp));
rep(i,2,n){
rep(j,1,i-1){
if(a[i]>a[j]){
dp[i]=min(dp[i],b[i]+b[j]);
}
}
}
//我们再获取b[i]+dp[j]的最小值,这即是我们想要的答案。
int ans=inf;
rep(i,2,n){
rep(j,1,i-1){
if(a[i]>a[j]){
ans=min(ans,b[i]+dp[j]);
}
}
}
if(ans==inf)
cout<<-1<<endl;
else
cout<<ans<<endl;
}
return 0;
}


举报

相关推荐

0 条评论