難度:4/5
Second Try: 4.4/5 Used Time: 44:56
main.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
| #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include "lock.h"
int m, n; int seq[128][3] = {0}; Locks* l;
static inline int sign(int n){ return (n > 0 ? 1 : 0); } static inline int my_pow(int a, int b){ if(b == 0) return 1; int n = 1; for(int i = 0; i < b; i++) n *= a; return n; }
int cnt_score(Locks* l, int num){ int cnt = 0; for(int i = 0; i < l->n; i++){ int flag = 0; for(int j = 0; j < 3; j++){ int light_num = abs(l->lockSeq[i][j]) - 1; int target = sign(l->lockSeq[i][j]); if(target == ((num >> light_num) & 1)){ flag = 1; break; } } if(flag == 1) cnt++; } return cnt; }
int main(){ scanf("%d %d", &m, &n); for(int i = 0; i < n; i++){ for(int j = 0; j < 3; j++){ scanf("%d", &seq[i][j]); } } l = init(seq, n);
int max_score = -1; int max_idx = -1; int upper_bound = my_pow(2, m); for(int i = 0; i < upper_bound; i++){ int now_score = cnt_score(l, i); if(now_score > max_score){ max_score = now_score; max_idx = i; } }
for(int i = 0; i < m; i++){ printf("%d", (max_idx >> i) & 1); } printf("\n"); printf("%d\n", max_score); printf("%d\n", max_idx); }
|
lock.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
| #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h> #include "lock.h"
static inline int sign(int n){ return (n > 0 ? 1 : 0); }
Locks* init(int lockSeq[][3], int n){ Locks* l = (Locks*) malloc(sizeof(Locks) * n); memcpy(l->lockSeq, lockSeq, sizeof(int) * 3 * n); l->n = n; return l; }
int numUnlocked(Locks *l, bool lights[], int m){ int cnt = 0; for(int i = 0; i < l->n; i++){ int flag = 0; for(int j = 0; j < 3; j++){ if(sign(l->lockSeq[i][j]) == lights[abs(l->lockSeq[i][j]) - 1]){ flag = 1; break; } } if(flag == 1) cnt++; } return cnt; }
|
lock.h
1 2 3 4 5 6 7 8 9
| typedef struct locks { int lockSeq[128][3]; int n; } Locks;
Locks* init(int lockSeq[][3], int n);
int numUnlocked(Locks *locks, bool lights[], int m);
|