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
Input
4
jorge
jose
oscar
jerry
Output
1
Input
7
kambei
gorobei
shichiroji
kyuzo
heihachi
katsushiro
kikuchiyo
Output
2
Input
5
mike
mike
mike
mike
mike
Output
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.
这个题自己当时想法挺复杂,自己光从那里纠结怎么分啊,
1个人 不用管
2个人 1 1 … +0;
3个人 2 1 … +1
4个人 2 2 … +2
5个人 2 3 …+4
6个人 3 3 …+6
这个怎么呢 就是先把这些人/2,之后先把这一半分到一个教室,之后用n-n/2 是剩下的人再分到另一个房间。
然后怎么计数呢 不难发现其实就是等差数列的和而已 两个人结果 1,三个人 3,就是小于这个数的和。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iomanip>
using namespace std;
int a[26];
int main()
{
	
	int n, s; cin >> n;
	for (int i = 0; i < n; i++)
	{
		char b[26];
		cin >> b;
		a[b[0] - 'a']++;
	}
	s = 0;
	for (int i = 0; i < 26; i++)
	{
		int x, y;
		x = a[i] / 2;
		y = a[i] - x;
		x--; y--;
		if (x > 0) s = s + x * (x + 1) / 2;
		if (y > 0) s = s + y * (y + 1) / 2;
	}
	cout << s;

}
	
发布了57 篇原创文章 · 获赞 5 · 访问量 1582

猜你喜欢

转载自blog.csdn.net/weixin_45988242/article/details/104710111