首页 > 代码库 > hdu 4938 Seeing People 排序+二分查找

hdu 4938 Seeing People 排序+二分查找

Seeing People

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 241    Accepted Submission(s): 61


Problem Description
There are two kinds of people.

If person i is the first kind of people, it locates at position (pi, 0) initially, starts moving at time ti with a constant velocity of (0, v1), and then sees every person sits at the line segment (xi, yi), (xi + wi, yi), include the two points, where (xi, yi) is the position of person i at that time and wi is its vision.

If person i is the second kind of people, it locates at position (0, pi) initially, starts moving at time ti with a constant velocity of (v2, 0), and then sees every person sits at the line segment (xi, yi), (xi, yi + wi), include the two points, where (xi, yi) is the position of person i at that time and wi is its vision.

Now given the position of people, please calculate the number of distinct people every person has finally seen.
 

Input
There are multiply test cases.

The first line contains an integer T(T<=10), indicates the number of cases.

For each test case, the first line contains three integers n, v1, v2(1<=n,v1,v2<=10^5), indicating the total number of two kinds of people, the speed of the first kind, and the speed of the second kind.

Then comes N lines, the i-th line contains four integers ai, ti, pi, wi (1<=ai<=2, 1<=ti,pi,wi<=10^5), describing a person, where ai is the type, ti is time the person starts moving, pi is the location and wi is the vision.

It is granted that for the same kind of people, all ti, pi are different.
 

Output
For each case, you should output "Case #k:" first, where k indicates the case number and counts from 1.

Then output N lines, each lines is an integer, indicates the number of distinct people the i-th person finally recorded.
 

Sample Input
1 2 1 1 1 1 1 1 2 2 2 1
 

Sample Output
Case #1: 1 1
 

Author
UESTC
 

Source
2014 Multi-University Training Contest 7

排序+二分查找,代码改了一下午,虽然花了很多时间,不过学到的东西很多


#include <cmath>
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int inf=1e9;
int v1,v2,n,T;
struct Node{
	long long t,p,w,kind,rank,cnt;
	double see_right,see_left;
	void init(int i)
	{
		double k=double (v2)/double(v1);
		cnt=0;
		rank=i;
		if(kind==1){
			see_left=p-t*v2;
			see_right=see_left+w;
		}
		else {
			see_left=p*k-t*v2;
			see_right=see_left+w*k;
		}
	}
	bool operator < (const Node& tem) const{
		return see_left < tem.see_left;
	}
}ans[100010];
double data1[100010],data2[100010];
bool cmp(Node a,Node b)
{
	return a.rank<b.rank;
}

void init ()
{
	scanf("%d %d %d ",&n,&v1,&v2);
	memset(data1,0,sizeof(data1));
	memset(data2,0,sizeof(data2));
	for(int i=0;i<n;i++)
	{
		scanf("%d %d %d %d",&ans[i].kind,&ans[i].t,&ans[i].p,&ans[i].w);
		ans[i].init(i);
	}
}
void slove()
{
	init();
	sort(ans,ans+n);
	int cnt1=0,cnt2=0;
	for(int i=0;i<n;i++)
	{
		if(ans[i].kind==1)
		{
			data1[cnt1++]=ans[i].see_left;
		}
		else {
			data2[cnt2++]=ans[i].see_left;
		}
	}
	data1[cnt1]=inf,data2[cnt2]=inf;
	for(int i=0;i<n;i++)
	{
		if(ans[i].kind==2){
			ans[i].cnt=upper_bound(data1,data1+cnt1+1,ans[i].see_right)-lower_bound(data1,data1+cnt1+1,ans[i].see_left);
		}
		else{
			ans[i].cnt=upper_bound(data2,data2+cnt2+1,ans[i].see_right)-lower_bound(data2,data2+cnt2+1,ans[i].see_left);
			
		}
	}
	sort(ans,ans+n,cmp);
	for(int i=0;i<n;i++)
	{
			printf("%d\n",ans[i].cnt);
	}
}
int main ()
{
	//freopen("1004.in","r",stdin);
	//freopen("data.out","w",stdout);
	int cas=0;
	scanf("%d",&T);
	while (T--)
	{

		printf("Case #%d:\n",++cas);
		slove();
	}
	return 0;
}