题目链接:
测试数据:
4
1 00 11 10 090 01 02 00 21 22 20 11 12 14-2 53 70 05 20有两种解法,第一种用二分查找,第二种用到hash存储:
解法一:
1 #include2 #include 3 #include 4 #include 5 #include 6 using namespace std; 7 int T; 8 struct TT 9 {10 int x,y;11 }node[1010];12 bool cmp(TT a,TT b)13 {14 if(a.x node[mid].y) L = mid +1;30 else R = mid-1;31 }32 else if(x>node[mid].x) L = mid+1;33 else R = mid-1;34 }35 return false;36 }37 int main()38 {39 int ans;40 int x1,y1,x2,y2,mx,my;41 while(scanf("%d",&T) && T)42 { ans = 0;43 for(int i =1;i<=T;i++)44 scanf("%d %d",&node[i].x,&node[i].y);45 sort(node+1,node+T+1,cmp);//乱了一下,开始从0,开始排序了46 for(int i = 1; i
解法二:本来想着哈希简简单单就过了呢,咋说也比二分省时间吧,可是呢,整了两个小时才搞定,还发现了一个问题;有待于求证:结构体数组赋值时间小于数组赋值时间,
1 #include2 #include 3 #include 4 #include 5 #include 6 using namespace std; 7 struct TT 8 { 9 int x,y,next;10 }node[1010],hash[1010];11 const int N = 1100;12 int vis[N][N];13 int T;14 const int MOD = 10007;15 int next[N],head[MOD],top = 1;16 void creath(int x,int y)17 {18 int key = abs(x)%MOD;19 hash[top].next = head[key];//这样写超时: next[top] = head[key];20 hash[top].x = x;21 hash[top].y = y;22 head[key] = top;23 top++;24 }25 bool cmp(TT a,TT b)26 {27 if(a.x =0;i = hash[i].next)//换成 i = next[i] 超时35 {36 if(hash[i].x==x && hash[i].y == y)37 return true;38 }39 return false;40 }41 int main()42 {43 int ans;44 int x1,y1,x2,y2,mx,my;45 while(scanf("%d",&T) && T)46 { memset(head,-1,sizeof(head));47 ans = 0;48 for(int i =1;i<=T;i++)49 {50 scanf("%d %d",&node[i].x,&node[i].y);51 creath(node[i].x,node[i].y);52 }53 sort(node+1,node+1+T,cmp);54 for(int i = 1; i