2013
11-12

# Widget Factory

The widget factory produces several different kinds of widgets. Each widget is carefully built by a skilled widgeteer. The time required to build a widget depends on its type: the simple widgets need only 3 days, but the most complex ones may need as many as 9 days.

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.

The input contains several blocks of test cases. Each case begins with a line containing two integers: the number 1 ≤ n ≤ 300 of the different types, and the number 1 ≤ m ≤ 300 of the records. This line is followed by a description of the m records. Each record is described by two lines. The first line contains the total number 1 ≤ k ≤ 10000 of widgets built by this widgeteer, followed by the day of week when he/she started working and the day of the week he/she was fired. The days of the week are given bythe strings MON’, TUE’, WED’, THU’, FRI’, SAT’ and SUN’. The second line contains k integers separated by spaces. These numbers are between 1 and n , and they describe the diofferent types of widgets that the widgeteer built. For example, the following two lines mean that the widgeteer started working on a Wednesday, built a Type 13 widget, a Type 18 widget, a Type 1 widget, again a Type 13 widget,and was fired on a Sunday.

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 .

For each test case, you have to output a single line containing n integers separated by spaces: the number of days required to build the different types of widgets. There should be no space before the first number or after the last number, and there should be exactly one space between two numbers. If there is more than one possible solution for the problem, then write Multiple solutions.’ (without the quotes). If you are sure that there is no solution consistent with the input, then write Inconsistent data.’(without the quotes).

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

8 3
Inconsistent data.

Huge input file, ‘scanf’ recommended to avoid TLE.

import java.io.*;
import java.util.HashMap;

public class Main{

public static final int MAX = 310;
static int equ,var;
static int a[][];
static int x[];
static HashMap< String,Integer> hash = new HashMap< String,Integer>();

public static void main(String[] args) throws Exception{
hash.put("MON",0);hash.put("TUE",1);hash.put("WED",2);hash.put("THU",3);
hash.put("FRI",4);hash.put("SAT",5);hash.put("SUN", 6);

while(true){
var = Integer.parseInt(str[0]);
equ = Integer.parseInt(str[1]);
if(var==0&&equ==0)
break;

a = new int[MAX][MAX];
x = new int[MAX];

for(int i=0;i< equ;i++){
int k = Integer.parseInt(str[0]);
a[i][var] = ((hash.get(str[2])-hash.get(str[1])+1)%7+7)%7;

for(int j=0;j< k;j++){
int t = Integer.parseInt(str[j])-1;
a[i][t]++;
a[i][t] %= 7;
}
}

int res = guass();

if(res == 0){
for(int i=0;i< var;i++)
if(x[i]< 3)
x[i] += 7;
for(int i=0;i< var-1;i++)
System.out.print(x[i]+" ");
System.out.print(x[var-1]);
System.out.println();
}else if(res == -1)
System.out.println("Inconsistent data.");
else
System.out.println("Multiple solutions.");

}

}

public static int guass() {

int row , col;
row = col = 0;

while(row< equ&&col< var){
int mr = row;
for(int i=row+1;i< equ;i++){//保证从上到下 每个式子都保留着 0...var 的变量
if(Math.abs(a[i][col])>Math.abs(a[mr][col]))
mr = i;
}

if(mr != row)
for(int i=col;i<=var;i++){
int tmp = a[row][i];
a[row][i] = a[mr][i];
a[mr][i] = tmp;
}

if(a[row][col]==0){
col++;
continue;
}

for(int i=row+1;i< equ;i++){//以第row个式子为基础化简其余剩下的式子（为了消去第col个变量)
if(a[i][col]!=0){
int L = lcm(Math.abs(a[i][col]),Math.abs(a[row][col]));
int ta = L/Math.abs(a[i][col]); int tb = L/Math.abs(a[row][col]);
if(a[i][col]*a[row][col]< 0)   //考虑异号的情况
tb = -tb;
for(int j=col;j<=var;j++)
a[i][j] = ((a[i][j]*ta-a[row][j]*tb)%7+7)%7;
}
}
row++;
col++;
}
//求解 var个未知数 只需要var个式子 如果有多余的式子(经化简后） 必全部为0否则无解
for(int i=row;i< equ;i++)
if(a[i][var]!=0)
return -1;
// 判断是否有变量没有在所有式子中出现或着当row指到最后一个式子时  即总式子量< 总未知数量
if(var>row)
return var-row;  // row 即为  所有式子中 所有的变量总数

for(int i=var-1;i>=0;i--){
int tmp = a[i][var];
for(int j=i+1;j< var;j++)
tmp = ((tmp-a[i][j]*x[j])%7+7)%7;
while(tmp%a[i][i]!=0)
tmp += 7;
x[i] = (tmp/a[i][i])%7;
}

return 0;
}

public static int lcm(int a, int b) {

return a*b/gcd(a,b);
}

public static int gcd(int a, int b) {

return b==0?a:gcd(b,a%b);
}
}`

1. 这道题这里的解法最坏情况似乎应该是指数的。回溯的时候
O(n) = O(n-1) + O(n-2) + ….
O(n-1) = O(n-2) + O(n-3)+ …
O(n) – O(n-1) = O(n-1)
O(n) = 2O(n-1)

2. /*
* =====================================================================================
*
* Filename: 1366.cc
*
* Description:
*
* Version: 1.0
* Created: 2014年01月06日 14时52分14秒
* Revision: none
* Compiler: gcc
*
* Author: Wenxian Ni (Hello World~), [email protected]
* Organization: AMS/ICT
*
* =====================================================================================
*/

#include
#include

using namespace std;

int main()
{
stack st;
int n,i,j;
int test;
int a[100001];
int b[100001];
while(cin>>n)
{
for(i=1;i>a[i];
for(i=1;i>b[i];
//st.clear();
while(!st.empty())
st.pop();
i = 1;
j = 1;

while(in)
break;
}
while(!st.empty()&&st.top()==b[j])
{
st.pop();
j++;
}
}
if(st.empty())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}