【HDU5857】Median
题意
给出一个长度为n的有序序列。给出m个询问,每个询问包括四个正整数l1,r1,l2,r2你用l1tor1的和l2tor2的元素来组成一个新的序列,然后找出这个序列的中位数。
分析
这是当时Spring Team Training D 的一道题,显而易见的模拟,我当时在场上写了190行。
赛后看了学长的代码后···orzzzzz!!
我当时分类的时候是把 “相交不包含(l2<r1&&r2>r1)”,和“包含(r2<r1)”作为两种情况来写。然而事实上,如果是包含,只需要swap(r1,r2)就可以按照相交不包含来处理了······
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream> using namespace std;
const int maxn=+;
int T,n,m,l1,r1,l2,r2;
int a[maxn];
int work(int x){
if(r1<l2){
if(x<=r1-l1+){
return a[l1+x-];
}
x=x-(r1-l1+);
return a[l2+x-];
}else{
int len=r1-l2+;
if(x<=r1-l1+-len+){
return a[l1+x-];
} if(x>r1-l1++len){
x=x-(r1-l1+);
return a[l2+x-];
}
x=x-(r1-l1+-len);
if(x%)
x=(x+)/;
else
x=x/;
return a[l1+(r1-l1+-len)+x-];
}
}
int main(){
scanf("%d",&T);
for(int t=;t<=T;t++){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=m;i++){
scanf("%d%d%d%d",&l1,&r1,&l2,&r2);
if(l1>l2){
swap(l1,l2);
swap(r1,r2);
}
if(r2<r1)
swap(r1,r2);
int len=r1-l1++r2-l2+;
if(len%){
double ans=(double)work((len+)/);
printf("%.1f\n",ans);
}else{
double ans=(double)work(len/)+(double)work(len/+);
printf("%.1f\n",ans/);
}
}
}
return ;
}
【HDU5857】Median的更多相关文章
- 【leetcode】Median of Two Sorted Arrays
题目简述: There are two sorted arrays A and B of size m and n respectively. Find the median of the two s ...
- 【leedcode】 Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
- 【leetcode】Median of Two Sorted Arrays(hard)★!!
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- 【HackerRank】Median
题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大 ...
- 【LeetCode】二分 binary_search(共58题)
[4]Median of Two Sorted Arrays [29]Divide Two Integers [33]Search in Rotated Sorted Array [34]Find F ...
- 【LeetCode】分治法 divide and conquer (共17题)
链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...
- 【sql】leetcode习题 (共 42 题)
[175]Combine Two Tables (2018年11月23日,开始集中review基础) Table: Person +-------------+---------+ | Column ...
- 【22.70%】【codeforces 591C】 Median Smoothing
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【概率论】4-5:均值和中值(The Mean and the Median)
title: [概率论]4-5:均值和中值(The Mean and the Median) categories: - Mathematic - Probability keywords: - Me ...
随机推荐
- 在ubuntu16.04上安装php7 mysql5.7 nginx1.10并支持http2
安装nginx 首先更新软件包 并且安装nginx sudo apt-get update sudo apt-get install nginx 开放防火墙配置 sudo ufw allow 'Ngi ...
- CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\....\App_Web_default.aspx.cdcab7d2.zii776dc.dll”--“拒绝访问。 ”
关于访问asp.net站点出现以下问题的解决办法: 问题: CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v4.0.30319\Tempora ...
- 【CSS3】 - 初识CSS3
.navdemo{ width:560px; height: 50px; font:bold 0/50px Arial; text-align:center; margin:40px auto 0; ...
- Python函数- setattr()
作用: setattr 函数对应函数 getatt(),用于设置属性值,该属性必须存在. 语法: setattr(object, name, value) object -- 对象. name -- ...
- Mysql 拿指定经纬度与数据库多条经纬度进行距离计算 (转)
公式如下,单位米: 第一点经纬度:lng1 lat1 第二点经纬度:lng2 lat2 round(6378.138*2*asin(sqrt(pow(sin( (lat1*pi()/180-lat2* ...
- form表单中name和id区别
HTML文本是由HTML命令组成的描述性文本,HTML命令可以说明文字.图形.动画.声音.表格.链接等.HTML的结构包括头部(Head).主体(Body)两大部分,其中头部描述浏览器所需的信息,而主 ...
- 使用ajax提交form表单,formData
http://www.cnblogs.com/zhuxiaojie/p/4783939.html formDatahttps://developer.mozilla.org/zh-CN/docs/We ...
- 关联数组的错误,mysql insert varchar 原生的错误
在写代码的时候,没注意犯了2个低级错误: 关联数组的错误 $array = ['id' => '03657', 'kf_phone ' => 18796442]; 然后你再读取的时候就需要 ...
- 你知道PING功能是怎么实现的吗
以太网的协议有层,而每层都包含有更多的协议.所谓协议,通俗的讲就是通信双方约定的规则. 今天我们介绍一些一个听起来陌生却有很常用的协议,ICMP协议. ICMP是(Internet Control ...
- 【原创】深入理解Docker容器和镜像 -- 分析了docker的命令含义
10张图带你深入理解Docker容器和镜像 镜像(Image)就是一堆只读层(read-only layer)的统一视角 要点:容器 = 镜像 + 读写层.并且容器的定义并没有提及是否要运行容器. 一 ...