1. 程式人生 > >Hdu1157 完全的水題,排序找中值的

Hdu1157 完全的水題,排序找中值的

ACM部落格_kuangbin 新部落格kuangbin.org
人一我百,人十我萬!追逐青春的夢想,懷著自信的心,永不放棄! by kuangbin 新部落格 kuangbin.org
ACM HDU 1157 Who’s in the Middle (完全的水題,排序找中值的)

Who’s in the Middle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3360 Accepted Submission(s): 1646

Problem Description
FJ is surveying his herd to find the most average cow. He wants to know how much milk this ‘median’ cow gives: half of the cows give as much or more than the median; half give as much or less.

Given an odd number of cows N (1 <= N < 10,000) and their milk output (1..1,000,000), find the median amount of milk given such that at least half the cows give the same amount of milk or more and at least half give the same or less.

Input
* Line 1: A single integer N

  • Lines 2..N+1: Each line contains a single integer that is the milk output of one cow.

Output
* Line 1: A single integer that is the median milk output.

Sample Input
5 2 4 1 3 5

Sample Output
3
Hint
INPUT DETAILS: Five cows with milk outputs of 1..5 OUTPUT DETAILS: 1 and 2 are below 3; 4 and 5 are above 3.

Source
USACO 2004 November



import java.util.Arrays;
import java.util.Scanner;

public
class Main{ /** * @param args */ static int a[]=new int[10010]; public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan=new Scanner(System.in); while(scan.hasNext()){ int n=scan.nextInt(); for(int i=0;i<n;i++){ a[i]=scan.nextInt(); } Arrays.sort(a,0,n); System.out.println(a[n/2]); } } }

相關推薦

Hdu1157 完全排序

ACM部落格_kuangbin 新部落格kuangbin.org 人一我百,人十我萬!追逐青春的夢想,懷著自信的心,永不放棄! by kuangbin 新部落格 kuangbin.org ACM HDU 1157 Who’s in the Middle (完

Crashing Robots(模擬)

nes 純粹 rst finall gpo without diameter als tar 1020: Crashing Robots 時間限制(普通/Java):1000MS/10000MS 內存限制:65536KByte 總提交: 207

hdoj-1036-Average is not Fast Enough!(坑)

ID print pan tdi size tin () ++ std 題目鏈接:Average is not Fast Enough! 1 #include <iostream> 2 #include <cstring> 3

模擬但仍需細心多考慮

== stream clas axis nbsp space sin clu 水題 //http://codeforces.com/contest/1065/problem/B#include <iostream> #include <cmath>

【HDU - 2072 】單詞數(字串讀入技巧sstream大法字串讀入格式)

題幹: lily的好朋友xiaoou333最近很空,他想了一件沒有什麼意義的事情,就是統計一篇文章裡不同單詞的總數。下面你的任務是幫助xiaoou333解決這個問題。 Input 有多組資料,每組一行,每組就是一篇小文章。每篇小文章都是由小寫字母和空格組成,沒有標點符號,遇到#時表示

B - Average Numbers CodeForces - 134A(思維)

You are given a sequence of positive integers a1, a2, …, an. Find all such indices i, that the i-th element equals the arithmetic mean of all othe

【 CodeForces - 864B】Polycarp and Letters(字串有坑)

題幹: Polycarp loves lowercase letters and dislikes uppercase ones. Once he got a string sconsisting only of lowercase and uppercase Latin lett

【CodeForces - 1A】Theatre Square(幾何)(CODEFORCES夢的開始)

題幹: Theatre Square in the capital city of Berland has a rectangular shape with the size n × m meters. On the occasion of the city's anni

uvaoj1339 - Ancient Cipher(思維排序字串加密)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4085 兩個字串,判斷能否把其中一個字串重排,然後對每個字母進行對映(比如對映到前一個字母

BZOJ5450: 轟炸(Tarjan縮點求最長路)

5450: 轟炸 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 43  Solved:18[Submit][Status][Discuss] Description 有n座

【HihoCoder - 1880】地鐵環線 (字首和模擬)

題幹: H市有一環線地鐵,一共包含N站,編號1~N。正向行駛的地鐵會按1 -> 2 -> 3 -> ... -> N -> 1的方向行駛,反向會按1 -> N -> N-1 -> ... -> 3 -> 2 -> 1的方向行駛

Problem A. Welcome(快速模擬出答案學習思維)

Problem A. Welcome Input file: Output file: Time limit: Memory limit: stdin stdout 1 second 128 megabytes ”How happy we are, To mee

【牛客 - 302哈爾濱理工大學軟體與微電子學院第八屆程式設計競賽同步賽(低年級)】 小樂樂算數字(快速冪lowbit)

題幹: 小樂樂最喜歡玩數字了。 小樂樂最近迷上了2這個整數,他覺得2的冪是一種非常可愛的數字。 小樂樂想知道整數x的最大的 2的冪 (2^y)的因子。 y為整數。   輸入描述: 輸入整數x。(1<=x<=1e18) 輸出描述: 輸出整數

【UVA - 10038】Jolly Jumpers (模擬標記)

題幹: 題目大意: 要任意相鄰的兩個數的絕對值在[1,n),而且這個範圍內的每個數都要出現一次。 解題報告:    直接模擬就行了、 AC程式碼: #include<cstdio> #include<iostream> #inc

【UVA - 11292】Dragon of Loowater (貪心模擬twopointer雙指標)

題幹: 題目大意: n條惡龍,m個勇士,用勇士來殺惡龍。一個勇士只能殺一個惡龍。而且勇士只能殺直徑不超過自己能力值的惡龍。每個勇士需要支付能力值一樣的金幣。問殺掉所有惡龍需要花費的最少金幣。 解題報告:    twopointer排序後從頭到尾掃一遍就行了。

每日一——刪除排序陣列的重複項

給定一個排序陣列,你需要在原地刪除重複出現的元素,使得每個元素只出現一次,返回移除後陣列的新長度。 不要使用額外的陣列空間,你必須在原地修改輸入陣列並在使用 O(1) 額外空間的條件下完成。 示例 : 給定 nums = [0,0,1,1,1,2,2,3

1022】Train Problem I (棧模擬思維)

題幹: As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because

leedcode第26刪除排序陣列的重複項——基於c++

RemoveDuplicates::clearDuplicates(vector<int>& nums) { /**方法一:暴力法 * 從前往後遍歷行不通,考慮從後往前遍歷,遇到重複項可以直接length - 1並捨棄。 * 0

1038A 】Equality (思維預處理字串)

題幹: You are given a string ss of length nn, which consists only of the first kk letters of the Latin alphabet. All letters in string ss a

XOR Clique 異或運算

(做題實在沒有思路的時候可以研究研究案例) 題目: BaoBao has a sequence a​1​​,a​2​​,...,a​n​​. He would like to find a subset S of {1,2,...,n} such that ∀i,j∈S,