题意

给出一个长度为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. 3.了解linux系统以及搭建学习环境

    目录: 1.linux的前世今生. 2.企业如何选择linux系统? 3.如何在虚拟机上安装linux系统?搭建学习环境. 1.linux的前世今生. 1).起源:先是贝尔实验室的Unix系统,因为各 ...

  2. win2003从组策略关闭端口(445/135/137/138/139/3389等)教程

    一些恶劣的病毒会从某些端口入侵计算机,因此关闭某些用不到的而又具有高风险的端口就显得很有必要,是服务器管理员要做的基本的安全防范.本文将介绍win2003系统在组策略关闭某一个端口的教程,文章以关闭4 ...

  3. 接口测试基础——第5篇xlrd模块

    读取Excel表格中的内容,不多说,直接上代码: # coding: utf-8 import xlrd # excel路径 excel_path = r'C:\Users\weiming\Deskt ...

  4. 【BZOJ2850】巧克力王国 KDtree

    [BZOJ2850]巧克力王国 Description 巧克力王国里的巧克力都是由牛奶和可可做成的.但是并不是每一块巧克力都受王国人民的欢迎,因为大家都不喜 欢过于甜的巧克力.对于每一块巧克力,我们设 ...

  5. Dawn 简单使用

     1. install npm install dawn -g 2. create project # 1. Create & Initialize $ dn init -t front # ...

  6. (装)Android杂谈--禁止TimePicker控件通过keyboard输入

    Android 4.1版本以上用的是类似与ios的滚动时间控件,但是4.1以下,用的TimePicker确实通过点击上下按钮来更改时间的,虽然也提供了编辑框编辑,但是可能会超出编辑范围 如果要禁止编辑 ...

  7. Pycharm安装工具包

    1.在settings->Project Interpreter->configure interpreters->Install中可以找到所有的模块. PyCharm IDE会帮助 ...

  8. 蓝桥杯 算法训练 ALGO-108 最大的体积

    算法训练 最大体积   时间限制:1.0s   内存限制:256.0MB 问题描述 每个物品有一定的体积(废话),不同的物品组合,装入背包会战用一定的总体积.假如每个物品有无限件可用,那么有些体积是永 ...

  9. 蓝桥杯 算法训练 ALGO-21 装箱问题

     算法训练 装箱问题   时间限制:1.0s   内存限制:256.0MB 问题描述 有一个箱子容量为V(正整数,0<=V<=20000),同时有n个物品(0<n<=30),每 ...

  10. yii console

    Here is a step by step to show how to run command in the server with yii framework. 1. Create the we ...