主题:第46次编程比赛第一题
tiancai007 [专家分:0] 发布于 2006-10-27 21:31:00
船长哈里正计划着一次太空远行,他准备从他所在的星球前往距离L单位远的另一个星球。现假设在途中每过1个单位的距离便会抵达有一个中间站。由于飞船的油仓装的油有限,所以飞船一次飞行最多只能飞行max单位,然后降落中间站加油。又因为每次起飞的代价非常高,所以每次飞行距离必须大于等于min,然后降落加油。现在因为其中几个中间站附近有海盗出没,所以船长希望尽可能的避免这些有危险的中间站降落。
现给出距离L,min和max,有危险的中间站的位置(起点为0),你的任务就是帮助船长找出最优的方案,即降落在危险中间站的次数最少。
输入文件:
输入文件feichuan.in的第一行有一个正整数L(1 <= L <= 109)。第二行有三个正整数min,max,n,分别表示飞船一次飞行的最小距离,最大距离,及危险中间站的个数,其中1 <= min <= max <= 10,1 <= n <= 100。第三行有n个不同的正整数分别表示这n个危险中间站在数轴上的位置(数据保证的起点和终点处没有危险)。所有相邻的整数之间用一个空格隔开。
输出:
用printf输出一个整数,即经过有危险的中间站自少次数
样例输入
10
2 3 5
2 3 6 7 8
样例输出
2
数据规模
对于30%的数据,L <= 10000;
对于全部的数据,L <= 109(10的9次方)。
回复列表 (共15个回复)
11 楼
红色羽毛 [专家分:30] 发布于 2006-10-29 00:31:00
回复,看帖
12 楼
luojh [专家分:0] 发布于 2006-10-29 00:42:00
好想看下!!
13 楼
zkp83 [专家分:40] 发布于 2006-10-29 05:13:00
帖子看不了??
14 楼
fyaxm [专家分:130] 发布于 2006-10-29 11:00:00
怎么就这样呢?
15 楼
火海时代 [专家分:230] 发布于 2006-10-29 12:42:00
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
char *a;
int L,min,max,n;
FILE *f = fopen("feichuan.in","r");
fscanf(f,"%d\n%d %d %d\n",&L,&min,&max,&n);
if(min == max)
{
int count = 0;
for(int i = 0; i < n; i++)
{
int temp;
fscanf(f,"%d ",&temp);
count += temp % min==0?1:0;
}
printf("%d\n",count);
}
else
{
int *b = (int *)malloc(sizeof(int)*(n));
int temp;
for(int i = 0; i < n; i++)
fscanf(f,"%d ",&b[i]);
for(int i = 0; i < n - 1; i++)
for(int j = 0; j < n - 1 - i; j++)
if(b[j] > b[j+1])
{
temp = b[j];
b[j] = b[j+1];
b[j+1] = temp;
}
if(b[0] > max)
{
temp = b[0]-b[0]%max-max;
for(int i = 0; i < n; i++)
b[i]-= temp;
L -= temp;
}
for(int i = 1; i < n; i++)
if(b[i]-b[i-1] > max)
{
temp = (b[i]-b[i-1])-(b[i]-b[i-1])%max-max;
for(int j = i; j < n; j++)
b[j]-= temp;
L -= temp;
}
a = (char *)malloc(sizeof(char)*(L+1));
memset(a,0,sizeof(char)*(L+1));
for(int i = 0; i < n; i++)
a[b[i]] = 1;
free(b);
for(int i = 1; i < min; i++)
a[i] = -1;
for(int i = min; i <= L; i++)
{
int temp = 9999;
for(int j = min; j <= max; j++)
if(i-j>=0&&a[i-j]!=-1)
{
if(temp > a[i-j]+a[i])
temp = a[i-j]+a[i];
}
if(temp != 9999)
a[i] = temp;
else
a[i] = -1;
}
printf("%d\n",a[L]);
free(a);
}
fclose(f);
getchar();
return 0;
}
我来回复