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
The input file contains one or more problems. The first line of a problem consists of six integers, n m x1 y1 x2 y2. The number of cardboard partitions is n (0 < n <= 5000) and the number of toys is m (0 < m <= 5000). The coordinates of the upper-left corner and the lower-right corner of the box are (x1,y1) and (x2,y2), respectively. The following n lines contain two integers per line, Ui Li, indicating that the ends of the i-th cardboard partition is at the coordinates (Ui,y1) and (Li,y2). You may assume that the cardboard partitions do not intersect each other and that they are specified in sorted order from left to right. The next m lines contain two integers per line, Xj Yj specifying where the j-th toy has landed in the box. The order of the toy locations is random. You may assume that no toy will land exactly on a cardboard partition or outside the boundary of the box. The input is terminated by a line consisting of a single 0.
Output
The output for each problem will be one line for each separate bin in the toy box. For each bin, print its bin number, followed by a colon and one space, followed by the number of toys thrown into that bin. Bins are numbered from 0 (the leftmost bin) to n (the rightmost bin). Separate the output of different problems by a single blank line.
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
As the example illustrates, toys that fall on the boundary of the box are “in” the box.
题意 : 求每个区间里的玩具数
这题应该是用叉积做 结果模拟给模拟出来了
大致思路是 判断玩具是否在隔板’/'或隔板‘‘的左边 每次输入一个玩具位置就要判断他在哪个区间 找到之后break 用一个数组存每个区间的玩具数 再输出
关键是如何判断 这里我们用到相似三角形
我们设隔板上面的坐标是(u,y1),下面的坐标是(l,y2),玩具的坐标(x,y),如果隔板是’/'的 那么如果x<l,那么他一定在这个区间里,如果x在[l,u]里,那么我们就要用到相似三角形
在这里插入图片描述

三角形CLU和三角形BAU互为相似三角形,那么就满足BU/CU=AB/LC;
即(x-U)/(y-U)=(YA-Y2)/(Y1-Y2),求出YA后和y进行比较 如果y>=YA,那么这个玩具就在这个区间内,隔板为’'的也可以这么推

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=1e6+10;
#define  inf 0x3f3f3f3f
double  u[N],l[N];   //隔板位置
int th[N];
int main()
{
    int n,m;
    double x1,y1,x2,y2,s;  //隔板数量 玩具数量 左上角 右下角
    while(~scanf("%d",&n)&&n)
    {
        memset(th,0,sizeof(th));
        scanf("%d %lf %lf %lf %lf",&m,&x1,&y1,&x2,&y2);
        for(int i=0; i<n; i++)
        {
            scanf("%lf %lf",&u[i],&l[i]);   //上  下
        }
        for(int i=0; i<m; i++)
        {
            double  x,y;
            int h=1;
            scanf("%lf %lf",&x,&y);
            for(int j=0; j<n; j++)
            {

                if(x<u[j]&&l[j]<=u[j])   //   在/的左边
                {
                    if(x<=l[j])
                    {
                        th[j]++;
                        h=0;
                        break;
                    }
                    s=(x-l[j])*1.0/(u[j]-l[j])*(y1-y2)+y2;
                    if(y>=s)
                    {
                        th[j]++;
                        h=0;
                        break;
                    }

                }
                if(x<l[j]&&l[j]>u[j])    //在  \的左边
                {
                    if(x<=u[j])
                    {
                        th[j]++;
                        h=0;
                        break;
                    }
                    s=(l[j]-x)*1.0/(l[j]-u[j])*(y1-y2)+y2;
                    if(y<=s)
                    {
                        th[j]++;
                        h=0;
                        break;
                    }
                }
            }
            if(h)  //如果以上情况都没有  可能是最后面 有空格
                th[n]++;
        }
      //  printf("Box\n");
        //sort(th,th+1+n);    //每个分区所放的玩具数
        for(int i=0; i<=n; i++)
            printf("%d: %d\n",i,th[i]);
            printf("\n");
        /* int k=0;
         for(int i=1; i<=n; i++)
         {
             k=0;
             for(int j=0;j<=n;j++)    //记录分区玩具数等于i的分区数
             {
                if(th[j]==i)
                {
                    k++;
                }
             }
             if(!k)
                 printf("%d: %d\n",i,k);
         }
         printf("\n");*/
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言是一种广泛使用的编程语言,它具有高效、灵活、可移植性强等特点,被广泛应用于操作系统、嵌入式系统、数据库、编译器等领域的开发。C语言的基本语法包括变量、数据类型、运算符、控制结构(如if语句、循环语句等)、函数、指针等。在编写C程序时,需要注意变量的声明和定义、指针的使用、内存的分配与释放等问题。C语言中常用的数据结构包括: 1. 数组:一种存储同类型数据的结构,可以进行索引访问和修改。 2. 链表:一种存储不同类型数据的结构,每个节点包含数据和指向下一个节点的指针。 3. 栈:一种后进先出(LIFO)的数据结构,可以通过压入(push)和弹出(pop)操作进行数据的存储和取出。 4. 队列:一种先进先出(FIFO)的数据结构,可以通过入队(enqueue)和出队(dequeue)操作进行数据的存储和取出。 5. 树:一种存储具有父子关系的数据结构,可以通过中序遍历、前序遍历和后序遍历等方式进行数据的访问和修改。 6. 图:一种存储具有节点和边关系的数据结构,可以通过广度优先搜索、深度优先搜索等方式进行数据的访问和修改。 这些数据结构在C语言中都有相应的实现方式,可以应用于各种不同的场景。C语言中的各种数据结构都有其优缺点,下面列举一些常见的数据结构的优缺点: 数组: 优点:访问和修改元素的速度非常快,适用于需要频繁读取和修改数据的场合。 缺点:数组的长度是固定的,不适合存储大小不固定的动态数据,另外数组在内存中是连续分配的,当数组较大时可能会导致内存碎片化。 链表: 优点:可以方便地插入和删除元素,适用于需要频繁插入和删除数据的场合。 缺点:访问和修改元素的速度相对较慢,因为需要遍历链表找到指定的节点。 栈: 优点:后进先出(LIFO)的特性使得栈在处理递归和括号匹配等问题时非常方便。 缺点:栈的空间有限,当数据量较大时可能会导致栈溢出。 队列: 优点:先进先出(FIFO)的特性使得
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值