zoukankan      html  css  js  c++  java
  • Codeforces 863B Kayaking 暴力 水题

    传送门:http://codeforces.com/problemset/problem/863/B

    啊啊啊啊啊……做的时候卡了好久啊,想多了,还觉得是dp啥的……其实就是个暴力……

    暴力选两个人乘坐单人船,剩下的排序以后分为两组,记录差的最小值即可。

    #include<iostream>
    #include<string>
    #include<string.h>
    #include<cmath>
    #include<algorithm>
    #include<map>
    using namespace std;
    typedef long long ll;
    int n,sum,ans;
    int a[105],c[105];
    int main()
    {
        while (cin >> n)
        {
            sum = 0;
            for (int i = 0; i < 2 * n; i++)
            {
                cin >> a[i];
                sum += a[i];
            }
            sort(a, a + 2 * n);
            ans = 1e9;
            for (int i = 0; i < 2 * n; i++)
            {
                for (int j = i+1; j < 2 * n; j++)
                {
                    int cur = 0,b=0;
                    sum -= a[i];
                    sum -= a[j];
                    for (int k = 0; k < 2 * n; k++)
                    {
                        if (k != i&&k != j)
                            c[cur++] = a[k];
                    }
                    for (int k = 0; k < 2 * n-2; k+=2)
                        b += c[k];
                    ans = min(ans, abs(sum - b - b));
                    sum += a[i] + a[j];
                }
            }
            cout << ans << endl;
        }
        return 0;
    }
    #include<iostream>
    #include<string>
    #include<string.h>
    #include<cmath>
    #include<algorithm>
    #include<map>
    using namespace std;
    typedef long long ll;
    int n,sum,ans;
    int a[105],c[105];
    int main()
    {
        while (cin >> n)
        {
            sum = 0;
            for (int i = 0; i < 2 * n; i++)
            {
                cin >> a[i];
                sum += a[i];
            }
            sort(a, a + 2 * n);
            ans = 1e9;
            for (int i = 0; i < 2 * n; i++)
            {
                for (int j = i+1; j < 2 * n; j++)
                {
                    int cur = 0,b=0;
                    sum -= a[i];
                    sum -= a[j];
                    for (int k = 0; k < 2 * n; k++)
                    {
                        if (k != i&&k != j)
                            c[cur++] = a[k];
                    }
                    for (int k = 0; k < 2 * n-2; k+=2)
                        b += c[k];
                    ans = min(ans, abs(sum - b - b));
                    sum += a[i] + a[j];
                }
            }
            cout << ans << endl;
        }
        return 0;
    }
  • 相关阅读:
    解除win7系统静音
    temp--test audio micphone
    2015年年中总结
    通过对象成员地址求对象本身地址
    管理全局对象
    UTF-8 <==> unicode(WCHAR)
    [HEOI2016/TJOI2016]树
    P2382 化学分子式
    [PKUWC2018]Slay the Spire
    [Ynoi2019模拟赛]Yuno loves sqrt technology III
  • 原文地址:https://www.cnblogs.com/Egoist-/p/7597387.html
Copyright © 2011-2022 走看看