前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Codeforces Round #561 (Div. 2) A. Silent Classroom(贪心)

Codeforces Round #561 (Div. 2) A. Silent Classroom(贪心)

作者头像
风骨散人Chiam
发布2020-10-28 10:22:09
3640
发布2020-10-28 10:22:09
举报
文章被收录于专栏:CSDN旧文

A. Silent Classroom time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output 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 student

代码语言:javascript
复制
#include<stdio.h>
#include<string.h>
 
int main() 
{
	static int kk[26];
	int n, c, k, ans;
	scanf("%d", &n);
	while (n--) 
	{
		static char s[32];
		scanf("%s", s);
		kk[s[0] - 'a']++;
	}
	ans = 0;
	for (c = 0; c < 26; c++) 
	{
		k = kk[c] / 2;
		ans += k * (k - 1) / 2 + (kk[c] - k) * (kk[c] - k - 1) / 2;
	}
	printf("%d\n", ans);
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/03/02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档