zoukankan      html  css  js  c++  java
  • CF1181 B. Split a Number

    题目传送门:https://codeforces.com/problemset/problem/1181/B

    题目大意:
    给定一串长为 $ l(lleqslant10^5) $ 的数字,可将数字分成无前导零的两个数(A,B),问(A+B)的最小值


    将数字分成长度相等的两部分显然是最优的,我们只需要判断是否有前置零,将断点前移或后移判断即可

    /*program from Wolfycz*/
    #include<map>
    #include<cmath>
    #include<cstdio>
    #include<vector>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define Fi first
    #define Se second
    #define ll_inf 1e18
    #define MK make_pair
    #define sqr(x) ((x)*(x))
    #define pii pair<int,int>
    #define int_inf 0x7f7f7f7f
    using namespace std;
    typedef long long ll;
    typedef unsigned int ui;
    typedef unsigned long long ull;
    inline char gc(){
    	static char buf[1000000],*p1=buf,*p2=buf;
    	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
    }
    template<typename T>inline T frd(T x){
    	int f=1; char ch=gc();
    	for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')    f=-1;
    	for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<1)+(x<<3)+ch-'0';
    	return x*f;
    }
    template<typename T>inline T read(T x){
    	int f=1; char ch=getchar();
    	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
    	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
    	return x*f;
    }
    inline void print(int x){
    	if (x<0)	putchar('-'),x=-x;
    	if (x>9)	print(x/10);
    	putchar(x%10+'0');
    }
    const int digit=8;
    const int base=1e8;
    const int maxn=2e4;
    char s[maxn*digit],temp[maxn*digit];
    struct Bignum{
    	int V[maxn],len;
    	Bignum(){len=1,memset(V,0,sizeof(V));}
    	void read(char *s){
    		int l=strlen(s),tim=1;
    		reverse(s,s+l); len=(l-1)/digit+1;
    		for (int i=0;i<l;i++){
    			V[i/digit]+=tim*(s[i]-'0'),tim*=10;
    			if (tim==base)	tim=1;
    		}
    		reverse(s,s+l);
    	}
    	void print(){
    		printf("%d",V[len-1]);
    		for (int i=len-2;~i;i--)	printf("%0*d",digit,V[i]);
    		putchar('
    ');
    	}
    }Ans;
    Bignum operator +(Bignum x,Bignum y){
    	Bignum z; z.len=max(x.len,y.len);
    	for (int i=0;i<=z.len;i++)	z.V[i]+=x.V[i]+y.V[i],z.V[i+1]+=z.V[i]/base,z.V[i]%=base;
    	while (z.V[z.len])	z.V[z.len+1]+=z.V[z.len]/base,z.V[z.len]%=base,z.len++;
    	return z;
    }
    bool operator <(Bignum x,Bignum y){
    	if (x.len!=y.len)	return x.len<y.len;
    	for (int i=x.len;~i;i--)	if (x.V[i]!=y.V[i])	return x.V[i]<y.V[i];
    	return 0;
    }
    int main(){
    //	freopen(".in","r",stdin);
    //	freopen(".out","w",stdout);
    	int n=read(0);
    	scanf("%s",s); Ans.read(s);
    	memcpy(temp,s,sizeof(s));
    	for (int pos=n>>1;pos>0;pos--){
    		if (temp[pos]=='0')	continue;
    		Bignum A; A.read(temp+pos);
    		temp[pos]='';
    		Bignum B; B.read(temp);
    		if (A+B<Ans)	Ans=A+B;
    		temp[pos]=s[pos];
    		break;
    	}
    	for (int pos=(n>>1)+1;pos<n;pos++){
    		if (temp[pos]=='0')	continue;
    		Bignum A; A.read(temp+pos);
    		temp[pos]='';
    		Bignum B; B.read(temp);
    		if (A+B<Ans)	Ans=A+B;
    		temp[pos]=s[pos];
    		break;
    	}
    	Ans.print();
    	return 0;
    }
    
    作者:Wolfycz
    本文版权归作者和博客园共有,欢迎转载,但必须在文章开头注明原文出处,否则保留追究法律责任的权利
  • 相关阅读:
    AFNet3.0上传图片
    最新 AFNetworking 3.0 简单实用封装
    iOS开发密码输入数字和字母混合
    IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)(转)
    iOS开发探索-图片压缩处理
    常用第三方框架插件
    2.1创建直线
    1.4用向导创建Hello,world程序
    vs2008找不到ObjectARX MFC Support
    vc6.0错误:error C2653: 'CCreateEnt' : is not a class or namespace name
  • 原文地址:https://www.cnblogs.com/Wolfycz/p/14984847.html
Copyright © 2011-2022 走看看