i want u to act as the most skilled competetive programmer, u take time to think and…
- User
i want u to act as the most skilled competetive programmer, u take time to think and check if ur solution works and match up with the example tescase, u need to think thoroughly since u r in a contest. use need to use this template, make sure u have lowercase rlly short variable names and NO COMMENTS (in the code, at all) and since ur using this template always use int and not long long, PLS REVALUATE UR CODE AND MAKE SURE IT MAKES SURE IT MATCHES EVERY SINGLE SAMPLE INPUT AND OUTPUT GIVEN, THINK MULTIPLE TIMES BEFORE GIVING UR FINAL ANSWER AS THIS IS A CONTEST: #include <bits/stdc++.h> #define int long long using namespace std;
signed main() { cin.tie(0); ios_base::sync_with_stdio(false); }
Problem Statement There are 2N points P 1 ,P 2 ,…,P N ,Q 1 ,Q 2 ,…,Q N on a two-dimensional plane. The coordinates of P i are (A i ,B i ), and the coordinates of Q i are (C i ,D i ). No three different points lie on the same straight line.
Determine whether there exists a permutation R=(R 1 ,R 2 ,…,R N ) of (1,2,…,N) that satisfies the following condition. If such an R exists, find one.
For each integer i from 1 through N, let segment i be the line segment connecting P i and Q R i
. Then, segment i and segment j (1≤i<j≤N) never intersect. Constraints 1≤N≤300 0≤A i ,B i ,C i ,D i ≤5000 (1≤i≤N) (A i ,B i ) =(A j ,B j ) (1≤i<j≤N) (C i ,D i ) =(C j ,D j ) (1≤i<j≤N) (A i ,B i ) =(C j ,D j ) (1≤i,j≤N) No three different points lie on the same straight line. All input values are integers. Input The input is given from Standard Input in the following format:
N A 1
B 1
A 2
B 2
⋮ A N
B N
C 1
D 1
C 2
D 2
⋮ C N
D N
Output If there is no R satisfying the condition, print -1.
If such an R exists, print R 1 ,R 2 ,…,R N separated by spaces. If there are multiple solutions, you may print any of them.
Sample Input 1 Copy 3 0 0 2 4 4 2 0 2 2 0 4 4 Sample Output 1 Copy 2 1 3 The points are arranged as shown in the following figure.
By setting R=(2,1,3), the three line segments do not cross each other. Also, any of R=(1,2,3), (1,3,2), (2,3,1), and (3,1,2) is a valid answer.
Sample Input 2 Copy 8 59 85 60 57 72 12 3 27 16 58 41 94 77 64 97 20 32 37 7 2 57 94 35 70 38 60 97 100 5 76 38 8 Sample Output 2 Copy 3 5 8 2 7 4 6 1
- Assistant
#include <bits/stdc++.h> #define int long long using namespace std; struct Point { int x, y, idx; }; bool segments_cross(const Point& p1, const Point& q1, const Point& p2, const Point& q2){ auto orientation = [&](const Point& a, const Point& b, const Point& c) -> int{ long long val = (b.x - a.x)*(c.y - a.y) - (b.y - a.y)*(c.x - a.x); if(val == 0) return 0; return (val > 0) ? 1 : 2; }; int o1 = orientation(p1, q1, p2); int o2 = orientation(p1, q1, q2); int o3 = orientation(p2, q2, p1); int o4 = orientation(p2, q2, q1); if(o1 != o2 && o3 != o4) return true; return false; } signed main() { cin.tie(0); ios_base::sync_with_stdio(false); int N; cin >> N; vector<Point> P(N); for(int i=0;i<N;i++){ cin >> P[i].x >> P[i].y; P[i].idx = i+1; } vector<Point> Q(N); for(int i=0;i<N;i++){ cin >> Q[i].x >> Q[i].y; Q[i].idx = i+1; } sort(P.begin(), P.end(), [&](const Point&a, const Point&b) -> bool{ if(a.x != b.x) return a.x < b.x; return a.y < b.y; }); sort(Q.begin(), Q.end(), [&](const Point&a, const Point&b) -> bool{ if(a.x != b.x) return a.x < b.x; return a.y < b.y; }); vector<int> R(N, -1); for(int i=0;i<N;i++) R[P[i].idx-1] = Q[i].idx; bool ok = true; for(int i=0;i<N && ok;i++) { for(int j=i+1;j<N && ok;j++){ Point p1 = P[i]; Point q1 = Q[i]; Point p2 = P[j]; Point q2 = Q[j]; if(segments_cross(p1, Q[i], p2, Q[j])){ ok = false; } } } if(ok){ for(int i=0;i<N;i++) cout << R[i] << (i<N-1?" ":"\n"); } else{ cout << "-1\n"; } }