UVA 10385 Duathlon

Problem H
Duathlon
Input: standard input
Output: standard output
Time Limit: 15 seconds

A duathlon is a race that involves running r km and cycling k km. n contestants have entered the race; each contestant has different running and cycling speeds. One of the contestants has bribed the organizers to set r and k so that he can win by the maximum margin. You are to determine if this is possible and, if so, give r and k.

 

Input

The input file contains several sets of input. The description of each set is given below:

The first line of each set contains an integer t, the total distance of the race, in km. That is, r + k = t. The next line contains an integer n, the number of competitors. For each contestant, a line follows with two real numbers giving the running and cycling speed for that contestant. The last line of input gives the running and cycling speed of the contestant who has bribed the organizers. You may assume t does not exceed 100 km and n does not exceed 20. Input is terminated by end of file. Two consecutive sets may or may not be separated by a blank line.

Output

For each set of input produce one line of output. The output description for each set is given below:

If it is possible to fix the race as describe above, print a message giving r and k accurate to two decimal places, and the amount of seconds by which the cheater will win the race (0 is case some competitor ties him), as in the sample below. If it is not possible, print "The cheater cannot win."  There is no blank line between outputs for two consecutive sets.

Sample Input
100
3
10.0 40.0
20.0 30.0
15.0 35.0

100
3
10.0 40.0
20.0 30.0
15.0 25.0

Sample Output
The cheater can win by 612 seconds with r = 14.29km and k = 85.71km.

The cheater cannot win.

 

 

对于每个K用方程表示出所有的参赛者的时间。用所有人时的时间减去最后一个人的时间,是一个一元一次的函数,那么问题第一个人和第二个人差距最大就转换为每个对应的X轴的图形最小的那个

发现时凸性函数,上凸就是所求

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}
#define MAXN 25
double t,v1[MAXN],v2[MAXN];
int n;
double calcu(double r)
{
        double k = t - r;
        double ans = 1e8;
        double t1 = r / v1[n - 1] + k / v2[n - 1];
        for (int i = 0 ; i < n - 1; i++)
        {
                double tmp = r / v1[i] + k / v2[i];
                ans = min(ans,tmp - t1);
        }
        return ans;
}
int main()
{
        while (scanf("%lf",&t) != EOF)
        {
                scanf("%d",&n);
                for (int i = 0 ; i < n ; i ++) scanf("%lf%lf",&v1[i],&v2[i]);
                double l = 0 ,r = t;
                for (int i = 0 ; i < 100; i++)
                {
                        double midl = l + (r - l) / 3;
                        double midr = r - (r - l) / 3;
                        if (calcu(midl) > calcu(midr)) r = midr;
                        else l = midl;
                }
                if (calcu(l) < 0.00) printf("The cheater cannot win.\n");
                else printf("The cheater can win by %.0lf seconds with r = %.2lfkm and k = %.2lfkm.\n", calcu(l) * 3600, l, t - l);
        }
        return 0;
}

  

posted @ 2015-03-15 15:22  Commence  阅读(435)  评论(0编辑  收藏  举报