Show all threads Hide all threads Show all messages Hide all messages |
Why does a simple sort of pairs + finding the median not work? | sweepea | 1207. Median on the Plane | 12 Nov 2024 14:43 | 2 |
All the test cases I've come up with have been solved correctly by this method but no AC :( code: ``` #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<vector<int>> x(n, vector<int>(3, 0)); for (int i = 0; i < n; i++) { cin >> x[i][0]; cin >> x[i][1]; x[i][2] = i + 1; } sort(x.begin(), x.end()); auto med = (n - 1) / 2; cout << x[med][2] << " " << x[med + 1][2]; } ``` Edited by author 11.11.2024 16:23 Edited by author 11.11.2024 16:23 To answer my own question. The above code will select for the 2 median points. These don't necessarily create a median line. Consider the following test case: 4 0 10 1 1 2 2 3 10 The above alogorithm would select (1, 1) and (2,2), which partitions the plane into a section with 2 points, and a section with 0 points; the above approach is incorrect. |
Why WA on test case 7? please help | farid | 1207. Median on the Plane | 17 May 2022 23:27 | 2 |
#include <bits/stdc++.h> using namespace std;
int const N = 123456; #define pi acos(-1.0) typedef long long ll;
int ar[N],xar[N],uses[N];
struct points { double x, y; int id; points() {} points(double x, double y) : x(x), y(y) {} } ;
double ang(const points &p){ double res = atan2(p.y, p.x); if(res < 0) res += 2.0 * pi; return res; }
struct cmp{ inline bool operator () (const points &p1, const points &p2){ double ang1 = ang(p1)*(180/pi), ang2 = ang(p2)*(180/pi); if(fabs(ang1 - ang2) < 1e-9){ ll d1 = (ll)p1.x * (ll)p1.x + (ll)p1.y * (ll)p1.y; ll d2 = (ll)p2.x * (ll)p2.x + (ll)p2.y * (ll)p2.y;
return d1 < d2; } return ang1 < ang2; } };
points pt[N];
int main() { int n; cin >> n; for(int i = 0; i < n; i++){ cin >> pt[i].x >> pt[i].y; pt[i].id = i+1; } sort(pt, pt+n, cmp()); cout<<pt[0].id<<" "<<pt[(n/2)].id<<endl;
return 0; } I know this is an old post. but for others' reference, we should use long long to avoid integer overflow. |
TEST 4 ??? | Ruslan | 1207. Median on the Plane | 9 Mar 2022 23:03 | 1 |
|
WA #6 | Furkat Ahrolov | 1207. Median on the Plane | 22 Dec 2021 04:28 | 1 |
WA #6 Furkat Ahrolov 22 Dec 2021 04:28 Help please!! What is test 6???? |
WA#2 | JavXn1 | 1207. Median on the Plane | 6 May 2021 00:41 | 1 |
WA#2 JavXn1 6 May 2021 00:41 import math import operator n = int(input()) s = input() l1 = s.split(" ") x = int(l1[0]) y = int(l1[1]) sp = {} for i in range(n-1): s = input().split(" ") x1 = int(s[0]) y1 = int(s[1]) dx = x1-x dy = y1-y dl = math.sqrt(math.pow(dx,2)+math.pow(dy,2)) a = math.degrees(math.asin(dy/dl)) #print(a) sp.update({i+2:a}) sorted_tuples = sorted(sp.items(), key=operator.itemgetter(1)) #print(sorted_tuples) print("1") print(sorted_tuples[len(sorted_tuples)//2][0]) |
How do you think, why it`s not work? UPD: CLOSED!!! | AleshinAndrei | 1207. Median on the Plane | 12 Aug 2020 22:59 | 1 |
UPD: i have find mistake Edited by author 13.08.2020 00:16 |
I don't understand the problem | Dmitry Rudenko | 1207. Median on the Plane | 21 Jul 2020 14:24 | 2 |
Why do they want me to do? How to understand "equal-sized parts"? Same amount of dots on both sides Edited by author 21.07.2020 14:25 |
Please help, why WA on test #6? | Natasha | 1207. Median on the Plane | 15 Apr 2016 16:12 | 1 |
//1152 #include <iostream> #include <vector> #include <cmath> using namespace std; int main() { int n, x0, y0, index0; cin >> n; if (n == 2){ cout << 1 + ' ' + 2; } else { vector<vector<int>> p(n, vector<int>(2)); // [x, y] vector<double> k(n); for (int i = 0; i < n; i++) { cin >> p[i][0] >> p[i][1]; } x0 = y0 = 2147483647; for (int i = 0; i < n; i++) { if (p[i][0] < x0 ||p[i][0] == x0 && p[i][1] < y0){ x0 = p[i][0]; y0 = p[i][1]; index0 = i; } } for (int i = 0; i < n; i++) { //y = kx + m if (i == index0) { k[i] = 9223372036854775807; } else { if (x0 == p[i][0]) { k[i] = 9223372036854775807; } else { k[i] = (p[i][1] - y0) / (p[i][0] - x0); } } }
vector<int> d(n); for (int i = 0; i < n; i++) { d[i] = i; } double tempD; int tempI; for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < i; j++) { if (k[j + 1] < k[j]) { tempD = k[j + 1]; k[j + 1] = k[j]; k[j] = tempD; tempI = d[j + 1]; d[j + 1] = d[j]; d[j] = tempI; } } } cout << index0 + 1 << ' ' << d[n / 2 -1] + 1; } return 0; } |
WA#3 | Kamaldinov Dmitry | 1207. Median on the Plane | 20 Mar 2015 21:52 | 1 |
WA#3 Kamaldinov Dmitry 20 Mar 2015 21:52 |
Why wrong answer? | Grigorenko Vlad | 1207. Median on the Plane | 16 Jun 2013 17:17 | 1 |
using System; class point{ public int x, y, id; } class EntryPoint { static void Main() { point[] v=new point [10001]; int n = Convert.ToInt32(Console.ReadLine()); int min_i=0; int min = 20000000; for (int i = 0; i < n; i++) { v[i] = new point(); string[] str = Console.ReadLine().Split(' '); v[i].x = Convert.ToInt32(str[0]); v[i].y = Convert.ToInt32(str[1]); v[i].id = i+1; if (v[i].y < min) { min = v[i].y; min_i = i; } } swap(ref v[0], ref v[min_i]); for (int i = 0; i < n; i++) { v[i].x -= v[i].x; v[i].y -= v[i].y; } fast_sort(v,0,n-1); Console.WriteLine(min_i+1+" "+v[n/2+1].id); } static int fync(point a, point b) { if ((a.x > 0) && (b.y == 0)) return 1; if (a.x * b.y < a.y * b.x) return 1; return 0; } static void swap(ref point a, ref point b) { point tmp; tmp = a; a = b; b = a; } static void fast_sort(point[] arr,int low,int high) { int i,j; int x; i = low; j = high; x = arr[(i + j) / 2].y; do { while (arr[i].y < x) ++i; while (arr[j].y > x) --j; if (i <= j) { int temp = arr[i].y; arr[i].y = arr[j].y; arr[j].y = temp; i++; j--; } } while (i <= j); if (low < j) fast_sort(arr, low, j); if (i < high) fast_sort(arr, i, high); } } |
To admins: weak tests? | sklyack | 1207. Median on the Plane | 14 Mar 2013 02:23 | 1 |
My last submission (4829935) got AC, but I think this algo is wrong. For example, for test 8 0 0 3 -2 1 -3 -1 -5 -3 -3 -3 1 -5 6 3 1 it gives 1 5. Actually, I have no idea how it can get AC... |
Fast solution | ONU_Latysh | 1207. Median on the Plane | 4 Dec 2012 17:59 | 2 |
I solved using too mush time Can anyone explain me how some people did it in 0.031?? Please) This problem can be solved via sorting by angle in this time)) |
Does this test correct? | Andrew Sboev | 1207. Median on the Plane | 28 May 2012 21:58 | 1 |
3 0 0 0 1 1 0 ? Oh, sorry, of course not, because n is even. Edited by author 28.05.2012 21:59 |
WA 5 solution for C++ | ONU_Latysh | 1207. Median on the Plane | 13 Feb 2012 21:52 | 1 |
if you have WA 5 try changing all data types to long long(int64) same i guess goes for other languages I had int for coordinates and got WA 5 and then just changed it to long long and AC |
looking for help...... WA#2 | williamljb | 1207. Median on the Plane | 14 Jan 2012 13:07 | 4 |
program p1207; uses math; type realer=record rr:real; num:longint; end; var i,j,k,n,m,x,y,x0,y0:longint; r:array[1..10000]of realer; procedure qs(h,t:longint); var i,j:longint; k:real; o:realer; begin if h>=t then exit; i:=h;j:=t;k:=r[(i+j)div 2].rr; repeat while r[i].rr<k do inc(i); while r[j].rr>k do dec(j); if i<=j then begin o:=r[i]; r[i]:=r[j]; r[j]:=o; inc(i); dec(j); end; until i>j; qs(h,j); qs(i,t); end; begin readln(n); readln(x0,y0); for i:=2 to n do begin readln(x,y); x:=x-x0; y:=y-y0; if x=0 then if y>0 then r[i-1].rr:=0.5*pi else r[i-1].rr:=-0.5*pi else r[i-1].rr:=arctan(y/x); r[i-1].num:=i; end; qs(1,n-1); writeln(1,' ',r[n div 2].num); end. Any hints or tests can be helpful! Thanks! This test helped me to kill WA2 2 1 1 -1 -1 And don't forget about size of types! test BaJIuK 14 Jan 2012 13:07 4 -999999999 1000000000 -1000000000 999999999 1000000000 -1000000000 -1000000000 1000000000 out: 3 4 |
why WA text 3???help me | skyming | 1207. Median on the Plane | 30 Oct 2011 08:06 | 1 |
mycode: #include<stdio.h> #include<math.h> #include<algorithm> using namespace std; #define N 10002 struct node { double x,y; int num; }point[N]; int n; int cmp(node a,node b) { double pp=atan2(a.y-point[0].y,a.x-point[0].x); double qq=atan2(b.y-point[0].y,b.x-point[0].x); //double pp=(a.x-point[0].x)*(b.y-point[0].y)-(a.y-point[0].y)*(b.x-point[0].x); return pp>qq; } int main() { scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%lf%lf",&point[i].x,&point[i].y); point[i].num=i+1; } int pos=0; /*for(int i=1;i<n;i++) { if(point[i].x<point[pos].x||(point[i].x==point[pos].x)&&point[i].y<point[pos].y) pos=i; } swap(point[0],point[pos]);*/ sort(point+1,point+n,cmp); printf("%d %d\n",point[0].num ,point[n/2].num); return 0; } what it`s function in /* */ |
In test #13 more than two point belong to same line | maxx | 1207. Median on the Plane | 22 Sep 2011 17:54 | 2 |
It's 1-st point and two others. thank you! I choose 1st point and another point: Part of my code: //........ if ( A*p[j].x + B*p[j].y >= C ) ++up;//if use A*p[j].x + B*p[j].y > C => WA13 else if( A*p[j].x + B*p[j].y < C ) ++down; //........ if(up==down){ printf( "1 %hd\n", ++i ); return 0; } |
What is in test 9 | Uran | 1207. Median on the Plane | 13 Sep 2011 17:10 | 4 |
there is my code, its get WA on 9th test: #include <iostream.h> __int64 inline linea(long x1i,long y1i,long x2i,long y2i) {return y2i-y1i;} __int64 inline lineb(long x1i,long y1i,long x2i,long y2i) {return x1i-x2i;} __int64 inline linec(long x1i,long y1i,long x2i,long y2i) {return y1i*x2i-x1i*y2i;} int main() { const long con=20000; long n,i,j,k,p1,p2; long x[con],y[con]; __int64 a,b,c; cin>>n; p1=0;p2=1; for (i=1;i<n+1;i++){cin>>x[i]>>y[i];} for (i=1;i<n+1;i++) { if (p1==p2){break;} for (j=i+1;j<n+1;j++) { p1=0;p2=0; a=linea(x[i],y[i],x[j],y[j]); b=lineb(x[i],y[i],x[j],y[j]); c=linec(x[i],y[i],x[j],y[j]); for (k=1;k<i;k++) { if (-b*y[k]>=a*x[k]+c){p1++;} if (-b*y[k]<=a*x[k]+c){p2++;} } for (k=i+1;k<j;k++) { if (-b*y[k]>=a*x[k]+c){p1++;} if (-b*y[k]<=a*x[k]+c){p2++;} } for (k=j+1;k<n+1;k++) { if (-b*y[k]>=a*x[k]+c){p1++;} if (-b*y[k]<=a*x[k]+c){p2++;} } if (p1==p2) {cout<<i<<" "<<j;break;} } } return 0; } Can anybody help me? long x[con],y[con] -> long long :), but you can get tle in 14 :P I don't understand,it should be TLE. |
For Pascal, qsort works fine . | Nguyen Khac Tung | 1207. Median on the Plane | 30 Mar 2011 19:34 | 1 |
Just be careful with the angles. |
My program works right, please check it, or give me some tests please | Redjee | 1207. Median on the Plane | 9 May 2010 19:46 | 1 |
type point=record x,y:longint; end; var vert:array[1..10000] of point; visited:array[1..10000] of byte; min:point; n,i,k,imin,minvert:integer; Procedure Search_Min_Polar_Angle; var i:integer; min_angle,angle,tg,a,b:real; begin min_angle:=pi; for i:=1 to n do if (visited[i]=0) then begin a:=(vert[i].y-vert[imin].y); b:=(vert[i].x-vert[imin].x); if (b=0) then angle:=pi/2 else begin tg:=a/b; angle:=arctan(tg); end; if (angle<min_angle) then begin min_angle:=angle; minvert:=i; end; end; visited[minvert]:=1; end; Begin readln(n); for i:=1 to n do visited[i]:=0;
// naxojdenie samoy nijney levoy to4ki min.y:=maxlongint; for i:=1 to n do begin readln(vert[i].x,vert[i].y); if (vert[i].y<min.y) then begin min:=vert[i]; imin:=i; end else if (vert[i].y=min.y) and (vert[i].x<min.x) then begin min:=vert[i]; imin:=i; end; end; visited[imin]:=1; //----------------------------------- for k:=1 to n div 2 do Search_Min_Polar_Angle; writeln(imin,' ',minvert); End. Edited by author 09.05.2010 19:47 |