Luo Gu P1056-row seat (analog \ greedy)

Title Description

When the class is always some back and forth about the students and people whispering to each other, something that makes primary school teacher is very troublesome thing. However, snow teacher found some interesting phenomena, when the students of seating finalized, only limited D will whisper when the students in class. Students sit in the classroom became $ M $ row $ N $ columns, the position of the students sitting in row i and j column is $ (i, j) $, in order to facilitate the students in and out, set up in the classroom $ of transverse channels K $, $ L $ of longitudinal channels. So, smart snow thought of a way, perhaps the problem of students in class whispering to each other can be reduced: She intends to rearrange tables and chairs, tables and chairs to change the position of the channel between the students, because if a channel separating the two would whisper $ 2 $ students, then they would not whisper a. Would you please write a program to snow, we give the best channel division scheme. Under this scheme, the minimum number of students in class whispering.

Input and output formats

Input Format

The first line, with a \ (5 \) space-separated integers are \ (M, N, K, L, D (2 \ le N, M \ le 1000,0 \ le K <M, 0 \ le L <N, D \ le 2000) \) next \ (D \) rows, each row having \ (4 \) space-separated integers. The first \ (I \) row \ (4 \) integer \ (X_i, Y_i, P_i, Q_i \) , represents a sitting position \ ((X_i, Y_i) \ ) and \ ((P_i, Q_i) \ ) the two students will whisper (before and after they enter to ensure that adjacent or horizontally adjacent). Input data to ensure the uniqueness of the optimal solution.


Output Format

A total of two lines. The first line contains $ K $ integers $ a_1, a_2, ..., a_K $, $ represents the line between the first and A_1 $ $ a_1 + 1 $ line, between the first line and $ A_2 $ $ a_2 + 1 $ line, ..., $ a_k first row and $ $ a_K + $ 1 to open the passage between rows, where $ a_i <a_i + 1 $, each separated by a space between two integers (no end of line spaces). The second line contains L $ $ integer $ b_1, b_2, ..., b_L $, $ represents between a first column and B_1 $ $ b_1 + 1 $ row, first column and $ B_2 $ $ b_2 + 1 $ column, ..., $ B_L of $ $ b_L + column and the column between $ 1 to open up channels, where $ b_i <b_i + 1 $, each separated by a space between two integers (EOT no spaces).

Sample input and output

Input Sample # 1

4 5 1 2 3
4 2 4 3
2 3 3 3
2 5 2 4

Sample Output # 1

2
2 4

Explanation

The above figure by the symbol *, ※, + marked \ (3 \) position of the student will be whispering, FIG \ (3 \) thick line indicates the position of the channel, the channel division scheme is illustrated only the best solution.
In 2008 the popularity of Group II title

Thinking

You can direct simulation, must pay attention to the output from small to large.

Code

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000 + 10;
int M, N, K, L, D;
set<int> ansl, ansc;
struct node
{
    int id;
    int val;
    bool operator<(const node &rhs)
    {
        return val > rhs.val;
    }
} lin[MAXN], cal[MAXN];

bool cmp(const node& a, const node& b)
{
    return a.id < b.id;
}

void init(int m, int n)
{
    for (int i = 1; i <= m; i++)
    {
        lin[i].id = i;
        lin[i].val = 0;
    }
    for (int i = 1; i <= n; i++)
    {
        cal[i].id = i;
        cal[i].val = 0;
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    cin >> M >> N >> K >> L >> D;
    init(M, N);
    for (int i = 0; i < D; i++)
    {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        if (x1 == x2)
            cal[min(y1, y2)].val++;
        else
            lin[min(x1, x2)].val++;
    }
    sort(lin + 1, lin + 1 + M);
    sort(cal + 1, cal + 1 + N);
    sort(lin + 1, lin + 1 + K, cmp);
    sort(cal + 1, cal + 1 + L, cmp);
    for (int i = 1; i <= K; i++)
    {
        if (i != 1)
            cout << " ";
        cout << lin[i].id;
        if(i == K)cout << endl;
    }
    for (int i = 1; i <= L; i++)
    {
        if (i != 1)
            cout << " ";
        cout << cal[i].id;
        if(i == L)cout << endl;
    }
}

Guess you like

Origin www.cnblogs.com/YY666/p/11360209.html
Recommended