【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 ...
随机推荐
- OpenCV-Python cv2.imdecode()和cv2.imencode() 图片解码和编码
cv2.imdecode()函数从指定的内存缓存中读取数据,并把数据转换(解码)成图像格式;主要用于从网络传输数据中恢复出图像. cv2.imencode()函数是将图片格式转换(编码)成流数据,赋值 ...
- 设置一个严格的SESSION过期时间
认识一:当我们需要更改session生存时间的时候通行的做法是更改php.ini文件中 1: ; Name of the session (used as cookie name). 2: sessi ...
- JFinal自定义FreeMarker标签
为什么采用freemarker? 1.模板技术,不依附于语言和框架,前端和后端解耦,便于分工协作,更好的协同. 2.页面相应速度快 3.前端非常的灵活,采用自定义标签可以在不更改后端的基础上很容易的构 ...
- LeetCode Replace Words
原题链接在这里:https://leetcode.com/problems/replace-words/description/ 题目: In English, we have a concept c ...
- memcache使用经历
memcache可以看成是一个大内存库,因为可以集群,所以可以看成是无限大的内存库.既然是内存库,那么我们放的就是一些map了.map里就键值对,虽然内存可以通过集群的方式无限扩展,但单个键值对的大小 ...
- 将java程序打包成exe文件
一. 1. 项目右击,导出 jar文件 2. 下一步,选择 3. 完成 二. 1. 下载exe4j,并破解 2. 其他的步骤都好说,我主说这个步骤,一定要讲所有引用的jar包放到里面否则会报找不到文件 ...
- 010. VS2015创建MVC项目
1. 文件→新建→项目 2.选择空模板→选中MVC 3. 预览(如果是使用aspx, 则可以删除Views中的web.config, 如果使用Razor则不要删除, 否则会报Views/Home/In ...
- JavaScript笔记——正则表达式
正则表达式(regular expression)是一个描述字符模式的对象.JavaScript的 RegExp 类 表示正则表达式,而 String 和 RegExp 都定义了使用正则表达式进行强大 ...
- python中常用模块详解一
1.time 模块 import time s = time.localtime() # 把时间转化成格式化的时间,通过. 取得里面的年月日等 struct_time 格式 time.struct_t ...
- 使用Kismet进行网络扫描
执行命令启动Kismet root@sch01ar:~# kismet 这个界面是用来设置颜色的,单击Yes按钮,默认颜色灰色 这个界面显示正在使用root用户运行Kismet工具,单击OK 这个界面 ...