ZOJ3574(归并排序求逆数对)
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.
题意:一个长方形区域,n条直线切这长方形区域,问被这一个长方形区域被切成多少块?
思路:可推出公式:切成多少块=直线交点数+直线数+1;难点是要去求直线交点数。先开始暴力两个for(O(n^2))直接TLE,这里要用到归并排序求逆数对。
转:http://acshiryu.github.io/2014/12/16/ZOJ3574-Under-Attack-II%E8%A7%A3%E9%A2%98%E6%8A%A5%E5%91%8A/#more
假设,每一条边都已经固定在矩形上,然后,我们从矩形的左边开始看起,从下到上,映射到右边,如果在右边观看时,碰到有k个点(这些点对应的边一定要看过,也就是说对应左边的点要低)在上面,则说明该线段与前面的线段有k个交点。你也许不是很明白,看图,对应左边序号的123456,右边是246135,也就是说对于4号线,有1,3号线的左边低于4,右边高于4,则第4号线与前三条有两个交点,这不正是刚才要求的。现在转换成这样,是不是很熟悉,没错,求逆序对。
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
struct Node
{
int y1;
int y2;
};
Node node[+];
int t[+];
int a[+];
int cmp(Node a,Node b)
{
return a.y1<b.y1;
} int cnt;
void merge_sort(int x,int y)//归并排序模板
{
if(y-x>)
{
int m=x+(y-x)/;
int p=x,q=m,i=x;
merge_sort(x,m);
merge_sort(m,y);
while(p<m||q<y)
{
if(q>=y||(p<m&&a[p]<=a[q]))
t[i++]=a[p++];
else
{
t[i++]=a[q++];
cnt+=m-p;
}
}
for(i=x;i<y;i++)
a[i]=t[i];
}
}
int main()
{
int x1,x2;
while(~scanf("%d%d",&x1,&x2))
{
int n;
scanf("%d",&n);
memset(node,,sizeof(node));
memset(t,,sizeof(t));
int k,b;
for(int i=;i<n;i++)
{
scanf("%d%d",&k,&b);
node[i].y1=k*x1+b;
node[i].y2=k*x2+b;
}
cnt=;
sort(node,node+n,cmp);
for(int i=;i<n;i++)
a[i]=node[i].y2;
merge_sort(,n);
printf("%d\n",n+cnt+);
} return ;
} //1 2
//1
//1 5
ZOJ3574(归并排序求逆数对)的更多相关文章
- 归并排序(归并排序求逆序对数)--16--归并排序--Leetcode面试题51.数组中的逆序对
面试题51. 数组中的逆序对 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数. 示例 1: 输入: [7,5,6,4] 输出 ...
- poj 2299 Ultra-QuickSort :归并排序求逆序数
点击打开链接 Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 34676 Accepted ...
- [CF 351B]Jeff and Furik[归并排序求逆序数]
题意: 两人游戏, J先走. 给出一个1~n的排列, J选择一对相邻数[题意!!~囧], 交换. F接着走, 扔一硬币, 若正面朝上, 随机选择一对降序排列的相邻数, 交换. 若反面朝上, 随机选择一 ...
- POJ2299 Ultra-QuickSort(归并排序求逆序数)
归并排序求逆序数 Time Limit:7000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descri ...
- 【BZOJ4769】超级贞鱼 归并排序求逆序对
[BZOJ4769]超级贞鱼 Description 马达加斯加贞鱼是一种神奇的双脚贞鱼,它们把自己的智慧写在脚上——每只贞鱼的左脚和右脚上各有一个数.有一天,K只贞鱼兴致来潮,排成一列,从左到右第i ...
- hiho一下 第三十九周 归并排序求逆序数
题目链接:http://hihocoder.com/contest/hiho39/problem/1 ,归并排序求逆序数. 其实这道题也是可以用树状数组来做的,不过数据都比较大,所以要离散化预处理一下 ...
- poj 2299 Ultra-QuickSort 归并排序求逆序数对
题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题 ...
- 归并排序+归并排序求逆序对(例题P1908)
归并排序(merge sort) 顾名思义,这是一种排序算法,时间复杂度为O(nlogn),时间复杂度上和快排一样 归并排序是分治思想的应用,我们先将n个数不断地二分,最后得到n个长度为1的区间,显然 ...
- 2014 HDU多校弟五场A题 【归并排序求逆序对】
这题是2Y,第一次WA贡献给了没有long long 的答案QAQ 题意不难理解,解题方法不难. 先用归并排序求出原串中逆序对的个数然后拿来减去k即可,如果答案小于0,则取0 学习了归并排序求逆序对的 ...
随机推荐
- 认识什么是SEO
何为SEO? SEO是由英 文Search Engine Optimization缩写而来, 中文意译为“搜索引擎优化”,是指在了解搜索引擎自然排名机制的基础上,对网站进行内部及外部的调整优化,改进网 ...
- Android应用开发学习之Toast消息提示框
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 本文我们来看Toast消息提示框的用法.使用Toast消息提示框一般有三个步骤: 1. 创建一个Toast对象.可 ...
- PLSql连接远程Oracle方法
- 用CSS3实现对图片的放大效果
用CSS3对图片放大效果 .right_div .topicons li a:hover img{ -webkit-transform:scale(1.5,1.5); -moz-tra ...
- HashMap的使用方法及注意事项
99.Map(映射):Map 的keySet()方法会返回 key 的集合,因为 Map 的键是不能重复的,因此 keySet()方法的返回类型是 Set:而 Map 的值是可以重复的,因此 valu ...
- IE浏览器中hasLayout的介绍
haslayout是Windows Internet Explorer渲染引擎的一个内部组成部分.在InternetExplorer中,一个元素要么对自身的内容进行计算大小和组织,要么依赖于父元素来计 ...
- Android 加速Gradle构建项目
1. 升级gradle 进入项目文件夹$project/gradle/wrapper/gradle-wrapper.properties, 使用最新的gradle. 修改替换为最新的 distribu ...
- TortoiseSVN和VisualSVN-下载地址
isualSVN的下载地址http://www.visualsvn.com/visualsvn/ 它可以以插件的形式嵌入到visual studio里面,让团队协作更轻松,最新的版本已经支持Visua ...
- IOS开发几何类方法 CGGeometry.h文件
CGGeometry.h文件是用C语言实现的一个封装了许多常用几何方法的文件. 一.几个常用结构体 struct CGPoint { CGFloat x; CGFloat y; }; 定义一个点,设置 ...
- dispatch_group_t
最近在写的模块有这样一个问题,要保证所有block里面的东西全都回来之后再执行某一个 例如我要做完所有的数据库操作再刷新界面,数据库的内容很多,所有用到了group dispatch_group_t ...