Codeforces Round #392 (Div. 2) Unfair Poll

C. Unfair Poll
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.
Examples
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;

思路:一个稍微麻烦一点的模拟..

思路清晰的代码:

 1 #include <iostream>
 2 using namespace std;
 3 typedef long long ll;
 4 ll n,m,k,x,y,mx,mn;
 5 ll f(ll x, ll y, ll k)
 6 {
 7     ll ans=0;
 8     if(n==1)
 9     {
10         ans=k/m;
11         k-=ans*m;
12         if(k>=y)
13             ans++;
14     }
15     else if(x==1)
16     {
17         ll cnt=k/((n-1)*m);
18         k-=cnt*(n-1)*m;
19         ans+=(cnt+1)/2;
20         if(cnt&1)
21         {
22             for(ll i=n;k>0&&i>=1;i--,k-=m)
23                 if(i==x&&k>=y)
24                     ans++;
25         }
26         else
27         {
28             for(ll i=1;i<=n&&k>0;i++,k-=m)
29                 if(i==x&&k>=y)
30                     ans++;
31         }
32     }
33     else if(x==n)
34     {
35         ll cnt=k/((n-1)*m);
36         k-=cnt*(n-1)*m;
37         ans+=cnt/2;
38         if(cnt&1)
39         {
40             for(ll i=n;k>0&&i>=1;i--,k-=m)
41                 if(i==x&&k>=y)
42                     ans++;
43         }
44         else
45         {
46             for(ll i=1;i<=n&&k>0;i++,k-=m)
47                 if(i==x&&k>=y)
48                     ans++;
49         }
50     }
51     else
52     {
53         ans+=k/((n-1)*m);
54         k-=ans*(n-1)*m;
55         if(ans&1)
56         {
57             for(ll i=n;k>0&&i>=1;i--,k-=m)
58                 if(i==x&&k>=y)
59                     ans++;
60         }
61         else
62         {
63             for(ll i=1;i<=n&&k>0;i++,k-=m)
64                 if(i==x&&k>=y)
65                     ans++;
66         }
67     }
68     return ans;
69 }
70 int main()
71 {
72     cin>>n>>m>>k>>x>>y;
73     mx=mn=f(1,1,k);
74     for(ll i=1;i<=n;i++)
75         for(ll j=1;j<=m;j++)
76             mx=max(mx,f(i,j,k)),
77             mn=min(mn,f(i,j,k));
78     cout<<mx<<" "<<mn<<" "<<f(x,y,k);
79 }

自己的代码:

 1 #include <bits/stdc++.h>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<queue>
 6 #define pi acos(-1.0)
 7 #include<map>
 8 #define inf 2e8+500
 9 typedef  long long  ll;
10 using namespace std;
11 const int N=1e5+5;
12 int main()
13 {
14     ll n,m,x,y;
15     ll k;
16     ll maxn,minn,ans=0;
17     scanf("%lld%lld%lld%lld%lld",&n,&m,&k,&x,&y);
18     ll p1=m*(x-1)+y,p2=(n-x)*m+y;
19     ll a=k/(m*n),b=k%(m*n);
20     if(k<m*n){
21         maxn=1,minn=0;
22         if(k>=p1){ans=1;}
23     }
24     else {
25         if(n==1){minn=a;
26         ans=b>=p1?a+1:a;
27         maxn=b==0?a:a+1;
28         }
29         else {
30             if(k==m*n) maxn=minn=ans=1;
31             else {
32                 k-=m*n;
33                 a=k/((n-1)*m);
34                 b=k%((n-1)*m);
35                 if(!b) {maxn=n==2?a/2+1:a+1;minn=1+a/2;}
36                 else if(b!=0) {maxn=n==2?a/2+2:a+2,minn=1+a/2;}
37                 if(x<n&&x>1){
38                     ans=a+1;
39                     if(a&1){if(b+m>=p1) ans++;}
40                     else {if(b+m>=p2) ans++;}
41                 }
42                 else {
43                     if(x==1){
44                         ans=a/2+a%2+1;
45                         if(a%2==0&&(b+m>=p2)) ans++;
46                     }
47                     else if(x==n) {
48                         ans=a/2+1;
49                         if(a&1&&(b+m>=p1)) ans++;
50                     }
51                 }
52             }
53         }
54     }
55     printf("%lld %lld %lld\n",maxn,minn,ans);
56     return 0;
57 }

总结:别人的代码更容易想到,写起来也方便一些,自己的思路不够清晰。。

posted @ 2017-02-17 14:13  thges  阅读(103)  评论(0编辑  收藏  举报