社区讨论

TLE救命,会闭关

P15255[USACO26JAN2] Moo Hunt B参与者 2已保存回复 3

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
3 条
当前快照
1 份
快照标识符
@mlkkjl02
此快照首次捕获于
2026/02/13 15:31
6 天前
此快照最后确认于
2026/02/16 15:00
3 天前
查看原帖
CPP
#include <cstdio>
#include <cstring>
using namespace std;

const int MAX_MASK = 1 << 20;
int score[MAX_MASK] = {0};

int main() {
    register int N, K;
    scanf("%d%d", &N, &K);
    
    static int cnt[20][20][20];
    memset(cnt, 0, sizeof(cnt));
    register int x, y, z;
    for (int i = 0; i < K; ++i) {
        scanf("%d%d%d", &x, &y, &z);
        --x; --y; --z;
        cnt[x][y][z]++;
    }
    
    register int op_x, op_y, op_z, op_cnt;
    for (op_x = 0; op_x < N; ++op_x) {
        for (op_y = 0; op_y < N; ++op_y) {
            if (op_y == op_x) continue;
            for (op_z = 0; op_z < N; ++op_z) {
                if (op_z == op_x || op_z == op_y) continue;
                op_cnt = cnt[op_x][op_y][op_z];
                if (op_cnt == 0) continue;
                
                const int fix_bit = 1 << op_x;
                const int mask_bit = fix_bit | (1 << op_y) | (1 << op_z);
                for (register int mask = 0; mask < MAX_MASK; mask += (1 << (N > 16 ? 8 : 16))) {
                    for (register int sub = mask; sub < mask + (1 << (N > 16 ? 8 : 16)); ++sub) {
                        if (sub >= (1 << N)) break;
                        if ((sub & mask_bit) == fix_bit) {
                            score[sub] += op_cnt;
                        }
                    }
                }
            }
        }
    }
    
    register int max_score = 0;
    long long board_cnt = 0;
    const int total = 1 << N;
    for (register int mask = 0; mask < total; ++mask) {
        if (score[mask] > max_score) {
            max_score = score[mask];
            board_cnt = 1;
        } else if (score[mask] == max_score) {
            board_cnt++;
        }
    }
    
    printf("%d %lld\n", max_score, board_cnt);
    return 0;
}
TLE1,AC9,能给改好的代码更好,或处处注意,谢谢

回复

3 条回复,欢迎继续交流。

正在加载回复...