Codeforces #561 A.Silent Classroom

There are n students in the first grade of Nlogonia high school. The principal wishes to split the students into two classrooms (each student must be in exactly one of the classrooms). Two distinct students whose name starts with the same letter will be chatty if they are put in the same classroom (because they must have a lot in common). Let x be the number of such pairs of students in a split. Pairs (a,b) and (b,a) are the same and counted only once.

For example, if there are 6 students: “olivia”, “jacob”, “tanya”, “jack”, “oliver” and “jessica”, then:

splitting into two classrooms (“jack”, “jacob”, “jessica”, “tanya”) and (“olivia”, “oliver”) will give x=4 (3 chatting pairs in the first classroom, 1 chatting pair in the second classroom),
splitting into two classrooms (“jack”, “tanya”, “olivia”) and (“jessica”, “oliver”, “jacob”) will give x=1 (0 chatting pairs in the first classroom, 1 chatting pair in the second classroom).
You are given the list of the n names. What is the minimum x we can obtain by splitting the students into classrooms?

Note that it is valid to place all of the students in one of the classrooms, leaving the other one empty.

Input
The first line contains a single integer n (1≤n≤100) — the number of students.

After this n lines follow.

The i-th line contains the name of the i-th student.

It is guaranteed each name is a string of lowercase English letters of length at most 20. Note that multiple students may share the same name.

Output
The output must consist of a single integer x — the minimum possible number of chatty pairs.

Examples
inputCopy
4
jorge
jose
oscar
jerry
outputCopy
1
inputCopy
7
kambei
gorobei
shichiroji
kyuzo
heihachi
katsushiro
kikuchiyo
outputCopy
2
inputCopy
5
mike
mike
mike
mike
mike
outputCopy
4
Note
In the first sample the minimum number of pairs is 1. This can be achieved, for example, by putting everyone except jose in one classroom, and jose in the other, so jorge and jerry form the only chatty pair.

In the second sample the minimum number of pairs is 2. This can be achieved, for example, by putting kambei, gorobei, shichiroji and kyuzo in one room and putting heihachi, katsushiro and kikuchiyo in the other room. In this case the two pairs are kambei and kyuzo, and katsushiro and kikuchiyo.

In the third sample the minimum number of pairs is 4. This can be achieved by placing three of the students named mike in one classroom and the other two students in another classroom. Thus there will be three chatty pairs in one classroom and one chatty pair in the other classroom.

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<string>
#define ll long long
#define dd double
using namespace std;

int main() {
	ios::sync_with_stdio(false);
	ll n;
	cin >> n;
	string s;
	map<char, ll> ma;//判断这个开头字母是否已经被用过
	map<char, ll> ma1;//第一个房间
	map<char, ll> ma2;//第二个房间
	for (ll i = 0; i < n; i++) {
		cin >> s;
		if (ma[s[0]] == 0) {//如果这个开头字母没被用过
			ma1[s[0]]++;//进入房间1
			ma[s[0]] = 1;//让这个开头字母处于被用过的状态
		}
		else {
			ma2[s[0]]++;//如果被用过,进入房间2
			ma[s[0]] = 0;//让这个开头字母处于没被用过
		}
	}
	ll sum = 0;//总数
	for (ll i = 0; i < ma1.size(); i++) {//加上房间1内的
		if (ma1[i] == 2) {
			sum += 1;
		}
		else if (ma1[i] == 1) {
			continue;
		}
		else if (ma1[i] > 2) {//这个题看的是对数,所以三个人的时候,12 13 23,是三对
			ma1[i]--;					//4个人的时候就是12 13 14 23 24 34  共6对
			while (ma1[i] != 0) {
				sum += ma1[i];
				ma1[i]--;
			}
		}
	}
	for (ll i = 0; i < ma2.size(); i++) {//加上房间2内的
		if (ma2[i] == 2) {
			sum += 1;
		}
		else if (ma2[i] == 1) {
			continue;
		}
		else if (ma2[i] > 2) {
			ma2[i]--;
			while (ma2[i] != 0) {
				sum += ma2[i];
				ma2[i]--;
			}
		}
	}
	cout << sum << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值