【POJ2280】Amphiphilic Carbon Molecules——扫描线
2016-04-13 21:44
357 查看
Amphiphilic Carbon Molecules
Description
Shanghai Hypercomputers, the world’s largest computer chip manufacturer, has invented a new class of nanoparticles called Amphiphilic Carbon Molecules (ACMs). ACMs are semiconductors. It means that they can be either conductors or insulators of electrons, and thus possess a property that is very important for the computer chip industry. They are also amphiphilic molecules, which means parts of them are hydrophilic while other parts of them are hydrophobic. Hydrophilic ACMs are soluble in polar solvents (for example, water) but are insoluble in nonpolar solvents (for example, acetone). Hydrophobic ACMs, on the contrary, are soluble in acetone but insoluble in water. Semiconductor ACMs dissolved in either water or acetone can be used in the computer chip manufacturing process.
As a materials engineer at Shanghai Hypercomputers, your job is to prepare ACM solutions from ACM particles. You go to your factory everyday at 8 am and find a batch of ACM particles on your workbench. You prepare the ACM solutions by dripping some water, as well as some acetone, into those particles and watch the ACMs dissolve in the solvents. You always want to prepare unmixed solutions, so you first separate the ACM particles by placing an Insulating Carbon Partition Card (ICPC) perpendicular to your workbench. The ICPC is long enough to completely separate the particles. You then drip water on one side of the ICPC and acetone on the other side. The ICPC helps you obtain hydrophilic ACMs dissolved in water on one side and hydrophobic ACMs dissolved in acetone on the other side. If you happen to put the ICPC on top of some ACM particles, those ACMs will be right at the border between the water solution and the acetone solution, and they will be dissolved. Fig.1 shows your working situation.
Fig. 1
Your daily job is very easy and boring, so your supervisor makes it a little bit more challenging by asking you to dissolve as much ACMs into solution as possible. You know you have to be very careful about where to put the ICPC since hydrophilic ACMs on the acetone side, or hydrophobic ACMs on the water side, will not dissolve. As an experienced engineer, you also know that sometimes it can be very difficult to find the best position for the ICPC, so you decide to write a program to help you. You have asked your supervisor to buy a special digital camera and have it installed above your workbench, so that your program can obtain the exact positions and species (hydrophilic or hydrophobic) of each ACM particle in a 2D pictures taken by the camera. The ICPC you put on your workbench will appear as a line in the 2D pictures.
Fig. 2
Input
There will be no more than 10 test cases. Each case starts with a line containing an integer N, which is the number of ACM particles in the test case. N lines then follow. Each line contains three integers x, y, r, where (x, y) is the position of the ACM particle in the 2D picture and r can be 0 or 1, standing for the hydrophilic or hydrophobic type ACM respectively. The absolute value of x, y will be no larger than 10000. You may assume that N is no more than 1000. N = 0 signifies the end of the input and need not be processed. Fig.2 shows the positions of ACM particles and the best ICPC position for the last test case in the sample input.
Output
For each test case, output a line containing a single integer, which is the maximum number of dissolved ACM particles.
Sample Input
3
0 0 0
0 1 0
2 2 1
4
0 0 0
0 4 0
4 0 0
1 2 1
7
-1 0 0
1 2 1
2 3 0
2 1 1
0 3 1
1 4 0
-1 2 0
0
Sample Output
3
3
6
在平面上有一些点,每个点都有0,1其中一个属性。要求用一条直线将平面分成两部分。其中一部分取属性为0的点,另一部分取属性为1的点。若点在直线上全部取走,问最多能取多少个点。
分析:看到题的时候直接敲了一发O(n3)O(n^3)的暴力,果断的TLE了,硬伤。
对于v1→v2v_1 \rightarrow v_2我们可以发现我们仅仅需要统计的只是从v1v_1转换到v2v_2中点的变化,但是极角排序排序基点是左下角的点,所以对于在选定基点的左下方的点,我们可以将其进行关于基点的对称。
Time Limit: 20000MS | Memory Limit: 65536K |
---|
Shanghai Hypercomputers, the world’s largest computer chip manufacturer, has invented a new class of nanoparticles called Amphiphilic Carbon Molecules (ACMs). ACMs are semiconductors. It means that they can be either conductors or insulators of electrons, and thus possess a property that is very important for the computer chip industry. They are also amphiphilic molecules, which means parts of them are hydrophilic while other parts of them are hydrophobic. Hydrophilic ACMs are soluble in polar solvents (for example, water) but are insoluble in nonpolar solvents (for example, acetone). Hydrophobic ACMs, on the contrary, are soluble in acetone but insoluble in water. Semiconductor ACMs dissolved in either water or acetone can be used in the computer chip manufacturing process.
As a materials engineer at Shanghai Hypercomputers, your job is to prepare ACM solutions from ACM particles. You go to your factory everyday at 8 am and find a batch of ACM particles on your workbench. You prepare the ACM solutions by dripping some water, as well as some acetone, into those particles and watch the ACMs dissolve in the solvents. You always want to prepare unmixed solutions, so you first separate the ACM particles by placing an Insulating Carbon Partition Card (ICPC) perpendicular to your workbench. The ICPC is long enough to completely separate the particles. You then drip water on one side of the ICPC and acetone on the other side. The ICPC helps you obtain hydrophilic ACMs dissolved in water on one side and hydrophobic ACMs dissolved in acetone on the other side. If you happen to put the ICPC on top of some ACM particles, those ACMs will be right at the border between the water solution and the acetone solution, and they will be dissolved. Fig.1 shows your working situation.
Fig. 1
Your daily job is very easy and boring, so your supervisor makes it a little bit more challenging by asking you to dissolve as much ACMs into solution as possible. You know you have to be very careful about where to put the ICPC since hydrophilic ACMs on the acetone side, or hydrophobic ACMs on the water side, will not dissolve. As an experienced engineer, you also know that sometimes it can be very difficult to find the best position for the ICPC, so you decide to write a program to help you. You have asked your supervisor to buy a special digital camera and have it installed above your workbench, so that your program can obtain the exact positions and species (hydrophilic or hydrophobic) of each ACM particle in a 2D pictures taken by the camera. The ICPC you put on your workbench will appear as a line in the 2D pictures.
Fig. 2
Input
There will be no more than 10 test cases. Each case starts with a line containing an integer N, which is the number of ACM particles in the test case. N lines then follow. Each line contains three integers x, y, r, where (x, y) is the position of the ACM particle in the 2D picture and r can be 0 or 1, standing for the hydrophilic or hydrophobic type ACM respectively. The absolute value of x, y will be no larger than 10000. You may assume that N is no more than 1000. N = 0 signifies the end of the input and need not be processed. Fig.2 shows the positions of ACM particles and the best ICPC position for the last test case in the sample input.
Output
For each test case, output a line containing a single integer, which is the maximum number of dissolved ACM particles.
Sample Input
3
0 0 0
0 1 0
2 2 1
4
0 0 0
0 4 0
4 0 0
1 2 1
7
-1 0 0
1 2 1
2 3 0
2 1 1
0 3 1
1 4 0
-1 2 0
0
Sample Output
3
3
6
在平面上有一些点,每个点都有0,1其中一个属性。要求用一条直线将平面分成两部分。其中一部分取属性为0的点,另一部分取属性为1的点。若点在直线上全部取走,问最多能取多少个点。
分析:看到题的时候直接敲了一发O(n3)O(n^3)的暴力,果断的TLE了,硬伤。
对于v1→v2v_1 \rightarrow v_2我们可以发现我们仅仅需要统计的只是从v1v_1转换到v2v_2中点的变化,但是极角排序排序基点是左下角的点,所以对于在选定基点的左下方的点,我们可以将其进行关于基点的对称。
#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <string> #include <queue> #include <stack> #include <vector> #include <iostream> #include <algorithm> using namespace std; const double eps = 1e-6; const int Max = 1100; typedef struct node { int x,y,op; node(int x = 0,int y = 0):x(x),y(y) {} node operator + (const node &a)const { return node(x+a.x,y+a.y); } node operator - (const node &a)const { return node(x-a.x,y-a.y); } }Point; Point p[Max],s[Max]; int dbcmp(double x)//精度控制 { if(fabs(x)<eps) return 0; return x>0?1:-1; } double Cross(node a, node b) // 叉积 { return a.x*b.y-a.y*b.x; } double Dot(node a,node b) //点积 { return a.x*b.x+a.y*b.y; } double Distance(node a) //计算距离 { return a.x*a.x+a.y*a.y; } node p0; bool cmp(node a,node b) //比较函数 { int ans = dbcmp(Cross(a,b)); if(ans>0) { return true; } else if(ans==0) { return Distance(a)>=Distance(b); } return false; } int n; int main() { while(scanf("%d",&n)&&n) { for(int i = 0;i<n;i++) { scanf("%d %d %d",&p[i].x,&p[i].y,&p[i].op); } int ans = 0; for(int i = 0;i<n;i++) { int sum1 = 0,sum2 = 0; for(int j = 0;j<n;j++) { s[j].x = p[j].x -p[i].x; s[j].y = p[j].y-p[i].y; s[j].op = p[j].op; if(s[j].y<0||(s[j].y==0&&s[j].x<0)) { s[j].y = -s[j].y; s[j].x = -s[j].x; s[j].op = (1+s[j].op)%2; } if(s[j].op) sum1++; else sum2++; } sort(s,s+n,cmp); int ans1 = 0,ans2 = 0; int num1 = 0,num2 = 0; int j = 0 ,t =0 ; for(j = 0;j<n;j++) { if(s[j].op) ans1++; else ans2++; while(dbcmp(Cross(s[j],s[t]))) { if(s[t].op) num1++; else num2++; t++; } ans = max(ans,max(sum1-ans1+num2,sum2-ans2+num1)+(ans1-num1+ans2-num2)); } } printf("%d\n",ans); } return 0; }
相关文章推荐
- 【c语言】编写一个函数new,对n个字符开辟连续的存储空间,此函数应返回一个指针(地址),指向字符串开始的空间
- codeforces_455B
- Access转成Sql 2008步骤,同时解决自动编号问题,主键,id数值不重置
- 虚拟机类的加载过程
- java 树递归
- java8新语法学习
- JDK安装与环境变量配置
- 线段树 2016.4.15
- Golang实现ping
- sleep()方法和yield()方法的区别
- 【c语言】一个字符串,包含n个字符。将此字符串中从第m个字符开始的全部字符复制成为另一个字符串。
- poj_1236_Network of Schools
- 软件工程_6th weeks
- (java)从零开始之--异常处理(以文件拷贝为例)
- Java中的XML
- HDU1171(01背包)
- CountDownLatch
- oracle SELECT INTO 和 INSERT INTO SELECT 两种表复制语句详解
- POJ 3181 Dollar Dayz【递推 拆分存大数】
- 交通图最短路径算法