注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

长风明志的博客

不要也不能做下一个谁,应该且可以做第一个自己

 
 
 

日志

 
 

Jobdu 1004:Median  

2012-08-31 21:48:04|  分类: ACM |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:4780

解决:1314

题目描述:

    Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1={11, 12, 13, 14} is 12, and the median of S2={9, 10, 15, 16, 17} is 15. The median of two sequences is defined to be the median of the non-decreasing sequence which contains all the elements of both sequences. For example, the median of S1 and S2 is 13.
    Given two increasing sequences of integers, you are asked to find their median.

输入:

    Each input file may contain more than one test case.
    Each case occupies 2 lines, each gives the information of a sequence. For each sequence, the first positive integer N (≤1000000) is the size of that sequence. Then N integers follow, separated by a space.
    It is guaranteed that all the integers are in the range of long int.

输出:

    For each test case you should output the median of the two given sequences in a line.

样例输入:
4 11 12 13 14 5 9 10 15 16 17
样例输出:
13
 
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
  int n;
  int flag=0;
  long a;
  int i,count=0;
  vector<long> vect;
  while(cin>>n)        //前面的个数
  {
     i=1;
     while(i<=n)
     {
         cin>>a;
         vect.push_back(a);
         i++;
     }
     count+=n;
     flag++;
     if(!(flag%2))     //两行数据为一组
     {
        sort(vect.begin(),vect.end());
        if(count%2)    //have odd number integers
        cout<<vect[(int)(count/2)]<<endl;
        else           //have even number integers
        cout<<vect[(int)(count/2)-1]<<endl;
        count=0;
        vect.clear();    //重置vector
     }
  }
  return 0;
}
/**************************************************************
    Problem: 1004
    User: 长风明志
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:1512 kb
****************************************************************/
  评论这张
 
阅读(274)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017