A - Superset CodeForces - 97B(人生第一個分治法,感覺,像二分啊。。)
阿新 • • 發佈:2017-05-06
但是 ++ 是什麽 force else super 結構體 運算 代碼
/*
分治法,第一次做不是很懂,借鑒了神犇代碼,但實操之後感覺像二分,,可能做得少了或者就是。。。。
*/
題目大意:
一個集合裏有若幹點,要求你添加某些點後保證這個集合裏的任意兩點滿足以下三個條件中至少一個:
1.在一個水平線上 2.在一個豎直線上 3.兩點組成的矩形之間有點.
解題思路:
神犇所講,將點排序,在中間點處建一條豎直線,令其余點在其上投影,二分。
ps:不是很明白錯誤是什麽,,結構體裏重載<運算符可以,但是寫個cmp函數就報錯,不是很懂,貼上錯誤代碼,如果神犇們知道什麽錯誤,請評論告知,多謝;
錯誤代碼
#include<iostream> #include <vector> #includeView Code<algorithm> #include <queue> #include <stack> #include <set> #include <cstdio> #include <iterator> #include <sstream> #include <cmath> #include <deque> using namespace std; struct node { int x; int y; // bool operator <(const node b)const// { // if(x==b.x) return y<b.y; // else return x<b.x; // } }; int n; node p1[10050]; set<node > p; bool cmp(const node a,const node b) { return a.x<b.x; } void dfs(int l,int h) { if (l==h) return ; int mid; mid=(l+h)/2; for (int i=l; i<=h; i++) { node c; c.x=p1[mid].x; c.y=p1[i].y; p.insert(c); } dfs(l,mid); dfs(mid+1,h); } int main() { cin>>n; for (int i=0; i<n; i++) { cin>>p1[i].x>>p1[i].y; p.insert(p1[i]); } sort(p1,p1+n,cmp); dfs(0,n-1); cout<<p.size()<<endl; set<node >::iterator it; for (it=p.begin(); it!=p.end(); it++) { cout<<(*it).x<<" "<<(*it).y<<endl; } }
AC代碼:
#include<iostream> #include <vector> #include <algorithm> #include <queue> #include <stack> #include <set> #include <cstdio> #include <iterator> #include <sstream> #include <cmath> #include <deque> using namespace std; struct node { int x; int y; bool operator <(const node b)const { if(x==b.x) return y<b.y; else return x<b.x; } }; int n; node p1[10050]; set<node > p; bool cmp(const node a,const node b) { return a.x<b.x; } void dfs(int l,int h) { if (l==h) return ; int mid; mid=(l+h)/2; for (int i=l; i<=h; i++) { node c; c.x=p1[mid].x; c.y=p1[i].y; p.insert(c); } dfs(l,mid); dfs(mid+1,h); } int main() { cin>>n; for (int i=0; i<n; i++) { cin>>p1[i].x>>p1[i].y; p.insert(p1[i]); } sort(p1,p1+n); dfs(0,n-1); cout<<p.size()<<endl; set<node >::iterator it; for (it=p.begin(); it!=p.end(); it++) { cout<<(*it).x<<" "<<(*it).y<<endl; } }View Code
A - Superset CodeForces - 97B(人生第一個分治法,感覺,像二分啊。。)