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

Codeforces Round #436 (Div. 2)

C. Bus

A bus moves along the coordinate line Ox from the point x = 0 to the point x = a. After starting from the point x = 0, it reaches the pointx = a, immediately turns back and then moves to the point x = 0. After returning to the point x = 0 it immediately goes back to the pointx = a and so on. Thus, the bus moves from x = 0 to x = a and back. Moving from the point x = 0 to x = a or from the point x = a tox = 0 is called a bus journey. In total, the bus must make k journeys.

The petrol tank of the bus can hold b liters of gasoline. To pass a single unit of distance the bus needs to spend exactly one liter of gasoline. The bus starts its first journey with a full petrol tank.

There is a gas station in point x = f. This point is between points x = 0 and x = a. There are no other gas stations on the bus route. While passing by a gas station in either direction the bus can stop and completely refuel its tank. Thus, after stopping to refuel the tank will contain b liters of gasoline.

What is the minimum number of times the bus needs to refuel at the point x = f to make k journeys? The first journey starts in the pointx = 0.

input

6 10 2 4

output

2

input

6 5 4 3

output

-1

Note

In the second example the bus can pass 10 units of distance without refueling. So the bus makes the whole first journey, passes 4 units of the distance of the second journey and arrives at the point with the gas station. Then it can refuel its tank, finish the second journey and pass 2 units of distance from the third journey. In this case, it will again arrive at the point with the gas station. Further, he can refill the tank up to 10 liters to finish the third journey and ride all the way of the fourth journey. At the end of the journey the tank will be empty.

In the third example the bus can not make all 3 journeys because if it refuels during the second journey, the tanks will contain only 5 liters of gasoline, but the bus needs to pass 8 units of distance until next refueling.

一句话题意:***好像又不能一句话了。给你四个整数a,b,f,k,

a表示你要开车从0到a,b表示你的油桶的容量为b,f表示可以在坐标为f的位置加油(f<a),k表示你要走k趟(0到a和a到0算两趟)

问完成k次最少加几次油,一开始油桶满

其实这道题并没什么思维含量,顶多是比较多的坑点吧,但是又因为pp的数据超强,导致并没有很多人fst

总的说就是暴力枚举状态,我是枚举汽车到0或a时的状态,根据当前油量,判断是否要在此次途中加油

注意:(1)如果第一次都到达不到加油点,输出-1

(2)如果加过了都大不了对面,输出-1

(3)在处理0,a的时候要千万小心

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int a,b,k,f;
 4 void print() {puts("-1"); exit(0);}
 5 int main(){
 6     scanf("%d%d%d%d",&a,&b,&f,&k);
 7     int res=b,i=0,pos=0,ans=0;
 8     while (i<k-1){
 9         if (res<abs(pos-f)) print; i++;
10         if (res<(a*2-abs(pos-f))){
11             ans++; if (i%2==1) res=b-(a-f); else res=b-f;
12         } else res-=a;
13         if (res<0) print();
14         if (i%2==1) pos=a; else pos=0;
15     }
16     if (res<abs(pos-f)) print(); if (pos==0) pos=a; else pos=0;
17     if (b<abs(pos-f)) print(); if (res<a) ans++;
18     printf("%d\n",ans);
19 }

时间: 2024-09-27 20:30:08

Codeforces Round #436 (Div. 2) C. Bus的相关文章

Codeforces Round #436 (Div. 2)【A、B、C、D、E】

Codeforces Round #436 (Div. 2) 敲出一身冷汗...感觉自己宛如智障:( codeforces 864 A. Fair Game[水] 题意:已知n为偶数,有n张卡片,每张卡片上都写有一个数,两个人每人选一个数,每人可以拿的卡片必须写有是自己选的数,问能否选择两个数使得两个人每人拿的卡片数一样多并且能拿光卡片.[就是看输入是不是只有两种数字] //:第一遍我看成字符串包含有选的数字也能拿,,这样写着居然过了..水题水题.. 1 #include<cstdio> 2

Codeforces Round #484 (Div. 2) B. Bus of Characters(markdowm版)

Codeforces Round #484 (Div. 2) B. Bus of Characters B. Bus of Characters time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output In the Bus of Characters there are nn rows of seat, each having 22

Codeforces Round #436 (Div. 2) F Cities Excursions

题意是给你一个有向图,点n <= 3000, 边m <= 3000,从s到t的路径必须是最小字典序,q<=400000次询问,从s到t中路径第k个点是什么,否则输出-1. 7 7 51 22 31 33 44 55 34 61 4 22 6 11 7 31 3 21 3 5 解释下样例2-6. 2-6的路径为2-3-4-5-3-5-3...-5-6无限循环,所以可以当作这个路径不存在,所以为-1 1-3的路径为1-2-3 如果在线做复杂度肯定要高,把询问的边存储,枚举出发点离线解决 用v

Codeforces Round #481 (Div. 3) E. Bus Video System

E. Bus Video System Example 1 input 3 5 2 1 -3 output 3 Example 2 input 2 4 -1 1 output 4 Example 3 input 4 10 2 4 1 2 output 2 题目大意: 车上只会显示y-x(y是离开站时的人数,x时到站前的人数),问第一站的人数可以有几种 分析: 我们先分析下这个数学问题的式子,设a[i]=y[i]-x[i]--① x[i+1]=y[i],这是显然易见的 然后我们将①从1到n项累加

Codeforces Round #491 (Div. 2) E - Bus Number + 反思

E - Bus Number 最近感觉打CF各种车祸.....感觉要反思一下, 上次读错题,这次想当然地以为18!肯定暴了longlong 而没有去实践, 这个题我看到就感觉是枚举每个数字的个数,但是我觉得算得时候会爆longlong 就想用大数,但是我去看别人交的全部都是C++,就感觉是不是有别的方法, 想了半天感觉时间来不及了就强行上了个java,结果时间来不及... 以后写题首先要读清楚题目,其次不要想当然,要去实践!!!!!!!!!!! 真的很烦. import java.math.Bi

【贪心】Codeforces Round #436 (Div. 2) D. Make a Permutation!

题意:给你一个长度为n的数组,每个元素都在1~n之间,要你改变最少的元素,使得它变成一个1~n的排列.在保证改动最少的基础上,要求字典序最小. 预处理cnt数组,cnt[i]代表i在原序列中出现的次数.b数组,代表没有出现过的数是哪些.b数组的长度就是答案. b数组是从小到大排好的,然后for循环b数组,同时用一个指针p指着a数组的当前位置,最开始指向开头,如果cnt[a[p]]==1,就向后跳,否则再看 是否b[i]<a[p]或者a[p]这个数是否已经出现过了(用个hav数组表示a[p]是否已

[Codeforces] Round #436 (Div. 2)

1 #include<cstdio> 2 #include<iostream> 3 using namespace std; 4 5 int n,cnt,ans,A,B; 6 int buck[500]; 7 8 int main(){ 9 scanf("%d",&n); 10 11 for(int i = 1;i <= n;i++){ 12 cin >> cnt; 13 if(!buck[cnt]){ 14 ans++; 15 if(

Codeforces Round #436 (Div. 2) B.Polycarp and Letters

因为难得又一次CF的比赛是非常清真的傍晚,超级少见啊 所以当然要打啦,于是rank:87,rating+=76,滞留在上紫的边缘 下面把几道觉得还不错的题目来总结一下 B.Polycarp and Letters Polycarp loves lowercase letters and dislikes uppercase ones. Once he got a string s consisting only of lowercase and uppercase Latin letters.

Codeforces Round #436 (Div. 2) E. Fire

题意:给你n个需要救得东西,每个东西给出t,d,p,表示需要花费t时间,在d时间之前,价值为p,问救出最多价值,并把每个东西序号输出,比如  3  3  4 ,这就无法救出 思路:dp,dp[i][j]表示救出第i个花费j时间救出最大价值,dp[i][j]=max(dp[i][j],dp[i-1][j-a[i][d]]+a[i].val)(j<=2000),再记录个g[i][j]表示第i个东西在j时间是救出来的,然后倒推 1 #include<bits/stdc++.h> 2 using