QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#117962 | #3224. Directions | i_am_noob# | WA | 53ms | 7860kb | C++14 | 3.1kb | 2023-07-02 18:11:47 | 2023-07-02 18:11:49 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define pii pair<int, int>
#define all(a) a.begin(), a.end()
#define sz(a) ((int)a.size())
const int mod = 1000'000'007, N = 300005;
int add(int x, int y){x+=y; if(x>=mod) x-=mod; return x;}
int sub(int x, int y){x-=y; if(x<0) x+=mod; return x;}
int mul(int x, int y){return ((ll)x)*y%mod;}
int Pow(int x, ll y=mod-2){int res=1; for(; y; x=mul(x,x),y>>=1) if(y&1) res=mul(res,x); return res;}
const double eps = 1e-8, pi = acos(-1);
int sign(ll x) {return abs(x) <= eps ? 0 : (x > 0 ? 1 : -1);}
struct Pt {
ll x, y, val;
Pt (ll _x=0, ll _y=0) : x(_x), y(_y) {}
Pt operator + (Pt o) {return Pt(x + o.x, y + o.y);}
Pt operator - (Pt o) {return Pt(x - o.x, y - o.y);}
Pt operator * (ll k) {return Pt(x * k, y * k);}
Pt operator / (ll k) {return Pt (x / k, y / k);}
ll operator * (Pt o) {return x * o.x + y * o.y;}
ll operator ^ (const Pt& o) const {return x * o.y - y * o.x;}
};
int ori(Pt o, Pt a, Pt b) {return sign((o - a) ^ (o - b));}
struct Line {
Pt a, b;
};
int pos(const Pt& a){return sign(a.y) == 0 ? sign(a.x) < 0 : sign(a.y) < 0;}
bool cmp(const Pt& a, const Pt& b){return pos(a) == pos(b) ? sign(a ^ b) > 0 : pos(a) < pos(b);}
int n;
vector<Pt> a;
ll get_val(int x, int y){
Pt tmp(x,y);
auto it=lower_bound(all(a),tmp,cmp);
if(it!=a.end()&&it->x==x&&it->y==y) return it->val;
return 1e18;
}
int nxt(int i){return i+1==n?0:i+1;}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n; a.resize(n);
for(int i=0; i<n; ++i){
cin >> a[i].x >> a[i].y >> a[i].val;
int g=__gcd(abs(a[i].x),abs(a[i].y));
if(g>0) a[i].x/=g,a[i].y/=g;
}
sort(all(a),cmp);
vector<Pt> b;
for(int i=0; i<n; ++i){
if(a[i].x==0&&a[i].y==0) continue;
if(sz(b)&&b.back().x==a[i].x&&b.back().y==a[i].y) b.back().val=min(b.back().val,a[i].val);
else b.pb(a[i]);
}
a=b;
n=sz(a);
ll res=1e18;
for(int i=0; i<n; ++i) res=min(res,get_val(a[i].x,a[i].y)+get_val(-a[i].x,-a[i].y)+get_val(a[i].y,-a[i].x)+get_val(-a[i].y,a[i].x));
ll minn=1e18,minid=-1;
for(int i=0; i<n; ++i) if(a[i].val<minn) minn=a[i].val,minid=i;
int pos1=minid,pos2=lower_bound(all(a),Pt(-a[minid].x,-a[minid].y),cmp)-a.begin();
if(pos2==n) pos2=0;
vector<Pt> vec1,vec2;
vec1.pb(a[minid]);
bool flag=0;
for(int i=nxt(pos1); i!=pos1; i=nxt(i)){
if(i==pos2) flag=1;
if(flag) vec2.pb(a[i]);
else vec1.pb(a[i]);
}
for(int _: {0,1}){
if(vec1.empty()) break;
if((vec1[0].x!=a[minid].x||vec1[0].y!=a[minid].y)&&(vec1[0].x!=-a[minid].x||vec1[0].y!=-a[minid].y)) break;
int cur=0;
ll minn=1e18;
while(cur<sz(vec2)&&sign(vec1[0]^vec2[cur])==0) cur++;
for(int i=1; i<sz(vec1); ++i){
while(cur<sz(vec2)&&sign(vec1[i]^vec2[cur])>0) minn=min(minn,vec2[cur].val),cur++;
res=min(res,vec1[0].val+vec1[i].val+minn);
}
swap(vec1,vec2);
}
cout << (res>1e17?-1:res) << "\n";
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 42ms
memory: 7668kb
input:
200000 -1 -1 1 1 1 1 0 1 1 1 0 1 1 -1 1 1 1 1 1 1 1 -1 1 1 1 -1 1 0 1 1 0 0 1 0 0 1 1 -1 1 -1 -1 1 0 0 1 1 0 1 -1 1 1 -1 1 1 1 1 1 1 1 1 -1 1 1 0 -1 1 1 -1 1 -1 0 1 1 0 1 1 -1 1 0 -1 1 1 0 1 -1 1 1 1 0 1 -1 0 1 1 1 1 1 0 1 -1 -1 1 -1 1 1 -1 -1 1 0 -1 1 0 -1 1 1 1 1 1 -1 1 0 1 1 1 1 1 -1 1 1 1 -1 1 0...
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 46ms
memory: 7860kb
input:
200000 -1 -1 2 1 0 1 1 0 2 1 -1 2 1 0 1 -1 1 1 -1 -1 2 1 1 1 0 -1 2 -1 -1 1 0 1 2 0 -1 2 -1 0 2 0 0 2 -1 1 1 1 1 1 -1 0 1 1 -1 2 1 0 1 0 0 1 1 0 2 0 -1 1 0 1 1 0 -1 2 -1 1 2 0 1 2 -1 1 2 0 1 1 0 1 1 1 1 2 1 1 1 1 0 2 1 1 2 -1 1 2 -1 1 1 0 1 2 0 -1 2 1 0 2 0 1 1 0 -1 1 1 -1 1 1 0 2 0 -1 1 -1 0 1 0 -1...
output:
3
result:
ok single line: '3'
Test #3:
score: 0
Accepted
time: 50ms
memory: 7664kb
input:
200000 1 0 555860533 -1 1 633479355 0 0 411890838 -1 -1 411764668 0 0 324117889 1 1 147426106 1 0 41681213 1 -1 169580394 1 -1 204074237 -1 1 265787176 1 -1 204010614 0 -1 582574240 0 -1 98238758 1 1 489573021 -1 1 747647275 1 -1 933893240 0 -1 663924164 0 0 470849190 1 -1 479419247 -1 -1 53695974 0...
output:
95355
result:
ok single line: '95355'
Test #4:
score: 0
Accepted
time: 45ms
memory: 7688kb
input:
200000 -1 0 1 -2 2 1 0 0 1 1 1 1 2 -2 1 0 2 1 -1 -1 1 0 0 1 1 1 1 2 1 1 0 1 1 -2 -1 1 -1 -1 1 -2 2 1 -1 -2 1 0 0 1 -2 0 1 2 1 1 1 -2 1 1 -2 1 -1 -2 1 0 1 1 0 -1 1 1 -2 1 0 1 1 2 0 1 -1 1 1 1 0 1 2 -2 1 -1 2 1 -2 1 1 -1 0 1 2 1 1 0 1 1 -1 -1 1 0 2 1 2 2 1 2 -2 1 1 -2 1 -2 0 1 -2 -2 1 -2 1 1 1 2 1 2 -...
output:
3
result:
ok single line: '3'
Test #5:
score: 0
Accepted
time: 47ms
memory: 7632kb
input:
200000 -2 1 2 2 1 2 1 -1 2 1 2 2 -1 2 1 1 -1 2 1 -1 2 -2 -1 2 1 2 1 2 -1 2 0 1 2 1 -1 1 -2 2 1 -2 1 2 -1 0 2 2 0 1 1 -2 2 -1 1 1 1 -2 1 -2 1 2 -1 1 2 2 -2 1 -2 1 2 -2 1 1 -2 2 1 0 -2 1 2 -2 2 0 2 2 2 2 2 2 2 2 0 2 2 -2 2 2 1 -2 1 1 0 2 -1 -2 1 -2 1 2 0 -1 1 2 0 1 -1 -1 2 1 -2 1 -2 -2 1 1 -1 2 -2 2 1...
output:
3
result:
ok single line: '3'
Test #6:
score: -100
Wrong Answer
time: 53ms
memory: 7844kb
input:
200000 -1 -2 29368112 2 -1 24391896 0 -2 58495223 0 1 266081519 -2 -2 381375524 2 1 738559870 2 -2 407460939 -1 1 562555301 -2 2 185971753 2 -1 886154605 1 1 842679105 1 0 300033644 2 1 430527773 -1 -1 295205658 -1 2 903654949 -2 2 674980542 2 -2 725974565 0 1 924537846 -2 1 828956777 0 2 145229295 ...
output:
30250
result:
wrong answer 1st lines differ - expected: '50007', found: '30250'