【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)就可以按照相交不包含来处理了······

   

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <iostream>
 5 
 6 using namespace std;
 7 const int maxn=100000+10;
 8 int T,n,m,l1,r1,l2,r2;
 9 int a[maxn];
10 int work(int x){
11     if(r1<l2){
12         if(x<=r1-l1+1){
13             return a[l1+x-1];
14         }
15         x=x-(r1-l1+1);
16         return a[l2+x-1];
17     }else{
18         int len=r1-l2+1;
19         if(x<=r1-l1+1-len+1){
20             return a[l1+x-1];
21         }
22 
23         if(x>r1-l1+1+len){
24             x=x-(r1-l1+1);
25             return a[l2+x-1];
26         }
27         x=x-(r1-l1+1-len);
28         if(x%2)
29             x=(x+1)/2;
30         else
31             x=x/2;
32          return a[l1+(r1-l1+1-len)+x-1];
33     }
34 }
35 int main(){
36     scanf("%d",&T);
37     for(int t=1;t<=T;t++){
38         scanf("%d%d",&n,&m);
39         for(int i=1;i<=n;i++)
40             scanf("%d",&a[i]);
41         for(int i=1;i<=m;i++){
42             scanf("%d%d%d%d",&l1,&r1,&l2,&r2);
43             if(l1>l2){
44                 swap(l1,l2);
45                 swap(r1,r2);
46             }
47             if(r2<r1)
48                 swap(r1,r2);
49             int len=r1-l1+1+r2-l2+1;
50             if(len%2){
51                 double ans=(double)work((len+1)/2);
52                 printf("%.1f
",ans);
53             }else{
54                 double ans=(double)work(len/2)+(double)work(len/2+1);
55                 printf("%.1f
",ans/2);
56             }
57         }
58     }
59 return 0;
60 }
View Code
原文地址:https://www.cnblogs.com/LQLlulu/p/8964294.html