题目链接:点这里
题意:一个人数n,有n个人名,要把他们分到两个教室里,名字首字母的尽量不要放一起,如果有名字首字母一样的放一起,就要加一张凳子。
思路:刚开始我找起了规律,然后wa了好多。。明明是一道水题。正解是开两个数组,只要每次读入把名字放到相同首字母比较少的那个数组里就好了。太菜了,浪费了我好久时间,打div2,最后太困了,只写了两道题,就去睡觉了,光荣掉分。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
const int maxn=50000+10;
int a[27], b[27];
int main()
{
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
int k;
cin >> k;
while (k--)
{
string d;
cin >> d;
if (a[d[0] - 'a'] <= b[d[0] - 'a'])
{
a[d[0] - 'a']++;
}
else
b[d[0] - 'a']++;
}
int res = 0;
for (int i = 0; i < 26; i++)
{
res += a[i] * (a[i] - 1) / 2;
res += b[i] * (b[i] - 1) / 2;
}
cout << res << endl;
return 0;
}