AtCoder ABC 155E Payment

Topic links: https://atcoder.jp/contests/abc155/tasks/abc155_e

Subject to the effect

  There are $ 10 {100} + 1 $ denominations of banknotes ^ ($ 1, 10, 10 ^ 2, 10 ^ 3, \ dots, 10 ^ {10 ^ {100}} $), now has a value of $ N $ of commodity, in order to buy this product, you will pay some of the bills businesses, while businesses may find you some of the bill (if you actually pay greater than $ N $ then), I ask how many bills the minimum required to complete this transaction it?

Analysis 1 (DP)

  Take a bit example, suppose a bit to $ X $, then the payment method to pay bits are only two, the first is to pay $ X $ face value of $ 1 $ bill; the second is a face value of prepaid $ 10 $ bill, businesses find $ 10 - X $ Zhang a face value of $ 1 $ bill. The remaining bits Similarly, the difference is that when considering high, if a high level of low pay took a second method, the number should be high on the plus one. To $ N = 67 $, for example, to use only $ 1 $ dollar bill to get $ 7 $ dollars, I'll give $ 10 $ yuan, the business passed on $ 3 $ yuan, this time $ N = 60 $, but I used a $ 10 $ dollar bills, so businesses then passed $ 10 $ dollars to me, so $ N = 70 $, you can see, the second strategy adopted after ten into one, while the single digits consumption bill is $ 10--7 = 3 $ Zhang.
  It can easily think DP can do, provided $ X (i) = N% 10 ^ {i + 1}, 0 \ leq i $, set $ Y (i) $ $ N $ is the number of right to left $ I $ numbers of defined $ dp [i] [0/1] $ of $ X (i) the most significant bit first and then the optimal solution / $ the second strategy, it is the state transition equation:

  $$
    \begin{align*}
    dp[i][0] &= min(Y(i) + dp[i - 1][0], Y(i) + dp[i - 1][1]) \\
    dp[i][1] &= min(1 + 10 - Y(i) + dp[i - 1][0], 1 + 10 - (Y(i) + 1) + dp[i - 1][1] - 1)
    \end{align*}
  $$

code show as below

. 1 #include <bits / STDC ++ H.>
 2  the using  namespace STD;
 . 3  
. 4  / * ------------------- the Define the Start ---------- --------- * / 
. 5 typedef BOOL BL;                         // boolean 
. 6 typedef char SB;                         // signed 1 byte, 8 
. 7 typedef unsigned char UB;                 // unsigned byte 1, 8 bit 
. 8 typedef short SW;                         // signed short, 16 
. 9 typedef unsigned shortThe UW;                 // unsigned short, 16 
10 typedef Long SDW;                         // signed integer, 32-bit 
. 11 typedef unsigned Long UDWs;                // unsigned integer, 32-bit 
12 is typedef Long  Long SLL;                     // there unsigned long integer, 64-bit 
13 is typedef unsigned long  long ULL;             // unsigned long, 64 
14 typedef char CH;                         // single character 
15 typedef a float R32;                        // 单精度浮点数
16 typedef double R64;                        // 双精度浮点数
17 
18 #define Rep(i, n) for (register SDW i = 0; i < (n); ++i)
19 #define For(i, s, t) for (register SDW i = (s); i <= (t); ++i)
20 #define rFor(i, t, s) for (register SDW i = (t); i >= (s); --i)
21 #define foreach(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
22 #define ms0(a) memset(a,0,sizeof(a))
23 #define msI(a) memset(a,0x7f,sizeof(a))
24 #define LOWBIT(x) ((x)&(-x))
25 
26 #define MP make_pair
27 #define PB push_back
28 #define ft first
29 #define sd second
30 #define ALL(x) x.begin(),x.end()
31 
32 #define pr(x) cout << #x << " = " << x << "  "
33 #define prln(x) cout << #x << " = " << x << endl
34 
35 const ULL mod = 1e9 + 7;                //常用模数(可根据题目需要修改)
36 const ULL inf = 0x7fffffff;                //Used to represent infinity 
37 [  const ULL = infLL 0x7fffffffffffffffLL;     // used to represent infinity 
38 is  / * ------------------- the Define End ------- ----------------------- * / 
39  
40  const UDWs maxN = 1e6 + . 7 ;
 41 is  String N;
 42 is SDW DP [maxN] [ 2 ]; 
 43 is  SDW ANS;
 44 is  
45  void INPUT () {
 46 is      >> CIN N;
 47  }
 48  
49  void Solve () {
 50      DP [ . 1 ] [ 0 ] = N [N.size () -1] - '0';
51     dp[1][1] = 11 - N[N.size() - 1] + '0'; 
52     
53     For(i, 2, N.size()) {
54         SDW Y = N[N.size() - i] - '0';
55 
56         dp[i][0] = min(Y + dp[i - 1][0], Y + dp[i - 1][1]);
57         dp[i][1] = min(1 + 10 - Y + dp[i - 1][0], 1 + 10 - (Y + 1) + dp[i - 1][1] - 1);
58     }
59     
60     ans = min(dp[N.size()][0], dp[N.size()][1]);
61 }
62 
63 void output(){
64     cout << ans << endl;
65 }
66 
67 int main() {
68     input();
69     solve();
70     output();
71     return 0;
72 }
View Code

 

Analysis (greedy)

  Then to bits, for example, when a bit less than 5, take the first strategy must be optimal, and when more than 5 bits, be sure to take the second strategy is optimal. Only when the bit is equal to 5, the two strategies consistent earnings, this time we should look at the impact of the carry for tens, if after ten digit carry is less than 5, then choose the first strategy, if more than 5, should be The second option, equal to 5, free. (For proof of greedy, really I do not know how license, anyway, the program is right ...)

code show as below

. 1 #include <bits / STDC ++ H.>
 2  the using  namespace STD;
 . 3  
. 4  / * ------------------- the Define the Start ---------- --------- * / 
. 5 typedef BOOL BL;                         // boolean 
. 6 typedef char SB;                         // signed 1 byte, 8 
. 7 typedef unsigned char UB;                 // unsigned byte 1, 8 bit 
. 8 typedef short SW;                         // signed short, 16 
. 9 typedef unsigned shortThe UW;                 // unsigned short, 16 
10 typedef Long SDW;                         // signed integer, 32-bit 
. 11 typedef unsigned Long UDWs;                // unsigned integer, 32-bit 
12 is typedef Long  Long SLL;                     // there unsigned long integer, 64-bit 
13 is typedef unsigned long  long ULL;             // unsigned long, 64 
14 typedef char CH;                         // single character 
15 typedef a float R32;                        // 单精度浮点数
16 typedef double R64;                        // 双精度浮点数
17 
18 #define Rep(i, n) for (register SDW i = 0; i < (n); ++i)
19 #define For(i, s, t) for (register SDW i = (s); i <= (t); ++i)
20 #define rFor(i, t, s) for (register SDW i = (t); i >= (s); --i)
21 #define foreach(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
22 #define ms0(a) memset(a,0,sizeof(a))
23 #define msI(a) memset(a,0x7f,sizeof(a))
24 #define LOWBIT(x) ((x)&(-x))
25 
26 #define MP make_pair
27 #define PB push_back
28 #define ft first
29 #define sd second
30 #define ALL(x) x.begin(),x.end()
31 
32 #define pr(x) cout << #x << " = " << x << "  "
33 #define prln(x) cout << #x << " = " << x << endl
34 
35 const ULL mod = 1e9 + 7;                //常用模数(可根据题目需要修改)
36 const ULL inf = 0x7fffffff;                //Used to represent infinity 
37 [  const ULL = infLL 0x7fffffffffffffffLL;     // used to represent infinity 
38 is  / * ------------------- the Define End ------- ----------------------- * / 
39  
40  const UDWs maxN = 1e6 + . 7 ;
 41 is  String N;
 42 is  SLL ANS;
 43 is  
44 is  void INPUT () {
 45      CIN >> N;
 46 is      N = " 0 " + N; 
 47  }
 48  
49  void Solve () {
 50      RFOR (I, N.size () - . 1 ,0) {
51         SLL x = N[i] - '0';
52         
53         if(x > 5 || x == 5 && N[i - 1] >= '5') {
54             ans += 10 - x;
55             ++N[i - 1];
56         }
57         else {
58             ans += x;
59         }
60     }
61 }
62 
63 void output(){
64     cout << ans << endl;
65 }
66 
67 int main() {
68     input();
69     solve();
70     output();
71     return 0;
72 }
View Code

 

Guess you like

Origin www.cnblogs.com/zaq19970105/p/12339997.html