此题的思路就是用2个变量i, j标记2个函数的位置,我是用一个结构体保存了终止点和值,然后先比较当前i和j的值大小,大的那个再跟当前h(x)的最小值比谁小,再判断当前的i和j的终止点谁大谁小,小的那个就要加加,如果相同,那2者都要加加。
AC代码:
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<ctime>
using namespace std;
#define NMAX 50000
#define ll long long
#define eps 1e-8
typedef struct node
{
double e;
double elem;
}pnode;
pnode node[105];
int sig(double x){return (x>eps)-(x<-eps);}
int main()
{
int i,j,a,b;
while(~scanf("%d",&a))
{
double pos = 0.0;
for(i = 0; i < a; i++)
{
double t1,t2;
scanf("%lf %lf",&t1,&t2);
node[i].elem = t1;
node[i].e = pos+t2;
pos += t2;
}
scanf("%d",&b);
pos = 0.0;
for(i = a; i < a+b; i++)
{
double t1,t2;
scanf("%lf %lf",&t1,&t2);
node[i].elem = t1;
node[i].e = pos+t2;
pos += t2;
}
i=0;
j=a;
double ans = NMAX;
while(i < a||j < a+b)
{
double e1 = node[i].elem,e2 = node[j].elem;
double t1 = node[i].e, t2 = node[j].e;
ans = min(max(e1,e2),ans);
if(sig(t1 - t2) > 0) j++;
if(sig(t1 - t2) < 0) i++;
if(sig(t1 - t2) == 0)
{
i++;
j++;
}
}
printf("%.3f\n",ans);
}
return 0;
}