当前位置:网站首页>C. Three displays codeforces round 485 (Div. 2)

C. Three displays codeforces round 485 (Div. 2)

2022-06-24 16:00:00 51CTO

Original link : ​https://codeforces.com/problemset/problem/987/C​

C. Three displays( Dynamic programming )Codeforces Round #485 (Div. 2)_dp
The test sample :

The sample input 1
5
2 4 5 4 10
40 30 20 10 40
Sample output 1
90
The sample input 2
3
100 101 100
2 4 5
Sample output 2
-1
The sample input 3
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Sample output 3
33

Tips :

No violence .

Their thinking : The seniors warned against violence , Or two violent attempts . After the game, I found it was ,dp Just solve it . Okay , Let's talk about ideas , This problem we are asking to solve can make C. Three displays( Dynamic programming )Codeforces Round #485 (Div. 2)_ minimum value _02 The minimum value of C. Three displays( Dynamic programming )Codeforces Round #485 (Div. 2)_ios_03, among C. Three displays( Dynamic programming )Codeforces Round #485 (Div. 2)_dp_04, So we can discuss one part of it first, that is C. Three displays( Dynamic programming )Codeforces Round #485 (Div. 2)_dp_05, We're going to use dp To solve the , use C. Three displays( Dynamic programming )Codeforces Round #485 (Div. 2)_#define_06 Express satisfaction C. Three displays( Dynamic programming )Codeforces Round #485 (Div. 2)_ minimum value _07 Of C. Three displays( Dynamic programming )Codeforces Round #485 (Div. 2)_dp_08 The minimum value of , We don't care C. Three displays( Dynamic programming )Codeforces Round #485 (Div. 2)_dp_09 Value , We just have to find the minimum , meanwhile C. Three displays( Dynamic programming )Codeforces Round #485 (Div. 2)_dp_09 Satisfy C. Three displays( Dynamic programming )Codeforces Round #485 (Div. 2)_ios_11 Just go . So after we get this minimum, we can get another minimum ? Let's split this comparison into two , So our original three levels of violence for The cycle is broken down into two ,OK, Let's look at the code .

AC Code :

      
      
/*

*
*/
#include<bits/stdc++.h> //POJ I won't support it

#define rep(i,a,n) for (int i=a;i<=n;i++) //i Is a cyclic variable ,a For the initial value ,n Is the limit value , Increasing
#define per(i,a,n) for (int i=a;i>=n;i--) //i Is a cyclic variable , a For the initial value ,n Is the limit value , Decline .
#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; // infinity
const int maxn = 1e5; // Maximum .
typedef long long ll;
typedef long double ld;
typedef pair < ll, ll > pll;
typedef pair < int, int > pii;
//******************************* Split line , The above is a custom code template ***************************************//

int n;
int a[ maxn], b[ maxn], dp[ maxn]; //dp[i] It means a[i]>a[j] bring b[i]+b[j] The minimum value , among 1<=j<i.
int main(){
//freopen("in.txt", "r", stdin);// When submitting, you should comment out
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]);
}
}
}
// We get it again b[i]+dp[j] The minimum value of , This is the answer we want .
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;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.


原网站

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