Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 5074 | Accepted: 1754 |
Description
The factory is currently in a state of complete chaos: recently, the factory has been bought by a new owner, and the new director has fired almost everyone. The new staff know almost nothing about building widgets, and it seems that no one remembers how many days are required to build each diofferent type of widget. This is very embarrassing when a client orders widgets and the factory cannot tell the client how many days are needed to produce the required goods. Fortunately, there are records that say for each widgeteer the date when he started working at the factory, the date when he was fired and what types of widgets he built. The problem is that the record does not say the exact date of starting and leaving the job, only the day of the week. Nevertheless, even this information might be helpful in certain cases: for example, if a widgeteer started working on a Tuesday, built a Type 41 widget, and was fired on a Friday,then we know that it takes 4 days to build a Type 41 widget. Your task is to figure out from these records (if possible) the number of days that are required to build the different types of widgets.
Input
4 WED SUN
13 18 1 13
Note that the widgeteers work 7 days a week, and they were working on every day between their first and last day at the factory (if you like weekends and holidays, then do not become a widgeteer!).
The input is terminated by a test case with n = m = 0 .
Output
Sample Input
2 3 2 MON THU 1 2 3 MON FRI 1 1 2 3 MON SUN 1 2 2 10 2 1 MON TUE 3 1 MON WED 3 0 0
Sample Output
8 3 Inconsistent data.
Hint
Huge input file, 'scanf' recommended to avoid TLE.
题意:有n个产品和m个工人工作记录。记录信息如下
第一行——工人完成产品数目num 工人被雇佣的日期s 工人被解雇的日期e(日期均为周一、二 ... 六、日)
第二行——num个产品 表示第多少类产品。
现在问你能否从给出的信息中求出每个产品加工所需的时间,若可以输出n个产品加工所需的天数,若不能确定输出Multiple solutions.,若确定无解输出Inconsistent data.。 注:每个产品加工天数范围是3 ~ 9天。
对第i个记录,用conduct表示产品加工所需天数,用num表示记录中产品的数目,date表示日期差e - s + 1。
(conduct[1] * num[1] + conduct[2] * num[2] + ... + conduct[n] * num[n]) % 7 = ((date % 7 ) + 7 ) % 7 .
思路:对每组记录构造一个类似上面的方程,得到m个n元模7线性方程组,然后高斯消元。注意高斯消元中的对7的取模以及输出时结果的限制。
AC代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#define MAXN 300+1
using namespace std;
int a[MAXN][MAXN];
int equ, var;
int x[MAXN];
int n, m;
int date(char *s)
{
if(s[0] == 'M')
return 1;
else if(s[0] == 'T')
{
if(s[1] == 'U')
return 2;
else
return 4;
}
else if(s[0] == 'W')
return 3;
else if(s[0] == 'F')
return 5;
else
{
if(s[1] == 'A')
return 6;
else
return 7;
}
}
int gcd(int a, int b){
return b == 0 ? a : gcd(b, a%b);
}
int lcm(int a, int b){
return a / gcd(a, b) * b;
}
//int used[MAXN];
void init_a()
{
equ = m; var = n;
memset(a, 0, sizeof(a));
char s[10], e[10];
for(int i = 0; i < m; i++)
{
int num;
scanf("%d%s%s", &num, s, e);
a[i][var] = ((date(e) - date(s) + 1) % 7 + 7) % 7;
//memset(used, 0, sizeof(used));
while(num--)
{
int type;
scanf("%d", &type);
a[i][type-1]++;
a[i][type-1] %= 7;
}
}
}
int Gauss()
{
int max_r, k;
int col = 0;
for(k = 0; k < equ && col < var; k++, col++)
{
max_r = k;
for(int i =k+1; i < equ; i++)
if(abs(a[i][col]) > abs(a[max_r][col]))
max_r = i;
if(max_r != k)
for(int i = col; i < var+1; i++)
swap(a[k][i], a[max_r][i]);
if(a[k][col] == 0)
{
k--;
continue;
}
for(int i = k+1; i < equ; i++)
{
if(a[i][col])
{
int LCM = lcm(abs(a[i][col]), abs(a[k][col]));
int ta = LCM / abs(a[i][col]);
int tb = LCM / abs(a[k][col]);
if(a[i][col] * a[k][col] < 0)
tb = -tb;
for(int j = col; j < var+1; j++)
a[i][j] = ((a[i][j] * ta - a[k][j] * tb) % 7 + 7) % 7;
}
}
}
for(int i = k; i < equ; i++)
if(a[i][col])
return -1;
if(k < var)
return var - k;
for(int i = var-1; i >= 0; i--)
{
int temp = a[i][var];
for(int j = i+1; j < var; j++)
if(a[i][j])
temp = ((temp - a[i][j] * x[j]) % 7 + 7) % 7;
while(temp % a[i][i]) temp += 7;
x[i] = temp / a[i][i] % 7;
}
return 0;
}
int main()
{
while(scanf("%d%d", &n, &m), n||m)
{
init_a();
int free_num = Gauss();
if(free_num == -1)
printf("Inconsistent data.\n");
else if(free_num == 0)
{
for(int i = 0; i < var; i++)
{
if(i > 0) printf(" ");//结果可能为0、1、2
printf("%d", x[i] < 3 ? x[i] + 7 : x[i]);
}
printf("\n");
}
else
printf("Multiple solutions.\n");
}
return 0;
}