CodeForces 1096D (linear dp)

Portal

• the meaning of problems

Given a string of length n s, for each $ s_ {i} $ has $ a_ {i} $ Value

Let you delete the minimum value, so that the sequence $ hard $ string does not exist

• ideas

Provided dp [1] is not the minimum cost to $ h $ prefix exists

dp [2] is not present in the HA $ $ prefix, i.e. the absence or absence of $ h $ $ $ A $ or $ HA minimum cost does not exist

Similarly, dp [3] HAR is the minimum cost to $ $ prefix does not exist, dp [4] is a minimal cost to $ $ prefix Hard absent

dp [i] can dp [i-1] to transfer, $ dp [i] = min (dp [i] + a, dp [i-1]) $

• Code

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 const int maxn=1e5+5;
 5 char s[maxn];
 6 ll dp[5];
 7 int main()
 8 {
 9     int n;
10     cin>>n;
11     scanf("%s",s+1);
12     for(int i=1;i<=n;i++)
13     {
14         ll x;
15         cin>>x;
16         if(s[i]=='h')
17             dp[1]+=x;
18         else if(s[i]=='a')
19             dp[2]=min(dp[2]+x,dp[1]);
20         else if(s[i]=='r')
21             dp[3]=min(dp[3]+x,dp[2]);
22         else if(s[i]=='d')
23             dp[4]=min(dp[4]+x,dp[3]);
24     }
25     cout<<dp[4]<<endl;
26 }
View Code

 

Guess you like

Origin www.cnblogs.com/MMMinoz/p/11493700.html