Codeforces Round #311 (Div. 2)

來源:互聯網
上載者:User

標籤:

 

水 A - Ilya and Diplomas

#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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.