题意:统计每个格子里面的玩具有多少。2318给出分隔板的时候是从左到右,2398的分隔板并没有排序,得自己排一下。两题就是输出有点不同,其他没什么···
POJ 2318:
#include <iostream> using namespace std; #define MAX 5999 struct 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; else low = 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 2000 struct 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; else low = 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; }