Codeforces Round #252 (Div. 2) C. Valera and Tubes

<传送门>

 

【题目大意】

C题:
给你一个一个n行m列的长方形表,相当于一个二维数组,不过是从1开始的,用(x,y)表示一个单元。
现在需要放置k个管道在这些单元中:
1.r>=2;
2.对于各个单元满足:|xi - xi + 1| + |yi - yi + 1| = 1 holds;
3.每个单元只能放一个管道;

如果满足一下条件,则安放管道在该单元:
1.没有一对管道共用一个单元;
2.每一个单元属于一些管道;

请帮助他使用这种方式来将k根管道安排在这个矩形中。

【题目分析】

前k-1步每次走2格,最后一次走完剩下的格子即可,简单的贪心。

 

 

Codeforces Round #252 (Div. 2) C. Valera and Tubes
#include <cstdio>

#include <iostream>

#include <algorithm>

#define MAXN 100010

#define ll long long

using namespace std;



int n, m, k, cnt, col, row;

int x[MAXN], y[MAXN];



int main(void) {

    while(cin >> n >> m >> k) {

        cnt = 1;

        x[1] = 1; row = 1;

        y[1] = 1; col = 1;

        while(cnt <= n*m) {

            

            cnt++;

            

            if((col==m&&row%2) || (col==1&&row%2==0)) {

                row++;

                x[cnt] = row;

                y[cnt] = col;

            }

            else {

                if(row % 2) {

                    col++;

                }

                else {

                    col--;

                }

                x[cnt] = row;

                y[cnt] = col;

            }

        }

        int j = 1;

        for(int i=1; i<k; ++i) {

            cout << "2 " << x[j] << " " << y[j] << " ";

            ++j;

            cout << x[j] << " " << y[j] << endl;

            ++j;

        }

        

        cout << cnt-j << " ";

        for(int i=j; i<cnt-1; ++i) {

            cout << x[i] << " " << y[i] << " ";

        }

        cout << x[cnt-1] << " " << y[cnt-1] << endl;

        

    }

    return 0;

}
View Code

 

 

 

 

 

 

 

你可能感兴趣的:(codeforces)