Codeforces 706C Hard Problem DP (Dynamic planning) problem

Source: Internet
Author: User

title link :http://codeforces.com/problemset/problem/706/C

The main idea : given n strings, each string can be reversed before and after the position (the first letter to the last, the second letter to the second to the penultimate) need to spend the strength of CI each time, require the given n string by the minimum strength in dictionary order, output strength value, Output-1 If it cannot be sorted by dictionary order

data range:2?≤? n? ≤?100?000, ci (0?≤? Ci? ≤?1e9) The total length of all strings will not exceed 1000000.

problem-solving ideas: This is a DP problem, DP[I][0/1] respectively, the first string is not moved or reversed, do DP problem must always remember the words of the seniors said that the word DP is very smart, think more and put themselves around.

at that time I did not make it to Baidu, learned the direct use of strings, but also useful reverse ( b < Span style= "COLOR: #ff0000" >[ i < Span style= "COLOR: #ff0000" >). begin (), b [ Span class= "PLN" >i ]. end ()); The range can be reversed directly.  

This problem has several pit points: a DP array to open long long, two flat often with the const int MAXN = 1<<30; Do this problem want to write up the result wrong, later only found a long long type to compare generally open to 1e15 more insurance; the rest is normal DP routines.

The code is as follows:

1#include <cstdio>2#include <cstring>3#include <cmath>4#include <cstdlib>5#include <algorithm>6#include <iostream>7 using namespacestd;8typedefLong LongLL;9 Const intMAXN =1e5;Ten ConstLL inf =1e15; One intN; ALL NUM[MAXN +5]; -LL DP[MAXN +5][2]; - stringA[MAXN +5], B[MAXN +5]; the intLen; - intMain () - { -scanf"%d", &n); +      for(inti =1; I <= N; i++) -scanf"%i64d", &num[i]); +      for(inti =1; I <= N; i++){ ACIN >>A[i]; atb[i]=A[i]; - Reverse (B[i].begin (), B[i].end ()); -     } -      for(inti =1; I <= N; i++) -dp[i][0] = dp[i][1] =inf; -dp[1][0] =0; indp[1][1] = num[1]; -     inti; to      for(i =2; I <= N; i++){ +         if(A[i] >= a[i-1]) -dp[i][0] = Dp[i-1][0]; the         if(B[i] >= a[i-1]) *dp[i][1] = Dp[i-1][0] +Num[i]; $         if(A[i] >= b[i-1])Panax Notoginsengdp[i][0] = min (dp[i][0], Dp[i-1][1]); -         if(B[i] >= b[i-1]) thedp[i][1] = min (dp[i][1], Dp[i-1][1] +num[i]); +         if(dp[i][0] = = inf && dp[i][1] ==inf) A              Break; the     } +     if(i = = n +1) -printf"%i64d\n", Min (dp[n][1], dp[n][0])); $     Else $printf"-1\n"); -     return 0; -}

Codeforces 706C Hard Problem DP (Dynamic planning) problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.