NC232843. Amphiphilic Carbon Molecules
描述
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.
输入描述
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.
输出描述
For each test case, output a line containing a single integer, which is the maximum number of dissolved ACM particles.
示例1
输入:
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
输出:
3 3 6
C++(clang++ 11.0.1) 解法, 执行用时: 1485ms, 内存消耗: 712K, 提交时间: 2023-04-10 14:28:57
#include <iostream> #include <cmath> #include <algorithm> #define x first #define y second using namespace std; const int N = 1e3 + 100; const double eps = 1e-8; int cmp(double a, double b) { if(abs(a - b) < eps) return 0; if(a < b) return -1; return 1; } int n; struct Point { int x, y, z; }p[N]; struct Data { int x, y; double angle; bool operator < (const Data& a) const { return cmp(angle, a.angle) < 0; } int operator * (const Data& a) const { return x * a.y - y * a.x; } }a[N]; int main() { while(scanf("%d", &n), n) { int ans = 0; for(int i = 0; i < n; i ++) scanf("%d%d%d", &p[i].x, &p[i].y, &p[i].z); if(n <= 2) { printf("%d\n", n); continue; } for(int i = 0; i < n; i ++) { int cnt = 0; for(int j = 0; j < n; j ++) if(i != j) { a[cnt] = { p[j].x - p[i].x, p[j].y - p[i].y }; if(p[j].z == 1) a[cnt].x = -a[cnt].x, a[cnt].y = -a[cnt].y; a[cnt].angle = atan2(a[cnt].y, a[cnt].x); cnt ++; } sort(a, a + cnt); int res = 2; for(int i = 0, j = 1; i < cnt; i ++, res --) { if(i == j) j = (j + 1) % cnt, res ++; while(i != j && a[i] * a[j] >= 0) j = (j + 1) % cnt, res ++; ans = max(ans, res); } } printf("%d\n", ans); } return 0; }
C++(g++ 7.5.0) 解法, 执行用时: 1526ms, 内存消耗: 592K, 提交时间: 2022-10-06 13:05:58
#include<iostream> #include<algorithm> #include<vector> #include<cmath> #define _for(i,L,R) for(int i=L;i<=R;++i) //#define int long long using namespace std; const int N=1e3+5; int n,res; class node{ public: int x,y,op; double ang; bool operator<(const node & o)const{ return ang<o.ang; } }p[N]; int cross(node a,node b) { return a.x*b.y-a.y*b.x; } void work(int u) { vector<node> vec; _for(i,1,n){ if(u==i) continue; int dx=p[i].x-p[u].x,dy=p[i].y-p[u].y; if(p[i].op) dx*=-1,dy*=-1; vec.push_back((node){dx,dy,0,atan2(1.0*dy,1.0*dx)}); } sort(vec.begin(),vec.end()); int sum=2,r=0,n=vec.size(); _for(l,0,n-1){ if(l==r) r=(r+1)%n,sum++; while(l!=r and cross(vec[l],vec[r])>=0) r=(r+1)%n,sum++; sum--; res=max(res,sum); } } signed main() { while(~scanf("%d",&n),n){ _for(i,1,n){ int x,y,op; scanf("%d%d%d",&x,&y,&op); p[i]={x,y,op}; } if(n<=3){ printf("%d\n",n); continue; } res=0; _for(i,1,n) work(i); printf("%d\n",res); } return 0; }