51nod 1267二分+优化试验场

最初,最开始的时候,万能的学姐曾经警告过我们,千万别用什么老狮子MAP,手撸map或者字典树。。。当时不甚理解。。。今天。。。这题直接卡掉了我的MAP,但是使用朴素方法进行二分。。。不加优化,,都不需要这个架势。。。直接相差了将近十倍,在我开了优化之后快了20倍左右。。。。

 

上代码:

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 const long long MAXN=1233;
 5 
 6 
 7 class node
 8 {
 9     public:
10     long long a,b,summ;
11 
12 };
13 node nodes[MAXN*MAXN];
14 bool cmp(node n,node n1)
15 {
16     return n.summ<n1.summ;
17 }
18 long long n;
19 long long arr[MAXN];
20 long long app[MAXN];
21 long long appoint=0;
22 long long point=0;
23 bool check(node &n1,node &n2)
24 {
25     if(n1.a==n2.a)return false;
26     if(n1.a==n2.b)return false;
27     if(n1.b==n2.a)return false;
28     if(n1.b==n2.b)return false;
29     return true;
30 }
31 void init()
32 {
33     cin>>n;
34     for(int i=0;i<n;++i)
35     {
36         cin>>arr[i];
37     }
38     
39     for(int i=0;i<n;++i)
40     {
41         for(int j=i+1;j<n;++j)
42         {
43             nodes[point].a=i;
44             nodes[point].b=j;
45             nodes[point].summ=arr[i]+arr[j];
46             point++;
47         }
48     }sort(nodes,nodes+point,cmp);
49 }
50 
51 bool succ=0;
52 set<long long>s1;
53 int main()
54 {
55     cin.sync_with_stdio(false);
56     init();
57     for(int i=0;i<point;++i)
58     {
59         if(s1.count(nodes[i].summ))continue;
60         s1.insert(nodes[i].summ);
61         if()
62         
63         node n1=nodes[i];
64         n1.summ=-n1.summ;
65         int pos=lower_bound(nodes,nodes+point,n1,cmp)-nodes;
66         while(nodes[pos].summ==n1.summ)
67         {
68             if(check(nodes[pos],n1))
69             {
70                 succ=1;
71                 break;
72             }pos++;
73         }
74         if(succ)break;
75 //        cout<<"target: "<<n1.summ<<"  answer: "<<nodes[pos].summ<<endl;
76     }
77     if(succ)cout<<"Yes"<<endl;
78     else cout<<"No"<<endl;
79     return 0;
80 }

 

posted @ 2017-08-17 20:15  六花的邪王真眼  阅读(165)  评论(0编辑  收藏  举报