题意

给出一个长度为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的更多相关文章

  1. 【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 ...

  2. 【leedcode】 Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

  3. 【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 ...

  4. 【HackerRank】Median

    题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大 ...

  5. 【LeetCode】二分 binary_search(共58题)

    [4]Median of Two Sorted Arrays [29]Divide Two Integers [33]Search in Rotated Sorted Array [34]Find F ...

  6. 【LeetCode】分治法 divide and conquer (共17题)

    链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...

  7. 【sql】leetcode习题 (共 42 题)

    [175]Combine Two Tables (2018年11月23日,开始集中review基础) Table: Person +-------------+---------+ | Column ...

  8. 【22.70%】【codeforces 591C】 Median Smoothing

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【概率论】4-5:均值和中值(The Mean and the Median)

    title: [概率论]4-5:均值和中值(The Mean and the Median) categories: - Mathematic - Probability keywords: - Me ...

随机推荐

  1. angularJs 模拟jQuery中的this

    在angularJs中,this指向$scope!可以$event配合使用$(event.target)实现,代码如下: HTML部分: <p ng-click="testClick( ...

  2. BaseCommand

    import java.io.Serializable; import android.util.Log; public class BaseCommand implements Serializab ...

  3. MySql必知必会实战练习(二)数据检索

    在上篇博客MySql必知必会实战练习(一)表创建和数据添加中完成了各表的创建和数据添加,下面进行数据检索和过滤操作. 1. Select子句使用顺序 select--->DISTINCT---& ...

  4. 模糊聚类算法(FCM)

    伴随着模糊集理论的形成.发展和深化,RusPini率先提出模糊划分的概念.以此为起点和基础,模糊聚类理论和方法迅速蓬勃发展起来.针对不同的应用,人们提出了很多模糊聚类算法,比较典型的有基于相似性关系和 ...

  5. 剑指offer-第四章解决面试题思路(字符串的排序)

    题目:输入一个字符串,打印出该字符串的全排列. 思路:将整个字符串分成两部分,第一部分为一个字符,将该字符和该字符后面的字符(直到最后一个字符)依次交换,确定第一个字符:然后固定第一个字符,将后面的字 ...

  6. oracle之 oradebug 命令用法

    0> oradebug使用步骤 1)启动sql*plus并以sysdba身份登入 2)连接到一个进程 3)设置一个事件或者进行诊断转储 4)检索trc文件名 5)与连接到的进程断开 1> ...

  7. 野村综合社,惠普2面,索尼,CDK面试经理

    今天疯了一口气面试了四个企业,自我介绍都说了七八遍,晚上回家到头就睡.中间接了oracle的电话,也不知道如何. 11:00野村面试 野村综合社北京流通部 面试3个人,一个英语部门负责人,一个日语负责 ...

  8. python3之scrapy安装使用

    需要安装的包 pip install scrapy selenium 可能需要卸载重装的模块   lxml cryptography  cffi  pypiwin32 pip uninstall xx ...

  9. Qt5.4中遇到找不到头文件<QApplication>等。

    从新学习Qt时,重装了Qt5.4,当运行Hello World例子时,遇到了下列的情况 <span style="font-size:18px;">#include & ...

  10. git 基本操作 https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013744142037508cf42e51debf49668810645e02887691000

    1.创建版本库 (即仓库  repository)简单理解为一个目录,这个目录里的所有文件都可以被git管理起来,每个文件的修改删除,git都能跟踪,一边任何时刻都可以追踪历史,或者在将来某个时刻可以 ...