-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask26-holstein.c
89 lines (78 loc) · 1.21 KB
/
task26-holstein.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
ID: zltsang1
LANG: C
TASK: holstein
*/
#include <stdio.h>
#define mnv 25
#define mng 15
int av,ag;
int vit[mnv],fd[mng][mnv];
int tag[1<<mng],bit[mng];
int cmp(const void*l,const void*r){
int a,b,c,d;
a=*(int*)l;
b=*(int*)r;
c=0;
while(a){c++;a=a&(a-1);}
d=0;
while(b){d++;b=b&(b-1);}
if(c==d)return a-b;
return c-d;
}
void sbit(int m){
int i;
for(i=0;i<ag;i++){
bit[i]=m&1<<i;
}
}
int ok(int mask){
int i,j,t;
for(i=0;i<av;i++){
t=vit[i];
for(j=0;j<ag;j++){
if(bit[j])t-=fd[j][i];
}
if(t>0)return 0;
}
return 1;
}
main () {
FILE *fin = fopen ("holstein.in", "r");
FILE *fout = fopen ("holstein.out", "w");
int i,j,c;
fscanf (fin, "%d", &av);
for(i=0;i<av;i++){
fscanf (fin, "%d", vit+i);
}
fscanf (fin, "%d", &ag);
for(i=0;i<ag;i++){
for(j=0;j<av;j++)
fscanf (fin, "%d", fd[i]+j);
}
for(i=0;i<1<<ag;i++){
tag[i]=i;
}
qsort(tag,1<<ag,sizeof(int),cmp);
for(i=0;i<1<<ag;i++){
sbit(tag[i]);
if(ok(tag[i]))break;
}
c=0;
for(i=0;i<ag;i++){
if(bit[i]){
c++;bit[i]=i+1;
}
}
fprintf (fout, "%d ",c);
for(i=0;i<ag;i++){
if(bit[i]){
--c;
if(!c){
fprintf (fout, "%d\n",bit[i]);
}
else fprintf (fout, "%d ",bit[i]);
}
}
exit (0);
}