algorithm >> next_permutation
해당하는 구간의 순열을 알아서 만들어준다.
자세한것은
http://www.cplusplus.com/reference/algorithm/next_permutation/
내부구현은
http://stackoverflow.com/questions/11483060/stdnext-permutation-implementation-explanation
이런식이라고 한다.
next_permutation을 활용한 소스
https://www.acmicpc.net/problem/1007
#include <cstdio> #include <algorithm> #include <cmath> #include <vector> using namespace std; struct Point{ int x; int y; Point(){ x=0;y=0; } Point(int _x, int _y){ x=_x; y=_y; } Point operator + (Point p){ Point ans; ans.x = x + p.x; ans.y = y + p.y; return ans; } Point operator - (Point p){ Point ans; ans.x = x-p.x; ans.y = y-p.y; return ans; } void init(){ x=0; y=0; } double scalar(){ return sqrt((double)x*x+(double)y*y); } }; void pointInit(Point point[]){ for(int i=0;i<22;++i)point[i].init(); } int main(){ Point point[22]; int n; int t; for(scanf("%d",&t);t;--t){ pointInit(point); scanf("%d",&n); for(int i=0;i<n;++i){ int x, y; scanf("%d%d",&x,&y); point[i].x=x; point[i].y=y; } vector<bool> v; for(int i=0;i<n/2;++i){ v.push_back(true); v.push_back(false); } double ans = 98765432123; sort(v.begin(),v.end()); do{ Point p; for(int i=0;i<v.size();++i){ if(v[i])p= p+point[i]; else p =p-point[i]; } ans = min(ans,p.scalar()); }while(next_permutation(v.begin(),v.end())); printf("%.6lf\n",ans); } }
댓글
댓글 쓰기