[1945CowBaseball-AcWingQuestionBank
Writing a violent method O(n^3) actually passed, but the data is a bit watery. Although violent law is not good, it can help us understand:
#include The question says xy<=yz<=2xyxy<=yz<=2xyxy<=yz<=2xy:
ok, now we look at it from the perspective of points: y−x<=z−y<=2(y−x)yx<=zy<=2(yx)y−x<=z−y<=2(y −x) 2y−x<=z<=3y−2x2y-x<=z<=3y-2x2y−x<=z<=3y−2x ok, the data range of this question is 100010001000. We can enumerate the two points x and y, which is O(n2)O(n^2)O(n2), which is 1e61e61e6. For the third point z, we You can go within the range of z found above,[2y−x,3y−2x][2y-x,3y-2x][2y−x,3y−2x]How many points there are in it shows how many legal z’s there are. We need to control the complexity of the third point to nlongnnlongnnlongn. In this case, the overall complexity is O(n2nlong)O(n^2 nlong)O(n2nlong)
using namespace std;
const int N=1e8+10;
int a[N];
int n,ans;
int main()
{
cin>>n;
for(int i=0;i
}
sort(a,a+n);
for(int x=0;x
So now our question is how to find[2y−x,3y−2x][2y-x,3y-2x][2y−x,3y−2x]observing the figure below, we find that we can transform the problem into:
The largest point greater than or equal to 2y-x
The largest point less than or equal to 3y-2x
#include
using namespace std;
const int N=1010;
int ans,n;
int a[N];
int main()
{
cin >>n;
for(int i=0;i
sort(a,a+n);
for(int i=0;i+2