Under Attack II
Time Limit: 5 Seconds
Memory Limit: 65536 KB
Because of the sucessfully calculation in Under Attack I, Doctor is awarded with Courage Cross and promoted to lieutenant. But the war seems to end in never, now Doctor has a new order to help anti-aircraft troops calculate the proper number of supply sites
needed for SAMs in battle regions.
According to intel, enemy bombers go straight across battle region and the bombing runs are continous. So their routes divides the region into several parts. The missles SAM needed are provided by supply sites. Because it‘s dangerous to cross fireline, Ufo
suggests that every part of battle regions divided by firelines should have a supply site so that the SAMs can safely get enough ammo.
Now that the task is clear, Doctor is asked to calculate how many supply sites are at least needed. The bombers‘ routes are lines
y=kx+b given in format as k,b, of course k b are same to their ordinary meanings. Assume the battle region is a rectangle with infinity height, the left x-cooridinate and right x-cooridinate are given so that the width
of rectangle is fixed.
Input
The input consists of multiple cases.
The first line are the left x-cooridinate a and right x-cooridinate
b of battle region. a b are both in the range of [0,1000]. Of course
a will not exceed b.
Next lines will describe enemy bombing routes number n.n can be up to 30000.
Following n lines are k and b of each bombing route.k
b are in range of [-100000,100000].
It‘s guaranteed that no three lines (including the two battle region bound lines) will go through one point.
Output
Output the least number of supply sites needed.
Sample Input
1 2 1 1 5
Sample Output
2
Hint
In sample, line y=x+5 divides the region between x=1 and x=2 into two parts, so the outcome is 2.
题意:告诉x轴上的一个区间范围,告诉n条直线,以及每条直线的k,b值,问这些直线能把XA 到 XB 这个区间分成多少个平面区间,任意三条直线保证不会交于一个点。
思路:对于区间内任意两条直线,如果不相交,那么就是分成三个区域,如果交点在范围内就是分成4个区域,如果交点在边界上,那么就是三个区域。多画几个图就会发现,范围内分的区域数,实际上等于直线数加交点数加一,那么现在关键就是求交点数,先把左边交点按照从小到大排序,相等时右边交点也从小到大排,交点的个数,就是逆序对数,那么可以用归并排序来求右边范围的逆序对数是多少。
#include <iostream> #include <stdio.h> #include <string> #include <cstring> #include <algorithm> #include <cmath> using namespace std; struct Node { int a,b; }f[30009]; int cmp(Node a,Node b) { if(a.a==b.a) return a.b<b.b; return a.a<b.a; } int tmp[30009]; int num; void mergefun(int le,int mid,int ri) { int p=le,q=mid+1,i=1; while(p<=mid && q<=ri ) { if(f[p].b>f[q].b) {tmp[i++]=f[q++].b;num+=(mid-p+1);} else tmp[i++]=f[p++].b; } while(p<=mid) tmp[i++]=f[p++].b; while(q<=ri) tmp[i++]=f[q++].b; int j=1; for(i=le;i<=ri;i++,j++) f[i].b=tmp[j]; } void mergesort(int le,int ri) { if(le==ri) return; int mid=(le+ri)/2; mergesort(le,mid); mergesort(mid+1,ri); mergefun(le,mid,ri); } int main() { int xa,xb; int k,b; int n; while(~scanf("%d%d",&xa,&xb)) { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d%d",&k,&b); f[i].a=(k*xa+b); f[i].b=(k*xb+b); } sort(f+1,f+n+1,cmp); num=0; mergesort(1,n); printf("%d\n",num+n+1); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。