标签:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; int a[4]; int main(void) { int n; cin >> n; int mn1, mx1, mn2, mx2, mn3, mx3; cin >> mn1 >> mx1; cin >> mn2 >> mx2; cin >> mn3 >> mx3; a[3] = mn3; a[2] = mn2; int res = n - a[2] - a[3]; if (res <= mx1) a[1] = res; else { a[1] = mx1; res -= a[1]; if (a[2] + res <= mx2) a[2] += res; else { a[2] = mx2; a[3] += (res - (a[2] - mn2)); } } cout << a[1] << " " << a[2] << " " << a[3] << endl; return 0; }
贪心 || 二分 B - Pasha and Tea
题意:有n个girl和n个boy喝茶,茶杯的容量不等,boy喝的是girl的两倍且boy喝的一样多,girl喝的一样多,问主人最多能倒出多少水
分析:第一反应是用二分搜索girl喝的茶容量,可惜写搓了,mid应该和茶量最小的girl以及boy去比较。看过的代码竟然99%都是贪心,晕~
代码(二分):
#include <cstdio> #include <algorithm> #include <cstring> #include <iostream> using namespace std; const int N = 1e5 + 10; const int INF = 0x3f3f3f3f; const double EPS = 1e-9; int a[N*2]; int main(void) { int n; int w; scanf ("%d%d", &n, &w); n *= 2; for (int i=1; i<=n; ++i) { scanf ("%d", &a[i]); } sort (a+1, a+1+n); double l = 0.0, r = w; double mid = 0.0, ans = 0.0; for (int i=1; i<=100; ++i) { mid = (l + r) / 2.0; if (mid >= 0 && mid <= a[1] && mid * 2 <= a[n/2+1] && mid * 1.5 * n <= w) { l = mid; ans = mid * 1.5 * n; } else r = mid; } printf ("%.6f\n", ans); return 0; }
代码(贪心):
#include <cstdio> #include <algorithm> #include <cstring> #include <iostream> using namespace std; const int N = 1e5 + 10; int a[N*2]; int main(void) { int n, w; scanf ("%d%d", &n, &w); n *= 2; for (int i=1; i<=n; ++i) scanf ("%d", &a[i]); sort (a+1, a+1+n); double ans = min (a[1] * 1.0, a[n/2+1] / 2.0); ans = min (ans * 3 * n / 2.0, w * 1.0); printf ("%.6f\n", ans); return 0; }
未完待续~
Codeforces Round #311 (Div. 2)
标签:
原文地址:http://www.cnblogs.com/Running-Time/p/4780632.html