1. 程式人生 > >Good Bye 2018 B. New Year and the Treasure Geolocation

Good Bye 2018 B. New Year and the Treasure Geolocation

傳送門

https://www.cnblogs.com/violet-acmer/p/10201535.html

 

題意:

  在二維空間中有 n 個 obelisk 點,n 個 p 點;

  存在座標T(x,y),obelisk 中的每個點 o[ i ] : (x,y) 都可以在 p 中找到一個點 p[ j ] : (x,y) 使得 o[ i ].x + p[ j ].x == T.x , o[ i ].y + p[ j ].y == T.y ;

  求出這個T點座標。

題解:

  我的做法--暴力列舉

  讓 o[1]點與每個 p[ i ] 點結合,假設 T( o[ 1 ].x + p[ j ].x , o[ 1 ].y + p[ j ].y ) ; 

  判斷其餘的o點能否找到某個p點,使得其座標和為T( ),如果全部找到,輸出T點座標,否則,列舉下一個點;

AC程式碼:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 using namespace std;
 6 #define mem(a,b) memset(a,b,sizeof(a))
 7 const int maxn=1e3+10;
 8 
 9 int n;
10 struct
Node 11 { 12 int x,y; 13 }o[maxn]; 14 struct Node1 15 { 16 int x,y; 17 }p[maxn]; 18 bool vis[maxn]; 19 20 bool cmp(Node1 _a,Node1 _b) 21 { 22 return _a.x < _b.x; 23 } 24 bool Find(int x,int y)//判斷p中有無點(x,y) 25 { 26 for(int i=1;i <= n;++i) 27 if(p[i].x == x && p[i].y == y)
28 return true; 29 return false; 30 } 31 void Solve() 32 { 33 sort(p+1,p+n+1,cmp); 34 for(int i=1;i <= n;++i) 35 { 36 int tX=o[1].x+p[i].x; 37 int tY=o[1].y+p[i].y; 38 bool flag=false; 39 for(int j=2;j <= n;++j) 40 if(!Find(tX-o[j].x,tY-o[j].y)) 41 flag=true; 42 43 if(!flag) 44 { 45 printf("%d %d\n",tX,tY); 46 return ; 47 } 48 } 49 } 50 int main() 51 { 52 scanf("%d",&n); 53 for(int i=1;i <= n;++i) 54 scanf("%d%d",&o[i].x,&o[i].y); 55 for(int i=1;i <= n;++i) 56 scanf("%d%d",&p[i].x,&p[i].y); 57 Solve(); 58 return 0; 59 }
列舉

  當時做的時候,就分析了一下時間複雜度O(n3),又看了一下資料範圍 n <= 1000,emmmm,可以過

                          

  賽後分析:

    其實,當時還想著用二分來著(查詢p中是否含有對應的(x,y)),因為看到了所有的xi != xj , yi != yj,但是比賽的時候並沒有寫,因為遍歷一遍p陣列比二分要容易好多。

    然後,今天擼了一發二分的程式碼,wa,又看了一遍題,發現漏了個條件  ,兩座標不等是用 or 連線的,而不是 and..........

    又換了個查詢方法,巢狀map

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<map>
 5 using namespace std;
 6 #define P pair<int ,int >
 7 const int maxn=1e3+10;
 8 
 9 int n;
10 P o[maxn];
11 P p[maxn];
12 map<int ,map<int ,bool> >mymap;//mymap[i][j] = true : p中含有點(x,y)
13 
14 void Solve()
15 {
16     for(int i=1;i <= n;++i)
17     {
18         bool flag=false;
19         P T=P(o[1].first+p[i].first,o[1].second+p[i].second);
20         for(int j=2;j <= n;++j)
21         {
22             int x=T.first-o[j].first;
23             int y=T.second-o[j].second;
24             if(mymap[x][y] == false)
25                 flag=true;
26         }
27         if(!flag)
28         {
29             printf("%d %d\n",T.first,T.second);
30             return ;
31         }
32     }
33 }
34 int main()
35 {
36 //    freopen("C:\\Users\\lenovo\\Desktop\\in.txt\\cf1091.txt","r",stdin);
37     scanf("%d",&n);
38     for(int i=1;i <= n;++i)
39     {
40         int x,y;
41         scanf("%d%d",&x,&y);
42         o[i]=P(x,y);
43     }
44     for(int i=1;i <= n;++i)
45     {
46         int x,y;
47         scanf("%d%d",&x,&y);
48         p[i]=P(x,y);
49         mymap[x][y]=true;
50     }
51     Solve();
52     return 0;
53 }
巢狀map查詢是否含有相應的p座標

    上網搜了一下map的時間複雜度,emmmm,log(n);

    然後,分析了一波程式碼時間複雜度,O(n2*log(n) );

    那麼,起不要比O(n3)快,提交一發看看,然鵝.......

                              

    莫非,巢狀map的時間複雜度不是log(n)???????