A. Silent Classroom

题目链接
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 students in another classroom. Thus there will be three chatty pairs in one classroom and one chatty pair in the other classroom.

题意:校长将学生分成两个教室(每个学生必须在一个教室中),每个学生都有一个特别的名字,它们名字的第一个字母相同则有很多共同话题(之处),于是它们在一个教室里就会很聊得来,那么我们需要安排学生到两个教室,使得产生尽可能少的聊天对;
解题思路:
对于第一个字母进行计数,我们需要知道第一字母相同个数出现的次数。首先,我们可以知道,第一个字母只出现一次的,不管安排到那个教室都不可能成为聊天对,那么我们就不用考虑它们,还有一个是出现两次的,我们只需要一个安排在一个教室那么就不会有聊天对了;
所以,我们只需要考虑的是出现次数大于2 的OK了。

#include <iostream>
#include <string>
using namespace std;
int main()
{
    int n,x=0,i,t;
    cin>>n;
    char a[101];
    int b[26]={0};
    string s;
    for(i=0;i<n;i++)
    {
        cin>>s;
        b[s[0]-97]++;//计数
    }
    /*for(i=0;i<26;i++)
    {
        cout<<b[i]<<" ";
    }
    cout<<endl;*/
    for(i=0;i<26;i++)
    {
        if(b[i]>2)//大于二的
        {
            t=b[i]/2;
            if(b[i]%2!=0)//奇数
            {
                x=x+ (t*(t-1))/2 + (t*(t+1))/2;
            }
            else//偶数
            {
                x=x+ 2*(t*(t-1))/2;
            }
        }
    }
    cout<<x;
}
全部评论

相关推荐

暴走萝莉莉:这是社招场吧,作为HR说个实话:这个维护关系的意思是要有政府资源,在曾经的工作中通过人脉资源拿下过大订单的意思。这个有相关管理经验,意思也是真的要有同岗位经验。应酬什么的对于业务成交来说就算不乐意也是常态,就是要求说话好听情商高,酒量好。
点赞 评论 收藏
分享
11-02 09:49
已编辑
货拉拉_测试(实习员工)
热爱生活的仰泳鲈鱼求你们别卷了:没事楼主,有反转查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务