ACM-ICPC或ICPC-国立中央大学.ppt

ACM-ICPC或ICPC-国立中央大学

* * * * * * * * * * * * * * * * * * * * * * * * * Tips 多準備(寫好)常用的函式或多使用C++ Standard Template Library E.G. #include stl.h stackint S; queuechar Q; priority_queueint PQ; stack - S.push(), S.top(), S.pop(), S.empty() You should always top on pop because top returns but does not remove the element on top, while pop removes but does not return the element. Linked implementations ensure the stack will never be full. queue – Q.front(), Q.back(), Q.push(), Q.pop(), and Q.empty() hash_map – H.erase(), H.find(), H.insert() priority_queue – PQ.push(), PQ.top(), PQ.pop(), PQ.empty() set – S.set_union, S.set_intersection sort or stable_sort ACM 10041 Vito’s family 題目: Background? The world-known gangster Vito Deadstone is moving to New York. He has a very big family there, all of them living in Lamafia Avenue. Since he will visit all his relatives very often, he is trying to find a house close to them. Problem? Vito wants to minimize the total distance to all of them and has blackmailed you to write a program that solves his problem. Input output Input? The input consists of several test cases. The first line contains the number of test cases. For each test case you will be given the integer number of relatives r ( 0 r 500) and the street numbers (also integers) where they live ( 0 si 30000 ). Note that several relatives could live in the same street number. Output? For each test case your program must write the minimal sum of distances from the optimal Vitos house to each one of his relatives. The distance between two street numbers si and sj is dij= |si-sj|. 題目目的: 給一個陣列 A[i] i = 0….n 找出 k, 使得 的值最小 輸出此最小值 Sample input output Sample Input? 2 2 2 4 3 2 4 6 Sample Output? 2 4 作法簡介 排序並找出中位數(median),再計算距離之總和為多少 中文Java程式碼 import java.util.*; class Vito { static int[] 門牌=new int[500]; static Scanner 輸入 = new Scanner(System.in); public static void main(String[] 參數) { int 狀況數 = 輸入.nextInt(); for(int i=1;i=狀況數;++

文档评论(0)

1亿VIP精品文档

相关文档