给一个多边形和一条过两点的线,输出面积小的。
看了代码你就知道解了。 long long的话一定要是fabs, 就因为这样给wa了两次。
#define maxn 50005
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
struct Node{
long long x, y;
}p[maxn];
double getarea(Node a, Node b, Node c){
return 0.5 * (double) fabs((b.y - a.y) * (c.x - a.x) - (b.x - a.x) * (c.y - a.y));
}
int T,n,Q,a,b;
double area[maxn],ans;
void get(){
area[0] = 0.0;
area[1] = 0.0;
for(int i = 2; i <= n - 1; i ++){
area[i] = area[i - 1] + getarea(p[0],p[i - 1], p[i]);
}
}
int main(){
int id = 0;
while(scanf("%d%d",&n, & Q) && (n + Q)){
id ++;
if(id >= 2) puts("");
for(int i = 0; i < n; i ++){
scanf("%lld%lld", &p[i].x , &p[i].y);
}
get();
for(int i = 0; i < Q; i ++){
scanf("%d%d", &a, &b);
if(a < b) swap(a,b);// a big
double left1 = area[a] - area[b] - getarea(p[0], p[b], p[a]);
double left2 = area[n - 1] - left1;
ans = min(left1,left2);
printf("%.1lf\n",ans);
}
}
return 0;
}