Codeforces C. Split a Number(貪心大數運算)

題目描述:

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Dima worked all day and wrote down on a long paper strip his favorite number n

consisting of l digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf. To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip. Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain.

Input

The first line contains a single integer l(2≤l≤100000) — the length of the Dima's favorite number.

The second line contains the positive integer n initially written on the strip: the Dima's favorite number.

The integer n consists of exactly l digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip.

Output

Print a single integer — the smallest number Dima can obtain.

Examples

Inputhtml


7
1234567

Output

1801

Input

3
101

Output

11

Note

In the first example Dima can split the number 1234567

into integers 1234 and 567. Their sum is 1801.

In the second example Dima can split the number 101 into integers 10 and 1. Their sum is 11.ios

Note that it is impossible to split the strip into "1" and "01" since the numbers can't start with zeros.git

思路:

題目是要把一個大數拆成兩個小數,而且讓他們加起來的和最小。數組

根據貪心的思想,能夠想到,若是把數從中間分開,從而讓兩個數的位數差很少時,二者的和應該是比較小的。可是題目中有要求,拆成的兩個數不能有前導零。咋辦。ide

就把全部不是零 的元素的索引提出來加到一個vector裏面,由於要取儘可能中間的數,就用一下lower_bound二分找原數組長度一半的位置mid,能找到vector裏接近mid的元素。函數

而後用這個元素所表示的索引,在原來的數組中索引附近遍歷,取到不一樣的分割位置,選出分割後的和最小者。post

可是數好像很大哎,只有用字符串來模擬了,傳說中的大數加法。spa

代碼:

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 using namespace std;
 5 int n;//接收數字位數
 6 string s;//接收數字
 7 vector<int> vec;//來存值不爲零的元素的索引
 8 string rm0(string a)//刪除前導零
 9 {
10     int i;
11     for(i = 0;i<a.size();i++)
12     {
13         if(a[i]!='0')
14         {
15             break;
16         }
17     }
18     a = a.substr(i);
19     return a;
20 }
21 string add(string a,string b)//大數加法
22 {
23     if(a.size()<b.size()) //先把二者位數補成同樣的
24     {
25         swap(a,b);
26     }
27     a = '0'+a;
28     while(b.size()<a.size())
29     {
30         b = '0'+b;
31     }
32     int c = 0;
33     for(int i = b.size()-1;i>=0;i--) //從低位到高位加
34     {
35         int sum = c+(int)a[i]-'0'+(int)b[i]-'0';
36         if(sum>=10)
37         {
38             a[i] = (char)sum-10+'0';
39             c = 1;
40         }
41         else
42         {
43             a[i] = (char)sum+'0';
44             c = 0;
45         }
46     }
47     a = rm0(a);
48     return a;
49 }
50 int larger(string a,string b)//判斷a是否是大於等於b
51 {
52     if(a.size()!=b.size())
53     {
54         return a.size()>b.size();
55     }
56     else
57     {
58         for(int i = 0;i<a.size();i++)
59         {
60             if(a[i]!=b[i])
61             {
62                 return a[i]>b[i];
63             }
64         }
65     }
66     return 1;
67 }
68 int main()
69 {
70     cin >> n >> s;
71     string a = "";
72     string b = "";
73     for(int i = 0;i<n;i++)
74     {
75         if(s[i]!='0')
76         {
77             vec.push_back(i);
78         }
79     }
80     int mid = lower_bound(vec.begin(),vec.end(),n/2)-vec.begin();//找不爲零的接近中間位置的數在原數組的索引
81     string ans = s;
82     for(int i = -2;i<3;i++)//在索引附近遍歷一遍
83     {
84         int pos = max(1,min((int)vec.size()-1,mid+i));
85         a = s.substr(0,vec[pos]);
86         b = s.substr(vec[pos]);
87         string res = add(a,b);
88         if(larger(ans,res))
89         {
90             ans = res;//取最小值
91         }
92     }
93     cout << ans << endl;
94     return 0;
95 }

參考文章:

我寫的可能不清楚,若是還有問題,參見推薦博客:code

茄子MinCodeforces Round #567 (Div. 2)B. Split a Number (字符串,貪心),https://www.cnblogs.com/qieqiemin/p/11033007.htmlhtm

另外本題涉及到一些實用的字符串處理函數,具體可見:

GGBeng,C++中substr函數的用法,https://www.cnblogs.com/xzxl/p/7243490.html

相關文章
相關標籤/搜索