nyoj 201 homework questions

homework questions

Time Limit: 3000 ms | Memory Limit: 65535 KB

Difficulty: 3

describe

Xiaobai has a course this semester called "Numerical Computing Methods", which is a method and process for effectively using digital computers to find approximate solutions to mathematical problems, as well as a subject composed of related theories ...

Today their Teacher S gave them a homework problem. Teacher S gave them a lot of points and let them use the Lagrangian interpolation formula to calculate the curve of a strictly monotonic function. Now Xiaobai has copied these points, but the problem has arisen. Because our Xiaobai classmates took a look in class, he copied down a lot of points, which means that the overall connection of these points is not necessarily strictly increasing or decreasing. . How can this be handled. To this end, our little white classmates have formulated the following rules for picking points:

1. Take as many points as possible to form a strict monotonic curve as points on the curve.

2. Calculate the equation of the curve through the Lagrange interpolation formula

However, he ran into another problem and found that he had written hundreds of points. [- -! Admire it] , this is difficult to deal with (O_O). Since the calculation amount of the Lagrangian interpolation formula is related to the number of points processed, he asked everyone to help him and help him count the maximum number of points on the curve to estimate the calculation amount.

Known: No two points have the same abscissa.

enter

This question contains multiple sets of data:
First, it is an integer T, which represents the number of sets of data.
Then, below is the T-group test data. For each set of data, there are two lines:
the first line: a number N (1<=N<=999), which represents the number of input points.
The second line: contains N pairs of numbers X (1<=x<=10000), Y (1<=Y<=10000), representing the horizontal and vertical coordinates of the selected point.

output

Each group of outputs occupies one line, and the output shares an integer, indicating the maximum number of points on the curve

sample input

2

2

1 2 3 4

3

2 2 1 3 3 4

Sample output

2

2

#include <iostream>  
#include <string>  
#include <cstdio>  
#include <cstring>  
#include <algorithm>  
using namespace std;  
  
struct node {  
    int x, y;  
}a[1100];  
  
int dp[1100], dp1[1100];  
#define mem(a) memset(a, 0, sizeof(a))  
  
int cmp(node a, node b) {  
  
    return a.x < b.x;      
}  
  
int main() {  
    int t;  
    scanf("%d",&t);  
    while (t --)
	{  
        int n;  
        scanf("%d",&n);  
        mem(a);  
        mem(dp);  
        for (int i = 1; i<=n; i++)
		{  
            scanf("%d%d",&a[i].x, &a[i].y);  
            dp[i] = 1;  
            dp1 [i] = 1;  
        }  
        sort(a+1, a+n+1, cmp);  
          
        for (int i = 2; i<=n; i++)
		{  
              
            for (int j = 1; j<i; j++)
			{ // increment or decrement
                if (a[j].y > a[i].y) dp[i] = max(dp[i], dp[j] + 1);  
                if (a[j].y < a[i].y) dp1[i] = max(dp1[i], dp1[j] + 1);  
            }  
        }   
        int maxn = 0;  
        for (int i = 1; i<=n; i++)
		{  
            maxn = max(max(dp[i], dp1[i]), maxn);  
        }  
        printf("%d\n",maxn);  
    }  
    return 0;  
}           

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324695295&siteId=291194637