HDU2050:折线分割平面(递推)

折线分割平面
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 41136 Accepted Submission(s): 27239

Problem Description
我们看到过很多直线分割平面的题目,今天的这个题目稍微有些变化,我们要求的是n条折线分割平面的最大数目。比如,一条折线可以将平面分成两部分,两条折线最多可以将平面分成7部分,具体如下所示。

在这里插入图片描述

Input
输入数据的第一行是一个整数C,表示测试实例的个数,然后是C 行数据,每行包含一个整数n(0<n<=10000),表示折线的数量。

Output
对于每个测试实例,请输出平面的最大分割数,每个实例的输出占一行。

Sample Input
2
1
2

Sample Output
2
7

此题和直线最多能分割多少平面思想类似,分割的平面数和交点的个数相关,要让每次增加的分割数最多,即让交点最多,f(n)之前的n-1次有n-1条折线,有2*(n-1)条线段,最多可以多产生22(n-1)个交点,由于射线数不变为2,增加的线段数即为增加的交点数22(n-1)条,得方程为:f(n) = f(n-1) + 4*(n-1) + 1;(n >= 3)

下面附上ac代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <ctype.h>
#include <queue>
#define INF 0x7ffffff
using namespace std;
typedef long long int ll;
int a[20000];
int main() {
    a[1] = 2;
    a[2] = 7;
    for(int i = 3; i < 10100; i++) { 
        a[i] = a[i-1] + 4*(i-1) + 1; //递推方程
    }
    int c,n;
    cin >> c;
    while(c--) {
        cin >> n;
        cout << a[n] << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43555854/article/details/86556997