https://codeforces.com/gym/102222/problem/J
考虑一组共底边的三角形
嵌套
⇔
\Leftrightarrow
⇔外面那个三角形左右两边角度都比里面那个大
于是对其中一个角度排序,另一个找LIS就行了
某一个角度相同的情况是不被允许的,于是在排序的时候处理一下第二关键字
坐标1e9,写叉积判断即可
对于直线两端的需要分别处理一下
对于倒着看字典序最小的LIS,考虑利用LIS时候lowerbound的位置
然后倒着找。
(正着看字典序最小也差不多,主要是保证能取到maxLength就行)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cfloat>
#define MAXN 100005
double eps = 1e-6;
class cVector
{
private:
public:
double x, y;
double CrossProduct(const cVector &b) const
{
return x * b.y - y * b.x;
}
double DotProduct(const cVector &b) const
{
return x * b.x + y * b.y;
}
cVector(double Arg1=.0, double Arg2=.0)
{
x = Arg1;
y = Arg2;
}
}PQ;
class cPoint
{
private:
public:
double x, y;
cVector PX, QX;
int Side;
int IdA;
cPoint(double Arg1 = .0, double Arg2 = .0, int Arg3 = 1, int Arg4 = 0)
{
x = Arg1;
y = Arg2;
Side = Arg3;
IdA = Arg4;
}
bool operator < (const cPoint &b) const
{
static double CP;
CP = QX.CrossProduct(b.QX);
// std::cout <<" "<< CP << "::::: " << QX.x << " " << QX.y << " " << b.QX.x << " " << b.QX.y << "CROSSPRODUCT" << std::endl;
if (Side == 1)
return CP <= -DBL_EPSILON;
else
return CP >= +DBL_EPSILON;
// Sort By Q
}
} P, Q;
cVector operator - (const cPoint &a, const cPoint &b)
{
return cVector(a.x - b.x, a.y - b.y);
}
bool CompareByP(const cPoint &a, const cPoint &b)
{
static double CP;
CP = a.PX.CrossProduct(b.PX);
if (fabs(CP - 0) <= DBL_EPSILON)
{
if (a.Side == -1)
return a.QX.CrossProduct(b.QX) <= -DBL_EPSILON;
else
return a.QX.CrossProduct(b.QX) >= +DBL_EPSILON;
}
return (a.Side == 1) ? (CP >= +DBL_EPSILON) : (CP <= -DBL_EPSILON);
}
class PointGroup
{
private:
public:
int Size;
cPoint Points[MAXN];
int LenOfLIS[MAXN];
int LIS[MAXN];
int LISLEN;
int ANS[MAXN];
PointGroup(int Arg1 = 0, cPoint *Arg2 = NULL)
{
if (Arg2 != NULL)
{
Size = Arg1;
memcpy(Points, Arg2, (Arg1 + 1) * sizeof(cPoint));
}
}
void setElement(int Id, cPoint Value)
{
Points[Id] = Value;
Points[Id].IdA = Id;
}
void getPointVector(int Id)
{
Points[Id].PX = Points[Id] - P;
Points[Id].QX = Points[Id] - Q;
}
void SortByP()
{
int N = Size;
std::sort(Points + 1, Points + 1 + N, CompareByP);
}
int ArrLowerBound(int *Arr, int L, int R, int Val)
{
static int Mid;
if (Points[Arr[R]] < Points[Val])
return R + 1;
while (L < R)
{
Mid = (L + R) >> 1;
// std::cout << "WARN" << L << "~" << R << ":" << Arr[Mid] << " " << Points[Arr[Mid]].IdA << " " << Points[Val].IdA << std::endl;
if (Points[Arr[Mid]] < Points[Val])
{
L = Mid + 1;
}
else
R = Mid;
}
return L;
}
void GetLIS()
{
if (!Size)
{
LISLEN = 0;
return;
}
LISLEN = 1;
LIS[LenOfLIS[1] = 1] = 1;
// std::cout << "INI-> " << Points[1].IdA << std::endl;
// std::cout << Size << std::endl;
for (int k, i = 2; i <= Size; ++i)
{
if (Points[LIS[LISLEN]] < Points[i])
{
// std::cout << "PushBack-> " << " " << Points[LIS[LenOfLIS[i-1]]].Side << " " << Points[LIS[LenOfLIS[i-1]]].IdA << " " << LenOfLIS[i-1] + 1 << " " << Points[i].IdA << std::endl;
LenOfLIS[i] = ++LISLEN;
LIS[LISLEN] = i;
}
else
{
k = ArrLowerBound(LIS, 1, LISLEN, i);
// std::cout << (Points[i] < Points[LIS[k]]) << "SIDES:" << Points[i].Side << " " << Points[LIS[k]].Side << std::endl;
// std::cout << "BinarySearchHere-> " << Points[LIS[k]].IdA << " " << k << " " << Points[i].IdA << std::endl;
LenOfLIS[i] = k;
LIS[k] = i;
}
// for (int j = 1; j <= LISLEN; ++j)
// {
// std::cout << i << "&&&&<<<&&&&" << LIS[j] << " " << Points[LIS[j]].IdA << " " << (Points[LIS[j]] < Points[LIS[j+1]]) << std::endl;
// }
}
for (int Ptr = LISLEN, i = Size; i >= 1; --i)
{
if (LenOfLIS[i] == LISLEN || Points[i] < Points[ANS[LenOfLIS[i] + 1]])
{
// puts("INNN");
if (LenOfLIS[i] > Ptr)
{
if (Points[i].IdA < Points[ANS[LenOfLIS[i]]].IdA)
{
Ptr = LenOfLIS[i];
ANS[Ptr] = i;
--Ptr;
}
}
else if (LenOfLIS[i] == Ptr)
{
ANS[Ptr] = i;
--Ptr;
}
}
// std::cout << LISLEN << " " << LenOfLIS[i] << std::endl;
}
// std::cout << LISLEN << std::endl;
// for (int i = 1; i <= Size; ++i)
// {
// std::cout << i << " " << Points[i].IdA << " " << Size << std::endl;
// }
for (int i = 1; i <= LISLEN; ++i)
{
// std::cout << ANS[i] << " " << Points[ANS[i]].IdA << " " << i << " " << LenOfLIS[ANS[i]] << std::endl;
LIS[i] = Points[ANS[LISLEN - i + 1]].IdA;
}
for (int i = 1; i <= LISLEN; ++i)
{
ANS[i] = LIS[i];
}
}
} A, A1, A2;
void Split(PointGroup &U, PointGroup &D, PointGroup &A)
{
int N = A.Size;
double CP;
// std::cout << N << " <- SizeOfA\n";
for (int i = 1; i <= N; ++i)
{
CP = A.Points[i].PX.CrossProduct(PQ);
if (-DBL_EPSILON <= CP && CP <= +DBL_EPSILON)
;
else if (CP <= -DBL_EPSILON)
{
U.Points[++U.Size] = A.Points[i];
U.Points[U.Size].Side = +1;
// std::cout << U.Size << " " << U.Points[U.Size].Side << " " << U.Points[U.Size].IdA << std::endl;
}
else
{
D.Points[++D.Size] = A.Points[i];
D.Points[D.Size].Side = -1;
// std::cout << D.Size << " " << D.Points[D.Size].Side << " " << D.Points[D.Size].IdA << std::endl;
}
}
// std::cout << U.Size << " <- SizeOfA1\n";
// std::cout << D.Size << " <- SizeOfA2\n";
}
void Input()
{
A1.Size = 0;
A2.Size = 0;
int n;
double xP, yP, xQ, yQ;
scanf("%lf%lf%lf%lf%d", &xP, &yP, &xQ, &yQ, &n);
P = cPoint(xP, yP);
Q = cPoint(xQ, yQ);
PQ = cVector(xQ - xP, yQ - yP);
A.Size = n;
for (int i = 1; i <= n; ++i)
{
scanf("%lf %lf", &xP, &yP);
A.setElement(i, cPoint(xP, yP));
A.getPointVector(i);
}
}
void GetAnswer(const PointGroup &A, const PointGroup &B, int *Ans, int &AnsLen)
{
AnsLen = std::max(A.LISLEN, B.LISLEN);
bool isABetter = false;
if (A.LISLEN == B.LISLEN)
{
for (int i = 1; i <= AnsLen; ++i)
{
// printf("%d %d\n", A.ANS[i], B.ANS[i]);
if (A.ANS[i] < B.ANS[i])
{
isABetter = true;
break;
}
if (A.ANS[i] > B.ANS[i])
{
isABetter = false; //**!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
break; // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!。。。。。。
}
}
}
if (A.LISLEN > B.LISLEN || (A.LISLEN == B.LISLEN && isABetter))
{
for (int i = 1; i <= AnsLen; ++i)
{
Ans[i] = A.ANS[i];
}
}
else
{
for (int i = 1; i <= AnsLen; ++i)
{
Ans[i] = B.ANS[i];
}
}
}
int Answer[MAXN], AnswerTop;
int main()
{
int T;
scanf("%d", &T);
for (int CaseNumber = 1; CaseNumber <= T; ++CaseNumber)
{
printf("Case #%d: ", CaseNumber);
Input();
Split(A1, A2, A);
A1.SortByP();
A2.SortByP();
// puts("NOW GETLIS A1");
A1.GetLIS();
// puts("NOW GETLIS A2");
A2.GetLIS();
GetAnswer(A1, A2, Answer, AnswerTop);
printf("%d\n", AnswerTop);
for (int i = 1; i <= AnswerTop; ++i)
{
printf("%d\n", Answer[i]);
}
}
// system("pause");
return 0;
}
LIS写错了,替换lowerbound之后并不能把之后的也都清空了
带劲
DATA_GENERATOR
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cfloat>
#include<vector>
#include<ctime>
using namespace std;
int N;
int main()
{
ios::sync_with_stdio(false);
srand((unsigned int)(time(NULL)));
N = 100000;
int T = 5;
cout << T << endl;
while (T--)
{
cout << (rand() % 2001 - 1000) << " " << (rand() % 2001 - 1000) << " ";
cout << (rand() % 2001 - 1000) << " " << (rand() % 2001 - 1000) << endl;
cout << N << endl;
for (int i = 1; i <= N; ++i)
{
cout << (rand() % 2001 - 1000) << " " << (rand() % 2001 - 1000) << endl;
}
}
return 0;
}
@echo off
:loop
data_generator.exe %random% > data.in
std.exe < data.in > std.out
meow.exe < data.in > my.out
fc my.out std.out
if not errorlevel 1 goto loop
pause
goto loop