https://vjudge.net/problem/CodeForces-864C

题意:两地之间有个加油站,往返走k个单程,最少加油多少次。

大佬几十行代码就解决,我却要用一百多行的if语句模拟解决。

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<math.h>
  6. #include<string>
  7. #include<queue>
  8. #define ll long long
  9. #define inf 0x3f3f3f3f
  10. using namespace std;
  11.  
  12. ll a,b,f,k;
  13. ll one,two;///加油站左边路程为one,右边路程为two
  14. ll t;
  15. ll nowb;
  16. ll sum;
  17.  
  18. int main()
  19. {
  20. while(scanf("%lld %lld %lld %lld",&a,&b,&f,&k)!=EOF)
  21. {
  22. one=f;
  23. two=a-f;
  24. nowb=b; ///当前体力
  25. sum=a*k; ///总路程
  26. t=; ///补充体力次数
  27. ll i; ///当前走了多少路程
  28. bool flag=true; ///能否走到
  29. int now=; ///now表示方向,1为右,-1为左
  30.  
  31. for(i=;i<sum;)///不加等于,不要i++
  32. {
  33. if(i==)///刚出发
  34. {
  35. if(nowb>=one)///能走到加油站
  36. {
  37. i+=one;
  38. nowb-=one;
  39. }
  40. else///否则直接凉
  41. {
  42. flag=false;
  43. break;
  44. }
  45. }
  46. else if( k% && i==sum-two)///奇数次,最后向右,最后要走two这一段
  47. {
  48. if(nowb>=two)///走得到
  49. {
  50. i+=two;
  51. nowb-=two;
  52. }
  53. else///走不到,加完再看能不能到
  54. {
  55. nowb=b;
  56. t++;
  57. if(nowb>=two)
  58. {
  59. i+=two;
  60. nowb-=two;
  61. }
  62. else
  63. {
  64. flag=false;
  65. break;
  66. }
  67. }
  68. }
  69. else if( k%== && i==sum-one)///偶数次,最后向左,最后要走one这一段
  70. {
  71. if(nowb>=one)
  72. {
  73. i+=one;
  74. nowb-=one;
  75. }
  76. else
  77. {
  78. nowb=b;
  79. t++;
  80. if(nowb>=one)
  81. {
  82. i+=one;
  83. nowb-=one;
  84. }
  85. else
  86. {
  87. flag=false;
  88. break;
  89. }
  90. }
  91. }
  92. else ///中间跑路,走两段one或者two
  93. {
  94. if(now==)///往右跑
  95. {
  96. if(nowb>=*two)///跑得到,减油,改方向
  97. {
  98. i+=*two;
  99. nowb-=*two;
  100. now=-;
  101. }
  102. else ///否则,加油
  103. {
  104. t++;
  105. nowb=b;
  106. if(nowb>=*two)///加完看能不能跑到
  107. {
  108. i+=*two;
  109. nowb-=*two;
  110. now=-;
  111. }
  112. else
  113. {
  114. flag=false;
  115. break;
  116. }
  117. }
  118. }
  119. else ///往左跑
  120. {
  121. if(nowb>=*one)
  122. {
  123. i+=*one;
  124. nowb-=*one;
  125. now=;
  126. }
  127. else ///否则,加油
  128. {
  129. t++;
  130. nowb=b;
  131. if(nowb>=*one)///加完看能不能跑到
  132. {
  133. i+=*one;
  134. nowb-=*one;
  135. now=;
  136. }
  137. else
  138. {
  139. flag=false;
  140. break;
  141. }
  142. }
  143. }
  144.  
  145. }
  146. }
  147. if(flag)
  148. printf("%d\n",t);
  149. else
  150. printf("-1\n");
  151. }
  152. return ;
  153. }

CodeForces - 864C-Bus-(模拟加油站问题)的更多相关文章

  1. [Codeforces 864C]Bus

    Description A bus moves along the coordinate line Ox from the point x = 0 to the point x = a. After ...

  2. Codeforces Round #436 (Div. 2)C. Bus 模拟

    C. Bus time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard input out ...

  3. Educational Codeforces Round 11B. Seating On Bus 模拟

    地址:http://codeforces.com/contest/660/problem/B 题目: B. Seating On Bus time limit per test 1 second me ...

  4. 【模拟】Codeforces 711A Bus to Udayland

    题目链接: http://codeforces.com/problemset/problem/711/A 题目大意: N个字符串,每个字符串5位,找到第一个出现两个OO的并改成++输出YES和改后字符 ...

  5. codeforces 660B B. Seating On Bus(模拟)

    题目链接: B. Seating On Bus time limit per test 1 second memory limit per test 256 megabytes input stand ...

  6. Codeforces 389B(十字模拟)

    Fox and Cross Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submi ...

  7. codeforces 591B Rebranding (模拟)

    Rebranding Problem Description The name of one small but proud corporation consists of n lowercase E ...

  8. Codeforces 626B Cards(模拟+规律)

    B. Cards time limit per test:2 seconds memory limit per test:256 megabytes input:standard input outp ...

  9. Codeforces 631C. Report 模拟

    C. Report time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...

随机推荐

  1. 多线程shell脚本检测主机存活

    局域网中分了很多网段,而IP地址使用情况也未知,前期也没有规划和记录,当新的主机需要使用固定IP的时候,能否第一时间知道哪个IP空闲就显得很重要了,如果一个一个去ping的话太浪费时间. 这里分享一个 ...

  2. php使用insert语句动态添加用户

    <html> <head> <title>Adding User</title> </head> <body> <h2&g ...

  3. Centos7 kernel 内核升级 GPU显卡驱动程序编译安装

    1.NVIDIA官网下载相关显卡驱动 #在服务器上查看网卡型号 lspci -mm | grep NVIDIA   #在NVIDIA官网下载相应型号驱动程序 https://www.geforce.c ...

  4. JVM-垃圾收集算法

    新生代:复制算法,IBM研究过,约98%对象是“朝生夕死”,所以不需要按照1:1来划分:HotSpot中默认是8:1:1,即每次新生代可用空间为90%,只有10%会被“浪费”, 当Survivor空间 ...

  5. SpringBoot启动源码探究---listeners.starting()

    1.首先调用starting()方法,其内部是一个对所有listener的starting()调用的for循环,然后每个listener调用另一个starting方法,其内部调用multicastEv ...

  6. [蓝桥杯]PREV-25.历届试题_城市建设

    问题描述 栋栋居住在一个繁华的C市中,然而,这个城市的道路大都年久失修.市长准备重新修一些路以方便市民,于是找到了栋栋,希望栋栋能帮助他. C市中有n个比较重要的地点,市长希望这些地点重点被考虑.现在 ...

  7. C# 6.0:Null – Conditional 操作符

    在引入nameof操作符的同时,C# 6.0 还引入了Null-Conditional操作符.它使开发者可以检查object引用链中的null值.这个null-conditional 操作符写作&qu ...

  8. 你云我云•兄弟夜谈会 第二季 5G

    0. 概况 时间:2019年1月29日 21:30~23:15 兄弟团:金孝(主持人).肖力.楼炜.张亮.孙杰.熊.世民 主题:5G 1. 5G超简单科普 金孝首先对大家做了超简单5G科普.5G 是第 ...

  9. C++primer第一章(部分)

    1.4.2 for 语句 for (init-statement; condition; expression) statement; step1:初始化 step2:判断条件,为真则执行循体:为假则 ...

  10. 2018/12/20 20:52:42 螺纹钢PTA豆粕

    如期向上,但是一点办法没有:没有好的入场位,不做不算错,面对诱惑不动如山也是一种修养,今晚看M5有没有3买,有的话可以看情况考虑要不要进场 PTA M30向下一笔过程中,等待M30当前一笔下跌结束,可 ...