现在的位置: 首页 > 综合 > 正文

cf 202 杂题 二分

2013年09月04日 ⁄ 综合 ⁄ 共 3425字 ⁄ 字号 评论关闭
C. Mafia
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1people
take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds.
What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?

Input

The first line contains integer n (3 ≤ n ≤ 105).
The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) —
the i-th number in the list is the number of rounds the i-th
person wants to play.

Output

In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least airounds.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cincout streams
or the %I64dspecifier.

Sample test(s)
input
3
3 2 2
output
4
input
4
2 2 2 2
output
3
Note

You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times:http://en.wikipedia.org/wiki/Mafia_(party_game).

/*
 * Author:  *****
 * Created Time:  2013/9/29 23:08:06
 * File Name: C.cpp
 * solve: C.cpp
 */
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<iostream>
#include<vector>
#include<queue>
//ios_base::sync_with_stdio(false);
//#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;
#define sz(v) ((int)(v).size())
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define repf(i, a, b) for (int i = (a); i <= (b); ++i)
#define repd(i, a, b) for (int i = (a); i >= (b); --i)
#define clr(x) memset(x,0,sizeof(x))
#define clrs( x , y ) memset(x,y,sizeof(x))
#define out(x) printf(#x" %d\n", x)
#define sqr(x) ((x) * (x))
typedef long long LL;

const int INF = 1000000000;
const double eps = 1e-8;
const int maxn = 100010;

int sgn(const double &x) {  return (x > eps) - (x < -eps); }

LL num[maxn];
int n;
LL maxround;
bool ok(LL round)
{
    LL cnt = 0;
    rep(i,0,n)
    {
        cnt += (round - num[i]);
    }
    
    return cnt >= round && cnt >= maxround;
}
int main() 
{
    //freopen("in.txt","r",stdin);
    scanf("%d",&n);
    
    maxround = 0;
    LL sum = 0;
    rep(i,0,n)
    {
        scanf("%I64d",&num[i]);
        maxround = max(maxround,num[i]);
        sum += num[i];
    }

    LL L = maxround;
    LL R = sum;
    LL mid;
    while(L <= R)
    {
        //cout<<"yes"<<endl;
        mid = (L + R)/2;
        if(ok(mid))
            R = mid - 1;
        else
            L = mid + 1;
    }
    
    cout<<L<<endl;
    return 0;
}

/*
 * Author:  *****
 * Created Time:  2013/9/29 23:08:06
 * File Name: C.cpp
 * solve: C.cpp
 */
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<iostream>
#include<vector>
#include<queue>
//ios_base::sync_with_stdio(false);
//#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;
#define sz(v) ((int)(v).size())
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define repf(i, a, b) for (int i = (a); i <= (b); ++i)
#define repd(i, a, b) for (int i = (a); i >= (b); --i)
#define clr(x) memset(x,0,sizeof(x))
#define clrs( x , y ) memset(x,y,sizeof(x))
#define out(x) printf(#x" %d\n", x)
#define sqr(x) ((x) * (x))
typedef long long LL;

const int INF = 1000000000;
const double eps = 1e-8;
const int maxn = 100010;

int sgn(const double &x) {  return (x > eps) - (x < -eps); }

LL num[maxn];
int n;
LL maxround;
bool ok(LL round)
{
    LL cnt = 0;
    rep(i,0,n)
    {
        cnt += (round - num[i]);
    }
    
    return cnt >= round;
}
int main() 
{
    //freopen("in.txt","r",stdin);
    scanf("%d",&n);
    
    maxround = 0;
    LL sum = 0;
    rep(i,0,n)
    {
        scanf("%I64d",&num[i]);
        maxround = max(maxround,num[i]);
        sum += num[i];
    }

    LL L = maxround;
    LL R = sum;
    LL mid;
    while(L <= R)
    {
        //cout<<"yes"<<endl;
        mid = (L + R)/2;
        if(ok(mid))
            R = mid - 1;
        else
            L = mid + 1;
    }
    
    cout<<L<<endl;
    return 0;
}

抱歉!评论已关闭.