https://codeforces.com/contest/1181/problem/B

从中间拆开然后用大数搞一波。

当时没想清楚奇偶是怎么弄,其实都可以,奇数长度字符串的中心就在len/2,偶数长度字符串的中心恰好是len/2和len/2-1。

但是要是作为末尾指针的位置来说的话

奇数长度字符串:把中心分给后半串,那么分割点是len/2。把中心分给前半串,那么分割点是len/2+1。

偶数长度字符串:分割点是len/2。

注意子串的取法,其实最方便的还是传头尾指针进去。

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. inline int read() {
  5. int x=0;
  6. int f=0;
  7. char c;
  8. do {
  9. c=getchar();
  10. if(c=='-')
  11. f=1;
  12. } while(c<'0'||c>'9');
  13. do {
  14. x=(x<<3)+(x<<1)+c-'0';
  15. c=getchar();
  16. } while(c>='0'&&c<='9');
  17. return f?-x:x;
  18. }
  19. inline void _write(int x) {
  20. if(x>9)
  21. _write(x/10);
  22. putchar(x%10+'0');
  23. }
  24. inline void write(int x) {
  25. if(x<0) {
  26. putchar('-');
  27. x=-x;
  28. }
  29. _write(x);
  30. putchar('\n');
  31. }
  32. struct BigInt {
  33. const static int mod=10000;
  34. const static int DLEN=4;
  35. int a[30005],len;
  36. BigInt() {
  37. memset(a,0,sizeof(a));
  38. len=1;
  39. }
  40. BigInt(int v) {
  41. memset(a,0,sizeof(a));
  42. len=0;
  43. do {
  44. a[len++]=v%mod;
  45. v/=mod;
  46. } while(v);
  47. }
  48. BigInt(const char *s) {
  49. memset(a,0,sizeof(a));
  50. int L=strlen(s);
  51. len = L/DLEN;
  52. if(L%DLEN)
  53. len++;
  54. int index=0;
  55. for(int i=L-1; i>=0; i-=DLEN) {
  56. int t=0;
  57. int k=i-DLEN+1;
  58. if(k<0)
  59. k=0;
  60. for(int j=k; j<=i; j++)
  61. t=t*10+s[j]-'0';
  62. a[index++] = t;
  63. }
  64. }
  65. BigInt operator+(const BigInt &b)const {
  66. BigInt res;
  67. res.len=max(len,b.len);
  68. for(int i=0; i<res.len; i++) {
  69. res.a[i]+=((i<len)?a[i]:0)+((i<b.len)?b.a[i]:0);
  70. res.a[i+1]+=res.a[i]/mod;
  71. res.a[i]%=mod;
  72. }
  73. if(res.a[res.len] > 0)
  74. res.len++;
  75. return res;
  76. }
  77. BigInt operator*(const BigInt &b)const {
  78. BigInt res;
  79. for(int i=0; i<len; i++) {
  80. int up = 0;
  81. for(int j=0; j<b.len; j++) {
  82. int temp=a[i]*b.a[j]+res.a[i+j]+up;
  83. res.a[i+j]=temp%mod;
  84. up=temp/mod;
  85. }
  86. if(up != 0)
  87. res.a[i+ b.len]=up;
  88. }
  89. res.len=len+b.len;
  90. while(res.a[res.len-1]==0&&res.len>1)
  91. res.len--;
  92. return res;
  93. }
  94. bool operator>(const BigInt &b)const {
  95. if(len>b.len)
  96. return true;
  97. else if(len==b.len) {
  98. int ln=len-1;
  99. while(a[ln]==b.a[ln]&&ln>=0)
  100. ln--;
  101. if(ln>=0&&a[ln]>b.a[ln])
  102. return true;
  103. else
  104. return false;
  105. } else
  106. return false;
  107. }
  108. void output() {
  109. printf("%d",a[len-1]);
  110. for(int i = len-2; i >=0 ; i--)
  111. printf("%04d",a[i]);
  112. printf("\n");
  113. }
  114. };
  115. char s[100005];
  116. char t[100005];
  117. int main() {
  118. #ifdef Yinku
  119. freopen("Yinku.in","r",stdin);
  120. //freopen("Yinku.out","w",stdout);
  121. #endif // Yinku
  122. int l;
  123. scanf("%d%s",&l,s);
  124. int m1=l/2;
  125. int m2=l/2+1;
  126. int h1=m1;
  127. while(s[h1]=='0')
  128. h1--;
  129. int h2=m2;
  130. while(h2<l&&s[h2]=='0')
  131. h2++;
  132. strncpy(t,s,h1);
  133. t[h1]='\0';
  134. BigInt a(t);
  135. strcpy(t,s+h1);
  136. BigInt b(t);
  137. BigInt c=a+b;
  138. strncpy(t,s,h2);
  139. t[h2]='\0';
  140. a=BigInt(t);
  141. strcpy(t,s+h2);
  142. b=BigInt(t);
  143. BigInt c2=a+b;
  144. if(c>c2)
  145. c2.output();
  146. else
  147. c.output();
  148. }

Codeforces - 1181B - Split a Number - 贪心的更多相关文章

  1. Codeforces C. Split a Number(贪心大数运算)

    题目描述: time limit per test 2 seconds memory limit per test 512 megabytes input standard input output ...

  2. 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 ...

  3. 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 ...

  4. Split the Number(思维)

    You are given an integer x. Your task is to split the number x into exactly n strictly positive inte ...

  5. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  6. [Codeforces 1214A]Optimal Currency Exchange(贪心)

    [Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...

  7. B. Split a Number(字符串加法)

    Dima worked all day and wrote down on a long paper strip his favorite number nn consisting of ll dig ...

  8. Codeforces 798D Mike and distribution - 贪心

    Mike has always been thinking about the harshness of social inequality. He's so obsessed with it tha ...

  9. Codeforces 980 E. The Number Games

    \(>Codeforces \space 980 E. The Number Games<\) 题目大意 : 有一棵点数为 \(n\) 的数,第 \(i\) 个点的点权是 \(2^i\) ...

随机推荐

  1. python Tkinter之Button

    Button小部件是一个标准的Tkinter的部件,用于实现各种按钮.按钮可以包含文本或图像,您可以调用Python函数或方法用于每个按钮. Tkinter的按钮被按下时,会自动调用该函数或方法. 该 ...

  2. thinkphp微信开发(消息加密解密)

    使用thinkphp官方的WeChat包,使用不同模式可以成功,但是安全模式就是不行,现将分析解决结果做下记录. 分析问题: 解密微信服务器消息老是不成功,下载下微信公众平台官方给出的解密文件和Wec ...

  3. leetcode 201. Bitwise AND of Numbers Range(位运算,dp)

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  4. I.MX6 FFmpeg 录制视频

    /************************************************************************* * I.MX6 FFmpeg 录制视频 * 说明: ...

  5. Ffmpeg转码研究一

    Ffmpeg是一款功能强大的视频处理工具,那么转码肯定不是问题的,因为项目的需求,对转码进行了研究.刚开始首先去看了ffmpeg源代码中的一个例子transcode.c,但是发现该例子更应该称之为re ...

  6. 左右选择框 js插件

    随着项目的进展,测试工程师在更多的浏览器中兼容性测试中,发现有些浏览器不支持option的触发事件,这就造成了先前一篇博文bootstrap 左右框多项选择示例 中左右选择框的失效,于是我就由原先的s ...

  7. Solaris/Linux 命令手册

    无意翻到之前收藏的一个文档,共享一下. Solaris/Linux 命令手册 1. 系统 # passwd:修改口令 # exit:退出系统 2. 文件 # cp:复制文件或目录,参数:-a递归目录, ...

  8. BZOJ4317: Atm的树+2051+2117

    BZOJ4317: Atm的树+2051+2117 https://lydsy.com/JudgeOnline/problem.php?id=4317 分析: 二分答案之后就变成震波那道题了. 冷静一 ...

  9. nodejs 上传图片(服务端输出全部代码)

    下面代码,全部都是nodejs端的,不用客户端代码.也就是,选择图片的form表单以及上传完毕预览图片的html,都是由node服务端输出的. 1 启动代码:(node upload.js) var ...

  10. [转] jquery操作select(取值,设置选中)

    每一次操作select的时候,总是要出来翻一下资料,不如自己总结一下,以后就翻这里了. 比如<select class="selector"></select&g ...