8.10 第七场 Fall with Trees
8.10 第七场 Fall with Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 2600 Accepted Submission(s): 618
Problem Description
Fall wants to draw a perfect binary tree.
We first stipulate that all nodes in the tree with the same depth also have the same y-coordinate in the plane. Define nodes with the same depth to be nodes at the same level, then the perfect binary tree has four properties.
- It is a full binary tree.
- The difference between the y-coordinates of two nodes at each adjacent level is a constant.
- The difference between the x-coordinates of two adjacent nodes at the same level is constant.
- The x-coordinate of each node is the average of the x-coordinates of its sons.
Fall has drawn the root node and its left and right sons of this binary tree. Now Fall intends to draw a total of k levels and cut the binary tree down and paste it on the wall afterwards, so he wants to know what is the area of the convex hull of all nodes of this perfect binary tree.
Hint
Here’s the picture of the perfect binary tree for the first example, whose area is SABC+SBCGD=14
Input
The input consists of multiple test cases.
The first line contains an integer T (T≤2×105) – the number of test cases.
For each test case:
In the first line, there is an integer k (2≤k≤104).
In the second line, there are six integers xroot,yroot,xlson,ylson,xrson,yrson∈[−104,104] ,which represent the coordinates of the root node and its sons.
It is guaranteed that all the coordinates meet the conditions of the question, which means:
- xlson+xrson=2×xroot
- ylson=yrson
- yroot>ylson,xlson<xrson
Output
For each test case, output a real number representing the answer, with three decimal places.
Sample Input
3
3
0 0 -2 -2 2 -2
4
0 0 -4 -2 4 -2
10000
0 0 -10000 -10000 10000 -10000
Sample Output
14.000
54.000
3999000000000.000
大概题意:
求题目描述的树所围成的面积
思路:
推导出复杂度O1
的求和公式后才能AC
代码:
//
// Created by Black on 2021/8/10.
//
#include<iostream>
#include<cmath>
#include <cstdio>
using namespace std;
int main() {
// std::ios::sync_with_stdio(false);
// std::cin.tie(0);
int t;
double k;
double xRoot, yRoot, xl, xr, yl, yr;
scanf("%d", &t);
while (t--) {
// cin >> k;
scanf("%lf", &k);
k--;
scanf("%lf%lf%lf%lf%lf%lf", &xRoot, &yRoot, &xl, &yl, &xr, &yr);
// cin >> xRoot >> yRoot >> xl >> yl >> xr >> yr;
double h = (yRoot - yr);
double sum = (xr - xRoot) * (k - (1 - pow(0.5, k))) * 2;
k--;
sum += (xr - xRoot) * (k - (1 - pow(0.5, k))) * 2;
sum *= h;
// for (int i = 1; i < k; ++i) {
// double xxr = 0, xxl = 0;
// xxr = xr + (xr - xRoot) / 2;
// xxl = xl - (xr - xRoot) / 2;
// sum += 1.0 * h * (xr - xl + xxr - xxl) / 2;
// xRoot = xr;
// xl = xxl;
// xr = xxr;
// }
printf("%.3f\n", sum);
}
return 0;
}
/**
*
* 4 4+2 4+2+1
* 4 4+2
* 相加乘h
*
* 2 2+1
* 2
* 相加乘h
*
*/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效