http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5499
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 a median 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.
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 contains M 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.
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.
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
15 Fresh_Cucumber Fried_Vermicelli Steamed_Stuffed_Bun 108 West_Lake_Water_Shield_Soup DongPo's_Braised_Pork DongPo's_Crisp
分析:
输入三个数,分别代表三个食物数量。按顺序输入每个东西的名字与质量,然后对于每种食物质量为中位数的加起来,最后输出。
直接结构体排序,输出中间的那个,如果是偶数输出中间较大者,
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <stack> 5 #include <queue> 6 #include <map> 7 #include <set> 8 #include <vector> 9 #include <math.h> 10 #include <algorithm> 11 using namespace std; 12 #define ls 2*i 13 #define rs 2*i+1 14 #define up(i,x,y) for(i=x;i<=y;i++) 15 #define down(i,x,y) for(i=x;i>=y;i--) 16 #define mem(a,x) memset(a,x,sizeof(a)) 17 #define w(a) while(a) 18 #define LL long long 19 const double pi = acos(-1.0); 20 #define Len 20005 21 #define mod 19999997 22 const int INF = 0x3f3f3f3f; 23 24 int t,a,b,c; 25 26 struct node 27 { 28 char name[105]; 29 int num; 30 }s[3][105]; 31 32 int cmp(node a,node b) 33 { 34 return a.num<b.num; 35 } 36 37 int main() 38 { 39 int i,j,k; 40 scanf("%d",&t); 41 w(t--) 42 { 43 scanf("%d%d%d",&a,&b,&c); 44 up(i,0,a-1) 45 scanf("%s%d",s[0][i].name,&s[0][i].num); 46 up(i,0,b-1) 47 scanf("%s%d",s[1][i].name,&s[1][i].num); 48 up(i,0,c-1) 49 scanf("%s%d",s[2][i].name,&s[2][i].num); 50 sort(s[0],s[0]+a,cmp); 51 sort(s[1],s[1]+b,cmp); 52 sort(s[2],s[2]+c,cmp); 53 int ans=s[0][a/2].num+s[1][b/2].num+s[2][c/2].num; 54 printf("%d %s %s %s\n",ans,s[0][a/2].name,s[1][b/2].name,s[2][c/2].name); 55 } 56 57 return 0; 58 }