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());就好。
/* ***********************************************
Author :111qqz
Created Time :2015年12月29日 星期二 15时30分54秒
File Name :code/cf/problem/12C.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=105;
7int n,m;
8map<string,int>mp;
9int a[N];
10map<string,int>::iterator it;
11vector<int >v;
12int main()
13{
14 #ifndef ONLINE_JUDGE
15 freopen("code/in.txt","r",stdin);
16 #endif
17 mp.clear();
18 cin>>n>>m;
19 for ( int i = 0; i < n ; i++) cin>>a[i];
20 sort(a,a+n);
1 for ( int i = 0 ; i < m ; i++)
2 {
3 string tmp;
4 cin>>tmp;
5 if (!mp[tmp])
6 {
7 mp[tmp] = 1;
8 }
9 else
10 {
11 mp[tmp]++;
12 }
13 }
1 for ( it = mp.begin() ; it !=mp.end() ; it++)
2 {
3 v.push_back(it->second);
4 }
5 int mi,mx;
6 mi=mx=0;
7 sort(v.rbegin(),v.rend());
8 for ( int i = 0 ; i < v.size() ; i++)
9 {
10 mi +=a[i]*v[i];
11// cout<<"v[i]:"<<v[i]<<endl;
12 mx +=a[n-1-i]*v[i];\
13 //cout<<"mi:"<<mi<<" mx:"<<mx<<endl;
14 }
15 cout<<mi<<" "<<mx<<endl;
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}