POJ 3119 Friends or Enemies? 模拟 计算题 需要预处理

来源:互联网 发布:php array 删除元素 编辑:程序博客网 时间:2024/05/21 10:45
Friends or Enemies?
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 429 Accepted: 178

Description

A determined army on a certain border decided to enumerate the coordinates in its patrol in a way to make it difficult for the enemy to know what positions they are referring to in the case that the radio signal used for communication is intercepted. The enumeration process chosen was the following: first it is decided where the axes x and y are; then, a linear equation that describes the position of the border relative to the the axes (yes, it is a straight line) is defined; finally, all points on the Cartesian plane that is not part of the border are enumerated, the number 0 being attributed to the coordinate (0, 0) and starting from there numbers being attributed to integral coordinates following a clockwise spiral, always skipping points that fall on the border (see Figure 1). If the point (0, 0) falls on the border, the number 0 is attributed to the first point that is not part of the border following the specified order.

Figure 1: Enumeration of points of integral coordinates

In fact the enemy does not have to know either what position the army is referring to or the system used to enumerate the points. Such a project, complicated the life of the army, once that it is difficult to determine whether two points are on the same side of the border or on opposite sides. That is where they need your help.

Input

The input contains several test cases. The first line of the input contains an integer N (1 ≤ N ≤ 100) which represents the quantity of test cases. N test cases follow. The first line of each test case contains two integers a and b (−5 ≤ a ≤ 5 and −10 ≤ b ≤ 10) which describe the equation of the border: y = ax + b. The second line of each test case contains an integer K, indicating the number of queries that follow it (1 ≤ K ≤ 1000). Each one of the following K lines describes a query, composed by two integers M and N representing the enumerated coordinates of two points (0 ≤ MN ≤ 65535).

Output

For each test case in the input your program should produce K + 1 lines. The first line should contain the identification of the test case in the form Caso X, where X should be substituted by the case number (starting from 1). The K following lines should contain the results of the K queries made in the corresponding case in the input, in the form:

Mesmo lado da fronteira (The same side of the border)

or

Lados opostos da fronteira (Opposite sides of the border)

Sample Input

21 21026 2525 1124 923 2825 925 125 09 123 1226 171 2120 11 22 33 44 55 66 77 88 99 1010 1111 12

Sample Output

Caso 1Mesmo lado da fronteiraMesmo lado da fronteiraMesmo lado da fronteiraMesmo lado da fronteiraMesmo lado da fronteiraLados opostos da fronteiraLados opostos da fronteiraLados opostos da fronteiraLados opostos da fronteiraLados opostos da fronteiraCaso 2Mesmo lado da fronteiraMesmo lado da fronteiraMesmo lado da fronteiraMesmo lado da fronteiraMesmo lado da fronteiraMesmo lado da fronteiraMesmo lado da fronteiraMesmo lado da fronteiraLados opostos da fronteiraMesmo lado da fronteiraMesmo lado da fronteiraLados opostos da fronteira


这题意思是战时判断两支军队间是否敌对,敌对的话输出Lados , 不是敌对就输出 Mesmo;

首先他会给你a 和b  代表直线 a*x+b=y  没有人是在这条线上的.

在给你 q个询问.

然后,从零点开始 逆时针,  照着图给的规则把一个个军队编号 放在某(x,y) 点上.

然后把对应军队编号所在的x,y  带入 a*x+b-y , 得到的数相乘, 判断是否异号,异号为敌对的;

因为6*10^4 个军队*1000个询问*100个案例  会tle;

所以预处理下 复杂度 是 6*10^4*100  暴力就可以过了; 代码有点乱. 主要靠自己找规律, 从0开始一个个模拟过来就好了


#include<stdio.h>struct po{int x,y;}p[70000];int main(){int t,a,b,q,n1,n2,x,y,num,flag,bu,tem,diyici,biaoji1,biaoji2;scanf("%d",&t);int cas=1;while(t--){printf("Caso %d\n",cas++);scanf("%d%d",&a,&b);scanf("%d",&q);x=0,y=0;num=-1;flag=0;bu=1;//两次 步数增加一tem=1;diyici=1;while(num<70000){if(x*a+b!=y)//不在线上;{num++;p[num].x=x;p[num].y=y;}if(flag==0){x--;}else if(flag==1){y++;}else if(flag==2){x++;}else if(flag==3){y--;}bu--;if(bu==0&&diyici==1){bu=tem;flag++;flag%=4;;diyici=2;}else if(bu==0&&diyici==2){bu=++tem;flag++;flag%=4;;diyici=1;}}while(q--){scanf("%d%d",&n1,&n2);biaoji1=a*p[n1].x+b-p[n1].y;biaoji2=a*p[n2].x+b-p[n2].y;if(biaoji1*biaoji2<0)puts("Lados opostos da fronteira");elseputs("Mesmo lado da fronteira");}}return 0;}




0 0
原创粉丝点击