Under Attack II
来源:互联网 发布:js弹出选择保存路径 编辑:程序博客网 时间:2024/05/18 23:56
Problem Description
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 linesy=kx+b given in format ask,b, of coursekb 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 coursea will not exceedb.
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.kb 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.
1
1 5
Sample Output2
Hint
In sample, line y=x+5 divides the region between x=1 and x=2 into two parts, so the outcome is 2.
在l,r区域之间求多条线段能把区域分成多少块,ans=区域内的交点个数+总线段条数+1
由于线与左右边界的焦点,k*l+b与k*r+b的值有正有负,统一按从大到小排序
结构按左边界从大到小,右边界从大到小排序
由于k*l+b的值太大,所以树状数组只存放右边界所会出现点的个数
用map存放右边界ki*r+bi在从上往下数第几个位置
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<map>#define Max 30010using namespace std;int j;long long c[Max];long long y[Max];map<long long,int> mp;struct me{long long x;long long y;}a[Max];int lowbit(int t){return t&(-t);}void insert(int t){while(t<=j-1){c[t]++;t+=lowbit(t);}}long long getsum(int t){long long sum=0;while(t>0){sum+=c[t];t-=lowbit(t);}return sum;}int imp(const void *a,const void *b){if(((me *)b)->x==((me *)a)->x)return ((me *)b)->y-((me *)a)->y;else return ((me *)b)->x-((me *)a)->x;}int imp1(const void *a,const void *b){return *(long long *)b-*(long long *)a;}int main(void){int l,r,n;int k,b,i;while(~scanf("%d%d",&l,&r)){scanf("%d",&n);mp.clear();memset(c,0,sizeof(c));for(i=0;i<n;i++){scanf("%d%d",&k,&b);a[i].x=k*l+b;a[i].y=k*r+b;y[i]=a[i].y;}qsort(a,n,sizeof(a[0]),imp);qsort(y,n,sizeof(y[0]),imp1);j=1;for(i=0;i<n-1;i++)if(y[i]!=y[i+1]){mp[y[i]]=j;j++;}mp[y[i]]=j;j++;long long sum=0;for(i=0;i<n;i++){ sum+=getsum(j-1)-getsum(mp[a[i].y]); insert(mp[a[i].y]); } sum=sum+n+1; printf("%lld\n",sum);}return 0;}
- Under Attack II
- zoj 3574 Under Attack II//归并排序
- 【归并排序】ZOJ-3574-Under Attack II
- ZOJ3574 Under Attack II【线段树】
- zoj Monthly, February 2012 - D Under Attack II (zoj 3574)
- ZOJ--3574--Under Attack II【线段树+欧拉公式】
- ZOJ 3574 Under Attack II 归并排序求逆序对
- ZOJ 3574 Under Attack II 归并排序求逆序对
- zoj 3573 Under Attack
- zoj 3573 Under Attack
- zoj 3573 Under Attack
- zoj 3573 Under Attack
- ZOJ 3573 Under Attack
- Cells Not Under Attack
- zoj3573 Under Attack (线段树)
- ZOJ 3575 Under Attack III
- B. Cells Not Under Attack
- B - Cells Not Under Attack
- C# List 筛选条件符合项目
- 软件架构设计之常用架构模式介绍
- 【Leetcode】Longest Substring Without Repeating Characters
- 软件架构设计
- Linux基础和C高级,day8,9,10,数据结构
- Under Attack II
- 实战Linux Bluetooth编程
- android 3G移植
- 生成license用于验证证书
- 关于程序员幽默笑话糗事名人名言经典语句
- sql语句操作表字段(一)
- MVC模式与思维模式
- sql语句操作表字段(二)
- HDU3389(阶梯博弈)