1. 程式人生 > 其它 >2021牛客暑期多校訓練營8 K. Yet Another Problem About Pi(幾何)

2021牛客暑期多校訓練營8 K. Yet Another Problem About Pi(幾何)

連結:https://ac.nowcoder.com/acm/contest/11259/K
來源:牛客網

題目描述

On a distant planet of dreams, Toilet-Ares is packing up the things and memories, ready to commerce a new adventurous road trip. With outstanding driving skills, the driving route may be a segment, a curve, a polyline, and it can be closed (end-to-end), even self-intersect, as long as the route is continuous. The fuel capacity, oddly enough, can support ππ km driving. Thus, the route has a maximum total length of ππ km.

The planet's surface is expansive and flat enough to regard as a plane. A Cartesian coordinate system, formed with longitude and latitude, is used to describe each geographical position on the planet. Every ww km, draw a line of points with some equal longitude, named meridian. Similarly, every dd km, draw a line of points with some equal latitude, named parallel. Notice that innumerous meridians are perpendicular to innumerous parallels, constructing a grid called graticule, dividing the plane into infinite cells. Inhabitants there are used to defining those cells as region

s, and to avoid conflict, positions on meridians or parallels belong to no region.

There are so many different kinds of landscapes to see, to admire, to experience. Toilet-Ares starts the drive at an arbitrary position on the planet. Whenever passing a region for the first time, Toilet-Ares will remember its visual feature (which is always distinguishable from any other region

). So, it will be easy for Toilet-Ares to count up the number of regions visited as the road trip ends.

For example, in both situations shown below, four different regions are visited along the route.

Just as the saying goes, "Where there is a will, there is a way." Toilet-Ares always attempts to figure out how many different regions at most can visit in the whole road trip. And you, as a friend, are here to answer this question.

輸入描述:

The input contains multiple test cases. The first line of input contains one integer TT (1≤T≤1051≤T≤105).

In the following TT lines, each line contains two real numbers w,dw,d (0<w,d≤50<w,d≤5), describing one test case. Any of them may be an integer or contains at most 8 decimal digits. It is guaranteed that there is no trailing zero in decimal place.

輸出描述:

For each test case, output the corresponding answer with an integer in one line.

示例1

輸入

複製

2
5 5
1.5 1.5

輸出

複製

4
8

注意到$\pi \(是實數,而w和d都是有理數,在網格線交點畫一個半徑為無窮小量的圓則可以佔據四個區域,這也是最少的情況。如果想要佔據的區域儘可能多,則路線要麼貼著網格走(在交點畫一個無窮小圓),要麼沿著斜線走(也是在交點畫一個無窮小圓)。而一段斜線\)\sqrt{w2+d2}$的貢獻為3,一段水平/垂直線的貢獻為2,令 \(a=min\{w,d\},b=\sqrt{w^2+d^2}\),則演算法是對 \(ax+by\leq \pi\) 的非負解求 \(2x+3y\) 的最大值,然後再加上起點的貢獻4。同時,x<3 與 y<2 至少有一成立(因為改變操作的情況只有在邊界時才會發生),因此直接列舉即可(當然也可以01揹包)。注意開long long以免溢位。

#include <bits/stdc++.h>
#define int long long
#define pi acos(-1)
using namespace std;
signed main() {
	int t;
	cin >> t;
	while(t--) {
		double w, d;
		cin >> w >> d;
		double a = min(w, d);
		double b = sqrt(w * w + d * d);
		long long ans = 0;
		//ax + by <= pi
		for(int i = 0; i < 3; i++) {
			if(pi - 1.0 * i * a >= 0) ans = max(ans, (long long) floor((pi - 1.0 * i * a) / b) * 3 + i * 2ll + 4);
			if(pi - 1.0 * i * b >= 0) ans = max(ans, (long long) floor((pi - 1.0 * i * b) / a) * 2 + i * 3ll + 4);
		}
		cout << ans << endl;
	}
}