B - Toy Storage

解决一个孩子随意乱扔玩具的问题,父母通过在玩具箱中放置隔板来确保不同玩具分开存放。本篇介绍了如何通过输入隔板位置和玩具落地点的数据,计算每个隔区内玩具的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing with them. They gave Reza a rectangular box to put his toys in. Unfortunately, Reza 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 Reza to find his favorite toys anymore.
Reza's parents came up with the following idea. They put cardboard partitions into the box. Even if Reza keeps throwing his toys into the box, at least toys that get thrown into different partitions stay separate. The box looks like this from the top:


We want for each positive integer t, such that there exists a partition with t toys, determine how many partitions have t, toys.

Input

The input consists of a number of cases. The first line consists of six integers n, m, x1, y1, x2, y2. The number of cardboards to form the partitions is n (0 < n <= 1000) and the number of toys is given in m (0 < m <= 1000). 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 each consists of two integers Ui Li, indicating that the ends of the ith cardboard is at the coordinates (Ui, y1) and (Li, y2). You may assume that the cardboards do not intersect with each other. The next m lines each consists of two integers Xi Yi specifying where the ith toy has landed in the box. You may assume that no toy will land on a cardboard.

A line consisting of a single 0 terminates the input.

Output

For each box, first provide a header stating "Box" on a line of its own. After that, there will be one line of output per count (t > 0) of toys in a partition. The value t will be followed by a colon and a space, followed the number of partitions containing t toys. Output will be sorted in ascending order of t for each box.

Sample Input

4 10 0 10 100 0
20 20
80 80
60 60
40 40
5 10
15 10
95 10
25 10
65 10
75 10
35 10
45 10
55 10
85 10
5 6 0 10 60 0
4 3
15 30
3 1
6 8
10 10
2 1
2 8
1 5
5 5
40 10
7 9
0

Sample Output

Box
2: 5
Box
1: 4
2: 1
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#define eps 1e-6
#define LL long long
using namespace std;

const int N = 5000+10;
int sum[N];
int cnt[1005];

struct Point {
	double x, y;
	Point(double x=0, double y=0): x(x), y(y) {}					//构造函数
	Point operator + (Point p) {return Point(x+p.x, y+p.y);}				//两向量相加
	Point operator - (Point p) {return Point(x-p.x, y-p.y);}				//两向量相减
	Point operator * (double k) {return Point(k*x, k*y);}				//向量的数乘
	Point operator / (double k) {return Point(x/k, y/k);}				//向量的数乘(乘以一个分数)
	bool operator < (const Point &p) const {return x!=p.x ? (x<p.x):(y<p.y);}	//给点排序时使用(可以根据需要变化)
	double norm() {return x*x + y*y;}							//向量的范数
	double abs() {return sqrt(norm());}							//向量的大小
	double dot(Point p) {return x*p.x + y*p.y;}					//两向量内积
	double cross(Point p) {return x*p.y - y*p.x;}					//两向量外积
};

typedef Point Vector;

int ccw(Point p0, Point p1, Point p2) {
	Vector v1 = p1-p0;
	Vector v2 = p2-p0;
	if(v1.cross(v2) > 0) return 1;			//逆时针方向
	if(v1.cross(v2) < 0) return -1;			//顺时针方向
	if(v1.dot(v2) < 0) return 2;			//同一直线上的反方向
	if(v1.norm() < v2.norm()) return -2;	//同一直线上的正方向
	return 0;								//在向量上
}

struct Line {
    Point a;
    Point b;
} line[N];

bool cmp1(Line m,Line n)
{
	return m.a.x<n.a.x;
}

void B_search(Point p,int n) {
    int l=0,r=n-1;
    int mid;
    while(l<r) {
        mid=(l+r)>>1;
        if(ccw(p,line[mid].a,line[mid].b)==1) l=mid+1;
        else r=mid;
    }
    if(ccw(p,line[l].a,line[l].b)==1) sum[l+1]++;
    else sum[l]++;
}

int main() 
{
    int n,m;
    double x1,y1,x2,y2;
    while(~scanf("%d",&n)&&n) {
        memset(sum,0,sizeof(sum));
        scanf("%d %lf %lf %lf %lf",&m,&x1,&y1,&x2,&y2);
        for(int i=0; i<n; i++) {
            double p_x1,p_x2;
            scanf("%lf %lf",&p_x1,&p_x2);
            line[i].a.x=p_x1;
            line[i].a.y=y1;
            line[i].b.x=p_x2;
            line[i].b.y=y2;
        }
        sort(line,line+n,cmp1);
        for(int i=0; i<m; i++) {
            double x,y;
            scanf("%lf %lf",&x,&y);
            Point p;
            p.x=x;
            p.y=y;
            B_search(p,n);
        }
        /*
        for(int j=0; j<=n ;j++){
        	printf("%d: %d\n",j,sum[j]);
    	}
    	*/
        for(int i=1; i<=m; i++)
            for(int j=0; j<=n ;j++){
            	if(sum[j] == i) cnt[i]++;
        }
        printf("Box\n");
        for(int i=1; i<=m; i++)
        	if(cnt[i]!=0) printf("%d: %d\n",i,cnt[i]);
        for(int i=1; i<=m; i++) cnt[i]=0;
    }
    return 0;
}

 

### 支持向量机 (SVM) 的玩具示例和数据集 支持向量机是一种强大的监督学习方法,广泛应用于分类和回归分析。为了帮助理解 SVM 的工作原理及其应用效果,可以利用一些经典的玩具数据集或可视化工具来探索其性能。 #### 常见的 SVM 玩具数据集 以下是几个常用的玩具数据集以及如何通过 Python 和 `scikit-learn` 库实现简单的 SVM 示例: 1. **Iris 数据集** Iris 数据集是一个经典的小型数据集,包含三类鸢尾花的数据样本,每类有 50 个实例,总共有 150 条记录。该数据集非常适合用于二元或多类别分类任务。 ```python from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.svm import SVC # 加载 Iris 数据集 iris = datasets.load_iris() X, y = iris.data, iris.target # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) # 创建并训练 SVM 模型 clf = SVC(kernel='linear', C=1.0, random_state=42) clf.fit(X_train, y_train) # 测试模型准确性 accuracy = clf.score(X_test, y_test) print(f"Accuracy on the test set: {accuracy:.2f}") ``` 2. **Moons 数据集** Moons 数据集由两个互锁的新月形分布组成,适合用来展示非线性决策边界的效果。 ```python from sklearn.datasets import make_moons import matplotlib.pyplot as plt # 生成 Moons 数据集 X, y = make_moons(n_samples=100, noise=0.1, random_state=42) # 可视化数据点 plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired, s=20) plt.title('Moons Dataset') plt.show() # 使用 RBF 核函数训练 SVM clf = SVC(kernel='rbf', gamma='auto', C=1.0, random_state=42) clf.fit(X, y) # 绘制决策边界 xx, yy = np.meshgrid(np.linspace(-2, 3, 500), np.linspace(-2, 2, 500)) Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.contourf(xx, yy, Z > 0, alpha=0.8, cmap=plt.cm.Paired) plt.scatter(X[:, 0], X[:, 1], c=y, edgecolors='k', cmap=plt.cm.Paired, s=20) plt.title('Decision Boundary with RBF Kernel') plt.show() ``` 3. **Circles 数据集** Circles 数据集由两组同心圆构成,同样适用于研究非线性核函数的表现。 ```python from sklearn.datasets import make_circles # 生成 Circles 数据集 X, y = make_circles(n_samples=100, factor=.3, noise=0.1, random_state=42) # 可视化数据点 plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired, s=20) plt.title('Circles Dataset') plt.show() # 训练带有 RBF 核函数的 SVM clf = SVC(kernel='rbf', gamma='auto', C=1.0, random_state=42) clf.fit(X, y) # 绘制决策边界 xx, yy = np.meshgrid(np.linspace(-1.5, 1.5, 500), np.linspace(-1.5, 1.5, 500)) Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.contourf(xx, yy, Z > 0, alpha=0.8, cmap=plt.cm.Paired) plt.scatter(X[:, 0], X[:, 1], c=y, edgecolors='k', cmap=plt.cm.Paired, s=20) plt.title('Decision Boundary with RBF Kernel') plt.show() ``` 以上代码展示了如何加载常见数据集、构建 SVM 模型,并绘制相应的决策边界[^1]。 #### 关于 SVM 复杂度的优势 值得注意的是,SVM 的预测效率主要取决于支持向量的数量而非特征空间的维度,这使得它在高维稀疏场景下表现尤为突出[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值