2025年Codeforces - 99B. Help Chef Gerasim - 排序

Codeforces - 99B. Help Chef Gerasim - 排序Help Chef Gerasim 题目链接 分类 implementati sortings 1 题意概述 给你 n 个茶杯 里面分别有 a i 毫升的水 现在要你最多倒一次使得所有杯子里面水的容量相同 2 解题思路 直接排序以后 最终答案就是 总 容 量 总 杯 子 数

大家好,我是讯享网,很高兴认识大家。

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  #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; }

讯享网
小讯
上一篇 2025-01-19 14:34
下一篇 2025-03-31 23:24

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/50551.html