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)???????

猜你喜欢

转载自www.cnblogs.com/violet-acmer/p/10201589.html