Hướng dẫn giải của Sum of Vectors
Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này
Code mẫu của flashmt
#include <iostream> #include <algorithm> #include <cmath> using namespace std; const double PI = acos(-1.0); struct vec { double angle; long long x,y; vec() {} vec(long long _x,long long _y) { x=_x; y=_y; angle=atan2(y,x); } friend bool operator <(vec u,vec v) { return u.angle<v.angle; } } a[30300]; int n; int main() { int x,y; cin >> n; for (int i=0;i<n;i++) { cin >> x >> y; vec u(x,y); a[i]=u; } sort(a,a+n); long long ans=0,sx=0,sy=0; int i=0,j=-1; double dif=0; while (i<n) { if (++j==n) j=0, dif+=PI*2; while (a[j].angle+dif-a[i].angle>PI && i<n) { sx-=a[i].x; sy-=a[i].y; ans=max(ans,sx*sx+sy*sy); i++; } sx+=a[j].x; sy+=a[j].y; ans=max(ans,sx*sx+sy*sy); } cout << ans << endl; }
Code mẫu của ladpro98
#include <bits/stdc++.h> #define ii pair<int, int> #define X first #define Y second #define long long long const int N = 60030; const double eps = 1e-6; using namespace std; ii a[N]; int n; void operator -= (ii &A, ii B) {A.X -= B.X, A.Y -= B.Y;} void operator += (ii &A, ii B) {A.X += B.X, A.Y += B.Y;} bool cmp(const ii &a, const ii &b) {return atan2(a.Y, a.X) + eps < atan2(b.Y, b.X);} long len(ii &A) {return (long)A.X * A.X + (long)A.Y * A.Y;} int main() { scanf("%d", &n); for(int i = 0; i < n; i++) scanf("%d %d", &a[i].X, &a[i].Y); sort(a, a + n, cmp); #define next(j) ((j) + 1) % n int j = 0; ii now = a[0]; long res = len(now); for(int i = 0; i < n; i++) { while (next(j) != i && ((long)a[i].X * a[next(j)].Y >= (long)a[i].Y * a[next(j)].X)) { j = next(j); now += a[j]; res = max(res, len(now)); } now -= a[i]; res = max(res, len(now)); } printf("%lld\n", res); return 0; }
Code mẫu của RR
//Written by RR {$R+,Q+} {$Mode objFPC} uses math; const FINP = ''; FOUT = ''; MAXN = 60111; var f1,f2 : text; n : longint; angle : array[1..MAXN] of double; x,y : array[1..MAXN] of int64; procedure openF; begin assign(f1,FINP); reset(f1); assign(f2,FOUT); rewrite(f2); end; procedure closeF; begin close(f1); close(f2); end; procedure swap(var a,b:int64); inline; var temp:int64; begin temp:=a; a:=b; b:=temp; end; procedure swapr(var a,b:double); inline; var temp:double; begin temp:=a; a:=b; b:=temp; end; function theta(x,y:int64):double; inline; var t:double; begin if (x=0) and (y=0) then exit(0); t:=y/(abs(x)+abs(y)); if x<0 then exit(2-t) else if y<0 then exit(4+t); exit(t); end; procedure inp; var i:longint; begin read(f1,n); for i:=1 to n do begin read(f1,x[i],y[i]); angle[i]:=theta(x[i],y[i]); end; end; procedure sort(l,r:longint); inline; var i,j:longint; mid:double; begin i:=l; j:=r; mid:=angle[l+random(r-l+1)]; repeat while angle[i]<mid do inc(i); while angle[j]>mid do dec(j); if i<=j then begin swapr(angle[i],angle[j]); swap(x[i],x[j]); swap(y[i],y[j]); inc(i); dec(j); end; until i>j; if i<r then sort(i,r); if l<j then sort(l,j); end; function val(x,y:int64):int64; inline; begin exit( x*x + y*y ); end; procedure solve; var i,last:longint; res,sx,sy,sum:int64; begin for i:=1 to n do begin x[n+i]:=x[i]; y[n+i]:=y[i]; end; last:=0; sx:=0; sy:=0; sum:=0; res:=0; for i:=1 to n do begin while (last+1<i+n) and (val(sx+x[1+last],sy+y[1+last])>sum) do begin inc(last); sx+=x[last]; sy+=y[last]; sum:=val(sx,sy); end; res:=max(res,sum); sx-=x[i]; sy-=y[i]; sum:=val(sx,sy); end; writeln(f2,res); end; begin openF; inp; if n>0 then sort(1,n); solve; closeF; end.
Code mẫu của hieult
#include<cstdio> #include<cmath> #include<cstring> #include<cstdlib> #include<cassert> #include<ctime> #include<algorithm> #include<iterator> #include<iostream> #include<cctype> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<list> #include<iostream> #include<complex> //#include<conio.h> #define oo 1111111111 #define MAX 222 #define PI 3.14159265 using namespace std; struct vec_tor{ long long x,y; double g; vec_tor(){x = 0,y=0,g=0;}; vec_tor(long long x1,long long y1){ x = x1; y = y1; g = atan2(y1,x1); } bool operator<(const vec_tor& V) const { return g < V.g; } vec_tor operator += (const vec_tor &a){ x += a.x; y += a.y; return *this; } vec_tor & operator -= (const vec_tor &a){ x -= a.x; y -= a.y; return *this; } long long kc(){ return x*x+y*y; } }; vector<vec_tor> V; vec_tor T; int n; long long KQ = 0,a,b; void xuly(){ int i = 0,j = 0, lan = 0; KQ = (T+=V[0]).kc(); while(true){ if(++j==n){j = 0; lan = 1;} while(V[j].g - V[i].g + lan*2*PI > PI){ KQ = max(KQ,(T-=V[i]).kc()); if(++i==n) return; } KQ = max(KQ,(T+=V[j]).kc()); } } int main(){ //freopen("MVECTOR.in","r",stdin); scanf("%d",&n); for(int i = 0;i<n;i++){ scanf("%lld %lld",&a,&b); V.push_back(vec_tor(a,b)); } sort(V.begin(),V.end()); xuly(); printf("%lld",KQ); // getch(); }
Code mẫu của ll931110
//#pragma comment(linker, "/STACK:16777216") #include <algorithm> #include <bitset> #include <cmath> #include <ctime> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <fstream> #include <functional> #include <iostream> #include <map> #include <set> #include <sstream> #include <stack> #include <queue> #include <vector> #include <utility> using namespace std; pair<int,int> p[30010]; vector< pair<int,int> > v; int n; double polar_angle(pair<int,int> A) { return atan2(1.0 * A.second,1.0 * A.first); } bool cmp(pair<int,int> A,pair<int,int> B) { double pA = polar_angle(A),pB = polar_angle(B); return pA < pB; } int main() { // freopen("vector.in","r",stdin); // freopen("vector.ou","w",stdout); scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d %d", &p[i].first, &p[i].second); sort(p,p + n,cmp); for (int i = 0; i < n; i++) if (i && (polar_angle(p[i]) - polar_angle(p[i - 1])) < 1e-8) { v[v.size() - 1].first += p[i].first; v[v.size() - 1].second += p[i].second; } else v.push_back(p[i]); int sz = v.size(); // for (int i = 0; i < sz; i++) cout << v[i].first << ' ' << v[i].second << endl; for (int i = 0; i < sz; i++) v.push_back(v[i]); long long ret = 0; int last = 0,curx = 0,cury = 0; for (int i = 0; i < sz; i++) { long long cur = 1LL * curx * curx + 1LL * cury * cury; while (last - i < sz) { curx += v[last].first; cury += v[last].second; last++; long long tmp = 1LL * curx * curx + 1LL * cury * cury; if (tmp < cur) { last--; curx -= v[last].first; cury -= v[last].second; break; } cur = tmp; } ret = max(ret,cur); curx -= v[i].first; cury -= v[i].second; } cout << ret << endl; }
Code mẫu của khuc_tuan
#include <cstdio> #include <algorithm> #include <cmath> #include <cstring> using namespace std; int n; pair<int,int> v[30030]; double g[30030]; long long cost(pair<int,int> v) { return (long long)v.first * v.first + (long long)v.second * v.second; } bool cmp1(pair<int,int> a, pair<int,int> b) { return atan2(a.second,a.first) < atan2(b.second,b.first); } double angle(int i, int j) { if(i <= j) return g[j] - g[i]; else return 2 * M_PI - g[i] + g[j]; } int main() { scanf("%d", &n); for(int i=0;i<n;++i) scanf("%d%d", &v[i].first, &v[i].second); sort( v, v + n, cmp1); for(int i=0;i<n;++i) g[i] = atan2(v[i].second, v[i].first); long long best = 0; int tx = 0, ty = 0, j = 0, sl = 0; for(int i=0;i<n;++i) { if(best < cost(make_pair(tx,ty))) best = cost(make_pair(tx,ty)); while(sl < n && angle(i,j) < M_PI) { ++sl; tx += v[j].first; ty += v[j].second; j = (j+1) % n; if(best < cost(make_pair(tx,ty))) best = cost(make_pair(tx,ty)); } tx -= v[i].first; ty -= v[i].second; sl--; } printf("%lld\n", best); return 0; }
Bình luận