Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 2440 | Accepted: 1002 |
Description
The Association for Computing Machinery (ACM) is considering new rules for its regional programming contests, in part to solve some software problems. For instance, the program that prints out the badges for each team was designed so that the same font size is used to print all badges for that team. However, this means that if one member of the team has a very long name, then a very small font will be used to print all of the team’s badges, and this wouldn't look very nice for someone with an extremely short name like "AL".
The initial solution proposed by the ACM was to put a limit on the length of contestants' names. Someone pointed out that this would discriminate against teams from certain regions where bigger names are more common (for instance, children in the home towns of well-known actors are more likely to be named after that actor – imagine how many children in Oakland, California have been named after the actor Mahershalalhashbaz Ali since he became one of the stars of the television series The 4400).
As a compromise, the ACM decided to change the rule to require that "no team member's name can have length more than two away from the average length of all the team member's names." Using this rule, a team consisting of "MAHERSHALALHASHBAZ", "AL", and "BILL" would be disqualified (average name length is 8, so AL and BILL are not within 2). However, "MAHERSHALALHASHBAZ", "NEBUCHADNEZZAR", and "BILLYBOBBENJAMIN" would be okay (average name length is 16, and all team member names have length within 2 of this number). Given the names of n students, determine whether or not they can be placed into teams of k members each so that each team meets the requirements of the new ACM rule.
Input
Input will consist of multiple test cases. Each test case begins with a line consisting of two positive integers n and k, where n ≤ 1000, k ≤ 8, and n is divisible by k. Following this are n lines, each containing a single name consisting only of upper case letters with no embedded, leading, or trailing blanks. These are the names of the n students who need to be organized into teams of size k each. No name will exceed 80 characters. The last test case is followed by a line containing two zeros.
Output
For each test case, output the case number (starting at 1) followed by either the word "yes" (meaning that it is possible to organize the students into teams of size k so that no student on that team has a name whose length is greater than distance 2 from the average name lengths of the members on that team), or "no" if it is not possible. Use the format shown in the sample output. Insert a blank line between cases.
Sample Input
3 3 MAHERSHALALHASHBAZ AL BILL 6 3 MAHERSHALALHASHBAZ AL NEBUCHADNEZZAR BILL BILLYBOBBENJAMIN JILL 0 0
Sample Output
Case 1: no Case 2: yes
Source
大体题意:
n 个队伍要分成k 个人一组,要求每个队伍内部不能存在一个人的名字 长于 平均数名字 + 2。
思路:
既然是每个人和平均数比较的话,我们肯定要对名字排序了, 这样名字长度最接近的一组肯定没问题了,
如果依然不能话,那就没解了!
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<iostream>
#include<set>
#include<map>
#include<stack>
#include<string>
#include<queue>
using namespace std;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
typedef long long ll;
typedef unsigned ULL;
struct Point{
double x,y;
Point(double x = 0,double y = 0):x(x),y(y){}
bool operator < (const Point& rhs) const {
return x < rhs.x || (fabs(x- rhs.x) < eps && y < rhs.y);
}
bool operator == (const Point & rhs) const {
return fabs(x - rhs.x) < eps && fabs(y - rhs.y) < eps;
}
};
typedef Point Vector;
Vector operator + (Vector A,Vector B) { return Vector(A.x+B.x,A.y + B.y); }
Vector operator - (Vector A,Vector B) { return Vector(A.x-B.x,A.y - B.y); }
Vector operator * (Vector A,double p) { return Vector(A.x*p,A.y*p); }
Vector operator / (Vector A,double p) { return Vector(A.x/p,A.y/p); }
double Cross(Vector A,Vector B) { return A.x*B.y - B.x*A.y; }
Point GetLineIntersection(Point P,Vector v,Point Q,Vector w){
Vector u = P-Q;
double t = Cross(w, u) / Cross(v, w);
return P + v*t;
}
int a[1007];
char s[1007];
int main(){
int n,m,kase = 0;
while(scanf("%d %d",&n,&m) == 2 && (n || m )){
for (int i = 0; i <n; ++i){
scanf("%s",s);
a[i] = strlen(s);
}
sort(a,a+n);
bool ok = true;
for (int i = 0; i < n; ++i){
int j;
int sum = 0;
for (j = i; j < n && j < i + m; ++j){
sum += a[j];
}
double aver = sum*1.0/m;
for (j = i; j < n && j < i + m; ++j){
if (a[j] > aver + 2){ok =false;}
}
i = j-1;
}
if (kase)puts("");
printf("Case %d: ",++kase);
if (ok)printf("yes\n");
else printf("no\n");
}
return 0;
}