Codeforces - 99B. Help Chef Gerasim - 排序

Help Chef Gerasim

题目链接

分类implementation sortings

1.题意概述

  • 给你n个茶杯,里面分别有a[i]毫升的水,现在要你最多倒一次使得所有杯子里面水的容量相同。

2.解题思路

  • 直接排序以后,最终答案就是 ,让水最多的倒给最少的即可,还有一些trick点,比如总杯子数不能整除总容量,还有需要倒多次的情况……

3.AC代码

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <functional>
#include <cmath>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <ctime>
using namespace std;
#define eps 1e-6
#define e exp(1.0)
#define pi acos(-1.0)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)(x).size())
#define All(x) (x).begin(),(x).end()
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define Close() ios::sync_with_stdio(0),cin.tie(0)
#define INF 1000000
#define maxn 1001
#define N 152
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int mod = 1e9 + 7;
/* head */
struct node {
    int id, num;
    friend bool operator< (const node& a, const node& b) {
        return a.num < b.num;
    }
/*  friend bool operator== (const node&a, const node& b) {
        return a.num == b.num;
    } */
} a[maxn], b[4];
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    long _begin_time = clock();
#endif
    int n;
    int sum = 0;
    scanf("%d", &n);
    rep(i, 1, n + 1) {
        scanf("%d", &a[i].num);
        a[i].id = i;
        sum += a[i].num;
    }
    sort(a + 1, a + n + 1);
    bool flag = 1;
    int cnt = 0;
    int mid = sum / n;
    rep(i, 1, n + 1) {
        if (a[i].num != mid) {
            b[cnt].id = a[i].id;
            b[cnt++].num = a[i].num;
        }
        if (cnt > 2) {
            flag = 0;
            break;
        }
    }
    if (!flag || sum % n)
        puts("Unrecoverable configuration.");
    else {
        if (cnt == 0) puts("Exemplary pages.");
        else {
            sort(b, b + cnt);
        //  rep(i, 0, 2) printf("%d %d\n", b[i].id, b[i].num);
            printf("%d ml. from cup #%d to cup #%d.\n", (b[1].num - b[0].num) / 2, b[0].id, b[1].id);
        }
    }
#ifndef ONLINE_JUDGE
    long _end_time = clock();
    printf("time = %ld ms.", _end_time - _begin_time);
#endif
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值