hdu multi-school eighth-field 1011 (hdu6667) Roundgod and Milk Tea greedy

Meaning of the questions:

There are several classes, each class some people want to drink milk tea, also offers some tea, a drink cup, but their own class of people can not drink their own milk classes, seeking to have the largest number of people drink tea.

answer:

The number of class press descending order.

To prevent their own class of people drink tea own class, the establishment of a buffer store in front of the rest of the class drink tea.

If the back of the class had tea tea, grab a drink later classes, if the class of drinking tea behind, went to see the buffer is not left remaining milk.

The last class of their own people drank or did not have to drink, and then his own squad has not been placed in front of the class who steal milk buffer.

Then turn to the next shift, then perform the above steps.

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long long ll;
typedef pair<int, LL>P;
const int M = 4e5 * 4 + 5;
const LL mod = 1e9 + 7;
const LL lINF = 0x3f3f3f3f3f3f3f3f;
#define ls (rt<<1)
#define rs (rt<<1|1)
LL gcd(LL a, LL b) { return b ? gcd(b, a%b) : a; }
LL quickpow(LL a, LL b, LL mod)
{
    LL res = 1;
    while (b)
    {
        if (b & 1)
            res = (res*a) % mod;
        a = (a*a) % mod;
        b >>= 1;
    }
    return res;
}
struct node {
    LL a, b;
}tr[M];
int t;
int n;
LL cnt;
LL ans;
int lid;
bool cmp(node x, node y)
{
    return x.b > y.b;
}
int main()
{
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
        {
            scanf("%lld%lld", &tr[i].a, &tr[i].b);
        }
        sort(tr + 1, tr + 1 + n, cmp);
        ans = cnt = 0;
        lid = 2;
        for (int i = 1; i <= n; i++)
        {
            while (lid < i)
            {
                cnt += tr[lid].a;
                lid++;
            }
            if (lid == i)
                lid++;
            while (tr[i].b)
            {
                if (lid <= n)
                {
                    LL tmp= min(tr[lid].a, tr[i].b);
                    ans += tmp;
                    tr[i].b -= tmp;
                    tr[lid].a -= tmp;
                    if(!tr[lid].a)
                        lid++;
                }
                else
                {
                    LL tmp= min(tr[i].b, cnt);
                    ans += tmp;
                    tr[i].b -= tmp;
                    cnt -= tmp;
                    if (!cnt)
                        break;
                }
            }
            cnt += tr[i].a;
            tr[i].a = 0;
        }
        printf("%lld\n", ans);
    }
}

 

Guess you like

Origin www.cnblogs.com/isakovsky/p/11355201.html