hdu 1034 (preprocess optimization, property of division to avoid if, decreasing order process)
2015-06-16 13:32
609 查看
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
use division’s floor property (5/2=2) to avoid if sentences. in version 2 here
if don’t divide input by 2, the code will be
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
version 3
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
mistakely put it to declaration part rather than just befor the for loop;
2nd, the third parameter of printf
mistakely wirite
which should be
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
[code]#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 ; 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
[code]t2=candies[i]; candies[i]=(t1+t2+1)>>1; t1=t2;
if don’t divide input by 2, the code will be
[code]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
[code]#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 ; 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
[code]#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 +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
[code]cnt_whistle=1;
mistakely put it to declaration part rather than just befor the for loop;
2nd, the third parameter of printf
mistakely wirite
[code]printf("%d %d\n",cnt_whistle,candies[0]<<1);
which should be
[code]printf("%d %d\n",cnt_whistle,p[0]<<1);
相关文章推荐
- Linux之mailx的使用
- Linux之mailx的使用
- Linux IPC总结(全)
- Linux 与 Linux Windows 文件共享 小知识
- 使用coding、daocloud和docker打造markdown纯静态博客
- Hadoop之——SQOOP笔记
- Linux/ubuntu目录解释
- Linux下使用Google Authenticator配置SSH登录动态验证码
- x64 linux c 调用约定
- 将Linux命令的结果作为下一个命令的参数[转]
- Looper类mm
- Linux Error: curses.h: No such file or directory 没有找到curses.h 头文件
- [系统维护] ubuntu 14.04 LTS X64,自带无线网卡 RTL8188EE 驱动不稳定问题
- ap(affinity propagation)聚类算法
- Linux共享内存使用常见陷阱与分析
- 如何在 Apache 中抵御暴力破解和 DDoS 攻击
- opencv cvHoughCircles() 霍夫圆变换
- hdu 1035 (usage of sentinel, proper utilization of switch and goto to make code neat)
- Tomcat 创建虚拟目录
- 基于opencv读取两个摄像头信息