TOYS
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TOYS相关的知识,希望对你有一定的参考价值。
TOYS
Calculate the number of toys that land in each bin of a partitioned toy box.
Mom and dad have a problem - their child John never puts his toys away when he is finished playing with them. They gave John a rectangular box to put his toys in, but John is rebellious and obeys his parents by simply throwing his toys into the box. All the toys get mixed up, and it is impossible for John to find his favorite toys.
John‘s parents came up with the following idea. They put cardboard partitions into the box. Even if John keeps throwing his toys into the box, at least toys that get thrown into different bins stay separated. The following diagram shows a top view of an example toy box.
For this problem, you are asked to determine how many toys fall into each partition as John throws them into the toy box.
Input
Output
Sample Input
5 6 0 10 60 0 3 1 4 3 6 8 10 10 15 30 1 5 2 1 2 8 5 5 40 10 7 9 4 10 0 10 100 0 20 20 40 40 60 60 80 80 5 10 15 10 25 10 35 10 45 10 55 10 65 10 75 10 85 10 95 10 0
Sample Output
0: 2 1: 1 2: 1 3: 1 4: 0 5: 1 0: 2 1: 2 2: 2 3: 2 4: 2
Hint
1 #include<cmath> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 #define LL long long 6 using namespace std; 7 int n,m,ans[10005]; 8 int uy,dy,U[10005],D[10005]; 9 int read(){ 10 int x=0,f=1; char ch=getchar(); 11 while (ch<‘0‘||ch>‘9‘){if (ch==‘-‘) f=-f; ch=getchar();} 12 while (ch>=‘0‘&&ch<=‘9‘) x=x*10+ch-‘0‘,ch=getchar(); 13 return x*f; 14 } 15 int cross(int x_0,int y_0,int x_1,int y_1){ 16 return x_0*y_1-x_1*y_0; 17 } 18 int main(){ 19 n=read(); 20 while (n){ 21 m=read(),U[0]=D[0]=read(),uy=read(),U[n+1]=D[n+1]=read(),dy=read(); 22 for (int i=1; i<=n; i++) U[i]=read(),D[i]=read(); 23 memset(ans,0,sizeof ans); 24 for (int i=1; i<=m; i++){ 25 int x=read(),y=read(); 26 int L=1,R=n+1,mid,pos; 27 while (L<=R){ 28 mid=(L+R)>>1; 29 if (cross(U[mid]-D[mid],uy-dy,x-D[mid],y-dy)>0) R=mid-1,pos=mid; else L=mid+1; 30 } 31 ans[pos-1]++; 32 } 33 for (int i=0; i<=n; i++) printf("%d: %d\n",i,ans[i]); 34 putchar(‘\n‘); 35 n=read(); 36 } 37 return 0; 38 }
以上是关于TOYS的主要内容,如果未能解决你的问题,请参考以下文章