poj 2318 TOYS (计算几何)
2015-11-08 15:34
477 查看
TOYS
Description
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.
View Code
Hint
As the example illustrates, toys that fall on the boundary of the box are "in" the box.
直接上代码
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 12453 | Accepted: 6013 |
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.
/************************************************************************* > File Name: code/poj/2318.cpp > Author: 111qqz > Email: rkz2013@126.com > Created Time: 2015年11月08日 星期日 10时09分41秒 ************************************************************************/ #include<iostream> #include<iomanip> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<map> #include<set> #include<queue> #include<vector> #include<stack> using namespace std; #define REP(i, n) for (int i=0;i<int(n);++i) typedef long long LL; typedef unsigned long long ULL; const int N=5E3+5; struct node { int x,y; }; struct node rec,rec2; struct node par ,par2 ; struct node toy ; int ans ,cnt ; int n,m; bool judge(node p1,node p2,node p3) //判断点是否在直线的[右侧!!] { int s = (p1.x-p3.x)*(p2.y-p3.y)-(p1.y-p3.y)*(p2.x-p3.x); if (s>0) return false; if (s<0) return true; } bool cmp(node a,node b) { if (a.x<b.x) return true; if (a.x==b.x&&a.y<b.y) return true; return false; } int main() { // freopen("in.txt","r",stdin); while (scanf("%d",&n)!=EOF&&n) { memset(ans,0,sizeof(ans)); memset(par,0,sizeof(par)); memset(par2,0,sizeof(par2)); memset(toy,0,sizeof(toy)); cin>>m>>rec.x>>rec.y>>rec2.x>>rec2.y; for ( int i = 1 ; i <= n ; i++) { cin>>par[i].x>>par2[i].x; par[i].y=rec.y; par2[i].y=rec2.y; } for ( int i = 1 ; i <= n-1 ; i++) { for ( int j = i+1 ; j <= n ; j++) { if (par[i].x>par[j].x) { swap(par[i].x,par[j].x); // swap(par[i].y,par[j].y); swap(par2[i].x,par2[j].x); // swap(par2[i].y,par2[j].y); } } } // for ( int i = 1 ; i <= n ; i++) // cout<<par[i].x<<endl; for ( int i = 1 ; i <= m ; i++ ) { cin>>toy[i].x>>toy[i].y; } int p; sort(toy+1,toy+m+1,cmp); //如果第i个娃娃在第k个分划中,那么排序后第i+1个娃娃至少在第k个分划中....(某大神说过,顺手就能写的优化顺手 // for ( int i = 1 ; i <= m ; i++) cout<<"x[i]:"<<toy[i].x<<" y[i]:"<<toy[i].y<<endl; for ( int i = 1 ; i <= m ; i++) { p = n + 1; //如果在所有board的右侧,那么一定是在最后一个分划中(n个板子形成n+1个分划) bool ok=false; for ( int j = 1 ; j <= n ; j++) { ok=judge(par2[j],par[j],toy[i]); if (!ok) { // cout<<"i:"<<i<<" j:"<<j<<" "<<par2[j].x<<" "<<par2[j].y<<" "<<par[j].x<<" "<<par[j].y<<endl; p = j; break; // cout<< "hhhhhh"<<"I:"<<i<<" j:"<<j<<endl; } } ans[p]++; } // cout<<"Box"<<endl; memset(cnt,0,sizeof(cnt)); for ( int i = 1 ; i <= n+1 ; i++) { // if (ans[i]==0) continue; // cnt[ans[i]]++; printf("%d: %d\n",i-1,ans[i]); } puts(""); // for ( int i = 1 ; i <= m ; i++) // { // if (cnt[i]==0) continue; // printf("%d: %d\n",i,cnt[i]); // } } return 0; }
View Code
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.
直接上代码
相关文章推荐
- 选择>努力
- jquery实现简易的移动端验证表单
- HDU 2064 二分图 匈牙利算法 零基本教学
- Tomcat之——添加CROS跨源请求
- nginx和lua开发二
- Android中常用对话框
- spring的bean配置文件中p:
- 解决Mysql乱码问题
- REM实战
- linux命令-vim命令模式
- [HDOJ4081]Qin Shi Huang's National Road System
- 【Java高级】反射+工厂模式+属性文件的实例应用
- 接口
- Linux之MariaDB基础总结(一)
- 数据结构基础5.2:二叉搜索树(BST)的基本操作(插入、查找、删除)
- 杭电2571 命运
- 索引(index)_普通索引、唯一索引和复合索引.索引查询
- Ajax上传文件
- poj 2074 Line of Sight (计算几何,细节题)
- Study of the Symbolic Exection