codeforces 12 C. Fruits

http://codeforces.com/contest/12/problem/C 题意:有n个价格价格,m个要买的东西(可能有相同的种类,设为k种),把n个标签中拿出k个给个贴上。。。问最大价钱和最少价钱分别是多少。 思路:贪心。不过要按照map的value排序。。然后发现其实不用排序。。因为map的key值其实不影响。

vector降序排列的话。。直接 sort(v.rbegin(),v.rend());就好。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2015年12月29日 星期二 15时30分54秒
 4File Name :code/cf/problem/12C.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int  N=105;
34int n,m;
35map<string,int>mp;
36int a[N];
37map<string,int>::iterator it;
38vector<int >v;
39int main()
40{
41	#ifndef  ONLINE_JUDGE 
42	freopen("code/in.txt","r",stdin);
43  #endif
44	mp.clear();
45	cin>>n>>m;
46	for ( int i =  0; i < n ; i++) cin>>a[i];
47	sort(a,a+n);
48
49	for ( int i = 0 ; i < m ; i++)
50	{
51	    string tmp;
52	    cin>>tmp;
53	    if (!mp[tmp])
54	    {
55		mp[tmp] = 1;
56	    }
57	    else
58	    {
59		mp[tmp]++;
60	    }
61	}
62
63
64	for ( it = mp.begin() ; it !=mp.end() ; it++)
65	{
66	    v.push_back(it->second);
67	}
68	int mi,mx;
69	mi=mx=0;
70	sort(v.rbegin(),v.rend());
71	for ( int  i = 0 ; i < v.size() ; i++)
72	{
73	    mi +=a[i]*v[i];
74//	    cout<<"v[i]:"<<v[i]<<endl;
75	    mx +=a[n-1-i]*v[i];\
76	    //cout<<"mi:"<<mi<<" mx:"<<mx<<endl;
77	}
78	cout<<mi<<" "<<mx<<endl;
79
80
81
82  #ifndef ONLINE_JUDGE  
83  fclose(stdin);
84  #endif
85    return 0;
86}