2021杭电多校第三场 D题—Game on Plane
思路:bob要选择斜率尽可能多的线画平行线,alice则要在当前选择到的边数中斜率相等的尽可能少(也可以理解为就是让每一种斜率出现的最大次数最小),就需要依次从不同的斜率中选一根循环往复
Problem Description
Alice and Bob are playing a game. In this game, there are n straight
lines on the 2D plane. Alice will select exactly k straight lines
l1,l2,…,lk among all the n straight lines first, then Bob will draw a
straight line L. The penalty of Bob is defined as the number of lines
in {l1,l2,…,lk} that shares at least one common point with L. Note
that two overlapping lines also share common points. Alice wants to
maximize the penalty of Bob while Bob wants to minimize it. You will
be given these n lines, please write a program to predict the penalty
of Bob for k=1,2,3,…,n if both of the players play optimally.
Input
The first line contains a single integer T (1≤T≤500), the number of
test cases. For each test case:The first line contains a single integer n (1≤n≤100000), denoting the
number of straight lines.Each of the next n lines contains four integers xai,yai,xbi and ybi
(0≤xai,yai,xbi,ybi≤109), denoting a straight line passes both
(xai,yai) and (xbi,ybi). (xai,yai) will never be coincided with
(xbi,ybi).It is guaranteed that the sum of all n is at most 1000000.
Output
For each test case, output n lines, the i-th (1≤i≤n) of which
containing an integer, denoting the penalty of Bob when k=i.
Sample Input
2
2
1 1 2 2
0 0 2 3
3
1 1 2 2
1 1 2 2
3 2 5 4
Sample Output
0
1
0
0
0
代码:
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<cstring>
#include<list>
#include<map>
#include<cstdio>
typedef long long ll;
typedef int64_t s64;
using namespace std;
typedef pair<int,int>p;
p a[100006];
int i,j,k;
int ans[100006];
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int main(){
int t;
scanf("%d",&t);
int dx,dy;
int x1,x2,y1,y2;
while(t--){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
dx=x1-x2;dy=y1-y2;
if(dx==0) dy=1; //如果垂直于坐标轴x y=1;
else if(dy==0) dx=1; //如果垂直于坐标轴y x=1;
else{
if(dx<0) {dx*=-1;dy*=-1;} // (-1,2) -> (1,-2) 因为要用x排序 则xy正负同时颠倒
//保证了斜率不变 但又可以排序
int d=gcd(abs(dx),abs(dy));
dx/=d;dy/=d;
}
//cout<<dx<<" "<<dy<<endl;
a[i]=p(dx,dy); //用pair存"斜率"(直接存坐标防止误差)
}
sort(a+1,a+1+n); //根据从小到大排序
for(i=1;i<=n;i++) ans[i]=0;
for(i=1;i<=n;i=j){
for(j=i;j<=n&&a[i]==a[j];j++); //记录了当前斜率相同的有几个
for(k=1;k<=j-i;k++) ans[k]++; //记录了斜率分布的个数
}
for(i=j=1;i<=n;i++){
while(ans[j]==0) j++; //j为最多可以平行的边数
ans[j]--;
printf("%d\n",i-j);
}
}
return 0;
}