题目链接:http://codeforces.com/problemset/problem/126/A
题意:两个水龙头出水温度为t1、t2,出水速度y1、y2在0到x1、x2之间调节,根据公式t = (t1y1+t2y2)/(y1+y2) 算出水温,给定t0,要求温度在t0以上并尽可能接近t0,满足条件的多组数据中选出水速度最快的。要求输出为整数。所有数据范围为1e6内int。
题解:很容易想到直接数学计算,但是由于要求输出的是int,很难处理精度、误差。可以先假定水都放满,若温度过低则降低水温低的出水速度,反之亦然。每次变化都为1,则复杂度为x1+x2。记录最低差值的即可。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <stack>
#include <set>
#include <queue>
#include <functional>
#include <map>
#include <bitset>
#define INF 0x7fffffff
#define REP(i,j,k) for(int i = j;i <= k;i++)
#define squr(x) (x) * (x)
#define lowbit(x) (x&(-x))
#define getint(x) scanf("%d", &(x))
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const double eps = 1e-15;
double t1, t2, t0, t;
LL x1, x2, yy1, yy2;
double ansy1, ansy2;
double del = -1;
int main(int argc, const char * argv[]) {
cin >> t1 >> t2 >> x1 >> x2 >> t0;
yy1 = x1; yy2 = x2;
while (yy1 > 0 || yy2 > 0) {
t = (t1 * yy1 + t2 * yy2) / (yy1 + yy2);
if (t - t0 >= -eps && (del == -1 || fabs(t - t0) < del)) {
del = fabs(t - t0);
ansy1 = yy1;
ansy2 = yy2;
}
//cout << yy1 << " " << yy2 << " " << t - t0 << endl;
if (fabs(t - t0) < eps) {
break;
} else if (t - t0 >0) {
yy2--;
} else {
yy1--;
}
}
cout << (int)ansy1 << " " << (int)ansy2 << endl;
return 0;
}