hdu 1034 (preprocess optimization, property of division to avoid if, decreasing order process) 分类: hdoj 2015-06-16 13:32 39人阅读 评论(0) 收藏
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) 收藏的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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) ...
- 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) ...
- 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 ...
随机推荐
- Android入门:绑定本地服务
一.绑定服务介绍 前面文章中讲过一般的通过startService开启的服务,当访问者关闭时,服务仍然存在: 但是如果存在这样一种情况:访问者需要与服务进行通信,则我们需要将访问者与服务进行绑定: ...
- Android控件之AutoCompleteTextView(自动匹配输入的内容)
一.功能 动态匹配输入的内容,如百度搜索引擎当输入文本时,可以根据内容显示匹配的热门信息 二.独特属性 android:completionThreshold = "2" — ...
- Fragment的2中载入方式!
1.静态有动态 代码如下: public class MainActivity extends AppCompatActivity { private ContentFragment cf; @Ove ...
- Unity5.1 新的网络引擎UNET(二) UNET 官方推荐Demo案例
http://blog.csdn.net/u010019717/article/details/46873153 视频 http://www.iqiyi.com/playlist391685502.h ...
- 调用webservice 417
在调用webservice时有些服务器一直返回“远程服务器返回错误: (417) Expectation failed” 这个提示,解决方案: 在调用代码的最开始加入如下一句:System.Net. ...
- 垂直居中,水平居中html例子
<!DOCTYPE html> <html lang="en"> <head> <title>Vertical Centering ...
- python 练习 23
python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务.其基本形式为: while 判断条件: 执行语句…… 执行语句可以是单个语句或语句 ...
- Python 练习 21
#!/usr/bin/python # -*- coding: UTF-8 -*- h = 0 leap = 1 from math import sqrt from sys import stdou ...
- Ubuntu配置LAMP+MediaWiki及常见问题
/*在实验室觉得文档传来传去太麻烦了,干脆在实验室内部搞个wiki算了,于是网上搜集搜集资料,配了一个,由于时间仓促,mediaWiki比较高级的东西没来的及细看,等以后用的时候再完善吧*/ 环境:U ...
- hdu 1598 find the most comfortable road(枚举+卡鲁斯卡尔最小生成树)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...