Poj 2318 toys/poj 2398 (binary, cross set)

Source: Internet
Author: User

Calculate the number of toys in each grid. 2318 when the separator board is given, it is left-to-right, And the 2398 separator board is not sorted. You have to arrange it yourself. The output of the two questions is a little different. Nothing else ···

Poj 2318:

#include <iostream>using namespace std;#define MAX 5999struct CardBoard{int x1, y1, x2, y2;double k;  } board[MAX];int cell[MAX];int judge ( int x, int y, CardBoard & b ){double t = b.x1 - b.k * ( b.y1 - y );if ( x < t )return -1;else return 1;}int main(){int n, m;int x1, y1, x2, y2;int U, L, x, y, p, i;while ( scanf("%d",&n) ){if ( n == 0 ) break;scanf("%d%d%d%d%d",&m,&x1,&y1,&x2,&y2);for ( i = 1; i <= n; i++ ){scanf("%d%d",&U,&L);board[i].x1 = U;board[i].y1 = y1;board[i].x2 = L;board[i].y2 = y2;board[i].k = 1.0*(board[i].x1-board[i].x2)/(board[i].y1-board[i].y2);}board[0].x1 = x1;board[0].y1 = y1;board[0].x2 = x1;board[0].y1 = y2;board[n+1].x1 = x2;board[n+1].y1 = y1;board[n+1].x2 = x2;board[n+1].y2 = y2;memset(cell,0,sizeof(cell));while ( m-- ){scanf("%d%d",&x,&y);int mid, low = 0, high = n+1;while ( high - low > 1 ){mid = ( high + low ) / 2;p = judge(x,y,board[mid]);if ( p < 0 )high = mid;elselow = mid;}cell[low]++;}for ( i = 0; i <= n; i++ )printf("%d: %d\n", i, cell[i]);putchar('\n');}return 0;}

Poj 2398:

#include <iostream>using namespace std;#define MAX 2000struct CardBoard{int x1, y1, x2, y2;} board[MAX];int cell[MAX], mark[MAX];struct Item { int t, num; } cnt[MAX];int cmp1 ( const void * a, const void * b ){return ((CardBoard*)a)->x1 - ((CardBoard*)b)->x1;}int cmp2 ( const void * a, const void * b ){return ((Item*)a)->num - ((Item*)b)->num;}int judge ( int x, int y, CardBoard & b ){return  -((b.x1-b.x2)*(y-b.y2)-(b.y1-b.y2)*(x-b.x2));}int main(){int n, m;int x1, y1, x2, y2;int U, L, x, y, i;while ( scanf("%d",&n) ){if ( n == 0 ) break;scanf("%d%d%d%d%d",&m,&x1,&y1,&x2,&y2);for ( i = 1; i <= n; i++ ){scanf("%d%d",&U,&L);board[i].x1 = U;board[i].y1 = y1;board[i].x2 = L;board[i].y2 = y2;}board[0].x1 = x1;board[0].y1 = y1;board[0].x2 = x1;board[0].y1 = y2;board[n+1].x1 = x2;board[n+1].y1 = y1;board[n+1].x2 = x2;board[n+1].y2 = y2;qsort ( board, n+1, sizeof(CardBoard), cmp1);memset(cell,0,sizeof(cell));memset(mark,0,sizeof(mark));memset(cnt,0,sizeof(cnt));while ( m-- ){scanf("%d%d",&x,&y);int mid, low = 0, high = n+1;while ( high - low > 1 ){mid = ( high + low ) / 2;if ( judge ( x, y, board[mid] ) < 0 )high = mid;elselow = mid;}cell[low]++;}for ( i = 0; i <= n; i++ )if ( cell[i] ) mark[cell[i]]++;int k = 0;for ( i = 1; i <= n; i++ ){if ( mark[i] != 0 ){k++;cnt[k].t = mark[i];cnt[k].num = i;}}printf("Box\n");qsort(cnt+1,k,sizeof(Item),cmp2);for ( i = 1; i <= k; i++ )printf("%d: %d\n",cnt[i].num, cnt[i].t); }return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.