题解
2025-09-26 20:00:58
发布于:广东
0阅读
0回复
0点赞
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Channel {
    int pos;    // 通道位置
    int count;  // 能隔开的交头接耳对数
};
bool compareCount(const Channel& a, const Channel& b) {
    return a.count > b.count;  // 按隔开数量降序
}
bool comparePos(const Channel& a, const Channel& b) {
    return a.pos < b.pos;      // 按位置升序
}
int main() {
    int M, N, K, L, D;
    cin >> M >> N >> K >> L >> D;
    
    vector<Channel> rowChannels(M), colChannels(N);
    
    // 初始化通道位置
    for (int i = 0; i < M; i++) {
        rowChannels[i].pos = i + 1;  // 第i行和第i+1行之间
        rowChannels[i].count = 0;
    }
    for (int i = 0; i < N; i++) {
        colChannels[i].pos = i + 1;  // 第i列和第i+1列之间
        colChannels[i].count = 0;
    }
    
    // 处理每对交头接耳的学生
    for (int i = 0; i < D; i++) {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        
        if (x1 == x2) { 
            // 同一行,左右相邻 - 需要纵向通道
            int col = min(y1, y2);
            colChannels[col - 1].count++;
        } else { 
            // 同一列,上下相邻 - 需要横向通道
            int row = min(x1, x2);
            rowChannels[row - 1].count++;
        }
    }
    
    // 选择最佳的K条横向通道
    sort(rowChannels.begin(), rowChannels.end(), compareCount);
    sort(rowChannels.begin(), rowChannels.begin() + K, comparePos);
    
    // 选择最佳的L条纵向通道
    sort(colChannels.begin(), colChannels.end(), compareCount);
    sort(colChannels.begin(), colChannels.begin() + L, comparePos);
    
    // 输出结果
    for (int i = 0; i < K; i++) {
        if (i > 0) cout << " ";
        cout << rowChannels[i].pos;
    }
    cout << endl;
    
    for (int i = 0; i < L; i++) {
        if (i > 0) cout << " ";
        cout << colChannels[i].pos;
    }
    cout << endl;
    
    return 0;
}
这里空空如也






有帮助,赞一个