【CodeForces - 758C Unfair Poll】 暴力 + 模擬

D - Unfair Poll


On the Literature lesson Sergei noticed an awful injustice, it seems that some students are asked more often than others.

Seating in the class looks like a rectangle, where n rows with m pupils in each.

The teacher asks pupils in the following order: at first, she asks all pupils from the first row in the order of their seating, then she continues to ask pupils from the next row. If the teacher asked the last row, then the direction of the poll changes, it means that she asks the previous row. The order of asking the rows looks as follows: the 1-st row, the 2-nd row, ..., the n - 1-st row, the n-th row, the n - 1-st row, ..., the 2-nd row, the 1-st row, the 2-nd row, ...

The order of asking of pupils on the same row is always the same: the 1-st pupil, the 2-nd pupil, ..., the m-th pupil.

During the lesson the teacher managed to ask exactly k questions from pupils in order described above. Sergei seats on the x-th row, on the y-th place in the row. Sergei decided to prove to the teacher that pupils are asked irregularly, help him count three values:

  1. the maximum number of questions a particular pupil is asked,
  2. the minimum number of questions a particular pupil is asked,
  3. how many times the teacher asked Sergei.

If there is only one row in the class, then the teacher always asks children from this row.

Input

The first and the only line contains five integers nmkx and y (1 ≤ n, m ≤ 100, 1 ≤ k ≤ 1018, 1 ≤ x ≤ n, 1 ≤ y ≤ m).

Output

Print three integers:

  1. the maximum number of questions a particular pupil is asked,
  2. the minimum number of questions a particular pupil is asked,
  3. how many times the teacher asked Sergei.
Example
Input
1 3 8 1 1
Output
3 2 3
Input
4 2 9 4 2
Output
2 1 1
Input
5 5 25 4 3
Output
1 1 1
Input
100 100 1000000000000000000 100 100
Output
101010101010101 50505050505051 50505050505051
Note

The order of asking pupils in the first test:

  1. the pupil from the first row who seats at the first table, it means it is Sergei;
  2. the pupil from the first row who seats at the second table;
  3. the pupil from the first row who seats at the third table;
  4. the pupil from the first row who seats at the first table, it means it is Sergei;
  5. the pupil from the first row who seats at the second table;
  6. the pupil from the first row who seats at the third table;
  7. the pupil from the first row who seats at the first table, it means it is Sergei;
  8. the pupil from the first row who seats at the second table;

The order of asking pupils in the second test:

  1. the pupil from the first row who seats at the first table;
  2. the pupil from the first row who seats at the second table;
  3. the pupil from the second row who seats at the first table;
  4. the pupil from the second row who seats at the second table;
  5. the pupil from the third row who seats at the first table;
  6. the pupil from the third row who seats at the second table;
  7. the pupil from the fourth row who seats at the first table;
  8. the pupil from the fourth row who seats at the second table, it means it is Sergei;
  9. the pupil from the third row who seats at the first table;

題意:一個教室中有n排m列的座位,現在老師會進行k次詢問,從第一排第一列到最後一排最後一列,再從最後一排第一列到第一排最後一列(最後一排和第一排只詢問了一次,每排從第一列開始詢問),問最大的詢問次數,最小的詢問次數和一個特殊位置的詢問次數。


分析:首先我們找到一個循環節,就是從第一排到最後一排,再回到第二排,這就是一次循環。有很多細節,計算循環節的時候不能直接用m*(2*n-2),因爲n的值可能爲1。設數組a[i][j]爲對應座位的詢問次數,因爲第一排和最後一排只詢問一次,這個時候就要特判n是否等於1,避免第一排和最後一排的a[i][j]賦值時重複。


代碼如下:

#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long

using namespace std;
const int MX = 105;
const int mod = 1e9 + 7;
const LL INF = 1e18 + 5;

LL a[MX][MX];

int main(){
    LL n, m, k, x, y;
    scanf("%I64d%I64d%I64d%I64d%I64d", &n, &m, &k, &x, &y);
    int sum = 0;
    for(int i = 1; i <= n; i++){
        sum += m;
    }
    for(int i = n-1; i >= 2; i--){
        sum += m;
    }
    LL p = k / sum;
    for(int i = 2; i <= n-1; i++){
        for(int j = 1; j <= m; j++){
            a[i][j] += 2*p;
        }
    }
    for(int j = 1; j <= m; j++){
        a[1][j] += p;
    }
    if(n != 1){
        for(int j = 1; j <= m; j++){
            a[n][j] += p;
        }
    }
    k -= sum * p;
    if(k){
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                a[i][j] += 1;
                k--;
                if(!k)  break;
            }
            if(!k)  break;
        }
    }
    if(k){
        for(int i = n-1; i >= 2; i--){
            for(int j = 1; j <= m; j++){
                a[i][j] += 1;
                k--;
                if(!k)  break;
            }
            if(!k)  break;
        }
    }
    LL ans1 = 0, ans2 = INF;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            ans1 = max(ans1, a[i][j]);
            ans2 = min(ans2, a[i][j]);
        }
    }
    printf("%I64d %I64d %I64d\n", ans1, ans2, a[x][y]);
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章