B. Lunar New Year and Food Ordering

#include<bits/stdc++.h>//标记数组法,在结构体外面开一个数组来标记这个元素是否被取完,对结构体排序,遍历结构体时如果标记数组为0跳过就好 
using namespace std;
typedef long long ll;
struct student{
    int index;
    int c;
    int r;
}stu[100010];
int num[100010];
int cost[100010];
bool cmp(student A,student B)
{
    if(A.c==B.c)
       return A.index<B.index;
    return A.c<B.c;
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        int a;
        scanf("%d",&a);
        stu[i].r=a;
        stu[i].index=i;
        num[i]=a;
    }
    for(int i=1;i<=n;i++)
    {
        int c;
        scanf("%d",&c);
        stu[i].c=c;
        cost[i]=c;
    }
    sort(stu+1,stu+1+n,cmp);
    int pos=1;
    for(int i=1;i<=m;i++)
    {
        int t,d;
        scanf("%d%d",&t,&d);
        if(pos==n+1)
        {
            printf("0\n");
            continue;
        }
        ll res=0;
        if(num[t])
        {
            if(num[t]>=d)
            {
                num[t]-=d;
                printf("%lld\n",1LL*d*cost[t]);
                continue;
            }
            else
            {
                d-=num[t];
                res+=(1LL*num[t]*cost[t]);
                num[t]=0;
            }
        }
        while(pos<=n)
        {
            while(pos<=n&&num[stu[pos].index]==0)
            {
                pos++;
            }
            if(pos==n+1)
              break;
            if(num[stu[pos].index]>d)
            {
                num[stu[pos].index]-=d;
                res+=(1LL*d*cost[stu[pos].index]);
                d=0;
            }
            else
            {
                d-=num[stu[pos].index];
                res+=(1LL*num[stu[pos].index]*cost[stu[pos].index]);
                num[stu[pos].index]=0;
                pos++;
            }
            if(d==0)
              break;
        }
        if(pos==n+1&&d>0)
          printf("0\n");
        else
          printf("%lld\n",res);
    }
}

猜你喜欢

转载自www.cnblogs.com/lishengkangshidatiancai/p/10347613.html