链接:https://codeforces.com/contest/1166/problem/A

题意:

There are nn 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 xx be the number of such pairs of students in a split. Pairs (a,b)(a,b) and (b,a)(b,a) are the same and counted only once.

For example, if there are 66 students: "olivia", "jacob", "tanya", "jack", "oliver" and "jessica", then:

  • splitting into two classrooms ("jack", "jacob", "jessica", "tanya") and ("olivia", "oliver") will give x=4x=4 (33 chatting pairs in the first classroom, 11 chatting pair in the second classroom),
  • splitting into two classrooms ("jack", "tanya", "olivia") and ("jessica", "oliver", "jacob") will give x=1x=1 (00 chatting pairs in the first classroom, 11 chatting pair in the second classroom).

You are given the list of the nn names. What is the minimum xx 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.

思路:

计算每个首字母出现的次数,均匀分配到两个教室,求每边的配对数即可。

代码:

#include <bits/stdc++.h>
using namespace std; typedef long long LL; int dic[30]; int main()
{
int n;
string s;
cin >> n;
for (int i = 1;i <= n;i++)
{
cin >> s;
char w = s[0];
w = tolower(w);
dic[w-'a']++;
}
int res = 0;
for (int i = 0;i < 26;i++)
{
int l = dic[i]/2;
int r = dic[i]-l;
l--;
r--;
if (l > 0)
res += (1+l)*l/2;
if (r > 0)
res += (1+r)*r/2;
}
cout << res << endl; return 0;
}

  

Codeforces Round #561 (Div. 2) A. Silent Classroom的更多相关文章

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

    A. Silent Classroom time limit per test1 second memory limit per test256 megabytes inputstandard inp ...

  2. Codeforces Round #561 (Div. 2) C. A Tale of Two Lands

    链接:https://codeforces.com/contest/1166/problem/C 题意: The legend of the foundation of Vectorland talk ...

  3. Codeforces Round #561 (Div. 2) B. All the Vowels Please

    链接:https://codeforces.com/contest/1166/problem/B 题意: Tom loves vowels, and he likes long words with ...

  4. Codeforces Round 561(Div 2)题解

    这是一场失败的比赛. 前三题应该是随便搞的. D有点想法,一直死磕D,一直WA.(赛后发现少减了个1……) 看E那么多人过了,猜了个结论交了真过了. 感觉这次升的不光彩……还是等GR3掉了洗掉这次把, ...

  5. Codeforces Round #561 (Div. 2) E. The LCMs Must be Large(数学)

    传送门 题意: 有 n 个商店,第 i 个商店出售正整数 ai: Dora 买了 m 天的东西,第 i 天去了 si 个不同的个商店购买了 si 个数: Dora 的对手 Swiper 在第 i 天去 ...

  6. Codeforces Round #561 (Div. 2)

    C. A Tale of Two Lands 题意: 给出 n 个数,问有多少点对(x,y)满足 |x-y| ≤ |x|,|y| ≤ |x+y|: (x,y) 和 (y,x) 表示一种答案: 题解: ...

  7. Codeforces Round #561 (Div. 2) A Tale of Two Lands 【二分】

    A Tale of Two Lands 题目链接(点击) The legend of the foundation of Vectorland talks of two integers xx and ...

  8. Codeforces Round #383 (Div. 2) 题解【ABCDE】

    Codeforces Round #383 (Div. 2) A. Arpa's hard exam and Mehrdad's naive cheat 题意 求1378^n mod 10 题解 直接 ...

  9. Codeforces Round #441 (Div. 2)【A、B、C、D】

    Codeforces Round #441 (Div. 2) codeforces 876 A. Trip For Meal(水题) 题意:R.O.E三点互连,给出任意两点间距离,你在R点,每次只能去 ...

随机推荐

  1. LinkedList_1.打印两个有序链表的公共部分

    思路: 实例化出两个链表($link_a, $link_b),比较连个链表当前元素的大小,谁小谁执行next()方法继续比较,当出现相当的时候把相等的值塞入数组$common里,当两个链表有一个元素比 ...

  2. VC Q&A (原创)

    Q1:External Dependencies有什么作用? A1:(网友答复:)External   Dependencies是说你没有把这个文件加入到这个工程中,但是需要这个文件的支持.当然有时是 ...

  3. MySQL记录_20160919

    1.首先先看下什么是MySQL. MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 最流行的关系型数据库管理系统,其开放源码这一特点 ...

  4. Mesos以及Marathon安装总结

    安装了将近一周的环境了,终于把Mesos以及Marathon给安装上了,我指的离线安装. 策略1: 严格的按照官网的流程: http://mesos.apache.org/gettingstarted ...

  5. BZOJ1382:[Baltic2001]Mars Maps

    浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://www.lydsy.com/JudgeOnline/prob ...

  6. 一致性哈希算法原理、避免数据热点方法及Java实现

     一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网中的热点(Hot spot)问题,初衷和CARP十分类似.一致性哈希修正了CARP使用的简 单 ...

  7. 生成分布式随机ID

    经测试,最快的一种 public class Generator { // should be between 40 (34 years) and 42 (139 years) ; // should ...

  8. [MTC3]Cracking SHA1-Hashed Passwords

    题目地址:https://www.mysterytwisterc3.org/en/challenges/level-ii/cracking-sha1-hashed-passwords 解题关键:根据键 ...

  9. 项目debug2

    用户登录后,邮件发送失败?为什么呢? 密码得是,授权码,而不是,qq的密码.

  10. IDEA上使用github上传代码

    这里的origin是表示我创建一个名为origin的仓库吗? 早已经存在了,我该怎么删除这个wenda呢? 将它修改为wenda1,如下: 点击项目,右击: 再点击项目,右击,选择commit: 问题 ...