题目:
Expedition
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16238 | Accepted: 4623 |
Description
A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to run over a rock and puncture the truck's fuel tank. The truck now leaks one unit of fuel every unit of distance it travels.
To repair the truck, the cows need to drive to the nearest town (no more than 1,000,000 units distant) down a long, winding road. On this road, between the town and the current location of the truck, there are N (1 <= N <= 10,000) fuel stops where the cows can stop to acquire additional fuel (1..100 units at each stop).
The jungle is a dangerous place for humans and is especially dangerous for cows. Therefore, the cows want to make the minimum possible number of stops for fuel on the way to the town. Fortunately, the capacity of the fuel tank on their truck is so large that there is effectively no limit to the amount of fuel it can hold. The truck is currently L units away from the town and has P units of fuel (1 <= P <= 1,000,000).
Determine the minimum number of stops needed to reach the town, or if the cows cannot reach the town at all.
To repair the truck, the cows need to drive to the nearest town (no more than 1,000,000 units distant) down a long, winding road. On this road, between the town and the current location of the truck, there are N (1 <= N <= 10,000) fuel stops where the cows can stop to acquire additional fuel (1..100 units at each stop).
The jungle is a dangerous place for humans and is especially dangerous for cows. Therefore, the cows want to make the minimum possible number of stops for fuel on the way to the town. Fortunately, the capacity of the fuel tank on their truck is so large that there is effectively no limit to the amount of fuel it can hold. The truck is currently L units away from the town and has P units of fuel (1 <= P <= 1,000,000).
Determine the minimum number of stops needed to reach the town, or if the cows cannot reach the town at all.
Input
* Line 1: A single integer, N
* Lines 2..N+1: Each line contains two space-separated integers describing a fuel stop: The first integer is the distance from the town to the stop; the second is the amount of fuel available at that stop.
* Line N+2: Two space-separated integers, L and P
* Lines 2..N+1: Each line contains two space-separated integers describing a fuel stop: The first integer is the distance from the town to the stop; the second is the amount of fuel available at that stop.
* Line N+2: Two space-separated integers, L and P
Output
* Line 1: A single integer giving the minimum number of fuel stops necessary to reach the town. If it is not possible to reach the town, output -1.
Sample Input
4 4 4 5 2 11 5 15 10 25 10
Sample Output
2
用的C语言 代码AC情况:
代码1 C:
# include <stdio.h>
# define MAX 1000000
void insert(int Tree[],int X);//插入元素X
int Delmax(int Tree[]);//删除堆最大值
void paixu(int A[][MAX],int left,int right);//快速排序
void change(int *A,int *B) //交换函数
{
int C=*A;
*A=*B;
*B=C;
}
int Q[MAX+1],WW[2][MAX];//数组Q表示最大堆 Q[0]用来存储当前堆存取的数的数量 Q[1]开始存数
long long sum;
int main(){
int L,P,N;
int i,local=0,oil=0,dis;//sum为加油的次数 local为当前的位置 oil为油箱中汽油的量
// freopen("qwe.txt","r",stdin);
scanf("%d",&N);
for(i=0;i<N;i++)
scanf("%d %d",&WW[0][i],&WW[1][i]);
scanf("%d %d ",&L,&P);
for(i=0;i<N;i++)
WW[0][i]=L-WW[0][i];
oil=P; WW[0][N]=L; //为了方便把终点当作加油量为0的加油站
paixu(WW,0,N);
for(i=0;i<=N;i++)
{
dis=WW[0][i]-local;//接下来要行进的距离
while(oil<dis)//当剩余的油量小于接下来要行驶的距离
{
if(!Q[0])//如果队列空
{
printf("-1\n");
return 0;
}
oil+=Delmax(Q);//油量加上最大值
sum++;
}
oil-=dis;//油量减去当前行驶的距离
local=WW[0][i];//地点到达A[i]
insert(Q,WW[1][i]);
}
printf("%lld\n",sum);//因为终点当作了一个加油站 所以sum要减一
return 0;
}
void insert(int Tree[],int X)
{
int par,i=++Tree[0]; //插入X 后 Tree[0]+1
while(i>1) //直到i不是根节点
{
par=(i>>1); //父节点为par
if(Tree[par]>=X) break;//如果父节点满足最大堆的特性 则插入当前的位置即可
Tree[i]=Tree[par]; //否则调整堆 即位置上移
i=par;
}
Tree[i]=X;//插入新节点
}
int Delmax(int Tree[])
{
int i=1,root=Tree[1],R,L,X=Tree[Tree[0]--];//X记录当前末尾节点 root为根 即为最大值
while(i*2<=Tree[0])
{
L=i*2;R=L+1;//Left Right 记录左右节点
if(R<=Tree[0]&&Tree[R]>Tree[L])//比较两个子节点的值的大小
L=R;
if(Tree[L]<=X) break;//如果所有的位置已经调整好 跳出循环
Tree[i]=Tree[L];//否则继续调整堆
i=L;
}
Tree[i]=X;
return root;
}
void paixu(int A[][MAX],int left,int right)
{
int i=left,j=right,temp=A[0][left];
if(left>=right) return;
while(i!=j)
{
while(A[0][j]>=temp && i<j)j--;
while(A[0][i]<=temp && i<j)i++;
if(i<j)
{
change(&A[0][i],&A[0][j]);
change(&A[1][i],&A[1][j]);
}
}
change(&A[0][left],&A[0][i]);
change(&A[1][left],&A[1][i]);
paixu(A,left,i-1);
paixu(A,i+1,right);
}
代码2 C:
# include <stdio.h>
# define MAX 1000000
int C,Q[MAX+1],WW[2][MAX];//数组Q表示最大堆 Q[0]用来存储当前堆存取的数的数量 Q[1]开始存数
# define change(A,B) {C=*A;*A=*B;*B=C;}
int N,i,oil,local,sum;//sum为加油的次数 local为当前的位置 oil为油箱中汽油的量
void insert(int Tree[],int X)
{
int par,i=++Tree[0]; //插入X 后 Tree[0]+1
while(i>>1) //直到i不是根节点
{
par=(i>>1); //父节点为par
if(Tree[par]>=X) break;//如果父节点满足最大堆的特性 则插入当前的位置即可
Tree[i]=Tree[par]; //否则调整堆 即位置上移
i=par;
}
Tree[i]=X;//插入新节点
}
int Delmax(int Tree[])
{
int i=1,root=Tree[1],R,L,X=Tree[Tree[0]];//X记录当前末尾节点 root为根 即为最大值
while((i<<1)<Tree[0])
{
L=(i<<1);R=L+1;//Left Right 记录左右节点
if(R<Tree[0]&&Tree[R]>Tree[L])//比较两个子节点的值的大小
L=R;
if(Tree[L]<=X) break;//如果所有的位置已经调整好 跳出循环
Tree[i]=Tree[L];//否则继续调整堆
i=L;
}
Tree[0]--;
Tree[i]=X;
return root;
}
void paixu(int A[][MAX],int left,int right)
{
int i=left,j=right,temp=A[0][left];
if(left>=right) return;
while(i!=j)
{
while(A[0][j]<=temp && i<j) j--;
while(A[0][i]>=temp && i<j)i++;
if(i<j)
{
change(&A[0][i],&A[0][j]);
change(&A[1][i],&A[1][j]);
}
}
change(&A[0][left],&A[0][i]);
change(&A[1][left],&A[1][i]);
paixu(A,left,i-1);
paixu(A,i+1,right);
}
int main(){
// freopen("qwe.txt","r",stdin);
scanf("%d",&N);
for(i=0;i<N;i++)
scanf("%d %d",&WW[0][i],&WW[1][i]);
paixu(WW,0,N-1);
i=0;
scanf("%d %d ",&local,&oil);
insert(Q,oil);
while(local>0&&Q[0]){
sum++;
local-=Delmax(Q);
while(i<N&&local<=WW[0][i])
insert(Q,WW[1][i++]);
}
printf("%d\n",local>0?-1:(sum-1));//因为终点当作了一个加油站 所以sum要减一
return 0;
}