HDU多校Round 1

Solved:5

rank:172

A.Maximum Multiple

#include <stdio.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int n;
        scanf("%d", &n);
        ll ans = -1;
        if(n % 3 == 0)
        {
            ll x = n / 3;
            ans = max(ans, x * x * x);
        }
        if(n % 4 == 0)
        {
            ll x = n / 4;
            ans = max(ans, x * x * x * 2);
        }
        printf("%lld
", ans);
    }
    return 0;
}
View Code

B.Balanced Sequence

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;

char s[100005];
struct node
{
    int l, r;
}E[100005];

bool cmp(node A, node B)
{
    int tmp1 = min(A.l, B.r);
    int tmp2 = min(A.r, B.l);
    if(tmp1 == tmp2) return A.r < B.r;
    else return tmp1 > tmp2;
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int ans = 0; int cnt = 0;
        int suml = 0, sumr = 0;
        int n;
        scanf("%d", &n);
        for(int i = 1; i <= n; i++)
        {
            scanf("%s", s);
            int len = strlen(s);

            int cnl = 0, cnr = 0;
            for(int j = 0; j < len; j++)
            {
                if(s[j] == '(') cnl++;
                else
                {
                    if(cnl)
                    {
                        cnl--;
                        ans += 2;
                    }
                    else cnr++;
                }
            }
            if(cnl == 0) sumr += cnr;
            else if(cnr == 0) suml += cnl;
            else
            {
                cnt++;
                E[cnt].l = cnl;
                E[cnt].r = cnr;
            }
        }
        sort(E + 1, E + 1 + cnt, cmp);

        for(int i = 1; i <= cnt; i++)
        {
            if(suml >= E[i].r)
            {
                ans += E[i].r * 2;
                suml -= E[i].r;
                suml += E[i].l;
            }
            else
            {
                ans += suml * 2;
                suml = E[i].l;
            }
        }
        ans += min(suml, sumr) * 2;
        printf("%d
", ans);
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/lwqq3/p/9357295.html