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 学习了归并排序求逆序对的 ...
随机推荐
- 多系统实现单点登录方案:SSO 单点登录
一.什么是单点登录SSO(Single Sign-On) SSO是一种统一认证和授权机制,指访问同一服务器不同应用中的受保护资源的同一用户,只需要登录一次,即通过一个应用中的安全验证后,再访问其他应用 ...
- 【HDU1102】Constructing Roads(MST基础题)
最小生成树水题.prim一次AC #include <iostream> #include <cstring> #include <cstdlib> #includ ...
- Micro Python - Python for microcontrollers
Micro Python - Python for microcontrollers MicroPython
- iOS程序员对算法的要求
算法和数据结构(鉴于二者的关联,以下统称算法),对于程序员的重要性一直是个具有争议性的话题.有一些程序员内心对算法有着天然的排斥,面试当中一旦考察算法知识,会被不少程序员吐槽,但有部分公司又一直在坚持 ...
- python学习之路-9 socket网络编程
socket基础 socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. so ...
- cocos2dx 3.0 研究(4)渲染分析
http://blog.csdn.net/epeaktop/article/details/26730909中已经说明了程序的设计,让我们来看看在cocos2dx 3.0中代码是怎样实现的. void ...
- 给一个int型整数,如何将这个整数的奇偶位互换
题目: 假设一个8为整数是(10101100)b那么奇偶互换之后就是(01011100)b.假设机器是32位的 注意: 8位中最低位开始数,最低位是第0位,是偶数为,次低位时第1位,是偶数位. 做法: ...
- 虚拟化之docker
1,什么是docker dokcer是应用容器,它是基于系统的虚拟化,他不是基于硬件的虚拟化(KVM),可能这样说有点抽象,我在知乎看到过一个比喻, docker思想是来源于集装箱的,在一艘大船上,可 ...
- 使用bootstrapvalidator的remote验证经验
这里需要说一下,bootstrapvalidator的帮助文档写的比较简单,对于remote验证器的说明更是如此,在经历多方测试之后才明白如何使用这个验证器. 一个典型的ajax验证代码如下: 服务端 ...
- (原)Matlab的svmtrain和svmclassify
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5554551.html 参考网址: http://www.cnblogs.com/zhangchaoya ...