12C - Fruits - CodeForces Solution


greedy implementation sortings *1100

Please click on ads to support us..

Python Code:

import sys; R = sys.stdin.readline
n,m = map(int,R().split())
a = sorted(map(int,R().split()))
dic = {}
for _ in range(m):
    s = R()
    if s not in dic: dic[s] = 1
    else: dic[s] += 1
dic = sorted(dic.values(),reverse=True)
res1,res2 = 0,0
for i in range(len(dic)):
    res1 += a[i]*dic[i]
    res2 += a[-i-1]*dic[i]
print(res1,res2)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int a[200];
map<string,int>mp;
struct code{
	string s;
	int q;
}b[200];
int n,m;
int mx;
int mn;
int qwe[200];
int c[200];
bool cmp(code x,code y)
{
	return x.q>y.q;
}
void f(int x)
{
	if(x==m+1)
	{
		int ans=0;
		for(int i=1;i<=m;i++)
		{
			ans+=c[i]*b[i].q;
		}
		mx=max(mx,ans);
		mn=min(mn,ans);
		return;
	}
	for(int i=1;i<=n;i++)
	{
		if(qwe[i]==0)
		{
			qwe[i]=1;
			c[x]=a[i];
			f(x+1);
			qwe[i]=0;
		}
	}
	return;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=m;i++)
	{
		cin>>b[i].s;
		if(mp[b[i].s])
		{
			b[mp[b[i].s]].q+=1;
			m--;
			i--;
		}
		else
		{
			mp[b[i].s]=i;
			b[i].q+=1;
		}
	}
	sort(b+1,b+m+1,cmp);
//	printf("\n");
//	for(int i=1;i<=m;i++)
//	{
//		cout<<b[i].s<<" "<<b[i].q<<endl;
//	}
	for(int i=1;i<=m;i++)
	{
		mx+=b[i].q*a[n-i+1];
	}
	for(int i=1;i<=m;i++)
	{
		mn+=b[i].q*a[i];
	}
	printf("%d %d",mn,mx);
	return 0;
}


Comments

Submit
0 Comments
More Questions

1697C - awoo's Favorite Problem
165A - Supercentral Point
1493A - Anti-knapsack
1493B - Planet Lapituletti
747B - Mammoth's Genome Decoding
1591C - Minimize Distance
1182B - Plus from Picture
1674B - Dictionary
1426C - Increase and Copy
520C - DNA Alignment
767A - Snacktower
1365A - Matrix Game
714B - Filya and Homework
31A - Worms Evolution
1691A - Beat The Odds
433B - Kuriyama Mirai's Stones
892A - Greed
32A - Reconnaissance
1236D - Alice and the Doll
1207B - Square Filling
1676D - X-Sum
1679A - AvtoBus
1549A - Gregor and Cryptography
918C - The Monster
4B - Before an Exam
545B - Equidistant String
1244C - The Football Season
1696B - NIT Destroys the Universe
1674A - Number Transformation
1244E - Minimizing Difference