Meltdown

Meltdown

时间: 1ms        内存:128M

描述:

A polygon is lowered at a constant speed of v metres per minute from the air into a liquid that dissolves it at a constant speed of c metres per minute from all sides. Given a point (x,y) inside the polygon that moves with the polygon, determine when the liquid reaches the point.

The border between air and liquid always has y-coordinate 0, and the liquid only eats away from the sides of the polygon in 2 dimensions. The polygon does not rotate as it is lowered into the liquid, and at time 0, it is not touching the liquid.

Unlike the polygon, which is flat (2-dimensional), the liquid exists in three dimensions. Therefore, the liquid seeps into cavities in the polygon. For example, if the polygon is "cup-shaped", the liquid can get "inside" the cup, as in the diagram below.

输入:

The input consists of several test cases.

The first line of each test case contains the five integers N, x, y, v, and c, where 3 <= N <= 30, -100 <= x <= 100, 1 <= y <= 100, and 1 <= c < v <= 10.

The following N lines of the test case each contain one vertex of the polygon. The ith line contains the two integers x, y, where -100 <= x <= 100, 1 <= y <= 100.

The vertices of the polygon are given in counter-clockwise order. The border of the polygon does not intersect or touch itself, and the point (x,y) lies strictly inside the polygon—it does not lie on the border of the polygon.

Input is terminated by a line containing 0 0 0 0 0. These zeros are not a test case and should not be processed.

输出:

For each test case, output the first time in minutes that the liquid reaches the specified point, rounded to four decimal places.

示例输入:

4 0 50 2 1
-1 10
1 10
1 90
-1 90
0 0 0 0 0

示例输出:

25.8660

提示:

参考答案(内存最优[800]):

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <string>
#include <vector>
using namespace std;

#define SZ(v) int((v).size())
#define FR(i,a,b) for(int i=(a);i<(b);++i)
#define FOR(i,n) FR(i,0,n)
#define FORI(i,v) FOR(i,SZ(v))
#define CLR(x,a) memset(x,a,sizeof(x))
#define setmax(a,b) a = max(a,b)
#define setmin(a,b) a = min(a,b)
typedef long long ll;

int N,x,y,v,c;
int xs[30],ys[30];
double sqr(double x) { return x*x; }
double f(int i, int j, double a) {
  double x0 = xs[i]*(1-a) + xs[j]*a;
  double y0 = ys[i]*(1-a) + ys[j]*a;

  return sqrt(sqr(x-x0)+sqr(y-y0))/c + y0/v;
}
void doit() {
  scanf("%d%d%d%d%d",&N,&x,&y,&v,&c);
  if (N==0) exit(0);

  FOR(i,N) {
    scanf("%d%d",&xs[i],&ys[i]);
  }

  double t = 1e100;
  FOR(i,N) {
    int j = (i+1)%N;
    double lo = 0, hi = 1;

    FOR(k,200) {
      double m1 = (2*lo+hi)/3, m2 = (lo+2*hi)/3;
      
      if (f(i,j,m1) < f(i,j,m2)) hi = m2;
      else lo = m1;
    }

    setmin(t, f(i,j,lo));
  }

  printf("%.4lf\n",t);
}
int main() {
  while (1) doit();
}

参考答案(时间最优[12]):

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <string>
#include <vector>
using namespace std;

#define SZ(v) int((v).size())
#define FR(i,a,b) for(int i=(a);i<(b);++i)
#define FOR(i,n) FR(i,0,n)
#define FORI(i,v) FOR(i,SZ(v))
#define CLR(x,a) memset(x,a,sizeof(x))
#define setmax(a,b) a = max(a,b)
#define setmin(a,b) a = min(a,b)
typedef long long ll;

int N,x,y,v,c;
int xs[30],ys[30];
double sqr(double x) { return x*x; }
double f(int i, int j, double a) {
  double x0 = xs[i]*(1-a) + xs[j]*a;
  double y0 = ys[i]*(1-a) + ys[j]*a;

  return sqrt(sqr(x-x0)+sqr(y-y0))/c + y0/v;
}
void doit() {
  scanf("%d%d%d%d%d",&N,&x,&y,&v,&c);
  if (N==0) exit(0);

  FOR(i,N) {
    scanf("%d%d",&xs[i],&ys[i]);
  }

  double t = 1e100;
  FOR(i,N) {
    int j = (i+1)%N;
    double lo = 0, hi = 1;

    FOR(k,200) {
      double m1 = (2*lo+hi)/3, m2 = (lo+2*hi)/3;
      
      if (f(i,j,m1) < f(i,j,m2)) hi = m2;
      else lo = m1;
    }

    setmin(t, f(i,j,lo));
  }

  printf("%.4lf\n",t);
}
int main() {
  while (1) doit();
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注