[CF 249D]Donkey and Start解題報告

題意

第一象限上有n<=10^5個點。以分數的形式給定兩個斜率k1和k2(這樣可以表示tan90°的情況)。保證對應傾斜角在[0°,90°]內。從原點開始,從我們所在的點作兩條斜率分別爲k1和k2的射線,並在射線所夾區域內部(不含射線上)選一個點,並繼續這樣做下去,直到無點可選。問途中最多經過多少個點。

像這樣:



它的座標是x軸往左,y軸往上……我估計這麼做的原因是爲了迷惑你……

分析

這道題的關鍵是很明顯的:假設過原點,斜率爲k1和k2的單位向量分別爲v1和v2,那我們就以(v1,v2)爲基底建立一組新的座標系。而在新的座標系中,規則就變成了:下一步只能選擇x和y座標均嚴格大於當前點的點。

換言之,將所有點按x座標爲第一關鍵字,y座標爲第二關鍵字排序後,我們要求的也就是從原點出發的最長上升序列。當然需要注意,該序列中的x座標也必須單調上升。

所以按照求LIS的經典方法,用一個樹狀數組維護即可。

代碼

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
const int SIZEN=100010;
class BIT{
public:
	int n;
	int s[SIZEN];
	#define lowbit(x) ((x)&(-x))
	void clear(int _n){
		n=_n;
		memset(s,0,sizeof(s));
	}
	void modify(int x,int t){
		for(;x<=n;x+=lowbit(x)) s[x]=max(s[x],t);
	}
	int pref_max(int x){
		int ans=0;
		for(;x;x-=lowbit(x)) ans=max(ans,s[x]);
		return ans;
	}
};
class Point{
public:
	LL x,y;
	int id;
};
void print(const Point &p){
	cout<<"("<<p.x<<","<<p.y<<" "<<p.id<<")";
}
bool operator < (const Point &a,const Point &b){
	if(a.x==b.x) return a.y<b.y;
	return a.x<b.x;
}
LL operator * (const Point &a,const Point &b){
	return a.x*b.x+a.y*b.y;
}
int N;
Point P[SIZEN];
Point alpha1,alpha2;
LL Y[SIZEN]={0};int tot=0;
void coor_trans(void){//座標變換
	for(int i=0;i<=N;i++){
		LL a=P[i].x*alpha2.y-alpha2.x*P[i].y;
		LL b=alpha1.x*P[i].y-P[i].x*alpha1.y;
		a*=-1,b*=-1;//強行改規則,只選x,y座標都小於它的
		P[i].x=a,P[i].y=b;
		Y[tot++]=b;
	}
	sort(Y,Y+tot);
	tot=unique(Y,Y+tot)-Y;
	for(int i=0;i<=N;i++) P[i].y=lower_bound(Y,Y+tot,P[i].y)-Y+1;
}
int dp[SIZEN]={0};
BIT T;
void work(void){
	sort(P,P+1+N);
	T.clear(N+1);
	for(int i=0,j=0;i<=N;i++){
		while(P[j].x<P[i].x){
			T.modify(P[j].y,dp[P[j].id]);
			j++;
		}
		dp[P[i].id]=T.pref_max(P[i].y-1)+1;
	}
	printf("%d\n",dp[0]-1);
}
void read(void){
	P[0].x=P[0].y=0;P[0].id=0;
	scanf("%d",&N);
	scanf("%I64d/%I64d",&alpha1.y,&alpha1.x);
	scanf("%I64d/%I64d",&alpha2.y,&alpha2.x);
	for(int i=1;i<=N;i++){
		scanf("%I64d%I64d",&P[i].x,&P[i].y);
		P[i].id=i;
	}
}
int main(){
	//freopen("t1.in","r",stdin);
	read();
	coor_trans();
	work();
	return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章