IMO, version 1 better than version 2, version 2 better than version 3.

make some preprocess to make you code simple and efficient. Here divide the input by 2, so you don’t have to do dividsion on each loop.

version 1 is best

thanks to

http://www.cnblogs.com/kuangbin/archive/2012/06/03/2532690.html

#include <cstdio>
#include <algorithm> #define MAXSIZE 10000 int candies[MAXSIZE]; int main() {
//freopen("input.txt","r",stdin);
int n, cnt_whistle, i, tmp;
while(scanf("%d",&n)!=EOF && n>0) {
for(i=0;i<n;++i) { scanf("%d",&tmp); candies[i]=tmp>>1; }
for(--n, cnt_whistle=1;;++cnt_whistle) {
tmp=candies[n];
for(i=n;i>0;--i) {
candies[i]=(candies[i-1]+candies[i]+1)>>1;
}
candies[0]=(tmp+candies[0]+1)>>1;
for(i=1;i<=n && candies[i-1]==candies[i];++i) ;
if(i-n==1) break;
}
printf("%d %d\n",cnt_whistle,candies[0]<<1);
}
return 0;
}

use division’s floor property (5/2=2) to avoid if sentences. in version 2 here

t2=candies[i];
candies[i]=(t1+t2+1)>>1;
t1=t2;

if don’t divide input by 2, the code will be

t2=candies[i]>>1;
candies[i]=t1+t2;
if(candies[i]%2) ++candies[i];
t1=t2;

how to avoid process the border conditions? version 2 better than version 3 on this aspect. version 1 accomplish this, thanks to

http://www.acmerblog.com/hdu-1034-candy-sharing-game-1285.html

version 2

#include <cstdio>
#include <algorithm> #define MAXSIZE 10000 int candies[MAXSIZE]; int main() {
//freopen("input.txt","r",stdin);
int n, cnt_whistle, i, t1,t2;
while(scanf("%d",&n)!=EOF && n>0) {
for(i=0;i<n;++i) { scanf("%d",&t1); candies[i]=t1>>1; }
for(--n, cnt_whistle=1;;++cnt_whistle) {
t1=candies[n];
for(i=0;i<=n;++i) {
t2=candies[i];
candies[i]=(t1+candies[i]+1)>>1;
t1=t2;
}
for(i=1;i<=n && candies[i-1]==candies[i];++i) ;
if(i-n==1) break;
}
printf("%d %d\n",cnt_whistle,candies[0]<<1);
}
return 0;
}

version 3

#include <cstdio>
#include <algorithm> #define MAXSIZE 10000 int candies[MAXSIZE];
int cand_temp[MAXSIZE]; int main() {
//freopen("input.txt","r",stdin);
int n, cnt_whistle, i, *p,*q;
while(scanf("%d",&n)!=EOF && n>0) {
for(i=0;i<n;++i) { scanf("%d",&t1); candies[i]=t1>>1; }
p=candies, q=cand_temp;
for(--n, cnt_whistle=1;;++cnt_whistle) {
for(i=1;i<=n;++i) {
q[i]=(p[i-1]+p[i]+1)>>1;
}
q[0]=(p[n]+p[0]+1)>>1;
std::swap(p,q);
for(i=1;i<=n && p[i-1]==p[i];++i) ;
if(i-n==1) break;
}
printf("%d %d\n",cnt_whistle,p[0]<<1);
}
return 0;
}

p.s. when you use swap tricks (as in version 3), be careful with the check points, e.g. the initialization part, the conclusion part, especially the items are used by multiple times.

here is mistake I made, 1st, initilization error

position of

cnt_whistle=1;

mistakely put it to declaration part rather than just befor the for loop;

2nd, the third parameter of printf

mistakely wirite

printf("%d %d\n",cnt_whistle,candies[0]<<1);

which should be

printf("%d %d\n",cnt_whistle,p[0]<<1);

版权声明:本文为博主原创文章,未经博主允许不得转载。// p.s. If in any way improment can be achieved, better performance or whatever, it will be well-appreciated to let me know, thanks in advance.

hdu 1034 (preprocess optimization, property of division to avoid if, decreasing order process) 分类: hdoj 2015-06-16 13:32 39人阅读 评论(0) 收藏的更多相关文章

  1. Hdu 1507 Uncle Tom's Inherited Land* 分类: Brush Mode 2014-07-30 09:28 112人阅读 评论(0) 收藏

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  2. hdu1171 Big Event in HDU(01背包) 2016-05-28 16:32 75人阅读 评论(0) 收藏

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  3. hdu, KMP algorithm, linear string search algorithm, a nice reference provided 分类: hdoj 2015-07-18 13:40 144人阅读 评论(0) 收藏

    reference: Rabin-Karp and Knuth-Morris-Pratt Algorithms By TheLlama– TopCoder Member https://www.top ...

  4. hdu 1712, multiple-choice knapsack, 分类: hdoj 2015-07-18 13:25 152人阅读 评论(0) 收藏

    reference: 6.4 knapsack in Algorithms(算法概论), Sanjoy Dasgupta University of California, San Diego Chr ...

  5. hdu 1047 (big integer sum, fgets or scanf, make you func return useful infos) 分类: hdoj 2015-06-18 08:21 39人阅读 评论(0) 收藏

    errors made, boundary conditions, <= vs < , decreasing vs increasing , ++, –, '0'/'1' vs 0/1 p ...

  6. Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏

    胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Subm ...

  7. Hdu 1009 FatMouse' Trade 分类: Translation Mode 2014-08-04 14:07 74人阅读 评论(0) 收藏

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. hdu 1232, disjoint set, linked list vs. rooted tree, a minor but substantial optimization for path c 分类: hdoj 2015-07-16 17:13 116人阅读 评论(0) 收藏

    three version are provided. disjoint set, linked list version with weighted-union heuristic, rooted ...

随机推荐

  1. 加速Eclipse使其成为超快的IDE

    按照下述步骤来加速Eclipse为超快的IDE,它适用于32和64位版本的Eclipse /JDK(OS为64位Windows 7). 1.禁用防病毒软件,或将JDK.Eclipse.workspac ...

  2. C语言 常用单词

    main 主要的      printf(print  format)格式输出     include  ,    return   ,if   ,else  ,switch   ,case 机箱:案 ...

  3. hiho_1051_补提交卡

    题目大意 给出1到100这100个数中的某些数字(各个数字不同),这些数字形成一个个间断的连续区间,向1-100中添加M个数字,使得添加后1-100中某连续区间的长度最大,求出添加M个数字后,最长的连 ...

  4. 利用ADO.NET导出大批量数据

    2015年12月,XX项目中需要做一个数据导出功能,当时所有页面的到处功能均已经实现,但有个页面数据量太大,导出过程中导出页面直接卡死.不得已我准备选用ADO.NET来重新完成这个功能,因为考虑到越偏 ...

  5. K-均值聚类及其在生物信息中的应用

    如果一点基础没有最好先拿起一本教材开始学,<机器学习实战>还不错,P93,书上有python源码和练习数据,非常适合新手. k均值聚类 名词:簇:相似度算法 伪代码 创建K个点作为起始质心 ...

  6. Linux 命令速查

    学生信,Linux是最最基本的技能,要尽量将自己的工作平台转移到Linux,编程写脚本,这样会极大的提升工作效率,找工作时也不会太怂.Linux所有的任务都是通过命令来完成的,具有高度的统一性.Lin ...

  7. Structs1 -配置例子(转)

    转自:(http://blog.csdn.net/xys_777/article/details/7542095) Action, ActionForm, ActionForward ,这三个对象构成 ...

  8. JQUERY学习(贰)

    一.操作一组jQuery对象 1.遍历 $("").each(fun); 2.求长度:length   size() 3.获得某一个jQuery对象 $("") ...

  9. DES加密算法

    DES全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法,1976年被美国联邦政府的国家标准局确定为联邦资料处理标准(FIPS),随后在国际上广泛流传开 ...

  10. poj---(2886)Who Gets the Most Candies?(线段树+数论)

    Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 10373   Acc ...