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 regions, 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.

2021牛客暑期多校训练营8 K. Yet Another Problem About Pi(几何)

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;
	}
}



上一篇:腾讯位置服务定位组件实现周边公用厕所远近排序分布图


下一篇:C#.NET面试题汇总系列四:多线程