Codeforces Round #567 (Div. 2)B. Split a Number (字符串,贪心)
B. Split a Number
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard 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
inputCopy
7
1234567
outputCopy
1801
inputCopy
3
101
outputCopy
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.
题意:
给你一个字符串表示一个整数,让你把整数分成两个分部,两个非空的部分,而且不能有一个部分有前导0,例如分成a,b, 两个部分,然后求哪种分开的方法可以让a+b 最小,输出a+b的数值。
思路:
首先我们处理出所有不是0数字的位置,加入到一个vector里,然后我们最vector 进行二分查找距离 len/2 最近的位置 (因为最靠中间分,答案最优),然后枚举二分得到的位置附件的几个位置,求a+b 中的最小值 。输出即可。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <bits/stdc++.h>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
string S(ll n){stringstream ss;string s;ss<<n;ss>>s;return s;}
ll N(string s){stringstream ss;ll n;ss<<s;ss>>n;return n;}
string rm0(string s){// 去除前导0函数
int i;
for(i=0;i<s.size()-1;i++)
if(s[i]!='0')
break;
return s.substr(i);
}
string ADD(string s,string t) {// 字符串整数相加,返回一个字符串
if(s.size()<t.size())swap(s,t);s='0'+s;
reverse(t.begin(), t.end());while(s.size()>t.size())t+='0';reverse(t.begin(), t.end());int c=0;
for(int i=s.size();i>=0;i--)
{
if(c){
if(s[i]=='9'){
s[i]='0';c=1;
}else{
s[i]=(char)(s[i]+1);c=0;
}
}
int sum=(int)s[i]+(int)t[i]-'0'*2;
if(sum>=10){
s[i]=(char)(sum-10+'0');c=1;
}
else
s[i]=(char)(sum+'0');
}
return rm0(s);
}
bool cmp(string s,string t) {
// s>=t 返回1
// s<t 返回0
if(s.size()!=t.size())
return s.size()>t.size();
for(int i=0;i<s.size();i++)
if(s[i]!=t[i])
return s[i]>t[i];
return 1;
}
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
int len;
string str;
cin>>len>>str;
std::vector<int> v;
v.clear();
rep(i,0,len)
{
if(str[i]!='0')
{
v.pb(i);
}
}
int k=lower_bound(ALL(v),len/2)-v.begin();
string ans=str;
repd(j,-2,3)
{
int id=max(1,min(sz(v)-1,k+j));
string s1=str.substr(0,v[id]);
string s2=str.substr(v[id]);
string w=ADD(s1,s2);
if(cmp(ans,w))
{
ans=w;
}
}
cout<<ans<<endl;
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
Codeforces Round #567 (Div. 2)B. Split a Number (字符串,贪心)的更多相关文章
- 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 ...
- DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game
题目传送门 /* 题意:b+1,b+2,...,a 所有数的素数个数和 DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1:否则它可以从一个数乘以素数递推过来 最后改为i之前所有素数个 ...
- 数学+DP Codeforces Round #304 (Div. 2) D. Soldier and Number Game
题目传送门 /* 题意:这题就是求b+1到a的因子个数和. 数学+DP:a[i]保存i的最小因子,dp[i] = dp[i/a[i]] +1;再来一个前缀和 */ /***************** ...
- Codeforces Round #567 (Div. 2)自闭记
嘿嘿嘿,第一篇文章,感觉代码可以缩起来简直不要太爽 打个div2发挥都这么差... 平均一题fail一次,还调不出错,自闭了 又一次跳A开B,又一次B傻逼错误调不出来 罚时上天,E还傻逼了..本来这场 ...
- Codeforces Round #567 (Div. 2)A
A. Chunga-Changa 题目链接:http://codeforces.com/contest/1181/problem/A 题目 Soon after the Chunga-Changa i ...
- Codeforces Round #514 (Div. 2) E. Split the Tree(倍增+贪心)
https://codeforces.com/contest/1059/problem/E 题意 给出一棵树,每个点都有一个权值,要求你找出最少条链,保证每个点都属于一条链,而且每条链不超过L个点 和 ...
- Codeforces Round #567 (Div. 2) E2 A Story of One Country (Hard)
https://codeforces.com/contest/1181/problem/E2 想到了划分的方法跟题解一样,但是没理清楚复杂度,很难受. 看了题解觉得很有道理,还是自己太菜了. 然后直接 ...
- Codeforces Round #567 Div. 2
A:签到. #include<bits/stdc++.h> using namespace std; #define ll long long #define inf 1000000010 ...
- Codeforces Round #567 (Div. 2) A.Chunga-Changa
原文链接:传送 #include"algorithm" #include"iostream" #include"cmath" using n ...
随机推荐
- AVLTree的实现以及左右旋转维持自平衡
AVL(Adelson-Velskii and Landis)树是带有平衡条件的二叉查找树.这个平衡条件必须要容易保持,而且它保证树的深度须是o(logN).最简单的想法是要求左右子树具有相同的高度, ...
- 555E Case of Computer Network
分析 一个连通块内的肯定不影响 于是我们先缩点 之后对于每个路径 向上向下分别开一个差分数组 如果两个数组同时有值则不合法 代码 #include<bits/stdc++.h> using ...
- leetcode 数组 (python)
1.题目描述 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数 ...
- 【flask_sqlalchemy】动态CURD类
环境: flask_sqlalchemy mysql from app import db class Curd(object): def __init__(self,modelName): self ...
- spring +spring+ hibernate配置1
这种配置方式是将Spring .SpringMVC.Hibernate三个模块分开配置,交叉引用!hibernate连接配置使用.properties文件 web.xml配置 <web-app ...
- between()函数
1 between函数返回一个布尔量,即如果在该范围内,返回True,否则返回False. 注意between()中括号内是左闭右闭区间 在dataframe中常可用来通过选择某一列元素的范围来选择另 ...
- 解决Pip install Pillow 失败问题
当我在使用Django一个上传图片功能的时候, Django 提示我安装 Pillow这个图片处理的库, 当我尝试安装的时候. 总是提示安装失败 报如下错误. v = self._sslobj.rea ...
- 【MM系列】SAP 物料帐下修改物料的价格
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 物料帐下修改物料的价格 ...
- cocos2dx基础篇(10) 按钮控件CCControlButton
[3.x] (1)去掉 “CC” (2)对象类 CCObject 改为 Ref (3)按钮事件回调依旧为 cccontrol_selector ,没有使用 CC_CALLBACK_2 (4)按钮状态 ...
- LeetCode.874-走路机器人模拟(Walking Robot Simulation)
这是悦乐书的第335次更新,第360篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第205题(顺位题号是874).网格上的机器人从点(0,0)开始并朝北.机器人可以接收三 ...