Luo Gu P1056-row seat solution to a problem

P1056 seat row

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 a \ (M \) line \ (N \) column, sitting in \ (i \) line \ (j \) position of the column is students \ ((i, j) \ ) \ ((I, J) \) , students out for convenience, is provided in the classroom \ (K \) of transverse channels, \ (L \) longitudinal channel strip.

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 one channel separates the two will whisper students, 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 Format

The first line, separated by a space with five integers are \ (M \) , \ (N \) , \ (K \) , \ (L \) , \ (D \) \ ((2 \ le N, M \ le 1000,0 \ le K <M, 0 \ le L <N, D \ le 2000) \)

The next \ (D \) lines, each line separated by a space of 44 integers. The first \ (I \) . 4 rows integer \ (X_i \) , \ (Y_i \) , \ (P_i \) , \ (Q_i \) , represents a sitting position \ ((X_i, Y_i) \ ) and \ ((P_i, Q_i) \) of 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 integers KK \ (A_1, A_2, ..., a_k \) , represents the \ (A_1 \) rows and (a_1 + 1 \) \ between th, \ (A_2 \) rows and \ (a_2 + 1 \) between row, ..., the \ (a_k \) th and \ (a_K + 1 \) between the rows to open up channels, where \ (a_i <a_i. 1 + \) , each two between integers separated by spaces (no end of line spaces).

The second line contains \ (L \) integer \ (B_1, B_2, ..., B_L \) , represents the \ (B_1 \) column and \ (b_1 + 1 \) between the columns, the \ (B_2 \) column and \ (b_2 + 1 \) between columns, ..., the \ (B_L \) column and \ (b_L + 1 \) between the rows to open up channels, where \ (b_i <b_i + 1 \ ) , between two integers each separated by a space (no spaces EOT).

Sample input and output

Input # 1 copy

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

Output # 1 copy

2
2 4

Description / Tips

The above figure by the symbol *, ※, + marked 33 pairs of students will whispering position, the position in FIG. 33 thick line represents the channel, the channel division scheme is illustrated only the best solution.

In 2008 the popularity of Group II title

[Thinking]

Sort + greedy
greedy principle is very simple good think
is the priority channel in rows or columns can be separated by up to whisper above the students
in order to achieve the purpose of separating most of the students whisper
input for each position whisper whispering classmates,
then judge it is about adjacent or neighboring front and rear
If it is about a small adjacent that these two positions inside the row in the bucket inside counter counts
if it is, then it is adjacent before and after treatment in accordance with the above line, it is small

Why are small record it?
Since the output format which say!
\ (a_1 \) represents \ (a_1 \) ? row and (a_1 + 1 \) \ between the lines
so naturally you want to record the slightly smaller

After the completion of the recording will sort, but when it comes to sorting
the previous record of when you need to use the structure of the recording
because you need to output is the number of rows instead of each line capable of separating students
structure inside a store counter and a mark so how much is a variable number of
the sort was time to redefine what the structure sorting like

Sorted according to the size of the first counter
these greedy greedy inside out before K can be separated by a large number of students
but the title also implies that you
Yaoan numbered in ascending order of output for each row or column
so it needs to make another bit sequence ,
but this is the sort of mark is in accordance with what number to sort
the small number on the front output

[Complete code]

#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;
const int Max = 1005;
struct node
{
    int js,hao;
};
node hang[Max],lie[Max];

bool cmp(const node a,const node b)
{
    return a.js > b.js;
}
bool cmp1(const node a,const node b)
{
    return a.hao < b.hao;
}

int main()
{
    int m,n,k,l,d;
    int x1,x2,y1,y2;
    cin >> m >> n >> k >> l >> d;
    for(int i = 1;i <= m;++ i)
        hang[i].hao = i;
    for(int i = 1;i <= n;++ i)
        lie[i].hao = i;
    for(int i = 1;i <= d;++ i)
    {
        cin >> x1 >> y1 >> x2 >> y2;
        if(x1 == x2)
            lie[min(y1,y2)].js ++;
        else
        if(y1 == y2)
            hang[min(x1,x2)].js ++;
    }
    sort(lie + 1,lie + Max + 1,cmp);
    sort(lie + 1,lie + l + 1,cmp1);
    
    sort(hang + 1,hang + Max + 1,cmp);
    sort(hang + 1,hang + k + 1,cmp1);

    for(int i = 1;i <= k;++ i)
        cout << hang[i].hao << " ";
    cout << endl;
    for(int i = 1;i <= l;++ i)
        cout << lie[i].hao << " ";
    return 0; 
}

Guess you like

Origin www.cnblogs.com/acioi/p/11608235.html