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;
}
發佈了350 篇原創文章 · 獲贊 47 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章