Problem Description 
Ruins is driving a car to participating in a programming contest. As on a very tight schedule, he will drive the car without any slow down, so the speed of the car is non-decrease real number.

Of course, his speeding caught the attention of the traffic police. Police record N positions of Ruins without time mark, the only thing they know is every position is recorded at an integer time point and Ruins started at 0.

Now they want to know the minimum time that Ruins used to pass the last position.

Input 
First line contains an integer T, which indicates the number of test cases.

Every test case begins with an integers N, which is the number of the recorded positions.

The second line contains N numbers a1, a2, ⋯, aN, indicating the recorded positions.

Limits 
1≤T≤100 
1≤N≤105 
0< ai≤109 
ai< ai+1

Output 
For every test case, you should output ‘Case #x: y’, where x indicates the case number and counts from 1 and y is the minimum time.

Sample Input



6 11 21

Sample Output

Case #1: 4

【题意】:给一些距离点,要求速度(real number)非递减,求从0开始到最后一点的最少时间。eg:(6)6(5)11(10)21  ,  要时间最少,最后一段为1s,中段5/5=1,最前为6/3=2(因为保持速度非递减)。1+1+2=4。

【分析】:贪心。从后往前推,因为没有给出对速度的限制,最后一段时间最小必定为1s,通过已经预设的1s可以求得最后一段的速度。又要保持速度非递减,可以枚举时间1s,2s,3s···,直到速度小于等于后一段的就可以。时间是个整数,但是速度不一定是个整数。于是精度问题要格外小心。

【代码】:

#include<string.h>
#include<cstdio>
#include<iostream>
#define maxn 100005
int main()
{
int t;
int n,sum;
int a[maxn];
scanf("%d",&t);
for(int cas=;cas<=t;cas++)
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
double v=(a[n]-a[n-]);
sum=;
for(int i=n-;i>=;i--)
{
for(int j=(a[i]-a[i-])/v; ;j++)
{
double y=(a[i]-a[i-])*1.0/j;
if(y<=v)
{
sum+=j;
v=y;
break;
}
}
}
printf("Case #%d: %d\n",cas,sum);
}
return ;
}

贪心,逆向

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define eps 1e-8
int s[];
int main()
{
int t,n,x,ans,tt=;
double v;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
ans=;
for(int i=;i<=n;i++)
scanf("%d",&s[i]);
v=s[n]-s[n-];
for(int i=n-;i;i--)
{
x=s[i]-s[i-];
if(x<=v+eps)
{
ans++;
v=x;
}
else
{
ans+=(int(double(x-eps)/v)+);
v=double(x)/(int(double(x-eps)/v)+);
}
}
printf("Case #%d: %d\n",tt++,ans);
}
return ;
}

别人家的

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
const int maxn = 1e5+;
int a[maxn], t, n, ca = ; int main(void)
{
cin >> t;
while(t--)
{
scanf("%d", &n);
for(int i = ; i <= n; i++)
scanf("%d", &a[i]);
ll ans = ;
double spe = a[n]-a[n-];
for(int i = n; i > ; i--)
{
double len = (a[i]-a[i-])*1.0;
int t = len/spe;
ans += t;
if(len/t != spe)
{
ans++;
spe = len/(t+);
}
}
printf("Case #%d: %d\n", ca++, ans);
}
return ;
}

别人家的*2

*********************************************别人的想法:http://blog.csdn.net/queuelovestack/article/details/52984042

由于车速是非递减的,那么从第N-1个位置到第N个位置最优的用时为1

对于相邻的两段,我们有

稍微做个转化,可得

即相同时间间隔内,后者所驶过的路程要大于等于前者

已知最后一段行驶时间间隔为1,行驶路程为a[n]-a[n-1]

倒数第二段行驶路程为a[n-1]-a[n-2]

则倒数第二段的行驶时间间隔为

得到倒数第二项之后可以由倒数第二项推出倒数第三项

反向遍历一遍即可

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<bitset>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = ;
const int M = ;
const int inf = ;
const int mod = ;
int s[N];
int main()
{
int t,n,i,j,p=;
__int64 ans,a,b,c;
scanf("%d",&t);
while(t--)
{
ans=;
scanf("%d",&n);
for(i=;i<=n;i++)
scanf("%d",&s[i]);
a=s[n]-s[n-];b=;
for(i=n;i>=;i--)
{
c=s[i]-s[i-];
b=(b*c+a-)/a;
a=c;
ans+=b;
}
printf("Case #%d: %I64d\n",p++,ans);
}
return ;
}

别人家的*3

HDU 5935 Car【贪心,枚举,精度】的更多相关文章

  1. Hdu 4864(Task 贪心)(Java实现)

    Hdu 4864(Task 贪心) 原题链接 题意:给定n台机器和m个任务,任务和机器都有工作时间值和工作等级值,一个机器只能执行一个任务,且执行任务的条件位机器的两个值都大于等于任务的值,每完成一个 ...

  2. POJ 1018 Communication System 贪心+枚举

    看题传送门:http://poj.org/problem?id=1018 题目大意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m个厂家提供生产,而每个厂家生产的同种设备都 ...

  3. D - 淡黄的长裙 HDU - 4221(贪心)

    D - 淡黄的长裙 HDU - 4221(贪心) James is almost mad! Currently, he was assigned a lot of works to do, so ma ...

  4. HDU 5303 Delicious Apples (贪心 枚举 好题)

    Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

  5. hdu 4091 Zombie’s Treasure Chest 贪心+枚举

    转自:http://blog.csdn.net/a601025382s/article/details/12308193 题意: 输入背包体积n,绿宝石体积s1,价值v1,蓝宝石体积s2,价值v2,宝 ...

  6. Delicious Apples (hdu 5303 贪心+枚举)

    Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

  7. HDU 5303 Delicious Apples (2015多校第二场 贪心 + 枚举)

    Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

  8. HDU 5353—— Average——————【贪心+枚举】

    Average Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total S ...

  9. HDU 4803 Poor Warehouse Keeper (贪心+避开精度)

    555555,能避开精度还是避开精度吧,,,,我们是弱菜.. Poor Warehouse Keeper Time Limit: 2000/1000 MS (Java/Others)    Memor ...

随机推荐

  1. Hadoop上配置Hbase数据库

    已有环境: 1. Ubuntu:14.04.2 2.jdk: 1.8.0_45 3.hadoop:2.6.0 4.hBase:1.0.0 详细过程: 1.下载最新的Hbase,这里我下载的是hbase ...

  2. ACM 竞赛高校联盟 练习赛 第二场 B&C

    B. 题解: 枚举约数即可,判断n个数能否填约数的整数倍 #include <iostream> #include <cstring> #include <cstdio& ...

  3. [Leetcode] Length of last word 最后一个单词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters' ', return the le ...

  4. BZOJ2818 GCD 【莫比乌斯反演】

    2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MB Submit: 6826 Solved: 3013 [Submit][Status][Discuss ...

  5. 安徽师大附中%你赛day7 T2 乘积 解题报告

    乘积 题目背景 \(\mathrm{Smart}\) 最近在潜心研究数学, 他发现了一类很有趣的数字, 叫做无平方因子数. 也就是这一类数字不能够被任意一个质数的平方整除, 比如\(6\).\(7\) ...

  6. 设计一个JavaScript框架需要编写哪些模块

    在这个js框架随处乱跑的时代,你是否考虑过写一个自己的框架?下面的内容也许会有点帮助. 一个框架应该包含哪些内容? 1. 语言扩展 大部分现有的框架都提供了这部分内容,语言扩展应当是以ECMAScri ...

  7. poj 1523 割点 tarjan

    Description Consider the two networks shown below. Assuming that data moves around these networks on ...

  8. 旋转数组 [ LeetCode ]

    原题地址:https://leetcode-cn.com/problems/rotate-array/description/ 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. ...

  9. hive连接数

    使用hive分析日志作业很多的时候,需要修改mysql的默认连接数 修改方法   打开/etc/my.cnf文件 在[mysqld]  中添加 max_connections=1000 重启mysql ...

  10. IDEA2017 使用(二)

    1.鼠标悬浮在方法上显示api 2.关闭拼写检查 3.自动导入包(存在多个包时需要手动导入) 4.设置方法线