ZOJ 3875 Lunch Time(結構體排序)
The 999th Zhejiang Provincial Collegiate Programming Contest will be held in Marjar University. The canteen of Marjar University is making preparations for this grand competition. The canteen provides a lunch set of three types: appetizer, main course and dessert. Each type has several dishes with different prices for choosing.
Edward is the headmaster of Marjar University. One day, to inspect the quality of dishes, he go to the canteen and decides to choose amedian set for his lunch. That means he must choose one dish from each of appetizers, main courses and desserts. Each chosen dish should at the median price among all dishes of the same type.
For example, if there are five dessert dishes selling at the price of 2, 3, 5, 10, 30, Edward should choose the dish with price 5 as his dessert since its price is located at the median place of the dessert type. If the number of dishes of a type is even, Edward will choose the dish which is more expensive among the two medians.
You are given the list of all dishes, please write a program to help Edward decide which dishes he should choose.
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first line contains three integers S, M and D (1 <=S, M, D <= 100), which means that there are S dishes of appetizer, M dishes of main course and D dishes of dessert.
Then followed by three parts. The first part contains S lines, the second and the last part containsM and D lines respectively. In each line of the three parts, there is a string and an integer indicating the name and the price of a dish. The name of dishes will only consist of non-whitespace characters with no more than 50 characters. The price of dishes are non-negative integers less than or equal to 1000. All dish names will be distinct.
Output
For each test case, output the total price of the median set, together with the names of appetizer, main course and dessert, separated by a single space.
Sample Input
2 1 3 2 Fresh_Cucumber 4 Chow_Mein 5 Rice_Served_with_Duck_Leg 12 Fried_Vermicelli 7 Steamed_Dumpling 3 Steamed_Stuffed_Bun 4 2 3 1 Stir-fried_Loofah_with_Dried_Bamboo_Shoot 33 West_Lake_Water_Shield_Soup 36 DongPo's_Braised_Pork 54 West_Lake_Fish_in_Vinegar 48 Longjing_Shrimp 188 DongPo's_Crisp 18
Sample Output
15 Fresh_Cucumber Fried_Vermicelli Steamed_Stuffed_Bun 108 West_Lake_Water_Shield_Soup DongPo's_Braised_Pork DongPo's_Crisp 題意是說,有S, M and D三種食物,分別有S, M and D個,拿食物的時候取中間價值的,如果沒有剛好中間價值的就取較中間的價值中大的那個。輸出拿的三種食物的總價值和名稱。
/*
* Copyright (c) 2016, 煙臺大學計算機與控制工程學院
* All rights reserved.
* 檔名稱:zoj.cpp
* 作 者:單昕昕
* 完成日期:2016年4月2日
* 版 本 號:v1.0
*/
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
struct N//結構體儲存名稱和價值
{
string na;
int v;
};
int cmp(const void *x,const void *y)//結構體排序
{
return (((N*)x)->v>((N*)y)->v);//對價值降序排列
}
int main()
{
N a[1005];
int t;
cin>>t;
int s,m,d,ans;
string x,y,z;
while(t--)
{
ans=0;
cin>>s>>m>>d;
int i;
//s
for(i=0; i<s; ++i)
{
cin>>a[i].na;
cin>>a[i].v;
}
qsort(a,s,sizeof(a[0]),cmp);//排序
ans+=(a[(s/2)].v);//取中間價值
x=a[(s/2)].na;//記錄名稱
//m
for(i=0; i<m; ++i)
{
cin>>a[i].na;
cin>>a[i].v;
}
qsort(a,m,sizeof(a[0]),cmp);
ans+=(a[(m/2)].v);
y=a[(m/2)].na;
//d
for(i=0; i<d; ++i)
{
cin>>a[i].na;
cin>>a[i].v;
}
qsort(a,d,sizeof(a[0]),cmp);
ans+=(a[(d/2)].v);
z=a[(d/2)].na;
cout<<ans<<" "<<x<<" "<<y<<" "<<z<<endl;
}
return 0;
}
相關文章
- C++結構體排序C++結構體排序
- 排名——採用結構體排序結構體排序
- 【c++】結構體sort排序C++結構體排序
- Runtime中的 isa 結構體結構體
- 有關struct timeval結構體 以及 gettimeofday()函式Struct結構體函式
- pat—結構體排序(用map彌補struct缺陷)結構體排序Struct
- 理解Golang的Time結構Golang
- C4top-搶紅包 (模擬-結構體排序)結構體排序
- 資料結構與排序資料結構排序
- 【資料結構】希爾排序!!!資料結構排序
- 【資料結構】希爾排序資料結構排序
- 【資料結構】堆排序資料結構排序
- 【資料結構】快速排序資料結構排序
- 資料結構(python) —— 【18排序: 桶排序】資料結構Python排序
- HDU 6015-Skip the Class(模擬-結構體排序)結構體排序
- 【資料結構】歸併排序!!!資料結構排序
- 【資料結構】選擇排序!!!資料結構排序
- 【資料結構】氣泡排序資料結構排序
- 【資料結構】選擇排序資料結構排序
- 【資料結構】歸併排序資料結構排序
- 複習資料結構:排序(一)——插入排序資料結構排序
- 複習資料結構:排序(三)——選擇排序資料結構排序
- 結構體中套用其他_結構體結構體
- Runtime備忘-資料結構資料結構
- 資料結構第10章 排序資料結構排序
- 資料結構32:選擇排序資料結構排序
- 資料結構之計數排序資料結構排序
- 資料結構 堆排序 c Swift資料結構排序Swift
- Oracle體系結構:記憶體結構和程式結構(轉)Oracle記憶體
- 結構體結構體
- UVA 10881 Piotr's Ants(等效變換 sort結構體排序)結構體排序
- (戀上資料結構筆記):計數排序、基數排序 、桶排序資料結構筆記排序
- Oracle體系結構之-記憶體結構Oracle記憶體
- 【PHP資料結構】插入類排序:簡單插入、希爾排序PHP資料結構排序
- 資料結構--排序--插入排序--python語言描述資料結構排序Python
- 複習資料結構:排序演算法(六)——堆排序資料結構排序演算法
- 複習資料結構:排序演算法(七)——桶排序資料結構排序演算法
- 複習資料結構:排序演算法(八)——基排序資料結構排序演算法