zoukankan      html  css  js  c++  java
  • 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
    ");
                continue;
            }
            ll res=0;
            if(num[t])
            {
                if(num[t]>=d)
                {
                    num[t]-=d;
                    printf("%lld
    ",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
    ");
            else
              printf("%lld
    ",res);
        }
    }
  • 相关阅读:
    [离散数学II]2017.5.9
    mysql内连接、左连接、右连接
    Android平台介绍
    软技能(面试)1
    流程控制练习题
    函数:算法
    linux系统文件
    App测试需注意
    python-循环
    python-正则表达式
  • 原文地址:https://www.cnblogs.com/lishengkangshidatiancai/p/10347613.html
Copyright © 2011-2022 走看看