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
Input
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.
Note that it is impossible to split the strip into "1" and "01" since the numbers can't start with zeros.
思路:
题目是要把一个大数拆成两个小数,并且让他们加起来的和最小。
根据贪心的思想,可以想到,如果把数从中间分开,从而让两个数的位数差不多时,两者的和应该是比较小的。但是题目中有要求,拆成的两个数不能有前导零。咋办。
就把所有不是零 的元素的索引提出来加到一个vector里面,因为要取尽量中间的数,就用一下lower_bound二分找原数组长度一半的位置mid,能找到vector里接近mid的元素。
然后用这个元素所表示的索引,在原来的数组中索引附近遍历,取到不同的分割位置,选出分割后的和最小者。
但是数好像很大哎,只有用字符串来模拟了,传说中的大数加法。
代码:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n;//接收数字位数
string s;//接收数字
vector<int> vec;//来存值不为零的元素的索引
string rm0(string a)//删除前导零
{
int i;
for(i = ;i<a.size();i++)
{
if(a[i]!='')
{
break;
}
}
a = a.substr(i);
return a;
}
string add(string a,string b)//大数加法
{
if(a.size()<b.size()) //先把两者位数补成一样的
{
swap(a,b);
}
a = ''+a;
while(b.size()<a.size())
{
b = ''+b;
}
int c = ;
for(int i = b.size()-;i>=;i--) //从低位到高位加
{
int sum = c+(int)a[i]-''+(int)b[i]-'';
if(sum>=)
{
a[i] = (char)sum-+'';
c = ;
}
else
{
a[i] = (char)sum+'';
c = ;
}
}
a = rm0(a);
return a;
}
int larger(string a,string b)//判断a是不是大于等于b
{
if(a.size()!=b.size())
{
return a.size()>b.size();
}
else
{
for(int i = ;i<a.size();i++)
{
if(a[i]!=b[i])
{
return a[i]>b[i];
}
}
}
return ;
}
int main()
{
cin >> n >> s;
string a = "";
string b = "";
for(int i = ;i<n;i++)
{
if(s[i]!='')
{
vec.push_back(i);
}
}
int mid = lower_bound(vec.begin(),vec.end(),n/)-vec.begin();//找不为零的接近中间位置的数在原数组的索引
string ans = s;
for(int i = -;i<;i++)//在索引附近遍历一遍
{
int pos = max(,min((int)vec.size()-,mid+i));
a = s.substr(,vec[pos]);
b = s.substr(vec[pos]);
string res = add(a,b);
if(larger(ans,res))
{
ans = res;//取最小值
}
}
cout << ans << endl;
return ;
}
参考文章:
我写的可能不清楚,如果还有问题,参见推荐博客:

茄子Min,Codeforces Round #567 (Div. 2)B. Split a Number (字符串,贪心),https://www.cnblogs.com/qieqiemin/p/11033007.html
另外本题涉及到一些实用的字符串处理函数,具体可见:
GGBeng,C++中substr函数的用法,https://www.cnblogs.com/xzxl/p/7243490.html
Codeforces C. Split a Number(贪心大数运算)的更多相关文章
- Codeforces - 1181B - Split a Number - 贪心
https://codeforces.com/contest/1181/problem/B 从中间拆开然后用大数搞一波. 当时没想清楚奇偶是怎么弄,其实都可以,奇数长度字符串的中心就在len/2,偶数 ...
- PAT甲题题解-1024. Palindromic Number (25)-大数运算
大数据加法给一个数num和最大迭代数k每次num=num+num的倒序,判断此时的num是否是回文数字,是则输出此时的数字和迭代次数如果k次结束还没找到回文数字,输出此时的数字和k 如果num一开始是 ...
- Codeforces Round #567 (Div. 2)B. Split a Number (字符串,贪心)
B. Split a Number time limit per test2 seconds memory limit per test512 megabytes inputstandard inpu ...
- Codeforces Round #567 (Div. 2) B. Split a Number
Split a Number time limit per test 2 seconds memory limit per test 512 megabytes input standard inpu ...
- 收藏的一段关于java大数运算的代码
收藏的一段关于java大数运算的代码: package study_02.number; import java.math.BigDecimal; import java.math.BigIntege ...
- [PKU2389]Bull Math (大数运算)
Description Bulls are so much better at math than the cows. They can multiply huge integers together ...
- HOJ 2148&POJ 2680(DP递推,加大数运算)
Computer Transformation Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4561 Accepted: 17 ...
- 大数运算(python2)
偶然又遇到了一道大数题,据说python大数运算好屌,试了一发,果然方便-1 a = int( raw_input() ); //注意这里是按行读入的,即每行只读一个数 b = int( raw_in ...
- java 大数运算[转]
用JAVA 实现算术表达式(1234324234324 + 8938459043545)/5 + 343434343432.59845 因为JAVA语言中的long 定义的变量值的最大数受到限制,例如 ...
随机推荐
- LODOP表格水平居中3(宽度为百分比)
如果一个表格在css样式等中设置了固定的宽度,想要实现表格在纸张中水平居中,可根据固定的宽度设置合适的左边距,如果打印项内容在打印项宽度中居中,可以设置打印项在纸张中居中.方法1:宽度固定,纸张大小固 ...
- 通Shell获取Tomcat进程号并杀死进程
#!/bin/bash echo "begin get tomcat8 pid" tomcat8_id=$( | grep -v grep | awk '{print $2}') ...
- 晶体管放大电路与Multisim仿真学习笔记
前言 开始写点博客记录学习的点滴,第一篇就写基本的共射极放大电路吧. 很多教材都是偏重理论,而铃木雅臣著作的<晶体管电路设计>是一本很实用的书籍,个人十分推荐! 下面开始我的模电重温之旅吧 ...
- 编程语言与python介绍
目录 一.编程语言的发展史 1.1 机器语言 1.2 汇编语言 1.3 高级语言 1.3.1 编译型 1.3.2 解释型 1.4 总结 2.python介绍 2.1 python解释器版 2.2 运行 ...
- ClickHouse中的循环复制集群拓扑
关系型数据库,但千万级表关联数据库基本上不太可能做到秒出:考虑过Sharding,但数据量大, 各种成本都很高:热数据存储到ElasticSearch,但无法跨索引关联,导致不得不做宽表, 因为权限, ...
- C语言学习笔记01——C语言概述
作者:Eventi 出处:http://www.cnblogs.com/Eventi 欢迎转载,也请保留这段声明.谢谢! 1 C语言的起源 1972年,贝尔实验室的丹尼斯·里奇(Dennis Ritc ...
- (十)pdf的构成之交叉引用表
交叉引用表(xref) 其中包含对文档中所有对象的引用.交叉引用表的目的是允许随机访问文件中的对象,因此我们不需要读取整个PDF文档来定位特定对象.每个对象由交叉引用表中的一个条目表示.(该表保存 ...
- pyspider 数据存入Mysql--Python3
一.不写入Mysql 以爬取哪儿网为例. 以下为脚本: from pyspider.libs.base_handler import * class Handler(BaseHandler): cra ...
- 【写法】为什么if判断中,值要倒着写
=============================================== 2019/8/27_第1次修改 ccb_warlock == ...
- java枚举enum总结大全
1.注意点 (1)枚举中的构造方法必须是private的. (2)枚举中可以定义抽象方法和一般方法,但枚举对象必须实现所有抽象方法. (3)枚举对象必须放在第一行. package classTwo0 ...